有了前面的相关知识,那才能通过软件与硬件的结合来实现显示系统的构建呢? 在YL-KL26Z开发板上,为了驱动LCD5110显示屏,需要构建相应的显示电路,其接口电路如下图所示。
在显示电路中,除了电源的连接外,仅需4个GPIO口即可完成线路的连接,且4个GPIO口均作为输出口来使用。在传送数据和指令时,是采用串行方式模拟SPI接口来执行操作的。 在显示过程中,需用到的函数有:字节指令写入函数LCD_write_byte Cmd()、字节数据写入函数LCD_write_byte Data()、显示定位函数LCD_set_XY()、清屏函数LCD_clear()、填充函数LCD_Fill()、LCD5110初始化函数LCD_Init()、单字符显示函数LCD_write_char()、字符串输出函数LCD_write_english_string()、 16*32点阵汉字输出函数LCD_write_dangehanz()、 12*24点阵汉字输出函数LCD_write_hanzi()、 界面显示函数jm()及延时函数等。 具体的LCD5110显示程序其代码如下: - #include "includes.h"
- // LCD5110显示汉字正常
- // RST PD2 复位控制
- // DC PD3 数据/指令选择
- // SDIN PD4 SPI接口数据收发
- // SCLK PD5 SPI接口时钟输出
- #define true 1
- #define flase 0
- #define SetLCD_SCLK_High() { GPIO_SET(MKL_PORTD,5);}
- #define SetLCD_SCLK_Low() { GPIO_CLR(MKL_PORTD,5);}
- #define SetLCD_SDIN_High() { GPIO_SET(MKL_PORTD,4);}
- #define SetLCD_SDIN_Low() { GPIO_CLR(MKL_PORTD,4);}
- #define SetLCD_DC_High() { GPIO_SET(MKL_PORTD,3);}
- #define SetLCD_DC_Low() { GPIO_CLR(MKL_PORTD,3);}
- #define SetLCD_RST_High() { GPIO_SET(MKL_PORTD,2);}
- #define SetLCD_RST_Low() { GPIO_CLR(MKL_PORTD,2);}
- #define Lcd_Max_x 84
- #define Lcd_Max_y 6
- #define LCD_Max_Dot_X 84 //84 48
- #define LCD_Max_Dot_Y 48 //84 48
- #define COM 0
- #define DAT 1
- #define STR 1
- #define HAN 2
- #define Nokia5110_basic_fun_set 0x20
- #define Nokia5110_expand_fun_set 0x21
- #define Nokia5110_dis_set 0x0c
- #define Nokia5110_DispMode_AllLightOff 0x08
- #define Nokia5110_DispMode_AllLightOn 0x09
- #define Nokia5110_DispMode_Reverse 0x0d
- #define Nokia5110_add_Yset 0x40 //Y
- #define Nokia5110_add_Xset 0x80 //X
- #define Nokia5110_tem_set 0x06
- #define Nokia5110_vot_set 0xD7
- void myDelay (INT32U ulTime)
- { INT32U i;
- i = 0;
- while (ulTime--)
- {
- for (i = 0; i < 5000; i++);
- }
- }
- unsigned char font6x8[][6] =
- {
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp
- { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // !
- { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // "
- { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // #
- ... ...
- };
- // 色彩识别仪
- unsigned char font16x16[][32]={
- {0x20,0x10,0xE8,0x24,0x27,0x24,0x24,0xE4,
- 0x24,0x34,0x2C,0x20,0xE0,0x00,0x00,0x00,
- 0x00,0x00,0x3F,0x42,0x42,0x42,0x42,0x43,
- 0x42,0x42,0x42,0x42,0x43,0x40,0x78,0x00},//色
- {0x00,0x14,0x64,0x04,0x0C,0xB2,0x02,0x23,
- 0x1A,0x00,0x20,0x10,0x08,0x86,0x60,0x00,
- 0x40,0x21,0x11,0x0D,0x03,0xFF,0x05,0x09,
- 0x11,0x80,0x84,0x42,0x21,0x10,0x0C,0x00},//彩
- {0x40,0x40,0x42,0xCC,0x00,0x00,0x00,0xFC,
- 0x04,0x04,0x04,0x04,0x04,0xFC,0x00,0x00,
- 0x00,0x00,0x00,0x3F,0x10,0x88,0x40,0x23,
- 0x19,0x01,0x01,0x09,0x11,0x23,0xC0,0x00},//识
- {0x00,0x3E,0x22,0xE2,0x22,0x22,0x22,0x3E,
- 0x00,0x00,0xF8,0x00,0x00,0xFF,0x00,0x00,
- 0x81,0x41,0x31,0x0F,0x41,0x81,0x41,0x3F,
- 0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00},//别
- {0x00,0x80,0x60,0xF8,0x07,0x00,0x1C,0xE0,
- 0x01,0x06,0x00,0xE0,0x1E,0x00,0x00,0x00,
- 0x01,0x00,0x00,0xFF,0x00,0x80,0x40,0x20,
- 0x13,0x0C,0x13,0x20,0x40,0x80,0x80,0x00},//仪
- };
- const unsigned char chinese_character[][24]=
- {
- //0 tian 天
- {0x20,0x22,0x22,0x22,0xA2,0x7E,0xA2,0x22,0x22,0x22,0x20,0x00,0x08,0x08,0x04,0x02,0x01,0x00,0x01,0x02,0x04,0x08,0x08,0x00},
- //1 jin 津
- {0x22,0x44,0x00,0x08,0xAA,0xAA,0xFF,0xAA,0xAA,0xBE,0x08,0x00,0x04,0x02,0x01,0x02,0x02,0x02,0x0F,0x02,0x02,0x02,0x02,0x00},
- //2 li 理
- {0x12,0x12,0xFE,0x12,0x3F,0xA5,0xA5,0xFF,0xA5,0xA5,0x3F,0x00,0x02,0x02,0x01,0x05,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00},
- //3 gong 工
- {0x00,0x02,0x02,0x02,0x02,0xFE,0x02,0x02,0x03,0x02,0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x04,0x04,0x00},
- //4 da 大
- {0x08,0x08,0x08,0x08,0xC8,0x3F,0xC8,0x08,0x08,0x0C,0x08,0x00,0x04,0x04,0x02,0x01,0x00,0x00,0x00,0x01,0x02,0x04,0x04,0x00},
- //5 xue 学
- {0x9C,0x84,0x95,0x96,0x95,0xD6,0xB4,0x94,0x87,0x94,0x8C,0x00,0x00,0x00,0x00,0x04,0x04,0x07,0x00,0x00,0x00,0x00,0x00,0x00}
- };
- long int delay_1us(void)
- {
- long int count=2;
- while(count--);
- return 0;
- }
- /*-----------------------------------------------------------------------
- LCD_write_byte Cmd
- -----------------------------------------------------------------------*/
- void LCD_write_CMD(unsigned char com)
- { unsigned char uci;
- SetLCD_DC_Low();
- for(uci=0;uci<8;uci++)
- {
- if(com & 0x80)
- {
- SetLCD_SDIN_High();
- }
- else
- {
- SetLCD_SDIN_Low();
- }
- SetLCD_SCLK_Low();
- com = com << 1;
- delay_1us();
- SetLCD_SCLK_High();
- }
- }
- /*-----------------------------------------------------------------------
- LCD_write_byte Data
- -----------------------------------------------------------------------*/
- void LCD_write_Data(unsigned char dt)
- { unsigned char uci;
- SetLCD_DC_High();
- for(uci=0;uci<8;uci++)
- { if(dt & 0x80)
- {
- SetLCD_SDIN_High();
- }
- else
- {
- SetLCD_SDIN_Low();
- }
- delay_1us();
- SetLCD_SCLK_Low();
- dt = dt << 1;
- SetLCD_SCLK_High();
- }
- }
- /*-----------------------------------------------------------------------
- LCD_set_XY
- x:0-83 Y:0-5
- -----------------------------------------------------------------------*/
- void LCD_set_XY(unsigned char X, unsigned char Y)
- {
- LCD_write_CMD(0x40 | (Y & 0X07)); // column
- LCD_write_CMD(0x80 | (X & 0X7F)); // row
- }
- /*-----------------------------------------------------------------------
- LCD_clear
- -----------------------------------------------------------------------*/
- void LCD_Clear(void)
- { int uii;
- LCD_set_XY(0,0);
- for(uii=0; uii<504; uii++)
- {
- LCD_write_Data(0x00);
- }
- }
- /*-----------------------------------------------------------------------
- LCD_Fill
- -----------------------------------------------------------------------*/
- void LCD_Fill(void)
- { uint16_t uii;
- LCD_set_XY(0,0);
- for(uii=0; uii<504; uii++)
- {
- LCD_write_Data(0xff);
- }
- }
- /*-----------------------------------------------------------------------
- LCD_Init LCD5110
- -----------------------------------------------------------------------*/
- void LCD_Init(void)
- { //RST PD2 复位控制
- //DC PD3 数据/指令选择
- //SDIN PD4 SPI接口数据收发
- //SCLK PD5 SPI接口时钟输出
- //PORT_ENABLE_CLK(MKL_PORTB); /* 使能PORT时钟 */
- PORT_ENABLE_CLK(MKL_PORTD);
- /* IO功能选择*/
- IO_FUN_SEL(MKL_PORTD,2,1);
- IO_FUN_SEL(MKL_PORTD,3,1);
- IO_FUN_SEL(MKL_PORTD,4,1);
- IO_FUN_SEL(MKL_PORTD,5,1);
- /* IO设置为输出模式 */
- GPIO_DDR_OUTPUT(MKL_PORTD,2);
- GPIO_DDR_OUTPUT(MKL_PORTD,3);
- GPIO_DDR_OUTPUT(MKL_PORTD,4);
- GPIO_DDR_OUTPUT(MKL_PORTD,5);
- /* IO设置为高电平 */
- GPIO_SET(MKL_PORTD,2);
- GPIO_SET(MKL_PORTD,3);
- GPIO_SET(MKL_PORTD,4);
- GPIO_SET(MKL_PORTD,5);
- SetLCD_RST_Low();
- delay_1us();
- SetLCD_RST_High();
- delay_1us();
- delay_1us();
- LCD_write_CMD(0x21);
- LCD_write_CMD(0xc0);
- LCD_write_CMD(0x06);
- LCD_write_CMD(0x13);
- LCD_write_CMD(0x20);
- LCD_Clear();
- LCD_write_CMD(0x0c);
- }
- /*-----------------------------------------------------------------------
- LCD_write_char
- -----------------------------------------------------------------------*/
- void LCD_write_char(unsigned char c)
- { unsigned char line;
- c -= 32;
- for (line=0; line<6; line++)
- {
- LCD_write_Data(font6x8[c][line]);
- }
- }
- /*-----------------------------------------------------------------------
- LCD_write_english_string
- -----------------------------------------------------------------------*/
- void LCD_write_english_string(unsigned char X,unsigned char Y,char *s)
- { LCD_set_XY(X,Y);
- while (*s)
- {
- LCD_write_char(*s);
- s++;
- }
- }
- void LCD_write_dangehanzi(unsigned char X,unsigned char Y,char s)
- { do
- { char line;
- LCD_set_XY(X,Y);
- for (line=0; line<16; line++)
- {
- LCD_write_Data(font16x16[s][line]);
- }
- LCD_set_XY(X,Y+1);
- for (line=16; line<32; line++)
- {
- LCD_write_Data(font16x16[s][line]);
- }
- }while(0);
- }
- void LCD_write_hanzi(unsigned char X,unsigned char Y,char s)
- { do
- { char line;
- LCD_set_XY(X,Y);
- for (line=0; line<12; line++)
- {
- LCD_write_Data(chinese_character[s][line]);
- }
- LCD_set_XY(X,Y+1);
- for (line=12; line<24; line++)
- {
- LCD_write_Data(chinese_character[s][line]);
- }
- }while(0);
- }
- void jm(void)
- { LCD_Clear();
- LCD_write_dangehanzi(0,0,0);
- LCD_write_dangehanzi(16,0,1);
- LCD_write_dangehanzi(32,0,2);
- LCD_write_dangehanzi(48,0,3);
- LCD_write_dangehanzi(64,0,4);
- LCD_write_hanzi(2,3,0);
- LCD_write_hanzi(14,3,1);
- LCD_write_hanzi(26,3,2);
- LCD_write_hanzi(38,3,3);
- LCD_write_hanzi(50,3,4);
- LCD_write_hanzi(62,3,5);
- LCD_write_english_string(0,5,"nokia5110 v1.0");
- }
- int main (void)
- {
- SystemCoreClockUpdate();
- SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK
- | SIM_SCGC5_PORTB_MASK
- | SIM_SCGC5_PORTC_MASK
- | SIM_SCGC5_PORTD_MASK
- | SIM_SCGC5_PORTE_MASK );
-
- /* 使能PORT时钟 */
- PORT_ENABLE_CLK(MKL_PORTD);
- LCD_Init();
- LCD_Clear();
- jm();
- while(1);
- }
复制代码 经程序的编译和下载,整个系统的构成和显示效果如图所示。
|