一、IIC协议
(一)概述
IIC(集成电路总线)是由飞利浦公司发明的通讯总线,用于连接微控制器及其外围设备,IIC属于半双工同步通信方式。IIC的数据线只有一根,就是SDA,同一时间不管发送还是接收,只能有一个方向进行操作。
(二)构成
IIC串行总线一般有两根信号线,一根是双向的数据SDA,另一根是时钟线SCL,其中时钟信号是由主控器件产生,所有接到IIC总线设备上的串行数据SDA都接到总线的SDA上,各设备的时钟线SCL接到总线的SCL上,对于并联在一条总线上的每个IC都有唯一的地址。
(三)时序分析
IIC总线在传输数据的过程中一共有三种类型信号,分别为:开始信号、结束信号和应答信号。
1.开始信号
时钟线处于高电平,数据线由高电平转为低电平。
2.结束信号
时钟线处于高电平,数据线由低电平转为高电平。
3.应答信号
发送器每发送一个字节(8个bit),就在时钟脉冲9期间释放数据线,由接收器反馈一个应答信号。应答信号为低电平时,规定为有效应答位(ACK,简称应答位),表示接收器已经成功地接收了该字节;应答信号为高电平时,规定为非应答位(NACK),一般表示接收器接收该字节没有成功。
(四)代码示例
IIC协议基本函数封装
#include "reg52.h"
#include <intrins.h>
sbit scl = P0^1;
sbit sda = P0^3;
void IIC_Start()
{
sda = 1;
scl = 1;
_nop_();
sda = 0;
}
void IIC_Stop()
{
sda = 0;
scl = 1;
_nop_();
sda = 1;
_nop_();
}
char IIC_ACK()
{
char flag;
sda = 1;//就在时钟脉冲9期间释放数据线
_nop_();
scl = 1;
_nop_();
flag = sda;
_nop_();
scl = 0;
_nop_();
return flag;
}
void IIC_Send_Byte(char dataSend)
{
int i;
for(i=0;i<8;i++){
scl = 0;//scl拉低,让sda做好数据准备
sda = dataSend & 0x80;//1000 0000获得dataSend的最高位
_nop_();//发送数据建立时间
scl = 1;//scl拉高开始发送
_nop_();
scl = 0;//发送完毕,拉低
_nop_();
dataSend = dataSend << 1;
}
}
void main()
{
}
二、OLED显示
(一)时序分析
写命令/数据编程步骤:
1. start()
2. 写入 b0111 1000 0x78
3. ACK
4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
5. ACK
6. 写入指令/数据
7. ACK
8. STOP
(二)显示一个点
1、初始化
2、显示位置
Oled_Write_Cmd(0x20); //选择一个位置,确认页寻址地址
Oled_Write_Cmd(0x02); //选择一个位置,确认页寻址地址
Oled_Write_Cmd(0xB0); //选择PAGE03、显示内容
Oled_Write_Data(0x08); //显示一个点
编程示例
#include "reg52.h"
#include <intrins.h>
sbit scl = P0^1;
sbit sda = P0^3;
void IIC_Start()
{
scl = 0;
sda = 1;
scl = 1;
_nop_();
sda = 0;
}
void IIC_Stop()
{
scl = 0;
scl = 1;
_nop_();
sda = 1;
_nop_();
}
char IIC_ACK()
{
char flag;
sda = 1;//就在时钟脉冲9期间释放数据线
_nop_();
scl = 1;
_nop_();
flag = sda;
_nop_();
scl = 0;
_nop_();
return flag;
}
void IIC_Send_Byte(char dataSend)
{
int i;
for(i=0;i<8;i++){
scl = 0;//scl拉低,让sda做好数据准备
sda = dataSend & 0x80;//1000 0000获得dataSend的最高位
_nop_();//发送数据建立时间
scl = 1;//scl拉高开始发送
_nop_();
scl = 0;//发送完毕,拉低
_nop_();
dataSend = dataSend << 1;
}
}
void OLED_Write_Cmd(char dataCmd)
{
//1.START
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte:(0)(0)000000 写入命令 (0)(1)000000 写入数据
IIC_Send_Byte(0x00);
//5.ACK
IIC_ACK();
//6.写入指令/数据
IIC_Send_Byte(dataCmd);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
void OLED_Write_Data(char dataData)
{
//1.START
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte:(0)(0)000000 写入命令 (0)(1)000000 写入数据
IIC_Send_Byte(0x40);
//5.ACK
IIC_ACK();
//6.写入指令/数据
IIC_Send_Byte(dataData);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
void OLED_Init(void)
{
OLED_Write_Cmd(0xAE);//--display off
OLED_Write_Cmd(0x00);//---set low column address
OLED_Write_Cmd(0x10);//---set high column address
OLED_Write_Cmd(0x40);//--set start line address
OLED_Write_Cmd(0xB0);//--set page address
OLED_Write_Cmd(0x81); // contract control
OLED_Write_Cmd(0xFF);//--128
OLED_Write_Cmd(0xA1);//set segment remap
OLED_Write_Cmd(0xA6);//--normal / reverse
OLED_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
OLED_Write_Cmd(0x3F);//--1/32 duty
OLED_Write_Cmd(0xC8);//Com scan direction
OLED_Write_Cmd(0xD3);//-set display offset
OLED_Write_Cmd(0x00);//
OLED_Write_Cmd(0xD5);//set osc division
OLED_Write_Cmd(0x80);//
OLED_Write_Cmd(0xD8);//set area color mode off
OLED_Write_Cmd(0x05);//
OLED_Write_Cmd(0xD9);//Set Pre-Charge Period
OLED_Write_Cmd(0xF1);//
OLED_Write_Cmd(0xDA);//set com pin configuartion
OLED_Write_Cmd(0x12);//
OLED_Write_Cmd(0xDB);//set Vcomh
OLED_Write_Cmd(0x30);//
OLED_Write_Cmd(0x8D);//set charge pump enable
OLED_Write_Cmd(0x14);//
OLED_Write_Cmd(0xAF);//--turn on oled panel
}
void main()
{
int a = 10;
//1.OLED初始化
OLED_Init();
//2.确认一个位置
//2.1确认页寻址模式
OLED_Write_Cmd(0x20);
OLED_Write_Cmd(0x02);
//2.2选择PAGE0 1011 0000
OLED_Write_Cmd(0xB0);
//3.显示一个点
OLED_Write_Data(0x08);
while(1);//防止程序退出
}
(三) 列地址
列初始地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
(四)显示字符/汉字/图片
先打开取模软件进行参数设置
取模格式选择C51格式
编程示例(只展示主程序部分)
1.显示字符‘A’
/*-- 文字: A --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
char A1[8] = {0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00};
char A2[8] = {0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20};
void main()
{
int i;
//1.OLED初始化
OLED_Init();
//2.确认一个位置
//2.1确认页寻址模式
OLED_Write_Cmd(0x20);
OLED_Write_Cmd(0x02);
OLED_Clear();
//2.2选择PAGE0
OLED_Write_Cmd(0xB0);
OLED_Write_Cmd(0x00);
OLED_Write_Cmd(0x10);
for(i=0;i<8;i++){
OLED_Write_Data(A1[i]);
}
//2.2选择PAGE1
OLED_Write_Cmd(0xB1);
OLED_Write_Cmd(0x00);
OLED_Write_Cmd(0x10);
for(i=0;i<8;i++){
OLED_Write_Data(A2[i]);
}
while(1);
}
2.显示“上官可编程”
/*-- 文字: A --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
char A1[8] = {0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00};
char A2[8] = {0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20};
/*-- 文字: 上 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char s1[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00};
code char s2[16] = {0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00};
/*-- 文字: 官 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char g1[16] = {0x10,0x0C,0x04,0xE4,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0xE4,0x04,0x14,0x0C,0x00};
code char g2[16] = {0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xF8,0x00,0x00,0x00};
/*-- 文字: 可 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char k1[16] = {0x00,0x02,0x02,0xF2,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x02,0x02,0x02,0x00};
code char k2[16] = {0x00,0x00,0x00,0x0F,0x04,0x04,0x04,0x0F,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00};
/*-- 文字: 编 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char b1[16] = {0x20,0x30,0xAC,0x63,0x30,0x00,0xFC,0x24,0x25,0x26,0x24,0x24,0x24,0x3C,0x00,0x00};
code char b2[16] = {0x22,0x67,0x22,0x12,0x52,0x38,0x07,0xFF,0x09,0x7F,0x09,0x3F,0x89,0xFF,0x00,0x00};
/*-- 文字: 程 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char c1[16] = {0x24,0x24,0xA4,0xFE,0x23,0x22,0x00,0x3E,0x22,0x22,0x22,0x22,0x22,0x3E,0x00,0x00};
code char c2[16] = {0x08,0x06,0x01,0xFF,0x01,0x06,0x40,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x41,0x00};
void main()
{
unsigned char i;
//1.OLED初始化
OLED_Init();
//2.确认一个位置
//2.1确认页寻址模式
OLED_Write_Cmd(0x20);
OLED_Write_Cmd(0x02);
OLED_Clear();
//2.2选择PAGE0 1011 0000
OLED_Write_Cmd(0xB0);
OLED_Write_Cmd(0x00);
OLED_Write_Cmd(0x10);
for(i=0;i<16;i++){
OLED_Write_Data(s1[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(g1[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(k1[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(b1[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(c1[i]);
}
//选择page1
OLED_Write_Cmd(0xB1);
OLED_Write_Cmd(0x00);
OLED_Write_Cmd(0x10);
for(i=0;i<16;i++){
OLED_Write_Data(s2[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(g2[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(k2[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(b2[i]);
}
for(i=0;i<16;i++){
OLED_Write_Data(c2[i]);
}
while(1);
}
3.显示bmp图片
code unsigned char bmpImage[] = {
/*-- 调入了一幅图像:C:\Users\25236\Desktop\嵌入式软件学习\2.51单片机\工具\字模软件\12864演示图片.bmp --*/
/*-- 宽度x高度=128x64 --*/
0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF0,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,
0xF0,0xE0,0xE0,0x60,0x70,0x70,0x30,0x30,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x1C,
0x0C,0x1C,0x1C,0x1C,0x18,0x18,0x18,0x18,0x18,0x30,0x30,0x30,0x60,0x60,0xE0,0xE0,
0xE0,0xF0,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xE0,0xC0,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,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,0x3E,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F,0x1F,0x07,0x07,0x03,
0x01,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,
0x01,0x01,0x03,0x07,0x0F,0x1F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,
0x7F,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x70,0x30,0x30,0x30,0xF0,0xE0,0x00,0x00,
0x40,0xE0,0x70,0x30,0x30,0x30,0x70,0xF0,0xE0,0x00,0x00,0x00,0xF0,0xF0,0x70,0x30,
0x30,0x70,0xF0,0xE0,0x00,0x00,0xC0,0xE0,0x70,0x30,0x30,0x30,0x30,0x74,0xFF,0xFF,
0x00,0x00,0x00,0xE0,0xF0,0x30,0x30,0x30,0x70,0xF0,0xE0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x03,0x07,0x0F,0x1F,0xFF,0xFF,0x0F,0x01,0x00,0x00,0x00,0x80,0xE0,0xF0,
0xF8,0xF8,0xFC,0xFC,0x7E,0x7E,0x7E,0xFE,0xFC,0xF8,0x60,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0xF8,0xFC,0xFC,0xFE,0x7E,0xFE,0xFC,0xF8,
0xF8,0xF0,0xE0,0x80,0x00,0x00,0x00,0x01,0x0F,0xFF,0xFF,0x1F,0x0F,0x07,0x03,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x60,0x60,0xE0,0x60,0x7F,0x3F,0x02,0x00,
0x38,0x7D,0xFE,0x66,0xE6,0x66,0x76,0x7F,0x7F,0x00,0x00,0x00,0x7F,0x7F,0x00,0x00,
0x00,0x00,0x7F,0x7F,0x00,0x00,0x1F,0x3F,0x7A,0x60,0xE0,0xE0,0x60,0x78,0x7F,0x7F,
0x00,0x00,0x38,0x7D,0x7F,0xE6,0x46,0x66,0x76,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xE0,0x00,0x00,0x00,0x3E,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F,0x03,0x00,0x00,0x00,0x60,0xF0,0xF8,
0xF8,0xF8,0xF0,0xF0,0x70,0x00,0x00,0x00,0x07,0x3F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x7F,0x00,0x00,0x80,0xE0,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07,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,0x01,0x03,0x0F,0x1C,0x38,0x60,0xE0,0xC1,0x81,
0x83,0x03,0x03,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x83,
0x83,0x83,0xC1,0xE0,0x70,0x38,0x1C,0x0F,0x03,0x80,0x80,0xA0,0xB0,0xB8,0xFC,0xF7,
0xF7,0x92,0x94,0xDC,0xF8,0xD0,0xC0,0x40,0x00,0x00,0x00,0xC0,0xC2,0x46,0xFC,0x40,
0x00,0x10,0x70,0xC7,0x9E,0xC0,0xFE,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xF0,
0xA0,0xB0,0xB3,0xFF,0xB2,0x90,0xB0,0xF0,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x84,0x84,0x84,0x86,0xB6,0xE6,0x96,0x8E,0x86,0x86,0xC0,0xC0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x3F,0xFF,0xFF,0x7F,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0x7C,0x0C,0x0C,0x0C,0x0C,0x0C,
0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFF,0x7F,0xFF,
0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x18,0x89,0x8C,0x96,0xB7,0xD5,0xD6,
0xD6,0xD6,0xDF,0xD7,0xCE,0x5C,0x18,0x18,0x08,0x00,0x00,0x00,0x00,0x40,0x7F,0xB0,
0xD8,0x6C,0x30,0x19,0x0F,0x1F,0x78,0xE0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x08,0x1F,
0x0C,0x0D,0x0C,0x7F,0xC4,0x84,0x86,0x87,0x80,0xF0,0xF0,0xF0,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x01,0x31,0x60,0xC0,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x7F,0xF8,0xE1,0xE1,0xC1,0xC1,0x81,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0xC1,0x81,0xC1,0xE1,0xF5,0xFE,0x1F,
0x01,0x00,0x00,0xE0,0xC0,0x60,0x60,0xC0,0xC0,0x00,0x01,0xE1,0xC1,0x61,0x61,0xC1,
0xC1,0x81,0x01,0x01,0xE1,0xE0,0x60,0x60,0xC0,0x80,0x00,0x60,0xF8,0xF8,0x40,0x01,
0xC1,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x01,0xFD,0xF9,0x00,0xF8,0xFC,0x80,0xC0,0xE0,
0x60,0x00,0x00,0x00,0x01,0x01,0x01,0xC1,0xC1,0x61,0x61,0x61,0xE0,0xC0,0x00,0xC0,
0xE0,0x60,0x60,0x60,0xC0,0x00,0x01,0xE1,0xE0,0x60,0x60,0xE0,0xC0,0x60,0x60,0xE0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x03,0x07,0x03,0x03,
0x03,0x03,0x02,0x03,0x03,0x07,0x0F,0x1F,0x1F,0x1F,0x1F,0x1F,0x0F,0x07,0x00,0x00,
0x00,0x00,0x00,0xFF,0xFF,0x10,0x30,0x1F,0x0F,0x00,0x00,0xFF,0xBC,0x10,0x30,0x1F,
0x1F,0x0F,0x00,0x22,0xFF,0x18,0x10,0x10,0x1F,0x0F,0x00,0x00,0x1F,0x1F,0x10,0x08,
0x1E,0x1E,0x13,0x1B,0x1F,0x1F,0x00,0x00,0x1F,0x1F,0x00,0x1F,0x1F,0x03,0x07,0x1E,
0x18,0x00,0x00,0x18,0x18,0x00,0x04,0x0F,0x1D,0x30,0x10,0x10,0x1C,0x1C,0x00,0x0F,
0x1F,0x10,0x30,0x18,0x1F,0x07,0x00,0x1F,0x1F,0x00,0x00,0x1F,0x1F,0x00,0x00,0x1F,
};
void OLED_Show_Image(unsigned char *image)
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++){
OLED_Write_Cmd(0xB0 + i);//page0--page7
//每个page从0列
OLED_Write_Data(0x00);
OLED_Write_Data(0x10);
//0到127列,依次写入0,每写入数据,列地址自动偏移
for(j=128*i;j<128*(i+1);j++){
OLED_Write_Data(image[j]);
}
}
}
void main()
{
//1.OLED初始化
OLED_Init();
//2.确认一个位置
OLED_Write_Cmd(0x20);
OLED_Write_Cmd(0x02);
OLED_Clear();
OLED_Show_Image(bmpImage);
OLED_Write_Cmd(0xB0);
OLED_Write_Cmd(0x00);
OLED_Write_Cmd(0x10);
while(1);
}