树莓派系列教程:I2C编程
例程是通过i2c控制pcf8574 IO,使Pioneer 600扩展板的LED2闪烁。
一、bcm2835
#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;
}
编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序
gcc –Wall pcf8574 –o pfc8574 –lbcm2835
sudo ./pcf8574
注:(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
#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;
}
编译并执行,扩展板上的LED2灯开始闪烁了,Ctrl +C结束程序
gcc –Wall pcf8574 –o pfc8574 –lbcm2835
sudo ./pcf8574
注:(1)fd = wiringPiI2CSetup(0x20);初始化I2C设备,0x20为PCF8574的I2C地址,返回值是标准的Linux文件句柄,如果错误则返回-1.由此可知,wiringPi底层也是通过sysfs方式操作I2C设备/dev/i2c-1 wiringPi还有pcf8574的扩展库,也可以调用pcf8574的库操作IO.
#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;
}
编译并执行
gcc –Wall pcf8574.c –o pcf8474 –lwiringPi
sudo ./pcf8574
更多bcm2835库i2c操作函数请查看:
http://wiringpi.com/reference/i2c-library/
http://wiringpi.com/extensions/i2c-pcf8574/
三、python
首先执行如下命令安装smbus库
sudo apt-get install python-smbus
编辑程序
#!/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)
执行程序:
sudo python pcf8574.py
注:(1) import smbus 导入smbus模块
(2)bus = smbus.SMBus(1) 在树莓派版本2中,I2C设备位于/dev/I2C-1,所以此处的编号为1 python封装SMBUS操作函数具体代码请查看: https://github.com/bivab/smbus-cffi
四、sysfs
从上面编程,我们可以发现,wiring,python程序都是通过读写i2c设备文件/dev/I2C-1操作i2c设备。故我们也可以用c语言读写文件的形式操作i2c设备。
#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;
}
编译并执行
gcc –Wall pcf8574.c –o pcf8574
sudo ./pcf8574
注:
- 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.
系列教程
- 树莓派系列教程:人生若只如初见
- 树莓派系列教程:烧写镜像
- 树莓派系列教程:远程登录树莓派(SSH登陆/远程桌面登陆/串口登陆)
- 树莓派系列教程:更换软件源
- 树莓派系列教程:访问树莓派
- 树莓派系列教程:配置WiFi
- 树莓派系列教程:系统配置(raspi-config)
- 树莓派系列教程:中文输入法
- 树莓派系列教程:摄像头
- 树莓派系列教程:Linux常用命令以及vi/vim编辑器
- 树莓派系列教程:文件共享(samba)
- 树莓派系列教程:wiringPi、bcm2835、python库安装
- 树莓派系列教程:如何点亮一个LED灯(上)
- 树莓派系列教程:如何点亮一个LED灯(下)
- 树莓派系列教程:按键
- 树莓派系列教程:I2C
- 树莓派系列教程:I2C编程
- 树莓派系列教程:I2C总线控制BMP180
- 树莓派系列教程:Serial串口
- 树莓派系列教程:红外遥控
- 树莓派系列教程:RTC
- 树莓派系列教程:PCF8591 AD/DA
- 树莓派系列教程:SPI
- 树莓派系列教程:物联网之MQTT
- Alphabot树莓派教程:变身路由器
- Alphabot树莓派教程:刷LEDE(OpenWRT)系统变身路由器
- 树莓派引脚对照表
- SIM868 ppp拨号上网