Simple driver for the ST7920 graphic LCD 128x64. Basic functions for displaying internal ROM fonts like a 16x4 LCD Extended function to fill the screen with a bitmap
st7920.cpp@0:1a97e0243dbc, 2012-06-29 (annotated)
- Committer:
- Bas
- Date:
- Fri Jun 29 22:47:51 2012 +0000
- Revision:
- 0:1a97e0243dbc
Function added for displaying vertical oriented bitmaps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bas | 0:1a97e0243dbc | 1 | #include "st7920.h" |
Bas | 0:1a97e0243dbc | 2 | |
Bas | 0:1a97e0243dbc | 3 | ST7920::ST7920 (PinName _RS,PinName _RW, PinName _E, PinName DB0, PinName DB1, PinName DB2, PinName DB3, PinName DB4, PinName DB5, PinName DB6, PinName DB7) |
Bas | 0:1a97e0243dbc | 4 | : DB(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7),RS(_RS), RW(_RW), E(_E) { |
Bas | 0:1a97e0243dbc | 5 | |
Bas | 0:1a97e0243dbc | 6 | DB.output(); |
Bas | 0:1a97e0243dbc | 7 | E.write(0); |
Bas | 0:1a97e0243dbc | 8 | RS.write(0); |
Bas | 0:1a97e0243dbc | 9 | RW.write(0); |
Bas | 0:1a97e0243dbc | 10 | InitDisplay(); |
Bas | 0:1a97e0243dbc | 11 | } |
Bas | 0:1a97e0243dbc | 12 | |
Bas | 0:1a97e0243dbc | 13 | unsigned int ST7920::ByteReadLCD() { |
Bas | 0:1a97e0243dbc | 14 | unsigned int data; |
Bas | 0:1a97e0243dbc | 15 | |
Bas | 0:1a97e0243dbc | 16 | DB.input(); |
Bas | 0:1a97e0243dbc | 17 | E.write(1); |
Bas | 0:1a97e0243dbc | 18 | data = DB.read(); |
Bas | 0:1a97e0243dbc | 19 | E.write(0); |
Bas | 0:1a97e0243dbc | 20 | DB.output(); |
Bas | 0:1a97e0243dbc | 21 | |
Bas | 0:1a97e0243dbc | 22 | return data; |
Bas | 0:1a97e0243dbc | 23 | } |
Bas | 0:1a97e0243dbc | 24 | |
Bas | 0:1a97e0243dbc | 25 | void ST7920::ByteWriteLCD(unsigned int data) { |
Bas | 0:1a97e0243dbc | 26 | DB.output(); |
Bas | 0:1a97e0243dbc | 27 | E.write(1); |
Bas | 0:1a97e0243dbc | 28 | DB.write(data); |
Bas | 0:1a97e0243dbc | 29 | E.write(0); |
Bas | 0:1a97e0243dbc | 30 | } |
Bas | 0:1a97e0243dbc | 31 | |
Bas | 0:1a97e0243dbc | 32 | void ST7920::WriteInstruction(unsigned int Command) { |
Bas | 0:1a97e0243dbc | 33 | ReadBusyFlag(); |
Bas | 0:1a97e0243dbc | 34 | RS.write(0); |
Bas | 0:1a97e0243dbc | 35 | RW.write(0); |
Bas | 0:1a97e0243dbc | 36 | ByteWriteLCD(Command); |
Bas | 0:1a97e0243dbc | 37 | } |
Bas | 0:1a97e0243dbc | 38 | |
Bas | 0:1a97e0243dbc | 39 | void ST7920::WriteRAM(unsigned int data) { |
Bas | 0:1a97e0243dbc | 40 | ReadBusyFlag(); |
Bas | 0:1a97e0243dbc | 41 | RS.write(1); |
Bas | 0:1a97e0243dbc | 42 | RW.write(0); |
Bas | 0:1a97e0243dbc | 43 | ByteWriteLCD(data); |
Bas | 0:1a97e0243dbc | 44 | } |
Bas | 0:1a97e0243dbc | 45 | |
Bas | 0:1a97e0243dbc | 46 | void ST7920::ReadBusyFlag() { |
Bas | 0:1a97e0243dbc | 47 | unsigned char data; |
Bas | 0:1a97e0243dbc | 48 | |
Bas | 0:1a97e0243dbc | 49 | RS.write(0); |
Bas | 0:1a97e0243dbc | 50 | RW.write(1); |
Bas | 0:1a97e0243dbc | 51 | while ((data & 0x7F) == BUSY_FLAG_BF) { |
Bas | 0:1a97e0243dbc | 52 | data = ByteReadLCD(); |
Bas | 0:1a97e0243dbc | 53 | } |
Bas | 0:1a97e0243dbc | 54 | } |
Bas | 0:1a97e0243dbc | 55 | |
Bas | 0:1a97e0243dbc | 56 | unsigned int ST7920::Read_AC() { |
Bas | 0:1a97e0243dbc | 57 | RS.write(0); |
Bas | 0:1a97e0243dbc | 58 | RW.write(1); |
Bas | 0:1a97e0243dbc | 59 | return (ByteReadLCD() & 0x7F); |
Bas | 0:1a97e0243dbc | 60 | } |
Bas | 0:1a97e0243dbc | 61 | |
Bas | 0:1a97e0243dbc | 62 | unsigned int ST7920::ReadRAM() { |
Bas | 0:1a97e0243dbc | 63 | ReadBusyFlag(); |
Bas | 0:1a97e0243dbc | 64 | RS.write(1); |
Bas | 0:1a97e0243dbc | 65 | RW.write(1); |
Bas | 0:1a97e0243dbc | 66 | return ByteReadLCD(); |
Bas | 0:1a97e0243dbc | 67 | } |
Bas | 0:1a97e0243dbc | 68 | |
Bas | 0:1a97e0243dbc | 69 | void ST7920::InitDisplay() { |
Bas | 0:1a97e0243dbc | 70 | wait_ms(40); // wait 40ms |
Bas | 0:1a97e0243dbc | 71 | E.write(0); |
Bas | 0:1a97e0243dbc | 72 | WriteInstruction(FUNCTION_SET | DATA_LENGTH_DL); // 8 bits interface, RE=0 |
Bas | 0:1a97e0243dbc | 73 | wait_us(100); // wait 100us |
Bas | 0:1a97e0243dbc | 74 | WriteInstruction(FUNCTION_SET | DATA_LENGTH_DL); // again |
Bas | 0:1a97e0243dbc | 75 | wait_us(37); // wait 37us |
Bas | 0:1a97e0243dbc | 76 | WriteInstruction(DISPLAY_CONTROL | DISPLAY_ON_D ); // display on |
Bas | 0:1a97e0243dbc | 77 | wait_us(100); // wait 100us |
Bas | 0:1a97e0243dbc | 78 | WriteInstruction(DISPLAY_CLEAR); // clear display |
Bas | 0:1a97e0243dbc | 79 | wait_ms(10); // wait 10ms |
Bas | 0:1a97e0243dbc | 80 | WriteInstruction(ENTRY_MODE_SET | INCREASE_DECREASE_ID); // move cursor right |
Bas | 0:1a97e0243dbc | 81 | wait_ms(10); // wait 10ms |
Bas | 0:1a97e0243dbc | 82 | WriteInstruction(RETURN_HOME); |
Bas | 0:1a97e0243dbc | 83 | SetGraphicsMode(); |
Bas | 0:1a97e0243dbc | 84 | } |
Bas | 0:1a97e0243dbc | 85 | |
Bas | 0:1a97e0243dbc | 86 | //************************************************************************************************ |
Bas | 0:1a97e0243dbc | 87 | //public methodes |
Bas | 0:1a97e0243dbc | 88 | void ST7920::SetGraphicsMode() { |
Bas | 0:1a97e0243dbc | 89 | WriteInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL); |
Bas | 0:1a97e0243dbc | 90 | WriteInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE); //RE=1 (Extended funtion set) |
Bas | 0:1a97e0243dbc | 91 | WriteInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE | GRAPHIC_ON_G); |
Bas | 0:1a97e0243dbc | 92 | } |
Bas | 0:1a97e0243dbc | 93 | |
Bas | 0:1a97e0243dbc | 94 | void ST7920::SetTextMode() { |
Bas | 0:1a97e0243dbc | 95 | WriteInstruction(FUNCTION_SET | DATA_LENGTH_DL); // RE=0 (Basic funtion set) |
Bas | 0:1a97e0243dbc | 96 | } |
Bas | 0:1a97e0243dbc | 97 | |
Bas | 0:1a97e0243dbc | 98 | void ST7920::ClearScreen() { |
Bas | 0:1a97e0243dbc | 99 | WriteInstruction(FUNCTION_SET | DATA_LENGTH_DL); // RE=0 (Basic funtion set) |
Bas | 0:1a97e0243dbc | 100 | WriteInstruction(DISPLAY_CLEAR); |
Bas | 0:1a97e0243dbc | 101 | } |
Bas | 0:1a97e0243dbc | 102 | |
Bas | 0:1a97e0243dbc | 103 | void ST7920::ReturnHome() { |
Bas | 0:1a97e0243dbc | 104 | WriteInstruction(FUNCTION_SET | DATA_LENGTH_DL); //RE=0 (Basic funtion set) |
Bas | 0:1a97e0243dbc | 105 | WriteInstruction(RETURN_HOME); |
Bas | 0:1a97e0243dbc | 106 | } |
Bas | 0:1a97e0243dbc | 107 | |
Bas | 0:1a97e0243dbc | 108 | void ST7920::Standby() { |
Bas | 0:1a97e0243dbc | 109 | WriteInstruction(EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE); //RE=1 (Extended funtion set) |
Bas | 0:1a97e0243dbc | 110 | WriteInstruction(STANDBY); |
Bas | 0:1a97e0243dbc | 111 | } |
Bas | 0:1a97e0243dbc | 112 | |
Bas | 0:1a97e0243dbc | 113 | //Basic text functions |
Bas | 0:1a97e0243dbc | 114 | void ST7920::DisplayString(int Row,int Column,unsigned char *ptr,int length) { |
Bas | 0:1a97e0243dbc | 115 | int i=0; |
Bas | 0:1a97e0243dbc | 116 | |
Bas | 0:1a97e0243dbc | 117 | switch (Row) { |
Bas | 0:1a97e0243dbc | 118 | case 0: |
Bas | 0:1a97e0243dbc | 119 | Column|=0x80; |
Bas | 0:1a97e0243dbc | 120 | break; |
Bas | 0:1a97e0243dbc | 121 | case 1: |
Bas | 0:1a97e0243dbc | 122 | Column|=0x90; |
Bas | 0:1a97e0243dbc | 123 | break; |
Bas | 0:1a97e0243dbc | 124 | case 2: |
Bas | 0:1a97e0243dbc | 125 | Column|=0x88; |
Bas | 0:1a97e0243dbc | 126 | break; |
Bas | 0:1a97e0243dbc | 127 | case 3: |
Bas | 0:1a97e0243dbc | 128 | Column|=0x98; |
Bas | 0:1a97e0243dbc | 129 | break; |
Bas | 0:1a97e0243dbc | 130 | default: |
Bas | 0:1a97e0243dbc | 131 | Column=0x80; |
Bas | 0:1a97e0243dbc | 132 | break; |
Bas | 0:1a97e0243dbc | 133 | } |
Bas | 0:1a97e0243dbc | 134 | |
Bas | 0:1a97e0243dbc | 135 | if (Column%2!=0) { |
Bas | 0:1a97e0243dbc | 136 | Column-=1; |
Bas | 0:1a97e0243dbc | 137 | i=1; |
Bas | 0:1a97e0243dbc | 138 | } |
Bas | 0:1a97e0243dbc | 139 | WriteInstruction((unsigned int)Column); |
Bas | 0:1a97e0243dbc | 140 | |
Bas | 0:1a97e0243dbc | 141 | if (i==1) { |
Bas | 0:1a97e0243dbc | 142 | WriteRAM(' '); |
Bas | 0:1a97e0243dbc | 143 | } |
Bas | 0:1a97e0243dbc | 144 | for (i=0; i<length; i++) { |
Bas | 0:1a97e0243dbc | 145 | WriteRAM((unsigned int)ptr[i]); |
Bas | 0:1a97e0243dbc | 146 | } |
Bas | 0:1a97e0243dbc | 147 | } |
Bas | 0:1a97e0243dbc | 148 | |
Bas | 0:1a97e0243dbc | 149 | void ST7920::DisplayChar(int Row,int Column,int inpChr) { |
Bas | 0:1a97e0243dbc | 150 | int i=0; |
Bas | 0:1a97e0243dbc | 151 | |
Bas | 0:1a97e0243dbc | 152 | switch (Row) { |
Bas | 0:1a97e0243dbc | 153 | case 0: |
Bas | 0:1a97e0243dbc | 154 | Column|=0x80; // SET_DDRAM_ADDRESS |
Bas | 0:1a97e0243dbc | 155 | break; |
Bas | 0:1a97e0243dbc | 156 | case 1: |
Bas | 0:1a97e0243dbc | 157 | Column|=0x90; |
Bas | 0:1a97e0243dbc | 158 | break; |
Bas | 0:1a97e0243dbc | 159 | case 2: |
Bas | 0:1a97e0243dbc | 160 | Column|=0x88; |
Bas | 0:1a97e0243dbc | 161 | break; |
Bas | 0:1a97e0243dbc | 162 | case 3: |
Bas | 0:1a97e0243dbc | 163 | Column|=0x98; |
Bas | 0:1a97e0243dbc | 164 | break; |
Bas | 0:1a97e0243dbc | 165 | default: |
Bas | 0:1a97e0243dbc | 166 | Column=0x80; |
Bas | 0:1a97e0243dbc | 167 | break; |
Bas | 0:1a97e0243dbc | 168 | } |
Bas | 0:1a97e0243dbc | 169 | |
Bas | 0:1a97e0243dbc | 170 | if (Column%2!=0) { |
Bas | 0:1a97e0243dbc | 171 | Column-=1; |
Bas | 0:1a97e0243dbc | 172 | i=1; |
Bas | 0:1a97e0243dbc | 173 | } |
Bas | 0:1a97e0243dbc | 174 | WriteInstruction((unsigned int)Column); |
Bas | 0:1a97e0243dbc | 175 | |
Bas | 0:1a97e0243dbc | 176 | if (i==1) { |
Bas | 0:1a97e0243dbc | 177 | WriteRAM(' '); |
Bas | 0:1a97e0243dbc | 178 | } |
Bas | 0:1a97e0243dbc | 179 | WriteRAM((unsigned int)inpChr); |
Bas | 0:1a97e0243dbc | 180 | } |
Bas | 0:1a97e0243dbc | 181 | |
Bas | 0:1a97e0243dbc | 182 | // Graphic functions |
Bas | 0:1a97e0243dbc | 183 | void ST7920::FillGDRAM(unsigned char *bitmap) { |
Bas | 0:1a97e0243dbc | 184 | unsigned char i, j, k ; |
Bas | 0:1a97e0243dbc | 185 | |
Bas | 0:1a97e0243dbc | 186 | for ( i = 0 ; i < 2 ; i++ ) { |
Bas | 0:1a97e0243dbc | 187 | for ( j = 0 ; j < 32 ; j++ ) { |
Bas | 0:1a97e0243dbc | 188 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ; |
Bas | 0:1a97e0243dbc | 189 | if ( i == 0 ) { |
Bas | 0:1a97e0243dbc | 190 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS) ; |
Bas | 0:1a97e0243dbc | 191 | } else { |
Bas | 0:1a97e0243dbc | 192 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ; |
Bas | 0:1a97e0243dbc | 193 | } |
Bas | 0:1a97e0243dbc | 194 | for ( k = 0 ; k < 16 ; k++ ) { |
Bas | 0:1a97e0243dbc | 195 | WriteRAM( *bitmap++ ) ; |
Bas | 0:1a97e0243dbc | 196 | } |
Bas | 0:1a97e0243dbc | 197 | } |
Bas | 0:1a97e0243dbc | 198 | } |
Bas | 0:1a97e0243dbc | 199 | } |
Bas | 0:1a97e0243dbc | 200 | |
Bas | 0:1a97e0243dbc | 201 | void ST7920::FillGDRAM_Turned(unsigned char *bitmap) { |
Bas | 0:1a97e0243dbc | 202 | int i, j, k, m, offset_row, mask ; |
Bas | 0:1a97e0243dbc | 203 | unsigned char data; |
Bas | 0:1a97e0243dbc | 204 | |
Bas | 0:1a97e0243dbc | 205 | for ( i = 0 ; i < 2 ; i++ ) { //upper and lower page |
Bas | 0:1a97e0243dbc | 206 | for ( j = 0 ; j < 32 ; j++ ) { //32 lines per page |
Bas | 0:1a97e0243dbc | 207 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ; |
Bas | 0:1a97e0243dbc | 208 | if ( i == 0 ) { |
Bas | 0:1a97e0243dbc | 209 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS) ; |
Bas | 0:1a97e0243dbc | 210 | } else { |
Bas | 0:1a97e0243dbc | 211 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ; |
Bas | 0:1a97e0243dbc | 212 | } |
Bas | 0:1a97e0243dbc | 213 | mask=1<<(j%8); // extract bitnumber |
Bas | 0:1a97e0243dbc | 214 | //printf("mask: %d\r\n",mask); |
Bas | 0:1a97e0243dbc | 215 | for ( k = 0 ; k < 16 ; k++ ) { //16 bytes per line |
Bas | 0:1a97e0243dbc | 216 | offset_row=((i*32+j)/8)*128 + k*8; //y coordinate/8 = row 0-7 * 128 = byte offset, read 8 bytes |
Bas | 0:1a97e0243dbc | 217 | data=0; |
Bas | 0:1a97e0243dbc | 218 | for (m = 0 ; m < 8 ; m++) { // read 8 bytes from source |
Bas | 0:1a97e0243dbc | 219 | |
Bas | 0:1a97e0243dbc | 220 | if ((bitmap[offset_row+m] & mask)) { //pixel = 1 |
Bas | 0:1a97e0243dbc | 221 | data|=(128>>m); |
Bas | 0:1a97e0243dbc | 222 | } |
Bas | 0:1a97e0243dbc | 223 | } |
Bas | 0:1a97e0243dbc | 224 | WriteRAM(data) ; |
Bas | 0:1a97e0243dbc | 225 | } |
Bas | 0:1a97e0243dbc | 226 | } |
Bas | 0:1a97e0243dbc | 227 | } |
Bas | 0:1a97e0243dbc | 228 | } |
Bas | 0:1a97e0243dbc | 229 | |
Bas | 0:1a97e0243dbc | 230 | void ST7920::ClearGDRAM() { |
Bas | 0:1a97e0243dbc | 231 | unsigned char i, j, k ; |
Bas | 0:1a97e0243dbc | 232 | |
Bas | 0:1a97e0243dbc | 233 | for ( i = 0 ; i < 2 ; i++ ) { |
Bas | 0:1a97e0243dbc | 234 | for ( j = 0 ; j < 32 ; j++ ) { |
Bas | 0:1a97e0243dbc | 235 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | j) ; |
Bas | 0:1a97e0243dbc | 236 | if ( i == 0 ) { |
Bas | 0:1a97e0243dbc | 237 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS) ; |
Bas | 0:1a97e0243dbc | 238 | } else { |
Bas | 0:1a97e0243dbc | 239 | WriteInstruction(SET_GRAPHIC_RAM_ADDRESS | 0x08) ; |
Bas | 0:1a97e0243dbc | 240 | } |
Bas | 0:1a97e0243dbc | 241 | for ( k = 0 ; k < 16 ; k++ ) { |
Bas | 0:1a97e0243dbc | 242 | WriteRAM(0); |
Bas | 0:1a97e0243dbc | 243 | } |
Bas | 0:1a97e0243dbc | 244 | } |
Bas | 0:1a97e0243dbc | 245 | } |
Bas | 0:1a97e0243dbc | 246 | } |