Very simple library for controlling small graphic LCDs
Embed:
(wiki syntax)
Show/hide line numbers
lcd.cpp
00001 #include "lcd.h " 00002 00003 BusOut LcdDB(p21, p22, p23, p24, p25, p26, p27, p28); 00004 DigitalOut LcdRS(p18); 00005 DigitalOut LcdRW(p19); 00006 DigitalOut LcdEN(p20); 00007 00008 #define bit_set(p,m) ((p) |= (1<<m)) 00009 #define bit_clear(p,m) ((p) &= ~(1<<m)) 00010 00011 /* Delays needed to use the LCD */ 00012 #define COMMAND_DELAY 15 // ns 00013 #define SHORT_DELAY 10 // us 00014 #define LONG_DELAY 100 // ms 00015 #define STARTDELAY 50 // ms 00016 00017 gfxLcd::gfxLcd() 00018 { 00019 init(); 00020 } 00021 00022 void gfxLcd::init() 00023 { 00024 // Set all to low 00025 LcdDB = 0x00; 00026 LcdRS = 0x00; 00027 LcdRW = 0x00; 00028 LcdEN = 0x00; 00029 00030 wait_ms(STARTDELAY); 00031 00032 // TODO: Try if the amount of commands could be reduced 00033 // and delays shortened 00034 00035 // Specify 8 bit interface and basic instruction set 00036 write(0, 0x30); 00037 wait_us(LONG_DELAY); 00038 // Specify 8 bit interface and basic instruction set 00039 write(0, 0x30); 00040 wait_us(SHORT_DELAY); 00041 // Specify Display on, Cursor off and Blink off 00042 write(0, 0x0C); 00043 wait_us(SHORT_DELAY); 00044 // Display clear. 00045 write(0, 0x01); 00046 wait_ms(SHORT_DELAY); 00047 // AC Increase (cursor move right), don't shift the display 00048 write(0, 0x06); 00049 wait_us(SHORT_DELAY); 00050 // Select extended instruction set 00051 write(0, 0x34); 00052 wait_us(SHORT_DELAY); 00053 // Graphic display ON 00054 write(0, 0x36); 00055 00056 fillScreen(false); 00057 update(); // Refresh display 00058 } 00059 00060 void gfxLcd::write(bool rs, char c) 00061 { 00062 // Writes an instruction/data to the LCD 00063 LcdRS = rs; 00064 wait_us(SHORT_DELAY); 00065 LcdRW = 0; 00066 wait_us(SHORT_DELAY); 00067 LcdEN = 0; 00068 LcdDB = c; 00069 wait_us(SHORT_DELAY); 00070 LcdEN = 1; 00071 wait_us(COMMAND_DELAY); 00072 LcdEN = 0; 00073 } 00074 00075 void gfxLcd::fillScreen(bool color) 00076 { 00077 char v, h; 00078 uint32_t d; 00079 00080 d = (color == true ? 0xFFFFL : 0x0000L); 00081 00082 for (v=0; v<32; v++) 00083 { 00084 for (h=0; h<160; h++) 00085 { 00086 fbuf.dots[v][h].word = d; 00087 } 00088 } 00089 fbuf.refresh = true; 00090 } 00091 00092 void gfxLcd::putPixel(char x, char y, bool color) 00093 { 00094 uint32_t v, h, b; 00095 00096 v = y; 00097 h = x/16; 00098 b = 15 - (x%16); 00099 00100 // Modify the actual word 00101 if (color == true) 00102 bit_set(fbuf.dots[v][h].word, b); 00103 else 00104 bit_clear(fbuf.dots[v][h].word, b); 00105 00106 fbuf.refresh = true; 00107 } 00108 00109 void gfxLcd::update() 00110 { 00111 char v, h; 00112 00113 if (fbuf.refresh == true) 00114 { 00115 for (v=0; v<32; v++) 00116 { 00117 write(0, 0x80 | v); // Set Vertical Address 00118 write(0, 0x80 | 0); // Set Horizontal Address 00119 00120 for (h=0; h<10; h++) 00121 { 00122 write(1, fbuf.dots[v][h].byte[1]); // Write High Byte 00123 write(1, fbuf.dots[v][h].byte[0]); // Write Low Byte 00124 } 00125 } 00126 fbuf.refresh = false; 00127 } 00128 }
Generated on Fri Jul 15 2022 12:01:38 by
1.7.2