初始化、起始和停止
1.初始信号:初始SDA,SCL均为高
2.开始信号:SCL不变,拉低SDA
void IIC_Start() { OLED_SCLK_Set(); OLED_SDIN_Set(); OLED_SDIN_Clr(); }
3.结束信号:拉高SCL,拉低SDA,拉高SDA
void IIC_Stop() { OLED_SCLK_Set() ; OLED_SDIN_Clr(); OLED_SDIN_Set(); }
数据传输和响应信号
1.数据传输:拉低SCL,拉低/拉高SDA,拉高SCL,拉低SCL
void Write_IIC_Byte(unsigned char IIC_Byte) { unsigned char i; unsigned char m,da; da=IIC_Byte; OLED_SCLK_Clr(); for(i=0;i<8;i++) { m=da; m=m&0x80; if(m==0x80) {OLED_SDIN_Set();} else OLED_SDIN_Clr(); da=da<<1; OLED_SCLK_Set(); OLED_SCLK_Clr(); } }
2.响应信号(ACK):拉高SCL,等待SDA被拉低,拉低SCL
void IIC_Wait_Ack() { OLED_SCLK_Set(); while(GPIO_Pin_4==1); OLED_SCLK_Clr(); }
1.写命令:
void Write_IIC_Command(unsigned char IIC_Command) { IIC_Start(); Write_IIC_Byte(0x78); IIC_Wait_Ack(); Write_IIC_Byte(0x00); IIC_Wait_Ack(); Write_IIC_Byte(IIC_Command); IIC_Wait_Ack(); IIC_Stop(); }
2.写数据:
void Write_IIC_Data(unsigned char IIC_Data) { IIC_Start(); Write_IIC_Byte(0x78); IIC_Wait_Ack(); Write_IIC_Byte(0x40); //co为0,一个一个字节处理 IIC_Wait_Ack(); Write_IIC_Byte(IIC_Data); IIC_Wait_Ack(); IIC_Stop(); }