LCD屏具有较强的表现力,既适合文字信息的显示,也适合图像的表现。
这里使用的是一款0.96寸的LCD显示屏,其显示分辨率为80*160像素点,它以SPI的方式来工作。
其引脚与开发板的连接关系为:
CLK---PB4
DIN---PB5
RES--PB6
DC ---PB7
CS ---PC3
BLK---3.3V
所使用的接口如图1所示:
图1 引脚占用
图2连接形式
#defineOLED_SCLK_Set() gpio_set_level(GPIO_PB4, 1) //CLK
#defineOLED_SCLK_Clr() gpio_set_level(GPIO_PB4, 0)
#defineOLED_SDIN_Set() gpio_set_level(GPIO_PB5, 1) //
DIN#defineOLED_SDIN_Clr() gpio_set_level(GPIO_PB5, 0)
#defineOLED_RST_Set() gpio_set_level(GPIO_PB6, 1) //RES
#defineOLED_RST_Clr() gpio_set_level(GPIO_PB6,0)
#defineOLED_DC_Set() gpio_set_level(GPIO_PB7, 1) //DC
#defineOLED_DC_Clr() gpio_set_level(GPIO_PB7, 0)
#defineOLED_CS_Set() gpio_set_level(GPIO_PC3, 1) //CS
#defineOLED_CS_Clr() gpio_set_level(GPIO_PC3, 0)
以GPIO方式模拟SPI方式方式字节数据的函数为:
- voidLCD_Writ_Bus(unsignedchardat)
- {
- unsignedchari;
- 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();
- }
LCD屏的初始化函数为:
- voidLcd_Init(void)
- {
- OLED_RST_Clr();
- delay_ms(200);
- OLED_RST_Set();
- delay_ms(200);
- LCD_WR_REG(0x11);
- 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();
- }
实现色彩清除屏幕的函数为:
- voidLCD_Clear(unsignedint Color)
- {
- unsignedinti,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);
- }
- }
- }
实现图像显示的函数为:
- voidLCD_ShowPicturea(unsignedintx1,unsignedint y1,unsignedint x2,unsignedint y2)
- {
- unsignedint 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]);
- }
- }
此外,还配置了字符、数值及汉字显示函数,其验证显示效果的主程序为:
- int main (void)
- {
- sys_init(LDO_1P4_LDO_1P8, VBAT_MAX_VALUE_GREATER_THAN_3V6);
- user_read_flash_value_calib();
- CCLK_24M_HCLK_24M_PCLK_24M;
- user_init();
- delay_ms(1000);
- Lcd_Init();
- BACK_COLOR=YELLOW;
- POINT_COLOR=RED;
- LCD_Clear(RED);
- POINT_COLOR=RED;
- LCD_ShowPicturea(0,0,159,79);
- LCD_ShowChinese(0,0,8,16,RED);
- LCD_ShowChar(0,20,'V',0,RED);
- LCD_ShowNum(20,20,123,3,RED);
- while (1) ;
- return 0;
- }
经程序的编译和下载,其测试效果见图3所示,其显示效果该是相对不错的!
图3 显示效果