$ sudo raspi-config
安装 i2c 工具
pi@raspberrypi:~$ sudo apt-get install i2c-tools
连接 i2c 设备,执行测试命令
pi@raspberrypi:~$ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
成功读取到从机地址为:3c,显而易见3c本非物理从机地址,而是虚拟从机地址
如果出现 Error: Could not open file /dev/i2c-?' or
/dev/i2c/?': No such file or directory报错,可以修改 -y 参数:0或1或2等
#include
#include
int wiringPiI2CSetup (int devId);初始化wiringPi库i2c,devId是i2c设备地址,返回值是linux的文件描述符
int wiringPiI2CRead (int fd);直接读取数据,fd是wiringPiI2CSetup函数的返回值,返回值是读到的数据
int wiringPiI2CWrite (int fd, int data);直接写数据,fd同上,data是写入的数据
int wiringPiI2CWriteReg8 (int fd, int reg, int data);写数据到8位寄存器,fd同上,reg寄存器地址,data要写入的数据
int wiringPiI2CWriteReg16 (int fd, int reg, int data);写数据到16位寄存器,fd同上,reg寄存器地址,data要写入的数据
int wiringPiI2CReadReg8 (int fd, int reg);从寄存器读取8位数据,fd同上,reg寄存器地址,返回值读取到的数据
int wiringPiI2CReadReg16 (int fd, int reg);从寄存器读取16位数据,fd同上,reg寄存器地址,返回值读取到的数据
#include
#include
#include
#define IIC_ADRR 0x3c
#define OLED_CMD 0x00 //写命令
#define OLED_DATA 0x40 //写数据
void OLED_Clear(int fd)
{
unsigned char i,n;
for(i=0;i<8;i++)
{
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xb0+i); //设置页地址(0~7)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x00); //设置显示位置—列低地址
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x10); //设置显示位置—列高地址
for(n=0;n<128;n++)wiringPiI2CWriteReg8(fd, OLED_DATA, 0);
} //更新显示
}
void OLED_Init(int fd)
{
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x00);//---set low column address
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x10);//---set high column address
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x81);//--set contrast control register
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xCF); // Set SEG Output Current Brightness
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xA6);//--set normal display
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xA8);//--set multiplex ratio(1 to 64)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x3f);//--1/64 duty
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x00);//-not offset
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xd5);//--set display clock divide ratio/oscillator frequency
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xD9);//--set pre-charge period
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xDA);//--set com pins hardware configuration
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x12);
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xDB);//--set vcomh
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x40);//Set VCOM Deselect Level
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x02);//
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x8D);//--set Charge Pump enable/disable
wiringPiI2CWriteReg8(fd, OLED_CMD, 0x14);//--set(0x10) disable
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xA4);// Disable Entire Display On (0xa4/0xa5)
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xA6);// Disable Inverse Display On (0xa6/a7)
OLED_Clear(fd);
wiringPiI2CWriteReg8(fd, OLED_CMD, 0xAF); /*display ON*/
}
int main(int argc, char const *argv[])
{
int i2c_fd;
if(wiringPiSetup() != 0)
{
printf("wiringPi error!\n");
return -1;
}
i2c_fd = wiringPiI2CSetup(IIC_ADRR);
OLED_Init(i2c_fd);
wiringPiI2CWriteReg8(i2c_fd, OLED_CMD, 0xB0);
//画一条直线
for (int i = 0; i < 100; i++)
{
wiringPiI2CWriteReg8(i2c_fd, OLED_DATA, 0x80);
}
while (1);
return 0;
}