学习了在orangepi的Linux下的i2c驱动和示例代码的运行。
IIC全称Inter-Integrated Circuit (集成电路总线),是由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备。IIC属于半双工同步通信方式。
特点:
1. 简单性和有效性
由于接口直接在组件之上,因此IIC总线占用的空间非常小,减少了电路板的空间和芯片管脚的数量,降低了互联成本。总线的长度可高达25英尺,并且能够以10Kbps的最大传输速率支持40个组件
2. 多主控(multimastering)
其中任何能够进行发送和接收的设备都可以成为主总线。一个主控能够控制信号的传输和时钟频率。当然,在任何时间点上只能有一个主控。
构成:
IIC串行总线一般有两根信号线,一根是双向的数据线SDA,另一根是时钟线SCL,其时钟信号是由主控器件产生。所有接到IIC总线设备上的串行数据SDA都接到总线的SDA上,各设备的时钟线SCL接到总线的SCL上。对于并联在一条总线上的每个IC都有唯一的地址。
八拉八拉,还有包括iic的答应、非答应信号,和启动时序在wiringPi库里面都帮我们写好了,就不多说了。其实这个到阶段应该都熟悉STC51单片机、STM32单片机等了,对于iic的时序应该见怪不怪了。
在全志h616的手册和引脚图里面可以看到,orangepi的i2c引脚SDA SCK在26pin区。且支持 的i2c是i2c-3。
启动Linux系统确定在/dev目录下存在i2c的驱动文件。正常安装wiringpi的话,应该是有i2c-3和i2c-5的。
确认有驱动文件后安装i2c-tools。
在任意文件夹下键入以下指令
sudo apt-get install i2c-tools
在刷屏安装完成后,会显示以下内容:
表示系统识别到了地址为3c的i2c设备(驱动)。
在wiringpi给的示例代码中有一个oled_demo.c,我们将其复制到,工作代码文件夹。用vi编辑器打开它可以看到以下代码:
/*
* Copyright (c) 2015, Vladimir Komendantskiy
* MIT License
*
* SSD1306 demo of block and font drawing.
*/
//
// fixed for OrangePiZero by HypHop
//
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include "oled.h"
#include "font.h"
int oled_demo(struct display_info *disp) {
int i;
char buf[100];
//putstrto(disp, 0, 0, "Spnd spd 2468 rpm");
// oled_putstrto(disp, 0, 9+1, "Spnd cur 0.46 A");
oled_putstrto(disp, 0, 9+1, "Welcome to");
disp->font = font1;
// oled_putstrto(disp, 0, 18+2, "Spnd tmp 53 C");
oled_putstrto(disp, 0, 18+2, "----OrangePi----");
disp->font = font2;
// oled_putstrto(disp, 0, 27+3, "DrvX tmp 64 C");
oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
oled_putstrto(disp, 0, 36+4, "");
oled_putstrto(disp, 0, 45+5, "");
disp->font = font1;
// oled_putstrto(disp, 0, 54, "Total cur 2.36 A");
oled_putstrto(disp, 0, 54, "*****************");
oled_send_buffer(disp);
disp->font = font3;
for (i=0; i<100; i++) {
sprintf(buf, "Spnd spd %d rpm", i);
oled_putstrto(disp, 0, 0, buf);
oled_putstrto(disp, 135-i, 36+4, "===");
oled_putstrto(disp, 100, 0+i/2, ".");
oled_send_buffer(disp);
}
//oled_putpixel(disp, 60, 45);
//oled_putstr(disp, 1, "hello");
return 0;
}
void show_error(int err, int add) {
//const gchar* errmsg;
//errmsg = g_strerror(errno);
printf("\nERROR: %i, %i\n\n", err, add);
//printf("\nERROR\n");
}
void show_usage(char *progname) {
printf("\nUsage:\n%s <I2C bus device node >\n", progname);
}
int main(int argc, char **argv) {
int e;
char filename[32];
struct display_info disp;
if (argc < 2) {
show_usage(argv[0]);
return -1;
}
memset(&disp, 0, sizeof(disp));
sprintf(filename, "%s", argv[1]);
disp.address = OLED_I2C_ADDR;
disp.font = font2;
e = oled_open(&disp, filename);
if (e < 0) {
show_error(1, e);
} else {
e = oled_init(&disp);
if (e < 0) {
show_error(2, e);
} else {
printf("---------start--------\n");
if (oled_demo(&disp) < 0)
show_error(3, 777);
printf("----------end---------\n");
}
}
return 0;
}
编译运行的时候要注意加上i2c-3这个驱动文件不然无法运行:
正确的运行方式:
当oled屏幕正确接上去时,屏幕上就会开始显示测试内容。
至于具体怎么实现的,可以把wiping库的源码拿来啃。而我实力有限看不懂,就不哔哔了。
观察上面的oled_demo.c的代码,其实只要把oled_demo()函数稍微改一下就可以显示指定内容。
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include "oled.h"
#include "font.h"
int oled_show(struct display_info *disp) {
int i;
char buf[100];
oled_putstrto(disp, 0, 9+1, "Welcome to");
disp->font = font1;
oled_putstrto(disp, 0, 9+9, "orangepi i2c oled");
disp->font = font3;
oled_send_buffer(disp);
return 0;
}
void show_usage(char *progname) {
printf("\nUsage:\n%s <I2C bus device node >\n", progname);
}
int main(int argc, char **argv) {
int e;
char filename[32];
struct display_info disp;
if (argc < 2) {
show_usage(argv[0]);
return -1;
}
memset(&disp, 0, sizeof(disp));
sprintf(filename, "%s", argv[1]);
disp.address = OLED_I2C_ADDR;
disp.font = font2;
e = oled_open(&disp, filename);
e = oled_init(&disp);
oled_show(&disp);
return 0;
}
接下来的操作就和51、32单片机如出一辙了,不多解释啦。