IIC采用的是硬件的IIC和软件的IIC,配置IIC的初始化,我采用的是硬件的IIC在CPU的引脚是GPIOB的6和7,请看程序
void I2C_Init()
{
GPIO_InitTypeDef I2C_SDA;
GPIO_InitTypeDef I2C_SCL;
I2C_InitTypeDef I2C_Iint;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_SDA.GPIO_Mode = GPIO_Mode_AF_OD;
I2C_SDA.GPIO_Pin = GPIO_Pin_7;
I2C_SDA.GPIO_Speed = GPIO_Speed_50MHz;
I2C_SCL.GPIO_Mode = GPIO_Mode_AF_OD;
I2C_SCL.GPIO_Pin = GPIO_Pin_6;
I2C_SCL.GPIO_Speed = GPIO_Speed_50MHz;
I2C_Iint.I2C_Ack = I2C_Ack_Enable;
I2C_Iint.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Iint.I2C_ClockSpeed = 400000;
I2C_Iint.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_Iint.I2C_Mode = I2C_Mode_I2C;
I2C_Iint.I2C_OwnAddress1 = 0x30;
GPIO_Init(GPIOB,&I2C_SDA);
GPIO_Init(GPIOB,&I2C_SCL);
I2C_Init(I2C1,&I2C_Iint);
I2C_Cmd(I2C1,ENABLE);
I2C_DeInit(I2C1);
}
写IIC通讯字节
在前面先写一个查看IIC总线是否繁忙(1为繁忙)
void write_Byte(uint8_t addr,uint8_t Data)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);//开启I2C总线
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,OLED_ADDRES1,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,addr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData(I2C1,Data);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_GenerateSTOP(I2C1,ENABLE);
}
写命令和写数据,由于OLED的驱动手册写了请看程序
//写命令
void writeCmd(unsigned char Cmd)
{
write_Byte(0x00,Cmd);
}
//写数据
void writeData(unsigned char Data)
{
write_Byte(0x40,Data);
}
下面是OLED的屏幕初始化
void OLED_Init(void)
{
//I2C_configation();
delay_nms(100);
WriteCmd(0xAE); //display off
WriteCmd(0x20); //Set Memory Addressing Mode
WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
WriteCmd(0xc8); //Set COM Output Scan Direction
WriteCmd(0x00); //---set low column address
WriteCmd(0x10); //---set high column address
WriteCmd(0x40); //--set start line address
WriteCmd(0x81); //--set contrast control register
WriteCmd(0xff); // 0x00~0xff
WriteCmd(0xa1); //--set segment re-map 0 to 127
WriteCmd(0xa6); //--set normal display
WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
WriteCmd(0x3F); //
WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
WriteCmd(0xd3); //-set display offset
WriteCmd(0x00); //-not offset
WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
WriteCmd(0xf0); //--set divide ratio
WriteCmd(0xd9); //--set pre-charge period
WriteCmd(0x22); //
WriteCmd(0xda); //--set com pins hardware configuration
WriteCmd(0x12);
WriteCmd(0xdb); //--set vcomh
WriteCmd(0x20); //0x20,0.77xVcc
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
delay_nms(100);
}
下面也是芯片写的起始坐标以及全屏填充
void OELD_SetPos(unsigned char x,unsigned char y)
{
writeCmd(0xb0+y);
writeCmd((x&0xf0)>>4|0x10);
writeCmd((x&0x0f)>>4|0x01);
}
//全屏填充
void OLED_FILL(unsigned char Fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
writeCmd(0xb0+m);
writeCmd(0x00);
writeCmd(0x10);
for(n=0;;n<128;n++)
{
writeData(Fill_Data)
}
}
}
//清屏
void CLS(void)
{
OLED_FILL(0x00);
}
OLED打开和关闭的程序
要设置的有关闭显示和开启显示;打开和关闭电荷泵以及电荷泵设置
//打开OLED
void OLED_OPEN(void)
{
writeCmd(0x8d);//开启设置电荷
writeCmd(0xAf);//OLED开启
writeCmd(0x14);//开启电荷泵
}
void OLED_Close(void)
{
writeCmd(0x8d);//开启设置电荷
writeCmd(0xAe);//OLED开启
writeCmd(0x10);//开启电荷泵
}