用 c 讀取 i2c 的 register


C example

底下是個範例, 詳細資料參考 http://www.mjmwired.net/kernel/Documentation/i2c/dev-interface
 會更清楚.

write.c


#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <errno.h>

#define I2C_ADDR 0x20

int main (void)
{
    int value;
    int fd;

    fd = open("/dev/i2c-0", O_RDWR);

    if (fd < 0) {
        printf("Error opening file: %s\n", strerror(errno));
        return 1;
    }

    if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) {
        printf("ioctl error: %s\n", strerror(errno));
        return 1;
    }

    for (value=0; value<=255; value++) {
        if (write(fd, &value, 1) != 1) {
            printf("Error writing file: %s\n", strerror(errno));
        }

        usleep(100000);
    }

    return 0;
}


留言

張貼留言

這個網誌中的熱門文章

NMEA標準格式 -- GPS

網路 Transformer 的用途

cut,sed,awk 字串處理