现在的显示器件实在是太丰富了,且价格也不很贵,这为应用带来了极大的方便。
为了便于图片的显示,这里使用的是一款0.96寸的显示屏,其分辨率为160*80,别开其尺寸不大,但其显示的清晰度很高。
为使用的灵活性,这里是采用GPIO模拟SPI的方式来驱动该显示屏。
该显示屏与开发板的连接关系为:
BLK-------PB8
CS--------PB7
RS--------PB6
RESET---PB5
SCL-------PA5
SDA------PA7
实现高低电平的引脚定义为:
#define OLED_SCLK_Set() GPIO_Pins_Set(GPIOA, GPIO_PIN_5) //CLK
#define OLED_SCLK_Clr() GPIO_Pins_Reset(GPIOA, GPIO_PIN_5)
#define OLED_SDIN_Set() GPIO_Pins_Set(GPIOA, GPIO_PIN_7) //DIN
#define OLED_SDIN_Clr() GPIO_Pins_Reset(GPIOA, GPIO_PIN_7)
#define OLED_RST_Set() GPIO_Pins_Set(GPIOB, GPIO_PIN_5) //RES
#define OLED_RST_Clr() GPIO_Pins_Reset(GPIOB, GPIO_PIN_5)
#define OLED_DC_Set() GPIO_Pins_Set(GPIOB, GPIO_PIN_6) //DC
#define OLED_DC_Clr() GPIO_Pins_Reset(GPIOB, GPIO_PIN_6)
#define OLED_CS_Set() GPIO_Pins_Set(GPIOB, GPIO_PIN_7) //CS
#define OLED_CS_Clr() GPIO_Pins_Reset(GPIOB, GPIO_PIN_7)
#define OLED_BLK_Set() GPIO_Pins_Set(GPIOB, GPIO_PIN_8) //BLK
#define OLED_BLK_Clr() GPIO_Pins_Reset(GPIOB, GPIO_PIN_8)
为显示屏所用引脚的配置函数为:
void LCD_Initialize()
{
GPIO_InitTypeGPIO_InitStructure;
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5|GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Current= GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOA,&GPIO_InitStructure);
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOB);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Current= GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOB,&GPIO_InitStructure);
}
模拟SPI方式发送字节数据的函数为:
void LCD_Writ_Bus(u8 dat)
{
u8 i;
OLED_CS_Clr();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
OLED_SDIN_Set();
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
OLED_CS_Set();
}
该显示屏的初始化函数为:
void Lcd_Init(void)
{
OLED_RST_Clr();
SysTick_Delay_Ms(200);
OLED_RST_Set();
SysTick_Delay_Ms(200);
LCD_WR_REG(0x11);
SysTick_Delay_Ms(100);
LCD_WR_REG(0x21);
LCD_WR_REG(0xB1);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB2);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB3);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB4);
LCD_WR_DATA8(0x03);
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0x62);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x04);
LCD_WR_REG(0xC1);
LCD_WR_DATA8(0xC0);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x00);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0x6A);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0xEE);
LCD_WR_REG(0xC5);
LCD_WR_DATA8(0x0E);
LCD_WR_REG(0xE0);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x12);
LCD_WR_DATA8(0x27);
LCD_WR_DATA8(0x37);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0xE1);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0F);
LCD_WR_DATA8(0x06);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x08);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x13);
LCD_WR_DATA8(0x26);
LCD_WR_DATA8(0x36);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_REG(0x36);
LCD_WR_DATA8(0x78);
LCD_WR_REG(0x29);
OLED_BLK_Set();
}
以色彩清屏的函数为:
void LCD_Clear(u16 Color)
{
u16 i,j;
LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
for(i=0;i<LCD_W;i++)
{
for (j=0;j<LCD_H;j++)
{
LCD_WR_DATA(Color);
}
}
}
实现字符串显示的函数为:
void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 color)
{
while(*p!='\0')
{
if(x>LCD_W-16)
{
x=0;
y+=16;
}
if(y>LCD_H-16)
{
y=x=0;
LCD_Clear(RED);
}
LCD_ShowChar(x,y,*p,0,color);
x+=8;
p++;
}
}
进行图片显示的函数为:
void LCD_ShowPicturea(u16 x1,u16 y1,u16 x2,u16 y2)
{
int i;
LCD_Address_Set(x1,y1,x2,y2);
for(i=0;i<12800;i++)
{
LCD_WR_DATA8(gImage_FJ[i*2]);
LCD_WR_DATA8(gImage_FJ[i*2+1]);
}
}
实现图1至图3所示效果的主程序为:
int main(void)
{
LCD_Initialize();
Lcd_Init();
LCD_Clear(BLACK);
LCD_ShowString(20,10,"N32G430 TEST",RED);
LCD_ShowString(20,30,"0.96' OLED",YELLOW);
SysTick_Delay_Ms(2000);
LCD_ShowPicturea(0,0,159,79);
SysTick_Delay_Ms(2000);
LCD_ShowPictureb(0,0,159,79);
while(1);
}
经程序的编译与下载,其显示效果如下:
图1显示字符串
图2显示风景图片
图3显示人物图片