简体   繁体   中英

Accessing values from i2c address

You can take a look at this website: http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NSK1042 to get a better understanding of what I'm talking about. For example, the website reads: the i2c address of the sensor is 0x10 and the table of the values there reads:

Address       Type           Contents

00 – 07H      chars           Serial Version Number

43H           byte            Sensor 1 DC Signal Strength

How can I access these values in C? Thanks.

These registers can be memory mapped. A few things you'll need to do:

  • map the device's physical memory to your programs address space
  • declare any pointers to this region as volatile

The volatile keyword will stop the compiler from "optimizing" the program to be incorrect. eg by assuming that reads to the same memory location will yield the same result because the program hasn't written to it.

The easy part of this is to declare a struct such that all the offsets are the same as the device and that each part has the right size.

ie

struct hitech {
    char serial_version[8];
    char manufacturer[8];
    /* etc */
};

volatile struct hitech *my_device;

The second part is working out where the device is mapped. If it's plugged in to your computer you should be able to see this. You might need to do one of the following: mmap the device's physical address. Or just write my_device = 0x< address >. Or a combination of the two.

From the website: "The I2C address of the IRSeeker V2 sensor is 0x10"

So you want to write 0x10 above for my_device.

Then you'll need to compile for the correct micro-controller and load your program at the correct location as firmware.

You'd be better off using their programming language.

Assuming they're not supplying an SDK for you to access these values:

// I'm assuming these are read-only, hence the "const"
const char *g_serialVersionNumber = (const char *)0x00; // be careful not to access more than 8 bytes
const unsigned char *g_sensor1DCSignalStrength = (const unsigned char *)0x43;

void main()
{
    printf("Serial version number: %s\n", g_serialVersionNumber);
    printf("Sensor 1 DC Signal Strength: %d\n", *g_sensor1DCSignalStrength);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM