本帖最后由 jinglixixi 于 2020-6-2 15:09 编辑
在学习开发板的使用过程中,对GPIO口的掌握是最基本的内容,也是最为有用和易于学习的内容之一。 通过对出厂DEMO的学习,我们可以快速地掌握GPIO口的输入/输出设置、高低电平的输出等。在此基础上,我们可以通过GPIO口来模拟I2C接口和SPI接口,进而能驱动相应的显示屏工作,在此共介绍3种显示屏的使用方法。
1.I2C_OLED屏显示 I2C_OLED屏是一种借由I2C接口工作的器件,共有4个引脚,它具有占用GPIO口资源少的优点。 该OLED屏与MCU的连接关系为: OLED_SCLK——PA4 OLED_SDIN——PA5 其余的两个引脚则接3.3V工作电源
定义GPIO口输出高低电平的语句为: #define OLED_SCLK_Set() GPIO_SetPinBits(4); //PA4 #define OLED_SCLK_Clr() GPIO_ResetPinBits(4); // PA4
#define OLED_SDIN_Set() GPIO_SetPinBits(5); //PA5 #define OLED_SDIN_Clr() GPIO_ResetPinBits(5); // PA5
对GPIO的功能定义函数为: void LCD_GPIO_Cogfig(void) { GPIO_SetDir(GPIO_PA4,1); GPIO_SetDir(GPIO_PA5,1); }
OLED屏的初始化函数为: void OLED_Init(void) { LCD_GPIO_Cogfig(); OLED_SCLK_Set(); OLED_SDIN_Set(); mdelay(800);//delay_ms(800); OLED_WR_Byte(0xAE,OLED_CMD);//--display off OLED_WR_Byte(0x00,OLED_CMD);//---setlow column address OLED_WR_Byte(0x10,OLED_CMD);//---sethigh column address OLED_WR_Byte(0x40,OLED_CMD);//--setstart line address OLED_WR_Byte(0xB0,OLED_CMD);//--setpage address OLED_WR_Byte(0x81,OLED_CMD);// contract control OLED_WR_Byte(0xFF,OLED_CMD);//--128 OLED_WR_Byte(0xA1,OLED_CMD);//setsegment remap OLED_WR_Byte(0xA6,OLED_CMD);//--normal/ reverse OLED_WR_Byte(0xA8,OLED_CMD);//--setmultiplex ratio(1 to 64) OLED_WR_Byte(0x3F,OLED_CMD);//--1/32duty OLED_WR_Byte(0xC8,OLED_CMD);//Comscan direction OLED_WR_Byte(0xD3,OLED_CMD);//-setdisplay offset OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//setosc division OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//setarea color mode off OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//SetPre-Charge Period OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//setcom pin configuartion OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//setVcomh OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//setcharge pump enable OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turnon oled panel }
实现图1所示效果的主程序为: int main(void) { InitDebug(); InitDelay(); GPIO_SetDir(GPIO_PA0, 1); OLED_Init(); OLED_Clear(); OLED_ShowString(0,0,"AC8711TEST",16); OLED_ShowString(0,2,"OLEDDisplay",16); OLED_ShowString(0,4,"jinglixixi",16); OLED_ShowString(0,6,"2020.5.30",16); while(1); }
图1 OLED屏显示效果
2.LCD5510屏显示 在掌握了I2C接口的模拟后,我们可以接下来学习SPI接口的模拟,进而来驱动SPI接口的LCD5510屏来实现显示功能。 该OLED屏与MCU的连接关系为: LCD_CLK——PA4 LCD_DIN——PA5 LCD_LED——PA7 LCD_CS——PA3 LCD_DC——PA2 LCD_RST——PA6 其余的两个引脚则接3.3V工作电源
定义GPIO口输出高低电平的语句为: #define LCD_LED_High() GPIO_SetPinBits(7); // PA7
#define LCD_CLK_H GPIO_SetPinBits(4); // PA4 #define LCD_CLK_L GPIO_ResetPinBits(4); //PA4
#define LCD_DIN_H GPIO_SetPinBits(5); // PA5 #define LCD_DIN_L GPIO_ResetPinBits(5); // PA5
#define LCD_DC_H GPIO_SetPinBits(2); // PA2 #define LCD_DC_L GPIO_ResetPinBits(2); // PA2
#define LCD_CS_H GPIO_SetPinBits(3); // PA3 #define LCD_CS_L GPIO_ResetPinBits(3); // PA3
#define LCD_RST_H GPIO_SetPinBits(6); //PA6 #define LCD_RST_L GPIO_ResetPinBits(6); //PA6
对GPIO的功能定义函数为: void LCD_GPIO_Cogfig(void) { GPIO_SetDir(GPIO_PA1,1); GPIO_SetDir(GPIO_PA2,1); GPIO_SetDir(GPIO_PA3,1); GPIO_SetDir(GPIO_PA4,1); GPIO_SetDir(GPIO_PA5,1); GPIO_SetDir(GPIO_PA6,1); GPIO_SetDir(GPIO_PA7,1); LCD_LED_High(); }
LCD5510屏的初始化函数为: void LCD5510_Init(void) { LCD_GPIO_Cogfig(); LCD_reset_hard; LCD_write_cmd(0x21); LCD_write_cmd(0x06); LCD_write_cmd(0x13); LCD_write_cmd(0xc8); LCD_write_cmd(0x20); LCD_write_cmd(0x0c); LCD_write_cmd(Y_Page_Addr); LCD_write_cmd(X_Col_Addr); LCD_clr_scr(); }
为实现字符的显示,配置的相应显示函数为: void LCD_printc(unsigned char x, unsignedchar y, unsigned char c_dat) { unsignedchar i, j;
c_dat-= 32; x<<= 3; //8 y<<= 1; //16 for(j= 0; j < 2; j++) { LCD_pos_byte(x,(y + j)); for(i= 0; i < 8; i++) LCD_write_dat(Font_code[c_dat][8* j + i]); } }
void LCD_prints(unsigned char x, unsignedchar y, unsigned char *s_dat) { while(*s_dat&& x < 10) {LCD_printc(x++,y, *s_dat); s_dat++;} }
实现图2所示效果的主程序为: int main(void) { InitDebug(); InitDelay(); GPIO_SetDir(GPIO_PA0, 1); mdelay(200); LCD5510_Init(); LCD_prints(0,0,"AC8711 TEST"); LCD_prints(0,1,"LCD5510 "); LCD_prints(0,2,"jinglixixi"); while (1); }
图2 LCD5510屏显示效果
3.SPI接口TFT屏显示
对于SPI接口,它还可以驱动TFT彩屏。 该TFT屏与MCU的连接关系为: LCD_ SCK ——PA4 LCD_ SDI ——PA5 LCD_LED ——PA7 LCD_CS ——PA3 LCD_DC ——PA2 LCD_ REST——PA6 其余的两个引脚则接3.3V工作电源
定义GPIO口输出高低电平的语句为: #define LCD_LED_High() GPIO_SetPinBits(7); // PA7 #define LCD_CS_High() GPIO_SetPinBits(3); // PA3 #define LCD_CS_Low() GPIO_ResetPinBits(3); // PA3 #define LCD_SCK_High() GPIO_SetPinBits(4); // PA4 #define LCD_SCK_Low() GPIO_ResetPinBits(4); // PA4 #define LCD_SDI_High() GPIO_SetPinBits(5); // PA5 #define LCD_SDI_Low() GPIO_ResetPinBits(5); // PA5 #define LCD_DC_High() GPIO_SetPinBits(2); // PA2 #define LCD_DC_Low() GPIO_ResetPinBits(2); // PA2 #define LCD_REST_High() GPIO_SetPinBits(6); // PA6 #define LCD_REST_Low() GPIO_ResetPinBits(6); // PA6
TFT彩屏的初始化函数为: void Lcd_Init(void) { LCD_REST_Low(); mdelay(20); LCD_REST_High(); mdelay(20); LCD_CS_Low(); LCD_WR_REG(0xCB); LCD_WR_DATA8(0x39); LCD_WR_DATA8(0x2C); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x34); LCD_WR_DATA8(0x02); LCD_WR_REG(0xCF); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0XC1); LCD_WR_DATA8(0X30); LCD_WR_REG(0xE8); LCD_WR_DATA8(0x85); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x78); LCD_WR_REG(0xEA); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x00); LCD_WR_REG(0xED); LCD_WR_DATA8(0x64); LCD_WR_DATA8(0x03); LCD_WR_DATA8(0X12); LCD_WR_DATA8(0X81); LCD_WR_REG(0xF7); LCD_WR_DATA8(0x20); LCD_WR_REG(0xC0); //Powercontrol LCD_WR_DATA8(0x23); //VRH[5:0] LCD_WR_REG(0xC1); //Powercontrol LCD_WR_DATA8(0x10); //SAP[2:0];BT[3:0] LCD_WR_REG(0xC5); //VCMcontrol LCD_WR_DATA8(0x3e); //????? LCD_WR_DATA8(0x28); LCD_WR_REG(0xC7); // VCMcontrol2 LCD_WR_DATA8(0x86); // -- LCD_WR_REG(0x36); // MemoryAccess Control LCD_WR_DATA8(0x48); // C8 LCD_WR_REG(0x3A); LCD_WR_DATA8(0x55); LCD_WR_REG(0xB1); LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x18); LCD_WR_REG(0xB6); // DisplayFunction Control LCD_WR_DATA8(0x08); LCD_WR_DATA8(0x82); LCD_WR_DATA8(0x27); LCD_WR_REG(0xF2); // 3GammaFunction Disable LCD_WR_DATA8(0x00); LCD_WR_REG(0x26); // Gammacurve selected LCD_WR_DATA8(0x01); LCD_WR_REG(0xE0); // Set Gamma LCD_WR_DATA8(0x0F); LCD_WR_DATA8(0x31); LCD_WR_DATA8(0x2B); LCD_WR_DATA8(0x0C); LCD_WR_DATA8(0x0E); LCD_WR_DATA8(0x08); LCD_WR_DATA8(0x4E); LCD_WR_DATA8(0xF1); LCD_WR_DATA8(0x37); LCD_WR_DATA8(0x07); LCD_WR_DATA8(0x10); LCD_WR_DATA8(0x03); LCD_WR_DATA8(0x0E); LCD_WR_DATA8(0x09); LCD_WR_DATA8(0x00); LCD_WR_REG(0XE1); //Set Gamma LCD_WR_DATA8(0x00); LCD_WR_DATA8(0x0E); LCD_WR_DATA8(0x14); LCD_WR_DATA8(0x03); LCD_WR_DATA8(0x11); LCD_WR_DATA8(0x07); LCD_WR_DATA8(0x31); LCD_WR_DATA8(0xC1); LCD_WR_DATA8(0x48); LCD_WR_DATA8(0x08); LCD_WR_DATA8(0x0F); LCD_WR_DATA8(0x0C); LCD_WR_DATA8(0x31); LCD_WR_DATA8(0x36); LCD_WR_DATA8(0x0F); LCD_WR_REG(0x11); //Exit Sleep mdelay(120); LCD_WR_REG(0x29); //Display on LCD_WR_REG(0x2c); }
实现显示功能的相关函数为: void LCD_Clear(uint16_t Color) { charVH,VL; uint16_ti,j; VH=Color>>8; VL=Color; 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_DATA8(VH); LCD_WR_DATA8(VL); } } }
void LCD_ShowChar(uint16_t x,uint16_ty,char num,char size,char mode) { char temp,t1,t; uint16_ty0=y; uint16_tcolortemp=POINT_COLOR; num=num-''; if(!mode) { for(t=0;t<size;t++) { if(size==12)temp=asc2_1206[num][t]; elsetemp=asc2_1608[num][t]; for(t1=0;t1<8;t1++) { if(temp&0x80) POINT_COLOR=colortemp; else POINT_COLOR=BACK_COLOR; LCD_DrawPoint(x,y); temp<<=1; y++; if(y>=320) { POINT_COLOR=colortemp; return; } if((y-y0)==size) { y=y0; x++; if(x>=240){POINT_COLOR=colortemp; return;} break; } } } } else { for(t=0;t<size;t++) { if(size==12)temp=asc2_1206[num][t]; //1206 elsetemp=asc2_1608[num][t]; //1608 for(t1=0;t1<8;t1++) { if(temp&0x80)LCD_DrawPoint(x,y); temp<<=1; y++; if(y>=320){POINT_COLOR=colortemp;return;} if((y-y0)==size) { y=y0; x++; if(x>=240){POINT_COLOR=colortemp;return;} break; } } } } POINT_COLOR=colortemp; }
void LCD_ShowString(uint16_t x,uint16_ty,uint16_t width,uint16_t height,char size,char *p) { char x0=x; width+=x; height+=y; while((*p<='~')&&(*p>=' ')) { if(x>=width){x=x0;y+=size;} if(y>=height)break; LCD_ShowChar(x,y,*p,size,1); x+=size/2; p++; } }
实现图3显示效果的函数及主程序为: void xianshi() { LCD_Clear(RED); BACK_COLOR=RED; POINT_COLOR=WHITE; LCD_ShowString(20,80,160,16,16,"AC8711TEST"); LCD_ShowString(20,120,200,16,16,"2.4SPI_TFT 240*320"); LCD_ShowString(20,240,160,16,16,"jinglxixi"); LCD_ShowString(20,280,200,16,16,"2020.5.31"); }
int main(void) { InitDebug(); InitDelay(); mdelay(200); GPIO_Configuration(); Lcd_Init(); xianshi(); while (1); }
图3 TFT彩屏显示效果
|