LPC2138微控制器之LCD12864实例
嵌入式领域使用得比较多的LCD是字符型、图形和TFT型LCD,此处选用Proteus支持的HDG12864F-3图形LCD进行模拟仿真。
首先需要熟悉HDG12864F-3的管脚功能、工作电压、工作原理,可以通过Hantronix官网下载相关手册。
电源配置如下:
其中一个很重要的部分是SED1565控制器的命令集:
Proteus仿真电路原理图如下:
实际调试的过程中发现,当HDG12864F-3横屏放置和竖屏放置时,其横纵轴随之变化(可能是Proteus的一个bug吧)。
源代码分为SED1565控制器和LCD显示两部分。
sed1565.h
#ifndef __LPC2138_SED1565_H__ #define __LPC2138_SED1565_H__ #define BIT_VALUE(BIT,X) \ { \ if (X) {\ IO1SET = 1 << BIT; \ } \ else \ { \ IO1CLR = 1 << BIT; \ IO1CLR = 0x0; \ } \ } #define BYTE_SET(OFFSET,X) \ { \ IO1CLR = 0xFF << OFFSET; \ IO1SET = (0xFF & X) << OFFSET; \ } #define BYTE_GET(OFFSET) ((IO1PIN >> OFFSET) & 0xFF) /* Reset Signal */ #define RESET(X) BIT_VALUE(20,X) /* Control/Data Select Signal, 0-Command 1-Data */ #define A0(X) BIT_VALUE(21,X) /* Read/Write Select Signal, 0-Write, 1-Read */ #define RW(X) BIT_VALUE(22,X) /* Enable Clock Input */ #define E(X) BIT_VALUE(23,X) /* Data Bus */ #define DATA(X) BYTE_SET(24,X) #define RDATA BYTE_GET(24) void sed1565_init(void); void open_display(unsigned char temp); void display_row(unsigned char temp); void display_page_addr(unsigned char temp); void display_col(unsigned char temp); unsigned char read_state(void); void data_write(unsigned char temp); unsigned char data_read(unsigned char temp); void adc_choice(unsigned char temp); void display_face(unsigned char temp); void display_all_onoff(unsigned char temp); void display_deflect_voltage(unsigned char temp); void read_change_write(void); void read_change_write_end(void); void reset(void); void com_direct_choice(unsigned char temp); #endif
sed1565.c
#include <lpc213x.h> #include "sed1565.h" void sed1565_init(void) { /* Configure P1.20~31 as output pins */ PINSEL2 = 0x00000000; IO1DIR = 0xFFF00000; } /* ** CMD1 Display ON/OFF */ void open_display(unsigned char temp) { A0(0); RW(0); E(1); if(temp) { DATA(0xaf); } else { DATA(0xae); } E(0); RW(1); } /* ** CMD2 Display Start Line Set */ void display_row(unsigned char temp) { A0(0); RW(0); E(1); DATA(temp|0x40); E(0); RW(1); } /* ** CMD3 Page Address Set */ void display_page_addr(unsigned char temp) { A0(0); RW(0); E(1); DATA(temp|0xb0); E(0); RW(1); } /* ** CMD4 Column Address Set */ void display_col(unsigned char temp) { A0(0); RW(0); E(1); DATA((temp>>4)|0x10); E(0); E(1); DATA(temp&0x0f); E(0); RW(1); } /* ** CMD5 Status Read */ unsigned char read_state() { unsigned char temp; A0(0); RW(1); E(1); temp = RDATA; E(0); return temp; } /* ** CMD6 Display Data Write */ void data_write(unsigned char temp) { A0(1); RW(0); E(1); DATA(temp); E(0); RW(1); } /* ** CMD7 Display Data Read */ unsigned char data_read(unsigned char temp) { A0(1); RW(1); E(1); DATA(0xff); temp = RDATA; E(0); return temp; } /* ** CMD8 ADC Select ** Sets the display RAM address ** SEG output correspondence ** 0:normal, 1:reverse */ void adc_choice(unsigned char temp) { A0(0); RW(0); E(1); DATA(0xa0|temp); E(0); RW(1); } /* ** CMD9 Display normal/reverse ** 0:normal, 1:reverse */ void display_face(unsigned char temp) { A0(0); RW(0); E(1); DATA(0xa6|temp); E(0); RW(1); } /* ** CMD10 Display all points ON/OFF ** 0: normal display ** 1: all point ON */ void display_all_onoff(unsigned char temp) { A0(0); RW(0); E(1); DATA(0xa4|temp); E(0); RW(1); } /* ** CMD11 LCD Bias Set */ void display_deflect_voltage(unsigned char temp) { A0(0); RW(0); E(1); DATA(0xa2|temp); E(0); RW(1); } /* ** CMD12 Read/Modify/Write ** Column address increment ** At writ: +1 ** At read: 0 */ void read_change_write() { A0(0); RW(0); E(1); DATA(0xe0); E(0); RW(1); } /* ** CMD13 End ** Clear Read/Modify/Write */ void read_change_write_end() { A0(0); RW(0); E(1); DATA(0xee); E(0); RW(1); } /* ** CMD14 Reset ** Internal Reset */ void reset() { A0(0); RW(0); E(1); DATA(0xe2); E(0); RW(1); } /* ** CMD15 Common Output Mode Select ** Select COM output scan direction ** 0: normal direction ** 1: reverse direction */ void com_direct_choice(unsigned char temp) { A0(0); RW(0); E(1); DATA(0xb0|(temp<<4)); E(0); RW(1); }
lcd.h
#ifndef __LPC2138_LCD_H__ #define __LPC2138_LCD_H__ void lcd_init(void); void smile(void); #endif
lcd.c
#include "sed1565.h" #include "lcd.h" unsigned char beauty[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0xFF,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xF0,0xF0,0xF8,0xFC,0xFC,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x5F,0x1F,0x1F,0x1F,0x3F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0x7F,0x1F,0x0F,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x01,0x01,0x01,0x01,0x03,0x02,0x20,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFB,0xF3,0xF7,0xE7,0xAF,0xAF,0x9F,0x03,0x00,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x01,0x43,0xE3,0x07,0xDF,0xBF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F, 0x0F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFC,0xF0,0xE9,0xF1,0xF1,0x87,0x03,0x01,0x7C,0x5A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x01,0x00,0x00,0x00,0x00,0x80,0xC1,0x3F,0x1F,0x03,0x02,0x02,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x1F,0x3F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF1,0xE1,0xE0,0xC0,0xC0,0x40,0x40,0x40,0x40,0x80,0x80,0x80,0x80,0xC0,0xC0,0xE0,0xA0,0x90,0x98,0x08,0x04,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF3,0xF3,0xE3,0xE3,0xF1,0xF1,0xF0,0xF8,0xFC,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xEE,0xFE,0xFE, 0xF4,0xFC,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0x78,0xF0,0xE0,0x80,0x40,0x80,0x80,0x80,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x08,0x08,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7D,0xFB,0xFA,0xFA,0xD7,0x1F,0x1F,0x3F,0x7D,0xE1,0xC2,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80,0x80,0xE0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,0xF0,0xE0,0xC0, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xF5,0xC2,0x82,0x00,0x01,0x07,0x0F,0x08,0x10,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x01,0x00,0x00,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x03,0x03,0x03,0x02,0x06,0x06,0x07,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x7F,0x7F,0x7F,0x7F, }; void lcd_init(void) { sed1565_init(); } void smile(void) { unsigned char a, b; for(a = 0; a < 8; a++) { reset(); display_col(0); display_page_addr(a); for(b = 0; b < 128; b++) { data_write(beauty[a * 128 + b]); } } }
其中beauty数组是通过LCD12864图片取模软件生成的。
显示效果如下: