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 GraphicsDisplay

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ST7920.cpp Source File

ST7920.cpp

00001 /* mbed UniGraphic library - Device specific class
00002 * for ST7920 by Karl Zweimueller https://developer.mbed.org/users/charly/
00003 * Copyright (c) 2015 Giuliano Dianda
00004 * Released under the MIT License: http://mbed.org/license/mit
00005 */
00006 #include "Protocols.h "
00007 #include "ST7920.h"
00008 
00009 /*this is a quite standard config for ST7920 and similars*/
00010 
00011 //////////////////////////////////////////////////////////////////////////////////
00012 // display settings ///////////////////////////////////////////////////////
00013 /////////////////////////////////////////////////////////////////////////
00014 #define IC_X_SEGS    256 // ST7920 SEG has range 64-256
00015 #define IC_Y_COMS    32  // ST7920 COM has 32 COMS fix
00016 // see ST7920_v4.0 page 1/49
00017 
00018 
00019 
00020 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)
00021     : LCD(displayproto, port, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00022 {
00023     hw_reset();
00024     BusEnable(true);
00025     init();
00026     cls();
00027     set_orientation(1);
00028     locate(0,0);
00029 }
00030 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)
00031     : LCD(displayproto, buspins, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00032 {
00033     hw_reset();
00034     BusEnable(true);
00035     init();
00036     cls();
00037     set_orientation(1);
00038     locate(0,0);
00039 }
00040 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)
00041     : LCD(displayproto, Hz, mosi, miso, sclk, CS, reset, DC, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name)
00042 {
00043     hw_reset();
00044     BusEnable(true);
00045     init();
00046     cls();
00047     set_orientation(1);
00048     locate(0,0);
00049 }
00050 
00051 //WriteInstructionRegister IR
00052 void ST7920::wir(unsigned char data)
00053 {
00054 
00055     /*
00056     Timing Diagram of Serial Mode Data Transfer on p.20
00057 
00058     |-------------------|------------------------|------------------------|
00059     |  Synchronizing    | 1-st Byte Higher data  |  2-nd Byte Lower data  |
00060     |-------------------|------------------------|------------------------|
00061     | 1 1 1 1 1 RW RS 0 | D7 D6 D5 D4  0  0  0  0| D3 D2 D1 D0  0  0  0  0|
00062     |-------------------|------------------------|------------------------|
00063     */
00064 
00065     wr_data8(0xf8);        // Sync.    - 1  1  1  1  1  0  0  0  RW=0,RS=0
00066     wr_data8(data & 0xf0); // HighData - D7 D6 D5 D4 0  0  0  0
00067     wr_data8(data << 4);   // LowData  - D3 D2 D1 D0 0  0  0  0
00068     if (data == 0x01) {
00069         // Display Clear
00070         wait_ms(1.6);
00071     } else {
00072         //everything else
00073         wait_us(72);
00074     }
00075 }
00076 
00077 //WriteDataRegister DR
00078 void ST7920::wdr(unsigned char data)
00079 {
00080     wr_data8(0xfa);        // Sync.    - 1  1  1  1  1  0  1  0 RW=0 RS=1
00081     wr_data8(data & 0xf0); // HighData - D7 D6 D5 D4 0  0  0  0
00082     wr_data8(data << 4);   // LowData  - D3 D2 D1 D0 0  0  0  0
00083     wait_us(72);
00084 }
00085 
00086 
00087 // init the lcd controller
00088 // init sequence is manufacturer specific
00089 void ST7920::init()
00090 {
00091     /* Start Initial Sequence ----------------------------------------------------*/
00092     wait_ms(40);
00093 
00094     // this is for a 128x64 LCD with SPI.
00095     // parallel-mode not implemented!
00096     
00097     //Function set [DL=1 8-bit interface; DL=0 4-bit interface;
00098     //              RE=1: extended instruction; RE=0: basic instruction]
00099     // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
00100     //  0  0   0   0   1  DL   X  RE   0   0
00101     // DisplayClear
00102     wir(0x01);
00103     // ReturnHome
00104     wir(0x02);
00105     // FunctionSet: BasicIR
00106     wir(0x30);
00107     // FunctionSet: ExtendedIR
00108     //wir(0x34);
00109 
00110     //Function set [DL=1 8-bit interface; DL=0 4-bit interface;
00111     //              RE=1: extended instruction; RE=0: basic instruction]
00112     // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
00113     //  0  0   0   0   1  DL   X  RE   0   0
00114     //
00115     // ExtFunctionSet Display OFF
00116     //wir(0x30);
00117 
00118     // DisplayControl [D=1: Display ON; C=1: Cursor ON; B=1: Character Blink ON]
00119     // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
00120     //  0  0   0   0   0   0   1   D   C   B
00121     // DisplayControl: Display ON CharacterBlink ON Cursor=On
00122     //wir(0x0f);
00123     // DisplayControl: Display ON CharacterBlink OFF Cursor=Off
00124     wir(0x0c);
00125 
00126     // Display Clear
00127     wir(0x01);
00128     //wait_ms(10);
00129     //Set cursor position and display shift when doing write or read operation
00130     // RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
00131     //  0  0   0   0   0   0   0   1 I/D   S
00132     // EntryModeSet: Increase, Shift right
00133     wir(0x06);
00134 
00135 }
00136 
00137 void ST7920::mirrorXY(mirror_t mode)
00138 {
00139     //not supported by hardware
00140 }
00141 
00142 void ST7920::cls(void)
00143 {
00144     //DisplayClear
00145     wir(0x01);
00146     //ReturnHome
00147     wir(0x02);
00148     
00149     memset(buffer,0x00,sizeX()*(sizeY()>>3));  // clear display buffer
00150 }
00151 
00152 // write the full screenbuffer to the LCD GDRAM
00153 void ST7920::copy_to_lcd(void)
00154 {
00155     int i,j;
00156 
00157     // Change to Extended InstructionSet
00158     //FunctionSet: Extended
00159     wir(0x36);
00160     for(i=0; i<32; i++) { //32 Doppelzeilen
00161         // SetGDRAM-Adress AC: 0
00162         //Vertical-Adr
00163         wir(0x80+i);
00164         //Horizontal-Adr
00165         wir(0x80);
00166 
00167         //write 16 Double-Bytes of data
00168         //upper part of Display
00169         for (j=0; j<8; j++) {
00170             wdr(buffer[(i*16) + (j*2)]);
00171             wdr(buffer[(i*16) + (j*2) +1]);
00172             //if (i==0) {wdr(0xAA); wdr(0xAA);}else {wdr(0x00); wdr(0x00);};
00173         }
00174         //lower part of Display
00175         for (j=0; j<8; j++) {
00176             wdr(buffer[(i*16)+512 + (j*2)]);
00177             wdr(buffer[(i*16)+512 + (j*2) +1]);
00178             //if (i==31) {wdr(0xAA); wdr(0xAA);}else {wdr(0x00); wdr(0x00);};
00179         }
00180         
00181 
00182         /*
00183         //obere Displayhälfte
00184         wdr(0xFF);
00185         wdr(0x00);
00186         wdr(0x00);
00187         wdr(0xAA);
00188         wdr(0xF0);
00189         wdr(0xAA);
00190         wdr(0xF0);
00191         wdr(0xAA);
00192         wdr(0xF0);
00193         wdr(0xAA);
00194         wdr(0xF0);
00195         wdr(0xAA);
00196         wdr(0xF0);
00197         wdr(0xAA);
00198         wdr(0xFF);
00199 
00200         //Zeile fertig
00201 
00202         // untere Displayhälfte
00203 
00204         wdr(0x00);
00205         wdr(0x00);
00206         wdr(0xFF);
00207         wdr(0xFF);
00208         wdr(0xAA);
00209         wdr(0xF0);
00210         wdr(0xAA);
00211         wdr(0xF0);
00212         wdr(0xAA);
00213         wdr(0xF0);
00214         wdr(0xAA);
00215         wdr(0xF0);
00216         wdr(0xAA);
00217         wdr(0xF0);
00218         wdr(0x00);
00219         wdr(0xFF);
00220         */
00221     }
00222 
00223     //back to BasicInstructionSet
00224     wir(0x30);
00225 
00226 }
00227 
00228 void ST7920::set_contrast(int o)
00229 {
00230     //not supported by hardware
00231 }
00232 
00233 void ST7920::invert(unsigned char o)
00234 {
00235     //not supported by hardware
00236 }
00237 
00238 void ST7920::wr_grambuf(unsigned short* data, unsigned int lenght)
00239 {
00240     // not needed, not supported
00241 }
00242 
00243 void ST7920::wr_gram(unsigned short data)
00244 {
00245     // not needed, not supported
00246 }
00247 
00248 void ST7920::wr_gram(unsigned short data, unsigned int count)
00249 {
00250     // not needed, not supported
00251 }
00252 
00253 void ST7920::wr_data16(unsigned short data)
00254 {
00255     // not needed, not supported
00256 }
00257 
00258 void ST7920::wr_cmd16(unsigned short cmd)
00259 {
00260     // not needed, not supported
00261 }
00262 
00263 void ST7920::wr_cmd8(unsigned char cmd)
00264 {
00265     // not needed, not supported
00266 }
00267 
00268 unsigned short ST7920::rd_gram(bool convert)
00269 {
00270     // not needed, not supported
00271     return(0);
00272 }
00273 
00274 unsigned int ST7920::rd_reg_data32(unsigned char reg)
00275 {
00276     // not needed, not supported
00277     return(0);
00278 }
00279 
00280 unsigned int ST7920::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
00281 {
00282     // not needed, not supported
00283     return(0);
00284 }