Simon Ford / Mbed 2 deprecated displays

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EA_QVGALCD.cpp Source File

EA_QVGALCD.cpp

00001 /* mbed Embedded Artists QVGA LCD Display Library
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  */
00005   
00006 #include "EA_QVGALCD.h"
00007 
00008 EA_QVGALCD::EA_QVGALCD(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst) : _spi(mosi, miso, sclk), _cs(cs), _rst(rst) {
00009 
00010     _spi.frequency(15000000);
00011     _spi.format(9);
00012     _cs = 1;
00013     _rst = 0;
00014     wait(0.001);
00015     _rst = 1;
00016     wait(0.001);
00017 
00018 // 3-wire SPI Format (see 7.1 c)
00019 // =================
00020 //
00021 // The 3-wire spi format uses 9-bit messages of the form:
00022 //
00023 // [Data/nControl D7 D6 D5 D4 D3 D2 D1 D0]
00024 //  (first)                         (last)
00025 // 
00026 // Most commands and data are 16-bit, so are achieved by sending two
00027 // command messages, [ CommandMSB | CommandLSB ], or two data messages
00028 // [ DataMSB | DataLSB ].
00029 //
00030 // Controlling the Display
00031 // =======================
00032 // 
00033 // The Index Register is used to setup which configuration register is
00034 // modified. This address is changed by sending a command message:
00035 //
00036 //     command [ 0 | index[7:0] ]
00037 //
00038 // The data that follows update the register.
00039 //
00040 //
00041 // 0x07 - Display Control
00042 // ======================
00043 //
00044 // [ 0 0 0 PT1 PT0 VLE2 VLE1 SPT | 0 0 GON DTE CM 0 D1 D0 ]
00045 // where:
00046 //   PT[1:0]  - ? (0)
00047 //   VLE[2:1] - ? (0)
00048 //   SPT      - ? (0)
00049 //   CM       - 8 colour mode enable
00050 //   GON      - 0 = Gate-off level VGH 
00051 //   DTE      - 0 = All gate outputs become VGL
00052 //              1 = Selected gate outputs become VGH
00053 //   D[1:0]   - D1 = Display enable
00054 //              D0 = Display operating
00055 
00056 // 15.5 On Sequence
00057 //
00058 // -> Set R07h: GON =1, DTE = 0, D[1:0] = 01
00059     _cs = 0;
00060     config(0x7
00061                 , 1 << 5 // GON
00062                 | 0 << 4 // DTE 
00063                 | 0 << 3 // CM
00064                 | 1 << 0 // D[1:0] = 01 - operate, but disp off
00065                 );
00066 // -> Set R00h to 0001h
00067 // R00h [ 00000000 | 0000000 OSCEN ]
00068 // OSCEN - 1 = oscillator enabled
00069 
00070     config(0x0, 1);
00071 
00072 // -> Set R07h: GON = 1, DTE = 0, D[1:0] = 11   
00073 
00074     config(0x7
00075                 , 1 << 5 // GON
00076                 | 1 << 4 // DTE 
00077                 | 0 << 3 // CM
00078                 | 3 << 0 // D[1:0] = 3 - operate, display on
00079                 );
00080                 
00081 // -> Set R10h at 0000h : Exit sleep mode               
00082 // R10h [ 00000000 | 0000000 SLP ]
00083 // SLP = enter sleep mode (retain but no function)
00084 
00085     config(0x10, 0);
00086 
00087 // -> wait 30ms
00088     wait(0.030);
00089     
00090 // -> LCD driver AC setting (R02h)
00091     config(0x02, 0x0600);
00092     config(0x01, 0x2b3f);  // 1011
00093     config(0x25, 0xa000);   // 70Hz freq
00094 
00095 // -> Entry Mode setting (R11h)
00096 // R11h [ VSMode DFM1 DFM0 TRANS OEDef WMode DMode1 DMode0 | TY1 TY0 ID1 ID0 AM LG2 LG1 LG0 ]
00097 //  VSMode = freq dependant on VSYNC
00098 //  DFM[1:0] colour display mode 11 - 65k, 10 - 262k
00099 //  TRANS - allow transparent display
00100 //  OEDef:
00101 //    When OEDef = 1, OE defines the display window.
00102 //    When OEDef = 0, the display window is defined by R4Eh and R4Fh.
00103 //  WMode: Select the source of data to write in the RAM.
00104 //     0 Normal data bus (POR)
00105 //     1 Generic interface
00106 //  Dmode - where to show from (0 = ram)
00107 // TY - 262k mode options
00108 // ID[1:0] [ VERTICAL HORIZONTAL ] increment = 1 decrement = 0
00109 // AM - 0 = horizontal display, 1 = vertical
00110 // LG - do maths n written data
00111 
00112     config(0x11
00113                 , 0 << 15 // VSMode
00114                 | 3 << 13 // DFM[1:0]
00115                 | 0 << 12 // TRANS
00116                 | 1 << 11 // OEDef
00117                 | 0 << 10 // WMode
00118                 | 0 << 8  // DMode[1:0]
00119                 | 0 << 6  // TY[1:0]
00120                 | 3 << 4  // ID[1:0]
00121                 | 0 << 3  // AM 
00122                 | 0 << 0  // LG[2:0]
00123                 );
00124     _cs = 1;
00125     
00126     cls();
00127 }
00128 
00129 void EA_QVGALCD::pixel(int x, int y, int colour) {
00130     window(x, y, 1, 1);
00131     putp(colour);
00132 }
00133      
00134 int EA_QVGALCD::width() { return 240; }
00135 int EA_QVGALCD::height() { return 320; }
00136      
00137 
00138 void EA_QVGALCD::command(int value) {
00139     _spi.write(value & 0xFF);
00140 }
00141 
00142 void EA_QVGALCD::data(int value) {
00143     _spi.write(value | 0x100);
00144 }
00145 
00146 void EA_QVGALCD::config(int index, int value) {
00147     command(0);
00148     command(index);
00149     data(value >> 8);
00150     data(value);
00151 }
00152 
00153 
00154 void EA_QVGALCD::window(int x, int y, int w, int h) {
00155     _cs = 0;
00156 
00157     int hstart = x;
00158     int hend = x + w - 1;
00159     int vstart = y;
00160     int vend = y + h - 1;
00161     config(0x44, (hend << 8) | hstart);
00162     config(0x45, vstart);
00163     config(0x46, vend);
00164 
00165     config(0x4E, hstart & 0xFF);
00166     config(0x4F, vstart & 0x1FF);
00167     command(0);
00168     command(0x22);
00169 
00170     _cs = 1;
00171 }
00172 
00173 void EA_QVGALCD::putp(int colour) {
00174     _cs = 0;
00175     int top    = ((colour >> (8+8)) & 0xF8) // r7-3
00176                | ((colour >> (5+8)) & 0x07); // g7-5 
00177     int bottom = ((colour >> 5) & 0xE0) // g4-2
00178                | ((colour >> 3) & 0x1F); // b7-3
00179                    
00180     data(top);
00181     data(bottom);
00182     _cs = 1; 
00183 }
00184