1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#define BASE_ADDR 0x27
int main()
{
int fd;
char writeGpio0Cmd[] = {0x4e, 0x00}; //0x4e = 0x27 << 1
char readGpio0Cmd[] = {0x4f}; //0x4f = 0x27 << 1 + 1
char gpio0 = 0x00;
fd = open("/dev/i2c-1", O_RDWR);
if(fd < 0)
{
perror("open");
exit(-1);
}
if(ioctl(fd, I2C_SLAVE, BASE_ADDR) < 0)
{
perror("ioctl");
exit(-1);
}
//par défaut GP0 est configuré en entrée
//pour lire un registre:
//send slave address, setting R/W bit to 0, then the command byte
write (fd, &writeGpio0Cmd[0], sizeof(char));
write (fd, &writeGpio0Cmd[1], sizeof(char));
//send slave address, setting R/W bit to 1
write (fd, readGpio0Cmd, sizeof(readGpio0Cmd));
read(fd, &gpio0, sizeof(char));
printf("gpio0 = 0x%x\n", gpio0);
close(fd);
return 0;
} |
Partager