嵌入式学习小组
直播中

陈茹

7年用户 165经验值
私信 关注

如何通过i2c控制pcf8574 IO使Pioneer 600扩展板的LED2闪烁?

例程是通过i2c控制pcf8574 IO,使Pioneer 600扩展板的LED2闪烁。

回帖(4)

曹利娟

2020-11-5 16:15:56
bcm2835
#include   int main(int argc, char **argv)  {      char buf[1];    if (!bcm2835_init())return 1;      bcm2835_i2c_begin();     bcm2835_i2c_setSlaveAddress(0x20);  //i2c address    bcm2835_i2c_set_baudrate(10000);    //1M baudrate    while(1)      {           buf[0] = 0xEF;    //LED ON        bcm2835_i2c_write(buf,1);        bcm2835_delay(500);        buf[0] = 0xFF;     //LED OFF        bcm2835_i2c_write(buf,1);        bcm2835_delay(500);     }        bcm2835_i2c_end();      bcm2835_close();      return 0;  }


  • 编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序

gcc -Wall pcf8574.c -o pfc8574 -lbcm2835sudo ./pcf8574
注:(1)bcm2835_i2c_begin(); 启动i2c操作,设置I2C相关引脚为复用功能

    (2)bcm2835_i2c_setSlaveAddress(0x20);  设置I2C从机设备的地址,此处为0x20。即PCF8574的地址。

      (3)bcm2835_i2c_write(buf,1);传输字节到i2c从设备,buf为要传输的数据,1表示传输一个字节
举报

陈艳

2020-11-5 16:16:10
wiringPi
#include #include int main (void){    int fd;     wiringPiSetup();    fd = wiringPiI2CSetup(0x20);    while (1)     {           wiringPiI2CWrite(fd,0xEF);  //LED ON        delay(500);        wiringPiI2CWrite(fd,0xFF);  //LED OFF        delay(500);    }       return 0;}


  • 编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序
  • gcc -Wall pcf8574.c -o pfc8574 -lbcm2835sudo ./pcf8574
    注:(1)fd = wiringPiI2CSetup(0x20);初始化I2C设备,0x20为PCF8574的I2C地址,返回值是标准的Linux文件句柄,如果错误则返回-1.由此可知,wiringPi底层也是通过sysfs方式操作I2C设备/dev/i2c-1

  • wiringPi还有pcf8574的扩展库,也可以调用pcf8574的库操作IO.

#include #include=#define EXTEND_BASE 64#define LED EXTEND_BASE + 4int main (void){    wiringPiSetup();    pcf8574Setup(EXTEND_BASE,0x20);    pinMode(LED,OUTPUT);    while (1)     {           digitalWrite(LED,LOW);  //LED ON        delay(500);        digitalWrite(LED,HIGH); //LED OFF        delay(500);    }       return 0;}
举报

蒋进如

2020-11-5 16:16:19
python



  • 首先执行如下命令安装smbus库

sudo apt-get install python-smbus


  • 编辑程序

#!/usr/bin/python# -*- coding:utf-8 -*-import smbusimport timeaddress = 0x20bus = smbus.SMBus(1)while True:    bus.write_byte(address,0xEF)    time.sleep(0.5)    bus.write_byte(address,0xFF)    time.sleep(0.5)


  • 执行程序:

sudo python pcf8574.py
注:(1)import smbus 导入smbus模块

    (2)bus = smbus.SMBus(1) 在树莓派版本2中,I2C设备位于/dev/I2C-1,所以此处的编号为1
举报

徐小婷

2020-11-5 16:16:31
sysfs



  • 从上面编程,我们可以发现,wiring,python程序都是通过读写i2c设备文件/dev/I2C-1操作i2c设备。故我们也可以用c语言读写文件的形式操作i2c设备。

#include #include #include#include#include#define I2C_ADDR 0x20#define LED_ON  0xEF#define LED_OFF 0xFFint main (void) {    int value;    int fd;     fd = open("/dev/i2c-1", O_RDWR);    if (fd < 0) {        printf("Error opening file: %sn", strerror(errno));        return 1;    }       if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) {        printf("ioctl error: %sn", strerror(errno));        return 1;    }       while(1)    {           if(value == LED_ON)value = LED_OFF;        else value = LED_ON;        if( write( fd , &value, 1 ) != 1) {            printf("Error writing file: %sn", strerror(errno));        }            usleep(1000000);     }    return 0;}编译并执行
gcc -Wall pcf8574.c -o pcf8574sudo ./pcf8574
注:(1)fd = open(“/dev/i2c-1”, O_RDWR); 打开设备,树莓派版本2的I2C设备位于/dev/i2c-1

    (2)ioctl(fd, I2C_SLAVE, I2C_ADDR) ; 设置I2C从设备地址,此时PCF8574的从机地址为0x20。I

    (3) write( fd , &value, 1 );向PCF8574写入一个字节,value便是写入的内容,写入的长度为1.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分