匿名
未登录
登录
丢石头百科
搜索
查看“树莓派系列教程:I2C编程”的源代码
来自丢石头百科
名字空间
页面
讨论
更多
更多
页面选项
查看
查看源代码
历史
←
树莓派系列教程:I2C编程
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
例程是通过i2c控制pcf8574 IO,使Pioneer 600扩展板的LED2闪烁。 == 一、bcm2835 == <syntaxhighlight lang="python"> #include <bcm2835.h> 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; } </syntaxhighlight> 编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序 <syntaxhighlight lang="python"> gcc –Wall pcf8574 –o pfc8574 –lbcm2835 sudo ./pcf8574</syntaxhighlight> 注:(1)bcm2835_i2c_begin(); 启动i2c操作,设置I2C相关引脚为复用功能 (2)bcm2835_i2c_setSlaveAddress(0x20); 设置I2C从机设备的地址,此处为0x20。即PCF8574的地址。 (3)bcm2835_i2c_write(buf,1); 传输字节到i2c从设备,buf为要传输的数据,1表示传输一个字节 更多bcm2835库i2c操作函数请查看: http://www.airspayce.com/mikem/bcm2835/group__i2c.html#ga1309569f7363853333f3040b1553ea64 == 二、wiringPi == <syntaxhighlight lang="python"> #include <wiringpi.h> #include <wiringpii2c.h> 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; }</syntaxhighlight> 编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序 <syntaxhighlight lang="python"> gcc –Wall pcf8574 –o pfc8574 –lbcm2835 sudo ./pcf8574</syntaxhighlight> 注:(1)fd = wiringPiI2CSetup(0x20);初始化I2C设备,0x20为PCF8574的I2C地址,返回值是标准的Linux文件句柄,如果错误则返回-1.由此可知,wiringPi底层也是通过sysfs方式操作I2C设备/dev/i2c-1 wiringPi还有pcf8574的扩展库,也可以调用pcf8574的库操作IO. <syntaxhighlight lang="python"> #include <wiringpi.h > #include=<pcf8574.h> #define EXTEND_BASE 64 #define LED EXTEND_BASE + 4 int 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; }</syntaxhighlight> 编译并执行 <syntaxhighlight lang="python"> gcc –Wall pcf8574.c –o pcf8474 –lwiringPi sudo ./pcf8574</syntaxhighlight> 更多bcm2835库i2c操作函数请查看: http://wiringpi.com/reference/i2c-library/ http://wiringpi.com/extensions/i2c-pcf8574/ == 三、python == 首先执行如下命令安装smbus库 <syntaxhighlight lang="python"> sudo apt-get install python-smbus</syntaxhighlight> === === 编辑程序<syntaxhighlight lang="python"> #!/usr/bin/python # -*- coding:utf-8 -*- import smbus import time address = 0x20 bus = smbus.SMBus(1) while True: bus.write_byte(address,0xEF) time.sleep(0.5) bus.write_byte(address,0xFF) time.sleep(0.5)</syntaxhighlight> 执行程序: <syntaxhighlight lang="python"> sudo python pcf8574.py</syntaxhighlight> 注:(1) import smbus 导入smbus模块 (2)bus = smbus.SMBus(1) 在树莓派版本2中,I2C设备位于/dev/I2C-1,所以此处的编号为1 python封装SMBUS操作函数具体代码请查看: https://github.com/bivab/smbus-cffi == 四、<b>sysfs</b> == 从上面编程,我们可以发现,wiring,python程序都是通过读写i2c设备文件/dev/I2C-1操作i2c设备。故我们也可以用c语言读写文件的形式操作i2c设备。 <syntaxhighlight lang="python"> #include <linux i2c-dev.h> #include <errno.h> #define I2C_ADDR 0x20 #define LED_ON 0xEF #define LED_OFF 0xFF int main (void) { int value; int fd; fd = open("/dev/i2c-1", 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; } while(1) { if(value == LED_ON)value = LED_OFF; else value = LED_ON; if( write( fd , &value, 1 ) != 1) { printf("Error writing file: %s\n", strerror(errno)); } usleep(1000000); } return 0; }</syntaxhighlight> 编译并执行 <syntaxhighlight lang="python"> gcc –Wall pcf8574.c –o pcf8574 sudo ./pcf8574</syntaxhighlight> 注: # fd = open("/dev/i2c-1", O_RDWR); 打开设备,树莓派版本2的I2C设备位于/dev/i2c-1 # ioctl(fd, I2C_SLAVE, I2C_ADDR) ; 设置I2C从设备地址,此时PCF8574的从机地址为0x20。 # write( fd , &value, 1 );向PCF8574写入一个字节,value便是写入的内容,写入的长度为1. == 系列教程 == {{Raspberry Pi Study}}
该页面使用的模板:
模板:Raspberry Pi Study
(
查看源代码
)
返回至
树莓派系列教程:I2C编程
。
导航
导航
首页
最近更改
随机页面
MediaWiki帮助
首页
首页
树莓派
主机
配件包
外壳
键鼠
电源
扩展板
显示屏
墨水屏
摄像模块
通信模块
继电器
电机驱动板
游戏机
产品分类
树莓派
Arduino
micro:bit
STM32
Espressif
WiFi模块
蓝牙模块
无线模块
LoRa模块
4G模块
GSM
GPRS
以太网
导航模块
北斗卫星
GPS
LCD
墨水屏
OLED
摄像头
USB模块
串口模块
RS232
RS485
CAN
传感器
温度模块
湿度模块
气压模块
继电器
电机模块
指纹模块
电平转换
音频模块
编程器
Wiki工具
Wiki工具
特殊页面
页面工具
页面工具
用户页面工具
更多
链入页面
相关更改
页面信息
页面日志