1.IIC协议介绍
IIC全称Inter-Integrated Circuit (集成电路总线) 简称I2C
是由PHILIPS公司在80年代开发的两线式串行总线,用于连接微控制器及其外围设备。IIC属于半双工同步通信方式
特点
简单性和有效性
由于接口直接在组件之上,因此IIC总线占用的空间非常小,减少了电路板的空间和芯片管脚的数量,降低了互联成本。总线的长度可高达25英尺,并且能够以10Kbps的最大传输速率支持40个组件
多主控(multimastering)
其中任何能够进行发送和接收的设备都可以成为主总线。一个主控能够控制信号的传输和时钟频率。当然,在任何时间点上只能有一个主控。
构成
IIC串行总线一般有两根信号线,一根是双向的数据线SDA,另一根是时钟线SCL,其时钟信号是由主控器件产生。所有接到IIC总线设备上的串行数据SDA都接到总线的SDA上,各设备的时钟线SCL接到总线的SCL上。对于并联在一条总线上的每个IC都有唯一的地址。
2.项目效果
在OLED屏幕上显示字符或者图像
3.IIC协议
就是根据IIC的时序对相应的寄存器写入数据,进行操作。
IIC总线在传输数据的过程中一共有三种类型信号,分别为:开始信号、结束信号和应答信号。
起始位,停止位,数据位,速度
这些信号中,起始信号是必需的,结束信号和应答信号
应答信号
发送器每发送一个字节(8个bit),在时钟脉冲9期间释放数据线,由接收器反馈一个应答信号。
应答信号为低电平时,规定为有效应答位(ACK,简称应答位),表示接收器已经成功地接收了该字节;
应答信号为高电平时,规定为非应答位(NACK),一般表示接收器接收该字节没有成功。
数据发送的时序
4.OLED写命令
4.1 OLED的寻址模式
寻址模式的寄存器
页地址模式
水平地址模式
行地址(page x)选择
列地址选择 (分低位设置和高位设置)
主函数最后 用while(1) 卡主,不然会一直设置OLED的屏幕,一直刷新。
5.代码实现- 图
需要配合 字膜提取软件+画图工具(自带) 转换为分辨率位128x64的bmp图像。
#include "reg52.h"
#include "intrins.h"
sbit sda=P0^3;
sbit scl=P0^1;
void i2c_start(void);
void i2c_stop(void);
char ack(void);
void send_messege_i2c(char messege);
void w_cmd_i2c(char cmd);
void w_data_i2c(char data1);
void init_i2c(void);
void clear_oled(void);
code char tx[]={
0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x01,0x03,0x03,0x03,0x07,0x07,0x05,0x07,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x01,
0x05,0x0F,0x0F,0x0F,0x07,0x05,0x07,0x05,0x07,0x07,0x07,0x0F,0x0F,0x0F,0x0F,0x0F,
0x0F,0x4F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,
0x1F,0x3F,0x1F,0x1F,0x7F,0x1F,0x1F,0x9F,0x9F,0x3F,0x7F,0x6F,0x6F,0x5F,0x9F,0xBF,
0xBF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x03,0x1F,0x1F,0x1F,0x3F,0x3F,0x1F,
0xCF,0xEF,0xF7,0xD7,0xDF,0xFB,0xFF,0x9F,0x9E,0x9F,0x8F,0x8F,0x9F,0x9F,0x9F,0xBF,
0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0xF0,0xF0,0xF8,0xF8,0xF8,0xF8,0xFC,0xFC,
0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFC,0xE0,0xC0,0x80,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x0C,0x44,0x60,0xF5,0xE0,0xC0,0xC0,0xC0,0xC0,0xC0,0xD8,0xFE,0xE7,0xE5,
0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x80,0x80,0x00,0x08,0x08,
0xC8,0x98,0x98,0x88,0x80,0x80,0xC0,0x81,0x81,0xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xDF,0x87,0x87,0xC3,0xC3,0xA3,0x91,0x91,0x11,0x11,0x19,0x99,0x89,0x09,0x09,
0x01,0x83,0xC3,0xC2,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x03,0x37,0x3D,0xBF,0xBF,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0x00,0x00,0x20,0x40,0x40,0x40,0x40,0x06,0x06,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1E,0x3E,0x7C,0x70,0xE0,0xC3,0x1F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,
0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x40,0x70,0x3C,0x1C,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xC0,0xA0,0xC0,0xC0,0x00,0xC5,0xFF,0xFF,0xFF,0xFF,0xFD,0xFD,0xFF,0xFF,0xFF,
0xFF,0xFF,0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x00,0xC0,0xC0,0x00,0xC0,0xC0,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0F,0x3F,0x7F,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xDF,0x9F,0x9F,0x1F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,0x9E,0x9C,0xFF,0xFB,0xFA,0xFC,0xFC,0xFC,0xEC,0xE8,
0xF8,0xF0,0xB0,0xB0,0xA0,0xE0,0xC0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x00,0x00,0x20,
0x00,0x00,0x00,0x40,0xC0,0xC0,0x44,0x04,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,
0xE0,0xD0,0xB0,0xF0,0xF8,0xF8,0xB8,0x9C,0xDC,0xDC,0x7E,0xFC,0xE4,0xEC,0xEC,0xF4,
0xF8,0xF8,0xFC,0xFC,0xF4,0xFE,0xFE,0xFE,0xFE,0xFF,0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; //这是page7的内容
void main(void)
{
unsigned char i;
unsigned int j;
/*-- 文字: 我 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
init_i2c();
clear_oled(); //清屏函数
w_cmd_i2c(0x20); //选择页寻址模式
w_cmd_i2c(0x02);
//w_cmd_i2c(0xB0); //选择page 1011 0xxx
//w_cmd_i2c(0x00); //设置起始地址 低位
//w_cmd_i2c(0x10); //设置起始地址 高位
//w_data_i2c(0x80); //发送数据
for(i=0;i<8;i++){
w_cmd_i2c(0xB0+i); //变换page 共有0-7 8个
w_cmd_i2c(0x00); //设置起始地址 低位
w_cmd_i2c(0x10); //设置起始地址 高位
//i=3;
for(j=128*i; j<(128*(i+1)); j++){ //变换的是tx中的数据
w_data_i2c(tx[j]); //tx中的数据
}
}
while(1){}
}
//下面是函数封装
void clear_oled(void)
{
char i; int j;
for(i=0;i<8;i++){
w_cmd_i2c(0xB0+i); //变换page
w_cmd_i2c(0x00); //设置起始地址 低位
w_cmd_i2c(0x10); //设置起始地址 高位
for(j=0;j<128;j++){
w_data_i2c(0x00); //写0就行
}
}
}
void init_i2c(void)
{
w_cmd_i2c(0xAE);//--display off
w_cmd_i2c(0x00);//---set low column address
w_cmd_i2c(0x10);//---set high column address
w_cmd_i2c(0x40);//--set start line address
w_cmd_i2c(0xB0);//--set page address
w_cmd_i2c(0x81); // contract control
w_cmd_i2c(0xFF);//--128
w_cmd_i2c(0xA1);//set segment remap
w_cmd_i2c(0xA6);//--normal / reverse
w_cmd_i2c(0xA8);//--set multiplex ratio(1 to 64)
w_cmd_i2c(0x3F);//--1/32 duty
w_cmd_i2c(0xC8);//Com scan direction
w_cmd_i2c(0xD3);//-set display offset
w_cmd_i2c(0x00);//
w_cmd_i2c(0xD5);//set osc division
w_cmd_i2c(0x80);//
w_cmd_i2c(0xD8);//set area color mode off
w_cmd_i2c(0x05);//
w_cmd_i2c(0xD9);//Set Pre-Charge Period
w_cmd_i2c(0xF1);//
w_cmd_i2c(0xDA);//set com pin configuartion
w_cmd_i2c(0x12);//
w_cmd_i2c(0xDB);//set Vcomh
w_cmd_i2c(0x30);//
w_cmd_i2c(0x8D);//set charge pump enable
w_cmd_i2c(0x14);//
w_cmd_i2c(0xAF);//--turn on oled panel
}
void w_cmd_i2c(char cmd)
{
i2c_start();
send_messege_i2c(0x78);
ack();
send_messege_i2c(0x00);
ack();
send_messege_i2c(cmd);
ack();
i2c_stop();
}
void w_data_i2c(char data1)
{
i2c_start();
send_messege_i2c(0x78);
ack();
send_messege_i2c(0x40);
ack();
send_messege_i2c(data1);
ack();
i2c_stop();
}
void i2c_start(void)
{
scl=1;
sda=0;
sda=1; //我的
/*
scl=0; //老师的
sda=1;
scl=1;*/
_nop_(); //
sda=0;
_nop_(); //
scl=0;
}
void i2c_stop(void)
{
scl=0;
sda=0;
scl=1;
_nop_(); //
sda=1;
_nop_(); //
sda=0; //老师不要这个
}
char ack(void) //应答信号
{
char flag;
sda=1;
//_nop_(); //老师要的
scl=1;
_nop_();
flag=sda;
//_nop_(); //老师要的
scl=0;
_nop_();
return flag;
}
void send_messege_i2c(char messege)
{
char i;
for(i=0;i<8;i++){
scl=0;
_nop_(); //必要
sda= messege & 0x80 ;
//_nop_(); //老师要的
scl=1;
_nop_();
scl=0;
messege=messege<<1;
}
}
6.编译的注意事项
其中的data 、xdata为ram的中的数据,可以随程序执行变化。
code中为rom中数据,调用后更改不了。5中的图片点阵太大,也不需要更改,编码在code中即可