1.IIC协议概述
IIC全称Inter-Integrated Circuit (集成电路总线)
是由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备。IIC属于半双工同步通信方式
(1)特点:
简单性和有效性。
由于接口直接在组件之上,因此IIC总线占用的空间非常小,减少了电路板的空间和芯片管脚的数量,降低了互联成本。总线的长度可高达25英尺,并且能够以10Kbps的最大传输速率支持40个组件
多主控(multimastering)
其中任何能够进行发送和接收的设备都可以成为主总线。一个主控能够控制信号的传输和时钟频率。当然,在任何时间点上只能有一个主控。
(2)构成
IIC串行总线一般有两根信号线,一根是双向的数据线SDA,另一根是时钟线SCL,其时钟信号是由主控器件产生。所有接到IIC总线设备上的串行数据SDA都接到总线的SDA上,各设备的时钟线SCL接到总线的SCL上。对于并联在一条总线上的每个IC都有唯一的地址。
IIC总线在传输数据的过程中一共有三种类型信号,分别为:开始信号、结束信号和应答信号。
//起始位,停止位,数据位,速度
这些信号中,起始信号是必需的,结束信号和应答信号
应答信号:
发送器每发送一个字节(8个bit),就在时钟脉冲9期间释放数据线,由接收器反馈一个应答信号。应答信号为低电平时,规定为有效应答位(ACK,简称应答位),表示接收器已经成功地接收了该字节;应答信号为高电平时,规定为非应答位(NACK),一般表示接收器接收该字节没有成功。
2.orangepiZero2的IIC接口
oled屏幕
启动 linux 系统后, 先确认下/dev 下存在 i2c-3 的设备节点
从命令运行结果能观察到系统支持I2C-3和I2C-5的驱动,而H616的外设我们看到只有一个IIC接
口,用的是IIC-3
Linux一切皆文件,每个硬件设备“对应”一个文件,由驱动程序提供映射
开始测试 i2c, 首先安装 i2c-tools
sudo apt-get install i2c-tools
3.代码编写
//1.oled.c
#include <errno.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include <stdint.h>
18
19 #include "oled.h"
20 #include "font.h"
21
22 int oled_demo(struct display_info *disp) {
23 int i;
24 char buf[100];
25
26 //putstrto(disp, 0, 0, "Spnd spd 2468 rpm");
27 // oled_putstrto(disp, 0, 9+1, "Spnd cur 0.46 A");
28 oled_putstrto(disp, 0, 9+1, "Welcome to");
29 disp->font = font1;
30 // oled_putstrto(disp, 0, 18+2, "Spnd tmp 53 C");
31 oled_putstrto(disp, 0, 18+2, "----OrangePi----");
32 disp->font = font2;
33 // oled_putstrto(disp, 0, 27+3, "DrvX tmp 64 C");
34 oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
35 oled_putstrto(disp, 0, 36+4, "");
36 oled_putstrto(disp, 0, 45+5, "");
37 disp->font = font1;
38 // oled_putstrto(disp, 0, 54, "Total cur 2.36 A");
39 oled_putstrto(disp, 0, 54, "*****************");
40 oled_send_buffer(disp);
41
42 disp->font = font3;
43 for (i=0; i<100; i++) {
44 sprintf(buf, "Spnd spd %d rpm", i);
45 oled_putstrto(disp, 0, 0, buf);
46 oled_putstrto(disp, 135-i, 36+4, "===");
47 oled_putstrto(disp, 100, 0+i/2, ".");
48 oled_send_buffer(disp);
49 }
50 //oled_putpixel(disp, 60, 45);
51 //oled_putstr(disp, 1, "hello");
52
53 return 0;
54 }
55
56 void show_error(int err, int add) {
57 //const gchar* errmsg;
58 //errmsg = g_strerror(errno);
59 printf("\nERROR: %i, %i\n\n", err, add);
60 //printf("\nERROR\n");
61 }
62
63 void show_usage(char *progname) {
64 printf("\nUsage:\n%s <I2C bus device node >\n", progname);
65 }
67 int main(int argc, char **argv) {
68 int e;
69 char filename[32];
70 struct display_info disp;
71
72 if (argc < 2) {
73 show_usage(argv[0]);
74
75 return -1;
76 }
77
78 memset(&disp, 0, sizeof(disp));
79 sprintf(filename, "%s", argv[1]);
80 disp.address = OLED_I2C_ADDR;
81 disp.font = font2;
82
83 e = oled_open(&disp, filename);
84
85 if (e < 0) {
86 show_error(1, e);
87 } else {
88 e = oled_init(&disp);
89 if (e < 0) {
90 show_error(2, e);
91 } else {
92 printf("---------start--------\n");
93 if (oled_demo(&disp) < 0)
94 show_error(3, 777);
95 printf("----------end---------\n");
96 }
97 }
98
99 return 0;
100 }
4.编译
现创建一个shell脚本 build.sh vi build.sh打开脚本文件
gcc $1 -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt
:wq 保存并退出
chmod +x build.sh
./build oled.c
5.运行
sudo ./a.out /dev/i2c-3