国民技术N32G457-OLED屏显示驱动
国民技术-N32G457-OLED屏显示驱动,OLED屏有多种类型,按接口方式可分I2C接口和SPI接口方式,按色彩可分为单色、双色及彩色方式。此外,按几何尺寸分又有0.91寸、0.96寸等规格。
从价格和占用引脚资源看,使用0.91寸I2C接口的单色屏是比较经济的。可以硬件和软件2种方式来实现OLED屏的显示驱动。从使用灵活性的角度,这里采用软件的方式来实现,并按如下关系来连接:
CLK---PE2
DIN ---PE4


图1 器件连接


故进行如下的定义:
#defineCLK_PIN    1
#defineDIN_PIN    3

#defineOLED_SCLK_Clr()  rt_pin_write(CLK_PIN,PIN_LOW);
#defineOLED_SCLK_Set()  rt_pin_write(CLK_PIN,PIN_HIGH);

#defineOLED_SDIN_Clr()  rt_pin_write(DIN_PIN,PIN_LOW);
#defineOLED_SDIN_Set()  rt_pin_write(DIN_PIN,PIN_HIGH);


为实现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)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
rt_hw_us_delay(2);
da=da<<1;
OLED_SCLK_Set();
rt_hw_us_delay(2);
OLED_SCLK_Clr();
rt_hw_us_delay(2);
}
}

OLED屏的初始化函数为:
void OLED_Init(void)
{
Write_IIC_Command(0xAE);
Write_IIC_Command(0x40);
Write_IIC_Command(0xb0);
Write_IIC_Command(0xc8);
Write_IIC_Command(0x81);
Write_IIC_Command(0xff);
Write_IIC_Command(0xa1);
Write_IIC_Command(0xa6);
Write_IIC_Command(0xa8);
Write_IIC_Command(0x1F);
Write_IIC_Command(0xd3);
Write_IIC_Command(0x00);
Write_IIC_Command(0xd5);
Write_IIC_Command(0xf0);
Write_IIC_Command(0xd9);
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);
Write_IIC_Command(0x02);
Write_IIC_Command(0x8d);
Write_IIC_Command(0x14);
Write_IIC_Command(0xdb);
Write_IIC_Command(0x49);
Write_IIC_Command(0xaf);
}

进行OLED屏的清屏函数为:
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);
}
}

实现字符串显示的函数为:
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
while (chr[j]!='\0')


使用以上函数,实现图2所示效果的程序代码为:
   rt_pin_mode(DIN_PIN, PIN_MODE_OUTPUT);
   OLED_Clear();
   OLED_ShowString(20,0,"N32G457TEST",16);
   OLED_ShowString(20,2,"OLEDDISPLAY",16);
图2 显示效果