前面我们介绍了驱动SPI接口LCD5110显示屏的方法,这次介绍一下如何驱动I2C接口的OLED屏,该OLED屏为0.91寸单色屏。 相对于LCD5110屏来讲,OLED屏更节省GPIO口。为了更便于使用,这里直接使用Arduino接口来控制OLED屏,相应的引脚连接关系为: SDA ---PC1 SCL ---PC0 具体的实物连接如下图所示: 实物连接与显示效果 为实现高低电平的输出其语句定义为: #define OLED_SCLK_Clr() PC0=0 #define OLED_SCLK_Set() PC0=1 #define OLED_SDIN_Clr() PC1=0 #define OLED_SDIN_Set() PC1=1 相应的OLED屏初始化函数为: void OLED_Init(void)
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0xb0);//Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8);//Set COM Output Scan Direction
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0xff);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x1F);
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x02);
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x49);//0x20,0.77xVcc
Write_IIC_Command(0xaf);//--turn on oled panel
}
模拟I2C的启动与停止的函数为: void IIC_Start()
{
OLED_SCLK_Set();
TIMER_Delay(TIMER0, 2);
OLED_SDIN_Set();
TIMER_Delay(TIMER0, 2);
OLED_SDIN_Clr();
TIMER_Delay(TIMER0, 2);
OLED_SCLK_Clr();
TIMER_Delay(TIMER0, 2);
}
void IIC_Stop()
{
OLED_SCLK_Set();
TIMER_Delay(TIMER0, 2);
OLED_SDIN_Clr();
TIMER_Delay(TIMER0, 2);
OLED_SDIN_Set();
TIMER_Delay(TIMER0, 2);
}
模拟I2C方式字节数据的函数为: 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)
{
相应的清屏函数为: void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD);
OLED_WR_Byte (0x00,OLED_CMD);
OLED_WR_Byte (0x10,OLED_CMD);
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}
OLED屏的字符显示函数为: void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{
unsigned char c=0,i=0;
c=chr-' ';
if(x>Max_Column-1){x=0;y=y+2;}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
else {
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
}
实现显示效果的主程序为: int main()
{
uint32_t i,j;
SYS_Init();
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
GPIO_SetMode(PC, BIT1, GPIO_MODE_OUTPUT);
GPIO_SetMode(PC, BIT0, GPIO_MODE_OUTPUT);
GPIO_SetMode(PH, BIT4, GPIO_MODE_INPUT);
PB14 = 1;
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"M471 TEST",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
while(1)
{
PB14 = 1;
TIMER_Delay(TIMER0, 500000);
PB14 = 0;
TIMER_Delay(TIMER0, 500000);
}
}
|