Raspberry Pi with I2C devices
C code drivers based on my earlier tests on NSLU2 (the Debian Slug)
PCF8574 - switch the lines
This code compiles on the Pi
/* 1) send the i2c address usage :- type with spaces but without the < > */ int i2c; int rc; int main(int argc, char *argv[]) /* address is the first number after the program name */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */ write(i2c,data,1) ; /* send the byte */ i2c = close(i2c); /* Sunspot and Google 090815 */ |
To compile do
gcc -o PCF8574_addr_byteout_slug PCF8574_addr_byteout_slug.c
PCF8574 - read the line states
/* 1) set the i2c address of the PCF8574 #include <stdio.h> int i2c; int main(int argc, char ** argv) /* address is the first number after the program name */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ data[0] = 255; read(i2c,data,1); /* now data(0) contains the byte of data on the 8 port lines*/ byte = data[0]; printf("BYTE = %d \n", byte); i2c = close(i2c); /* Sunspot and Google 090815 */ |
PCF8591
Read 1 of the 4 input channels and set the D to A voltage on the output pin
/* 1) send the i2c address #include <stdio.h> int i2c; int rc; int main(int argc, char** argv) address = atoi(argv[1]); /* decimal address is the first number after the program name */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ channel = channel + 64; /* enable the D/A output */ /*AD_line_x goes 0 to 255 as voltage input goes from 0 to supply voltage*/ i2c = close(i2c); |
LM75 I2C thermometer
/* int i2c; /* address is the first number after the program name */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ read(i2c,data,2); byte1 = data[0]; temperature = byte1*256+byte2; realtemp = (double)temperature / 2; i2c = close(i2c); /* Sunspot and Google 090815 */ |
more to come . . .