IIC协议(Inter-Integrated Circuit Protocol),也被称为I2C(Inter-Integrated Circuit),是一种串行通信协议,通常用于连接集成电路(IC)和外部设备,例如传感器、存储器、显示屏等。该协议由飞利浦(现在的恩智浦半导体)于上世纪80年代开发,旨在实现在狭小空间内进行低速、短距离的数据传输。
IIC协议采用主从架构。主设备控制通信总线,并负责启动和停止数据传输。从设备被动地响应主设备的指令,并提供所需的数据或执行相应的操作。
IIC协议的特点包括:
-
双线制:IIC总线由两根线构成,即串行数据线(SDA)和串行时钟线(SCL)。SDA负责数据传输,而SCL则负责同步时钟信号。
-
多主机支持:多个主设备可以在同一个IIC总线上共存,通过地址冲突解决算法实现对总线的控制。
-
支持多种速率:IIC协议支持不同的时钟频率,可以根据应用的需求选择适当的通信速率。
-
简单且成本低廉:由于只需要两根线路,因此成本低廉且易于实现。
IIC协议被广泛应用于各种嵌入式系统、传感器网络、物联网设备等领域,因其简单、可靠且成本低廉而备受青睐。
数据传输的完整流程如下:
-
起始信号(Start Signal):主设备发送起始信号,标志着数据传输的开始。它是在时钟信号(SCL)保持高电平时,数据线(SDA)由高电平转变为低电平。
-
地址+读/写位(Address + Read/Write Bit):主设备发送要通信的从设备地址,以及读/写位。地址的最高位指示了读取还是写入操作。
-
应答信号(Acknowledge Signal):接收设备(从设备)在成功接收到地址+读/写位后,发送应答信号作为确认。
-
数据传输(Data Transmission):主设备开始发送或接收数据,通过在每个时钟周期内在数据线(SDA)上改变电平来传输数据位。时钟信号(SCL)控制数据传输的时机和持续时间。
-
应答信号(Acknowledge Signal):每个数据字节的传输结束后,接收设备发送应答信号作为确认。
-
停止信号(Stop Signal):主设备发送停止信号,标志着数据传输的结束。它是在时钟信号(SCL)保持高电平时,数据线(SDA)由低电平转变为高电平。
这样的流程形成了完整的数据传输周期。起始信号和停止信号的发送以及应答信号的接收是确保数据传输顺利进行的重要步骤,而时钟信号和数据信号则负责实际的数据传输。
#include "reg52.h"
#include "intrins.h"
sbit SCL = P1^0;
sbit SDA = P1^1;
//起始信号
void IIC_Start()
{
SCL = 0;
SDA = 1;
SCL = 1;
_nop_();
SDA = 0;
_nop_();
}
//停止信号
void IIC_Stop()
{
SCL = 0;
SDA = 0;
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
//应答信号
char IIC_ACK()
{
char flag;
SDA = 1;
_nop_();
SCL = 1;
_nop_();
flag = SDA;
_nop_();
SCL = 0;
_nop_();
return flag;
}
//数据发送
void IIC_SendByte(char dataSend)
{
int i;
for(i=0;i<8;i++){
SCL = 0;
SDA = dataSend & 0x80;
_nop_();
SCL = 1;
_nop_();
SCL = 0;
_nop_();
dataSend = dataSend<<1;
}
}
//写命令
void Oled_Write_Cmd(char dataCmd)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_ACK();
IIC_SendByte(0x00);
IIC_ACK();
IIC_SendByte(dataCmd);
IIC_ACK();
IIC_Stop();
}
//写数据
void Oled_Write_Data(char dataData)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_ACK();
IIC_SendByte(0x40);
IIC_ACK();
IIC_SendByte(dataData);
IIC_ACK();
IIC_Stop();
}
//OLED初始化
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 Oled_Clear()
{
unsigned char i,j;
for(i=0;i<8;i++){
Oled_Write_Cmd(0xB0+i);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
for(j=0;j<128;j++){
Oled_Write_Data(0);
}
}
}
/*-- 调入了一幅图像:D:\桌面\无标题.bmp --*/
/*-- 宽度x高度=128x64 --*/
code unsigned char GGImage[] = {
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,0x80,0x00,0x80,0xC0,0x80,0x80,0x00,0x00,0x00,0x80,
0x80,0x80,0xC0,0xC0,0xE0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xE0,0xE0,0xE0,0xE0,0xE0,
0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xF8,0xF8,0xF8,
0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xF0,0xF0,0xF0,0xC0,0xC0,0x80,
0x80,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,0x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0x08,0x0C,0x0C,0x0C,0x06,0x06,0x05,0x05,0x04,
0x04,0x05,0x07,0x06,0x04,0x0C,0x0C,0x08,0x08,0x18,0x98,0xB8,0xF8,0xE8,0xC8,0x08,
0x08,0x08,0x08,0x04,0x84,0xC6,0xE2,0xF3,0xF1,0xF0,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,
0xF8,0xF0,0xF0,0xE3,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xE0,0xE0,0xE0,0xF0,
0xF0,0xF8,0xF8,0xF8,0xF8,0xFC,0xFF,0xFE,0xFC,0xF0,0x60,0xC0,0xC0,0x80,0x00,0x03,
0x0F,0x1F,0x1F,0x3F,0x3F,0x3F,0x3F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x3F,
0x3F,0x3F,0x1F,0x1F,0x0F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0x07,0x06,0x0C,0x0D,0x09,0x1B,0x1B,0x1B,0x13,0x12,0x12,0x12,0x16,0x16,
0x16,0x16,0x16,0x16,0x16,0x12,0x12,0x1B,0x1B,0x1B,0x0D,0x0D,0x07,0x03,0x01,0x00,
0x00,0x00,0x00,0x00,0x03,0x0F,0x1F,0x1F,0x1F,0x3F,0x3F,0x3F,0xBF,0x9F,0xDF,0x6F,
0x3F,0x1F,0x0F,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xFC,0xFF,0x7F,0x3F,0x9F,0x7F,0x3F,0x9F,0xBF,
0x3F,0xBF,0xBF,0x3F,0x7F,0xFF,0xFF,0x3F,0x3F,0xBF,0x3F,0x3E,0x6F,0x7B,0x7B,0x73,
0x36,0x1E,0x7C,0xFC,0xF8,0x38,0x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x00,0x00,0x00};
void Show_Image(char *GGImage)
{
unsigned char i;
int j;
for(i=0;i<8;i++){
Oled_Write_Cmd(0xB0+i);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
for(j=128*i;j<128*(i+1);j++){
Oled_Write_Data(GGImage[j]);
}
}
}
void main()
{
//1. OLED初始化
Oled_Init();
//2. 选择一个位置
//2.1 确认页寻址模式
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x02);
Oled_Clear();
//2.2 选择PAGE0 1011 0000
// 0xB0
Oled_Write_Cmd(0xB0);
//从page0的第0列开始写数据上半部分
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
Show_Image(GGImage);
while(1);
}