UniGraphic-Fork for ST7920-LCD-controller and SH1106. Tested with 128x64 LCD with SPI and 128x64-OLED with IIC
Dependents: UniGraphic-St7920-Test AfficheurUTILECO
Fork of UniGraphic by
Fork of the UniGraphic-Library for monochrome LCDs with ST7920 controller and 128x64-IIC-OLED-Display with SH1106-Controller
Had to adapt LCD for following reasons:
- Give access to screenbuffer buffer[] to parent class
- pixel() and pixel_read() as they are hardware-dependent
- added reset-pin to IIC-Interface
GraphicDisplay:: sends buffer to LCD when auto_update is set to true.
Testprogram for ST7920 can be found here:
https://developer.mbed.org/users/charly/code/UniGraphic-St7920-Test/
Inits/ST7920.cpp@35:b8d3f1e68000, 2017-05-21 (annotated)
- Committer:
- charly
- Date:
- Sun May 21 20:50:09 2017 +0000
- Revision:
- 35:b8d3f1e68000
- Parent:
- 34:a9648877491f
buitify
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charly | 34:a9648877491f | 1 | /* mbed UniGraphic library - Device specific class |
charly | 35:b8d3f1e68000 | 2 | * for ST7920 by Karl Zweimueller https://developer.mbed.org/users/charly/ |
charly | 34:a9648877491f | 3 | * Copyright (c) 2015 Giuliano Dianda |
charly | 34:a9648877491f | 4 | * Released under the MIT License: http://mbed.org/license/mit |
charly | 34:a9648877491f | 5 | */ |
charly | 34:a9648877491f | 6 | #include "Protocols.h" |
charly | 34:a9648877491f | 7 | #include "ST7920.h" |
charly | 34:a9648877491f | 8 | |
charly | 34:a9648877491f | 9 | /*this is a quite standard config for ST7920 and similars*/ |
charly | 34:a9648877491f | 10 | |
charly | 34:a9648877491f | 11 | ////////////////////////////////////////////////////////////////////////////////// |
charly | 34:a9648877491f | 12 | // display settings /////////////////////////////////////////////////////// |
charly | 34:a9648877491f | 13 | ///////////////////////////////////////////////////////////////////////// |
charly | 35:b8d3f1e68000 | 14 | #define IC_X_SEGS 256 // ST7920 SEG has range 64-256 |
charly | 35:b8d3f1e68000 | 15 | #define IC_Y_COMS 32 // ST7920 COM has 32 COMS fix |
charly | 35:b8d3f1e68000 | 16 | // see ST7920_v4.0 page 1/49 |
charly | 34:a9648877491f | 17 | |
charly | 34:a9648877491f | 18 | |
charly | 34:a9648877491f | 19 | |
charly | 34:a9648877491f | 20 | ST7920::ST7920(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const char *name, unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
charly | 34:a9648877491f | 21 | : LCD(displayproto, port, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
charly | 34:a9648877491f | 22 | { |
charly | 34:a9648877491f | 23 | hw_reset(); |
charly | 34:a9648877491f | 24 | BusEnable(true); |
charly | 34:a9648877491f | 25 | init(); |
charly | 34:a9648877491f | 26 | cls(); |
charly | 34:a9648877491f | 27 | set_orientation(1); |
charly | 34:a9648877491f | 28 | locate(0,0); |
charly | 34:a9648877491f | 29 | } |
charly | 34:a9648877491f | 30 | ST7920::ST7920(proto_t displayproto, PinName* buspins, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const char *name, unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
charly | 34:a9648877491f | 31 | : LCD(displayproto, buspins, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
charly | 34:a9648877491f | 32 | { |
charly | 34:a9648877491f | 33 | hw_reset(); |
charly | 34:a9648877491f | 34 | BusEnable(true); |
charly | 34:a9648877491f | 35 | init(); |
charly | 34:a9648877491f | 36 | cls(); |
charly | 34:a9648877491f | 37 | set_orientation(1); |
charly | 34:a9648877491f | 38 | locate(0,0); |
charly | 34:a9648877491f | 39 | } |
charly | 34:a9648877491f | 40 | ST7920::ST7920(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const char *name, unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) |
charly | 34:a9648877491f | 41 | : LCD(displayproto, Hz, mosi, miso, sclk, CS, reset, DC, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) |
charly | 34:a9648877491f | 42 | { |
charly | 34:a9648877491f | 43 | hw_reset(); |
charly | 34:a9648877491f | 44 | BusEnable(true); |
charly | 34:a9648877491f | 45 | init(); |
charly | 34:a9648877491f | 46 | cls(); |
charly | 34:a9648877491f | 47 | set_orientation(1); |
charly | 34:a9648877491f | 48 | locate(0,0); |
charly | 34:a9648877491f | 49 | } |
charly | 34:a9648877491f | 50 | |
charly | 34:a9648877491f | 51 | //WriteInstructionRegister IR |
charly | 34:a9648877491f | 52 | void ST7920::wir(unsigned char data) |
charly | 34:a9648877491f | 53 | { |
charly | 34:a9648877491f | 54 | |
charly | 34:a9648877491f | 55 | /* |
charly | 34:a9648877491f | 56 | Timing Diagram of Serial Mode Data Transfer on p.20 |
charly | 34:a9648877491f | 57 | |
charly | 34:a9648877491f | 58 | |-------------------|------------------------|------------------------| |
charly | 34:a9648877491f | 59 | | Synchronizing | 1-st Byte Higher data | 2-nd Byte Lower data | |
charly | 34:a9648877491f | 60 | |-------------------|------------------------|------------------------| |
charly | 34:a9648877491f | 61 | | 1 1 1 1 1 RW RS 0 | D7 D6 D5 D4 0 0 0 0| D3 D2 D1 D0 0 0 0 0| |
charly | 34:a9648877491f | 62 | |-------------------|------------------------|------------------------| |
charly | 34:a9648877491f | 63 | */ |
charly | 34:a9648877491f | 64 | |
charly | 34:a9648877491f | 65 | wr_data8(0xf8); // Sync. - 1 1 1 1 1 0 0 0 RW=0,RS=0 |
charly | 34:a9648877491f | 66 | wr_data8(data & 0xf0); // HighData - D7 D6 D5 D4 0 0 0 0 |
charly | 34:a9648877491f | 67 | wr_data8(data << 4); // LowData - D3 D2 D1 D0 0 0 0 0 |
charly | 34:a9648877491f | 68 | if (data == 0x01) { |
charly | 34:a9648877491f | 69 | // Display Clear |
charly | 34:a9648877491f | 70 | wait_ms(1.6); |
charly | 34:a9648877491f | 71 | } else { |
charly | 34:a9648877491f | 72 | //everything else |
charly | 34:a9648877491f | 73 | wait_us(72); |
charly | 34:a9648877491f | 74 | } |
charly | 34:a9648877491f | 75 | } |
charly | 34:a9648877491f | 76 | |
charly | 34:a9648877491f | 77 | //WriteDataRegister DR |
charly | 34:a9648877491f | 78 | void ST7920::wdr(unsigned char data) |
charly | 34:a9648877491f | 79 | { |
charly | 34:a9648877491f | 80 | wr_data8(0xfa); // Sync. - 1 1 1 1 1 0 1 0 RW=0 RS=1 |
charly | 34:a9648877491f | 81 | wr_data8(data & 0xf0); // HighData - D7 D6 D5 D4 0 0 0 0 |
charly | 34:a9648877491f | 82 | wr_data8(data << 4); // LowData - D3 D2 D1 D0 0 0 0 0 |
charly | 34:a9648877491f | 83 | wait_us(72); |
charly | 34:a9648877491f | 84 | } |
charly | 34:a9648877491f | 85 | |
charly | 34:a9648877491f | 86 | |
charly | 35:b8d3f1e68000 | 87 | // init the lcd controller |
charly | 34:a9648877491f | 88 | // init sequence is manufacturer specific |
charly | 34:a9648877491f | 89 | void ST7920::init() |
charly | 34:a9648877491f | 90 | { |
charly | 34:a9648877491f | 91 | /* Start Initial Sequence ----------------------------------------------------*/ |
charly | 34:a9648877491f | 92 | wait_ms(40); |
charly | 34:a9648877491f | 93 | |
charly | 35:b8d3f1e68000 | 94 | // this is for a 128x64 LCD with SPI. |
charly | 35:b8d3f1e68000 | 95 | // parallel-mode not implemented! |
charly | 35:b8d3f1e68000 | 96 | |
charly | 34:a9648877491f | 97 | //Function set [DL=1 8-bit interface; DL=0 4-bit interface; |
charly | 34:a9648877491f | 98 | // RE=1: extended instruction; RE=0: basic instruction] |
charly | 34:a9648877491f | 99 | // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 |
charly | 34:a9648877491f | 100 | // 0 0 0 0 1 DL X RE 0 0 |
charly | 34:a9648877491f | 101 | // DisplayClear |
charly | 34:a9648877491f | 102 | wir(0x01); |
charly | 34:a9648877491f | 103 | // ReturnHome |
charly | 34:a9648877491f | 104 | wir(0x02); |
charly | 34:a9648877491f | 105 | // FunctionSet: BasicIR |
charly | 34:a9648877491f | 106 | wir(0x30); |
charly | 34:a9648877491f | 107 | // FunctionSet: ExtendedIR |
charly | 34:a9648877491f | 108 | //wir(0x34); |
charly | 34:a9648877491f | 109 | |
charly | 34:a9648877491f | 110 | //Function set [DL=1 8-bit interface; DL=0 4-bit interface; |
charly | 34:a9648877491f | 111 | // RE=1: extended instruction; RE=0: basic instruction] |
charly | 34:a9648877491f | 112 | // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 |
charly | 34:a9648877491f | 113 | // 0 0 0 0 1 DL X RE 0 0 |
charly | 34:a9648877491f | 114 | // |
charly | 34:a9648877491f | 115 | // ExtFunctionSet Display OFF |
charly | 34:a9648877491f | 116 | //wir(0x30); |
charly | 34:a9648877491f | 117 | |
charly | 34:a9648877491f | 118 | // DisplayControl [D=1: Display ON; C=1: Cursor ON; B=1: Character Blink ON] |
charly | 34:a9648877491f | 119 | // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 |
charly | 34:a9648877491f | 120 | // 0 0 0 0 0 0 1 D C B |
charly | 34:a9648877491f | 121 | // DisplayControl: Display ON CharacterBlink ON Cursor=On |
charly | 34:a9648877491f | 122 | //wir(0x0f); |
charly | 34:a9648877491f | 123 | // DisplayControl: Display ON CharacterBlink OFF Cursor=Off |
charly | 34:a9648877491f | 124 | wir(0x0c); |
charly | 34:a9648877491f | 125 | |
charly | 34:a9648877491f | 126 | // Display Clear |
charly | 34:a9648877491f | 127 | wir(0x01); |
charly | 34:a9648877491f | 128 | //wait_ms(10); |
charly | 34:a9648877491f | 129 | //Set cursor position and display shift when doing write or read operation |
charly | 34:a9648877491f | 130 | // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 |
charly | 34:a9648877491f | 131 | // 0 0 0 0 0 0 0 1 I/D S |
charly | 34:a9648877491f | 132 | // EntryModeSet: Increase, Shift right |
charly | 34:a9648877491f | 133 | wir(0x06); |
charly | 34:a9648877491f | 134 | |
charly | 34:a9648877491f | 135 | } |
charly | 34:a9648877491f | 136 | |
charly | 34:a9648877491f | 137 | void ST7920::mirrorXY(mirror_t mode) |
charly | 34:a9648877491f | 138 | { |
charly | 35:b8d3f1e68000 | 139 | //not supported by hardware |
charly | 34:a9648877491f | 140 | } |
charly | 34:a9648877491f | 141 | |
charly | 34:a9648877491f | 142 | void ST7920::cls(void) |
charly | 34:a9648877491f | 143 | { |
charly | 34:a9648877491f | 144 | //DisplayClear |
charly | 34:a9648877491f | 145 | wir(0x01); |
charly | 34:a9648877491f | 146 | //ReturnHome |
charly | 34:a9648877491f | 147 | wir(0x02); |
charly | 34:a9648877491f | 148 | |
charly | 34:a9648877491f | 149 | memset(buffer,0x00,sizeX()*(sizeY()>>3)); // clear display buffer |
charly | 34:a9648877491f | 150 | } |
charly | 34:a9648877491f | 151 | |
charly | 35:b8d3f1e68000 | 152 | // write the full screenbuffer to the LCD GDRAM |
charly | 34:a9648877491f | 153 | void ST7920::copy_to_lcd(void) |
charly | 34:a9648877491f | 154 | { |
charly | 34:a9648877491f | 155 | int i,j; |
charly | 34:a9648877491f | 156 | |
charly | 34:a9648877491f | 157 | // Change to Extended InstructionSet |
charly | 34:a9648877491f | 158 | //FunctionSet: Extended |
charly | 34:a9648877491f | 159 | wir(0x36); |
charly | 34:a9648877491f | 160 | for(i=0; i<32; i++) { //32 Doppelzeilen |
charly | 34:a9648877491f | 161 | // SetGDRAM-Adress AC: 0 |
charly | 34:a9648877491f | 162 | //Vertical-Adr |
charly | 34:a9648877491f | 163 | wir(0x80+i); |
charly | 34:a9648877491f | 164 | //Horizontal-Adr |
charly | 34:a9648877491f | 165 | wir(0x80); |
charly | 34:a9648877491f | 166 | |
charly | 34:a9648877491f | 167 | //write 16 Double-Bytes of data |
charly | 35:b8d3f1e68000 | 168 | //upper part of Display |
charly | 34:a9648877491f | 169 | for (j=0; j<8; j++) { |
charly | 34:a9648877491f | 170 | wdr(buffer[(i*16) + (j*2)]); |
charly | 34:a9648877491f | 171 | wdr(buffer[(i*16) + (j*2) +1]); |
charly | 34:a9648877491f | 172 | //if (i==0) {wdr(0xAA); wdr(0xAA);}else {wdr(0x00); wdr(0x00);}; |
charly | 34:a9648877491f | 173 | } |
charly | 35:b8d3f1e68000 | 174 | //lower part of Display |
charly | 34:a9648877491f | 175 | for (j=0; j<8; j++) { |
charly | 34:a9648877491f | 176 | wdr(buffer[(i*16)+512 + (j*2)]); |
charly | 34:a9648877491f | 177 | wdr(buffer[(i*16)+512 + (j*2) +1]); |
charly | 34:a9648877491f | 178 | //if (i==31) {wdr(0xAA); wdr(0xAA);}else {wdr(0x00); wdr(0x00);}; |
charly | 34:a9648877491f | 179 | } |
charly | 34:a9648877491f | 180 | |
charly | 34:a9648877491f | 181 | |
charly | 34:a9648877491f | 182 | /* |
charly | 34:a9648877491f | 183 | //obere Displayhälfte |
charly | 34:a9648877491f | 184 | wdr(0xFF); |
charly | 34:a9648877491f | 185 | wdr(0x00); |
charly | 34:a9648877491f | 186 | wdr(0x00); |
charly | 34:a9648877491f | 187 | wdr(0xAA); |
charly | 34:a9648877491f | 188 | wdr(0xF0); |
charly | 34:a9648877491f | 189 | wdr(0xAA); |
charly | 34:a9648877491f | 190 | wdr(0xF0); |
charly | 34:a9648877491f | 191 | wdr(0xAA); |
charly | 34:a9648877491f | 192 | wdr(0xF0); |
charly | 34:a9648877491f | 193 | wdr(0xAA); |
charly | 34:a9648877491f | 194 | wdr(0xF0); |
charly | 34:a9648877491f | 195 | wdr(0xAA); |
charly | 34:a9648877491f | 196 | wdr(0xF0); |
charly | 34:a9648877491f | 197 | wdr(0xAA); |
charly | 34:a9648877491f | 198 | wdr(0xFF); |
charly | 34:a9648877491f | 199 | |
charly | 34:a9648877491f | 200 | //Zeile fertig |
charly | 34:a9648877491f | 201 | |
charly | 34:a9648877491f | 202 | // untere Displayhälfte |
charly | 34:a9648877491f | 203 | |
charly | 34:a9648877491f | 204 | wdr(0x00); |
charly | 34:a9648877491f | 205 | wdr(0x00); |
charly | 34:a9648877491f | 206 | wdr(0xFF); |
charly | 34:a9648877491f | 207 | wdr(0xFF); |
charly | 34:a9648877491f | 208 | wdr(0xAA); |
charly | 34:a9648877491f | 209 | wdr(0xF0); |
charly | 34:a9648877491f | 210 | wdr(0xAA); |
charly | 34:a9648877491f | 211 | wdr(0xF0); |
charly | 34:a9648877491f | 212 | wdr(0xAA); |
charly | 34:a9648877491f | 213 | wdr(0xF0); |
charly | 34:a9648877491f | 214 | wdr(0xAA); |
charly | 34:a9648877491f | 215 | wdr(0xF0); |
charly | 34:a9648877491f | 216 | wdr(0xAA); |
charly | 34:a9648877491f | 217 | wdr(0xF0); |
charly | 34:a9648877491f | 218 | wdr(0x00); |
charly | 34:a9648877491f | 219 | wdr(0xFF); |
charly | 34:a9648877491f | 220 | */ |
charly | 34:a9648877491f | 221 | } |
charly | 34:a9648877491f | 222 | |
charly | 34:a9648877491f | 223 | //back to BasicInstructionSet |
charly | 34:a9648877491f | 224 | wir(0x30); |
charly | 34:a9648877491f | 225 | |
charly | 34:a9648877491f | 226 | } |
charly | 34:a9648877491f | 227 | |
charly | 34:a9648877491f | 228 | void ST7920::set_contrast(int o) |
charly | 34:a9648877491f | 229 | { |
charly | 35:b8d3f1e68000 | 230 | //not supported by hardware |
charly | 34:a9648877491f | 231 | } |
charly | 34:a9648877491f | 232 | |
charly | 34:a9648877491f | 233 | void ST7920::invert(unsigned char o) |
charly | 34:a9648877491f | 234 | { |
charly | 35:b8d3f1e68000 | 235 | //not supported by hardware |
charly | 34:a9648877491f | 236 | } |
charly | 34:a9648877491f | 237 | |
charly | 34:a9648877491f | 238 | void ST7920::wr_grambuf(unsigned short* data, unsigned int lenght) |
charly | 34:a9648877491f | 239 | { |
charly | 35:b8d3f1e68000 | 240 | // not needed, not supported |
charly | 34:a9648877491f | 241 | } |
charly | 34:a9648877491f | 242 | |
charly | 34:a9648877491f | 243 | void ST7920::wr_gram(unsigned short data) |
charly | 34:a9648877491f | 244 | { |
charly | 35:b8d3f1e68000 | 245 | // not needed, not supported |
charly | 34:a9648877491f | 246 | } |
charly | 34:a9648877491f | 247 | |
charly | 34:a9648877491f | 248 | void ST7920::wr_gram(unsigned short data, unsigned int count) |
charly | 34:a9648877491f | 249 | { |
charly | 35:b8d3f1e68000 | 250 | // not needed, not supported |
charly | 34:a9648877491f | 251 | } |
charly | 34:a9648877491f | 252 | |
charly | 34:a9648877491f | 253 | void ST7920::wr_data16(unsigned short data) |
charly | 34:a9648877491f | 254 | { |
charly | 35:b8d3f1e68000 | 255 | // not needed, not supported |
charly | 34:a9648877491f | 256 | } |
charly | 34:a9648877491f | 257 | |
charly | 34:a9648877491f | 258 | void ST7920::wr_cmd16(unsigned short cmd) |
charly | 34:a9648877491f | 259 | { |
charly | 35:b8d3f1e68000 | 260 | // not needed, not supported |
charly | 34:a9648877491f | 261 | } |
charly | 34:a9648877491f | 262 | |
charly | 34:a9648877491f | 263 | void ST7920::wr_cmd8(unsigned char cmd) |
charly | 34:a9648877491f | 264 | { |
charly | 35:b8d3f1e68000 | 265 | // not needed, not supported |
charly | 34:a9648877491f | 266 | } |
charly | 34:a9648877491f | 267 | |
charly | 34:a9648877491f | 268 | unsigned short ST7920::rd_gram(bool convert) |
charly | 34:a9648877491f | 269 | { |
charly | 35:b8d3f1e68000 | 270 | // not needed, not supported |
charly | 34:a9648877491f | 271 | return(0); |
charly | 34:a9648877491f | 272 | } |
charly | 34:a9648877491f | 273 | |
charly | 34:a9648877491f | 274 | unsigned int ST7920::rd_reg_data32(unsigned char reg) |
charly | 34:a9648877491f | 275 | { |
charly | 35:b8d3f1e68000 | 276 | // not needed, not supported |
charly | 34:a9648877491f | 277 | return(0); |
charly | 34:a9648877491f | 278 | } |
charly | 34:a9648877491f | 279 | |
charly | 34:a9648877491f | 280 | unsigned int ST7920::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
charly | 34:a9648877491f | 281 | { |
charly | 35:b8d3f1e68000 | 282 | // not needed, not supported |
charly | 34:a9648877491f | 283 | return(0); |
charly | 34:a9648877491f | 284 | } |