Baseline for testing

Committer:
foxbrianr
Date:
Thu Jul 25 00:43:40 2019 +0000
Revision:
0:c428f1d60316
Child:
1:0f1dffc6103a
Baseline for testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foxbrianr 0:c428f1d60316 1 #include "mbed.h"
foxbrianr 0:c428f1d60316 2 #include "LCD.h"
foxbrianr 0:c428f1d60316 3
foxbrianr 0:c428f1d60316 4 LCD::LCD():
foxbrianr 0:c428f1d60316 5 LCD_E1(p17),
foxbrianr 0:c428f1d60316 6 LCD_E2(p18),
foxbrianr 0:c428f1d60316 7 LCD_RS(p19),
foxbrianr 0:c428f1d60316 8 LCD_RW(p20),
foxbrianr 0:c428f1d60316 9 //LCD_D4(p24),
foxbrianr 0:c428f1d60316 10 //LCD_D5(p23),
foxbrianr 0:c428f1d60316 11 //LCD_D6(p22),
foxbrianr 0:c428f1d60316 12 //LCD_D7(p21),
foxbrianr 0:c428f1d60316 13 //bus constructor is the reverse order of the pins in the byte order.
foxbrianr 0:c428f1d60316 14 // d0 - d7
foxbrianr 0:c428f1d60316 15 LCD_DATA( p24, p23, p22, p21)
foxbrianr 0:c428f1d60316 16 {
foxbrianr 0:c428f1d60316 17
foxbrianr 0:c428f1d60316 18
foxbrianr 0:c428f1d60316 19 LCD_DATA.output();
foxbrianr 0:c428f1d60316 20 LCD_DATA.mode(OpenDrain);
foxbrianr 0:c428f1d60316 21 LCD_E1.mode(OpenDrain);
foxbrianr 0:c428f1d60316 22 LCD_E2.mode(OpenDrain);
foxbrianr 0:c428f1d60316 23 LCD_RS.mode(OpenDrain);
foxbrianr 0:c428f1d60316 24 LCD_RW.mode(OpenDrain);
foxbrianr 0:c428f1d60316 25 isOutput=1;
foxbrianr 0:c428f1d60316 26
foxbrianr 0:c428f1d60316 27 }
foxbrianr 0:c428f1d60316 28 /*
foxbrianr 0:c428f1d60316 29 * Destructor the LCD.
foxbrianr 0:c428f1d60316 30 */
foxbrianr 0:c428f1d60316 31 LCD::~LCD()
foxbrianr 0:c428f1d60316 32 {
foxbrianr 0:c428f1d60316 33 }
foxbrianr 0:c428f1d60316 34
foxbrianr 0:c428f1d60316 35
foxbrianr 0:c428f1d60316 36
foxbrianr 0:c428f1d60316 37 void LCD::command1(char i) //Top half of the display
foxbrianr 0:c428f1d60316 38 {
foxbrianr 0:c428f1d60316 39 LCD_DATA.output();
foxbrianr 0:c428f1d60316 40 LCD_DATA = i;
foxbrianr 0:c428f1d60316 41 LCD_RW = 0;
foxbrianr 0:c428f1d60316 42 LCD_RS = 0;
foxbrianr 0:c428f1d60316 43 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 44 wait_us(40); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 45 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 46
foxbrianr 0:c428f1d60316 47 //printf("C1:%x\n\r", i);
foxbrianr 0:c428f1d60316 48 }
foxbrianr 0:c428f1d60316 49
foxbrianr 0:c428f1d60316 50 void LCD::command2(char i) //Bottom half of the display
foxbrianr 0:c428f1d60316 51 {
foxbrianr 0:c428f1d60316 52 LCD_DATA.output();
foxbrianr 0:c428f1d60316 53 LCD_DATA = i;
foxbrianr 0:c428f1d60316 54 LCD_RW = 0;
foxbrianr 0:c428f1d60316 55 LCD_RS = 0;
foxbrianr 0:c428f1d60316 56 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 57 wait_us(40); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 58 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 59
foxbrianr 0:c428f1d60316 60 //printf("C2:%x\n\r", i);
foxbrianr 0:c428f1d60316 61 }
foxbrianr 0:c428f1d60316 62
foxbrianr 0:c428f1d60316 63 void LCD::writedata1(char i) //Top half of the display
foxbrianr 0:c428f1d60316 64 {;
foxbrianr 0:c428f1d60316 65 LCD_DATA.output();
foxbrianr 0:c428f1d60316 66 LCD_DATA = i;
foxbrianr 0:c428f1d60316 67 LCD_RW = 0;
foxbrianr 0:c428f1d60316 68 LCD_RS = 1;
foxbrianr 0:c428f1d60316 69 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 70 wait_us(40); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 71 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 72 //printf("D1:%x\n\r", i);
foxbrianr 0:c428f1d60316 73 }
foxbrianr 0:c428f1d60316 74
foxbrianr 0:c428f1d60316 75 void LCD::writedata2(char i) //Bottom half of the display
foxbrianr 0:c428f1d60316 76 {
foxbrianr 0:c428f1d60316 77 LCD_DATA.output();
foxbrianr 0:c428f1d60316 78 LCD_DATA = i;
foxbrianr 0:c428f1d60316 79 LCD_RW = 0;
foxbrianr 0:c428f1d60316 80 LCD_RS = 1;
foxbrianr 0:c428f1d60316 81 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 82 wait_us(40); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 83 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 84 //printf("D2:%x\n\r", i);
foxbrianr 0:c428f1d60316 85 }
foxbrianr 0:c428f1d60316 86
foxbrianr 0:c428f1d60316 87 void LCD::nextline1(){
foxbrianr 0:c428f1d60316 88 writeCommand(0xc0,1,0); //set DDRAM address to 40 (line 2)
foxbrianr 0:c428f1d60316 89 }
foxbrianr 0:c428f1d60316 90 void LCD::nextline2(){
foxbrianr 0:c428f1d60316 91 writeCommand(0xc0,0,1); //set DDRAM address to 40 (line 4)
foxbrianr 0:c428f1d60316 92 }
foxbrianr 0:c428f1d60316 93
foxbrianr 0:c428f1d60316 94 void LCD::display(char *show){ //show character data on display
foxbrianr 0:c428f1d60316 95 char datum;
foxbrianr 0:c428f1d60316 96 int i;
foxbrianr 0:c428f1d60316 97 for (i=0;i<40;i++){
foxbrianr 0:c428f1d60316 98 datum=*show; //point to 1st line data
foxbrianr 0:c428f1d60316 99 writeData(datum,1,0); //write to 1st line
foxbrianr 0:c428f1d60316 100 ++show; //next letter
foxbrianr 0:c428f1d60316 101 }
foxbrianr 0:c428f1d60316 102 nextline1(); //move address to line 2
foxbrianr 0:c428f1d60316 103 for (i=0;i<40;i++){
foxbrianr 0:c428f1d60316 104 datum=*show;
foxbrianr 0:c428f1d60316 105 writeData(datum,1,0); //write to 2nd line
foxbrianr 0:c428f1d60316 106 ++show;
foxbrianr 0:c428f1d60316 107 }
foxbrianr 0:c428f1d60316 108 for (i=0;i<40;i++){
foxbrianr 0:c428f1d60316 109 datum=*show;
foxbrianr 0:c428f1d60316 110 writeData(datum,0,1); //write to 3rd line
foxbrianr 0:c428f1d60316 111 ++show;
foxbrianr 0:c428f1d60316 112 }
foxbrianr 0:c428f1d60316 113 nextline2(); //move address to line 4
foxbrianr 0:c428f1d60316 114 for (i=0;i<40;i++){
foxbrianr 0:c428f1d60316 115 datum=*show;
foxbrianr 0:c428f1d60316 116 writeData(datum,0,1); //write to 4th line
foxbrianr 0:c428f1d60316 117 ++show;
foxbrianr 0:c428f1d60316 118 }
foxbrianr 0:c428f1d60316 119 }
foxbrianr 0:c428f1d60316 120
foxbrianr 0:c428f1d60316 121
foxbrianr 0:c428f1d60316 122 void LCD::writeByte1(int value) {
foxbrianr 0:c428f1d60316 123
foxbrianr 0:c428f1d60316 124 // -------------------------------------------------
foxbrianr 0:c428f1d60316 125 LCD_DATA.output();
foxbrianr 0:c428f1d60316 126 LCD_DATA = value >> 4;
foxbrianr 0:c428f1d60316 127
foxbrianr 0:c428f1d60316 128 LCD_RW = 0;
foxbrianr 0:c428f1d60316 129 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 130 wait_ms(5); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 131 __nop();
foxbrianr 0:c428f1d60316 132 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 133
foxbrianr 0:c428f1d60316 134 // -------------------------------------------------
foxbrianr 0:c428f1d60316 135 wait_ms(1); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 136
foxbrianr 0:c428f1d60316 137 // -------------------------------------------------
foxbrianr 0:c428f1d60316 138 LCD_DATA = value >> 0;
foxbrianr 0:c428f1d60316 139 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 140 wait_ms(5); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 141 __nop();
foxbrianr 0:c428f1d60316 142 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 143
foxbrianr 0:c428f1d60316 144 //printf("x1:%x\n\r", value);
foxbrianr 0:c428f1d60316 145 }
foxbrianr 0:c428f1d60316 146
foxbrianr 0:c428f1d60316 147
foxbrianr 0:c428f1d60316 148 void LCD::writeByte2(int value) {
foxbrianr 0:c428f1d60316 149
foxbrianr 0:c428f1d60316 150 // -------------------------------------------------
foxbrianr 0:c428f1d60316 151 LCD_DATA.output();
foxbrianr 0:c428f1d60316 152 LCD_DATA = value >> 4;
foxbrianr 0:c428f1d60316 153 LCD_RW = 0;
foxbrianr 0:c428f1d60316 154 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 155 wait_ms(5); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 156 __nop();
foxbrianr 0:c428f1d60316 157 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 158
foxbrianr 0:c428f1d60316 159 // -------------------------------------------------
foxbrianr 0:c428f1d60316 160 wait_ms(1); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 161
foxbrianr 0:c428f1d60316 162 // -------------------------------------------------
foxbrianr 0:c428f1d60316 163 LCD_DATA = value >> 0;
foxbrianr 0:c428f1d60316 164 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 165 wait_ms(5); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 166 __nop();
foxbrianr 0:c428f1d60316 167 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 168
foxbrianr 0:c428f1d60316 169 //printf("x2:%x\n\r", value);
foxbrianr 0:c428f1d60316 170 }
foxbrianr 0:c428f1d60316 171
foxbrianr 0:c428f1d60316 172
foxbrianr 0:c428f1d60316 173
foxbrianr 0:c428f1d60316 174 /************************************************************************/
foxbrianr 0:c428f1d60316 175 void LCD::init(void)
foxbrianr 0:c428f1d60316 176 {
foxbrianr 0:c428f1d60316 177
foxbrianr 0:c428f1d60316 178 wait_ms(100);
foxbrianr 0:c428f1d60316 179
foxbrianr 0:c428f1d60316 180 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 181 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 182
foxbrianr 0:c428f1d60316 183 for (int i=0;i<3;i++) {
foxbrianr 0:c428f1d60316 184 command1(0x3);//Wake up
foxbrianr 0:c428f1d60316 185 command2(0x3);
foxbrianr 0:c428f1d60316 186 wait_ms(1); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 187 }
foxbrianr 0:c428f1d60316 188 command1(0x2);// Set 4 bit mode
foxbrianr 0:c428f1d60316 189 command2(0x2);
foxbrianr 0:c428f1d60316 190 wait_ms(1); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 191
foxbrianr 0:c428f1d60316 192
foxbrianr 0:c428f1d60316 193 LCD_RW = 0;
foxbrianr 0:c428f1d60316 194 LCD_RS = 0;
foxbrianr 0:c428f1d60316 195
foxbrianr 0:c428f1d60316 196 writeCommand(0x28,1,1); //Turn on display
foxbrianr 0:c428f1d60316 197
foxbrianr 0:c428f1d60316 198 writeCommand(LCD_setDisplayOff,1,1); //Turn on display
foxbrianr 0:c428f1d60316 199 //writeCommand2(LCD_setDisplayOff);
foxbrianr 0:c428f1d60316 200
foxbrianr 0:c428f1d60316 201 writeCommand(LCD_clearDisplay,1,1); //Clear display
foxbrianr 0:c428f1d60316 202 //writeCommand2(LCD_clearDisplay);
foxbrianr 0:c428f1d60316 203
foxbrianr 0:c428f1d60316 204 writeCommand(LCD_entryModeSet,1,1); //Entry mode cursor increment
foxbrianr 0:c428f1d60316 205 //writeCommand2(LCD_entryModeSet);
foxbrianr 0:c428f1d60316 206
foxbrianr 0:c428f1d60316 207 writeCommand(LCD_setDisplayOn,1,1); //Turn on display; no cursor
foxbrianr 0:c428f1d60316 208 //writeCommand2(LCD_setDisplayOn);
foxbrianr 0:c428f1d60316 209
foxbrianr 0:c428f1d60316 210
foxbrianr 0:c428f1d60316 211 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 212 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 213
foxbrianr 0:c428f1d60316 214 }
foxbrianr 0:c428f1d60316 215
foxbrianr 0:c428f1d60316 216
foxbrianr 0:c428f1d60316 217 void LCD::locate(int row, int column) {
foxbrianr 0:c428f1d60316 218 _row = row;
foxbrianr 0:c428f1d60316 219 _column = column;
foxbrianr 0:c428f1d60316 220
foxbrianr 0:c428f1d60316 221 }
foxbrianr 0:c428f1d60316 222
foxbrianr 0:c428f1d60316 223 void LCD::clear(int e1, int e2 ){
foxbrianr 0:c428f1d60316 224
foxbrianr 0:c428f1d60316 225 writeCommand(LCD_clearDisplay,e1,e2);
foxbrianr 0:c428f1d60316 226 _row = 0;
foxbrianr 0:c428f1d60316 227 _column = 0;
foxbrianr 0:c428f1d60316 228
foxbrianr 0:c428f1d60316 229 // for debugging, write spaces to each cell
foxbrianr 0:c428f1d60316 230 memset( displayBuffer, 0x85, sizeof(displayBuffer));
foxbrianr 0:c428f1d60316 231 }
foxbrianr 0:c428f1d60316 232
foxbrianr 0:c428f1d60316 233
foxbrianr 0:c428f1d60316 234
foxbrianr 0:c428f1d60316 235 int LCD::isBusy1(void)
foxbrianr 0:c428f1d60316 236 {
foxbrianr 0:c428f1d60316 237 int input = 0;
foxbrianr 0:c428f1d60316 238 int hi = 0;
foxbrianr 0:c428f1d60316 239 int lo = 0;
foxbrianr 0:c428f1d60316 240 int result = 0;
foxbrianr 0:c428f1d60316 241
foxbrianr 0:c428f1d60316 242 LCD_RW = 1;
foxbrianr 0:c428f1d60316 243 LCD_RS = 0;
foxbrianr 0:c428f1d60316 244 LCD_DATA.input(); // switch port back to output
foxbrianr 0:c428f1d60316 245
foxbrianr 0:c428f1d60316 246 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 247 __nop();
foxbrianr 0:c428f1d60316 248 lo = LCD_DATA.read() ; // read low bit
foxbrianr 0:c428f1d60316 249 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 250 wait_ms(1);
foxbrianr 0:c428f1d60316 251 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 252 hi = LCD_DATA.read() ; // read high bit
foxbrianr 0:c428f1d60316 253 __nop();
foxbrianr 0:c428f1d60316 254 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 255
foxbrianr 0:c428f1d60316 256 //printf("isBusy1? = 0x%02x 0x%02x \n\r", lo,hi );
foxbrianr 0:c428f1d60316 257
foxbrianr 0:c428f1d60316 258 if ((0x8 & input)); // wait until display is ready
foxbrianr 0:c428f1d60316 259 {
foxbrianr 0:c428f1d60316 260 result = 1;
foxbrianr 0:c428f1d60316 261 }
foxbrianr 0:c428f1d60316 262 result =0;
foxbrianr 0:c428f1d60316 263 LCD_RW = 0;
foxbrianr 0:c428f1d60316 264 LCD_DATA.output(); // switch port back to output
foxbrianr 0:c428f1d60316 265 return result;
foxbrianr 0:c428f1d60316 266 }
foxbrianr 0:c428f1d60316 267
foxbrianr 0:c428f1d60316 268 int LCD::isBusy2(void)
foxbrianr 0:c428f1d60316 269 {
foxbrianr 0:c428f1d60316 270
foxbrianr 0:c428f1d60316 271 int input = 0;
foxbrianr 0:c428f1d60316 272 int result = 0;
foxbrianr 0:c428f1d60316 273
foxbrianr 0:c428f1d60316 274 LCD_RW = 1;
foxbrianr 0:c428f1d60316 275 LCD_RS = 0;
foxbrianr 0:c428f1d60316 276
foxbrianr 0:c428f1d60316 277 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 278 __nop();
foxbrianr 0:c428f1d60316 279 input = LCD_DATA.read() >> 0; // read low bit
foxbrianr 0:c428f1d60316 280 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 281 __nop();
foxbrianr 0:c428f1d60316 282 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 283 input = LCD_DATA.read() >> 4; // read high bit
foxbrianr 0:c428f1d60316 284 __nop();
foxbrianr 0:c428f1d60316 285 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 286
foxbrianr 0:c428f1d60316 287 //printf("isBusy1? = 0x%02x \n\r", input );
foxbrianr 0:c428f1d60316 288
foxbrianr 0:c428f1d60316 289 if ((0x8 & input)); // wait until display is ready
foxbrianr 0:c428f1d60316 290 {
foxbrianr 0:c428f1d60316 291 result = 1;
foxbrianr 0:c428f1d60316 292 }
foxbrianr 0:c428f1d60316 293
foxbrianr 0:c428f1d60316 294 LCD_RW = 0;
foxbrianr 0:c428f1d60316 295 LCD_DATA.output(); // switch port back to output
foxbrianr 0:c428f1d60316 296
foxbrianr 0:c428f1d60316 297 return result;
foxbrianr 0:c428f1d60316 298 }
foxbrianr 0:c428f1d60316 299
foxbrianr 0:c428f1d60316 300
foxbrianr 0:c428f1d60316 301
foxbrianr 0:c428f1d60316 302 int LCD::isBusy(int e1, int e2)
foxbrianr 0:c428f1d60316 303 {
foxbrianr 0:c428f1d60316 304 int input = 0;
foxbrianr 0:c428f1d60316 305 int result = 0;
foxbrianr 0:c428f1d60316 306
foxbrianr 0:c428f1d60316 307 LCD_RW = 1;
foxbrianr 0:c428f1d60316 308 LCD_RS = 0;
foxbrianr 0:c428f1d60316 309 LCD_DATA.input(); // switch port back to output
foxbrianr 0:c428f1d60316 310
foxbrianr 0:c428f1d60316 311 if (e1) {
foxbrianr 0:c428f1d60316 312 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 313 __nop();
foxbrianr 0:c428f1d60316 314 input = LCD_DATA.read() >> 0; // read low bit
foxbrianr 0:c428f1d60316 315 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 316 __nop();
foxbrianr 0:c428f1d60316 317 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 318 input = LCD_DATA.read() >> 4; // read high bit
foxbrianr 0:c428f1d60316 319 __nop();
foxbrianr 0:c428f1d60316 320 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 321
foxbrianr 0:c428f1d60316 322 //printf("0x%02x \n\r", input );
foxbrianr 0:c428f1d60316 323
foxbrianr 0:c428f1d60316 324 if ((0x8 & input)); // wait until display is ready
foxbrianr 0:c428f1d60316 325 {
foxbrianr 0:c428f1d60316 326 result = 1;
foxbrianr 0:c428f1d60316 327 }
foxbrianr 0:c428f1d60316 328 }
foxbrianr 0:c428f1d60316 329
foxbrianr 0:c428f1d60316 330
foxbrianr 0:c428f1d60316 331 if (e2 && result==0) {
foxbrianr 0:c428f1d60316 332
foxbrianr 0:c428f1d60316 333 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 334 __nop();
foxbrianr 0:c428f1d60316 335 input = LCD_DATA.read() >> 0; // read low bit
foxbrianr 0:c428f1d60316 336 //printf("0x%02dFF \n\r", input );
foxbrianr 0:c428f1d60316 337 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 338 __nop();
foxbrianr 0:c428f1d60316 339 LCD_E2 = 1;
foxbrianr 0:c428f1d60316 340 input = LCD_DATA.read() >> 4; // read high bit
foxbrianr 0:c428f1d60316 341 __nop();
foxbrianr 0:c428f1d60316 342 LCD_E2 = 0;
foxbrianr 0:c428f1d60316 343
foxbrianr 0:c428f1d60316 344 if ((0x8 & input)); // wait until display is ready
foxbrianr 0:c428f1d60316 345 {
foxbrianr 0:c428f1d60316 346 result = 1;
foxbrianr 0:c428f1d60316 347 }
foxbrianr 0:c428f1d60316 348 }
foxbrianr 0:c428f1d60316 349 LCD_RW = 0;
foxbrianr 0:c428f1d60316 350 LCD_DATA.output(); // switch port back to output
foxbrianr 0:c428f1d60316 351
foxbrianr 0:c428f1d60316 352 return result;
foxbrianr 0:c428f1d60316 353 }
foxbrianr 0:c428f1d60316 354
foxbrianr 0:c428f1d60316 355 void LCD::waitBusy()
foxbrianr 0:c428f1d60316 356 {
foxbrianr 0:c428f1d60316 357 unsigned char statusBit;
foxbrianr 0:c428f1d60316 358 statusBit = isBusy(1,1);
foxbrianr 0:c428f1d60316 359 //int i=0;
foxbrianr 0:c428f1d60316 360 while (statusBit)
foxbrianr 0:c428f1d60316 361 {
foxbrianr 0:c428f1d60316 362 statusBit = isBusy(1,1);
foxbrianr 0:c428f1d60316 363 wait_us(5);
foxbrianr 0:c428f1d60316 364 }
foxbrianr 0:c428f1d60316 365 }
foxbrianr 0:c428f1d60316 366
foxbrianr 0:c428f1d60316 367 void LCD::writeData(char value, int e1, int e2 ){
foxbrianr 0:c428f1d60316 368
foxbrianr 0:c428f1d60316 369
foxbrianr 0:c428f1d60316 370 int hi=( value & 0xF0 ) >> 4;
foxbrianr 0:c428f1d60316 371 int lo=( value & 0x0F ) >> 0;
foxbrianr 0:c428f1d60316 372
foxbrianr 0:c428f1d60316 373 LCD_DATA.output();
foxbrianr 0:c428f1d60316 374
foxbrianr 0:c428f1d60316 375 if(e1) {
foxbrianr 0:c428f1d60316 376
foxbrianr 0:c428f1d60316 377 LCD_DATA = hi;
foxbrianr 0:c428f1d60316 378 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 379 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 380 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 381
foxbrianr 0:c428f1d60316 382 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 383
foxbrianr 0:c428f1d60316 384 LCD_DATA = lo;
foxbrianr 0:c428f1d60316 385 LCD_E1 = 1;
foxbrianr 0:c428f1d60316 386 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 387 LCD_E1 = 0;
foxbrianr 0:c428f1d60316 388 }
foxbrianr 0:c428f1d60316 389
foxbrianr 0:c428f1d60316 390 if(e2)
foxbrianr 0:c428f1d60316 391 {
foxbrianr 0:c428f1d60316 392 LCD_DATA = hi;
foxbrianr 0:c428f1d60316 393 LCD_E2= 1;
foxbrianr 0:c428f1d60316 394 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 395 LCD_E2= 0;
foxbrianr 0:c428f1d60316 396
foxbrianr 0:c428f1d60316 397 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 398
foxbrianr 0:c428f1d60316 399 LCD_DATA = lo;
foxbrianr 0:c428f1d60316 400 LCD_E2= 1;
foxbrianr 0:c428f1d60316 401 wait_us(1000); // wait 40 us for most commands
foxbrianr 0:c428f1d60316 402 LCD_E2= 0;
foxbrianr 0:c428f1d60316 403 }
foxbrianr 0:c428f1d60316 404 }
foxbrianr 0:c428f1d60316 405
foxbrianr 0:c428f1d60316 406 void LCD::writeCommand(char value, int e1, int e2){
foxbrianr 0:c428f1d60316 407
foxbrianr 0:c428f1d60316 408 LCD_RS = 0;
foxbrianr 0:c428f1d60316 409 writeData(value,e1,e2);
foxbrianr 0:c428f1d60316 410 }
foxbrianr 0:c428f1d60316 411
foxbrianr 0:c428f1d60316 412 void LCD::writeChar(char value, int e1, int e2){
foxbrianr 0:c428f1d60316 413 LCD_RS = 1;
foxbrianr 0:c428f1d60316 414 writeData(value,e1,e2);
foxbrianr 0:c428f1d60316 415 }
foxbrianr 0:c428f1d60316 416
foxbrianr 0:c428f1d60316 417
foxbrianr 0:c428f1d60316 418 void LCD::putc(const char c)
foxbrianr 0:c428f1d60316 419 {
foxbrianr 0:c428f1d60316 420 character(_row, _column, c);
foxbrianr 0:c428f1d60316 421 }
foxbrianr 0:c428f1d60316 422
foxbrianr 0:c428f1d60316 423 void LCD::printf(const char *message, ...)
foxbrianr 0:c428f1d60316 424 {
foxbrianr 0:c428f1d60316 425 char buffer[128];
foxbrianr 0:c428f1d60316 426 va_list argptr;
foxbrianr 0:c428f1d60316 427 va_start(argptr, message);
foxbrianr 0:c428f1d60316 428
foxbrianr 0:c428f1d60316 429 vsprintf( buffer, message, argptr);
foxbrianr 0:c428f1d60316 430 write (buffer);
foxbrianr 0:c428f1d60316 431
foxbrianr 0:c428f1d60316 432 va_end(argptr);
foxbrianr 0:c428f1d60316 433 }
foxbrianr 0:c428f1d60316 434
foxbrianr 0:c428f1d60316 435 void LCD::write(const char* text) {
foxbrianr 0:c428f1d60316 436 //_row = 0;
foxbrianr 0:c428f1d60316 437 //_column = 0;
foxbrianr 0:c428f1d60316 438 int characterAmount =0;
foxbrianr 0:c428f1d60316 439
foxbrianr 0:c428f1d60316 440 for(int i = 0; text[i] != '\0'; i++)
foxbrianr 0:c428f1d60316 441 characterAmount++;
foxbrianr 0:c428f1d60316 442
foxbrianr 0:c428f1d60316 443 for (int i = 0; i < characterAmount && i < rows() * columns(); i++){
foxbrianr 0:c428f1d60316 444 character(_row, _column, text[i]);
foxbrianr 0:c428f1d60316 445 }
foxbrianr 0:c428f1d60316 446 }
foxbrianr 0:c428f1d60316 447
foxbrianr 0:c428f1d60316 448 void LCD::writeLine(int line, const char* text ) {
foxbrianr 0:c428f1d60316 449
foxbrianr 0:c428f1d60316 450 _row = line;
foxbrianr 0:c428f1d60316 451 _column = 1;
foxbrianr 0:c428f1d60316 452
foxbrianr 0:c428f1d60316 453 int characterAmount =strlen(text);
foxbrianr 0:c428f1d60316 454
foxbrianr 0:c428f1d60316 455 int i=0;
foxbrianr 0:c428f1d60316 456 locate(_row,_column);
foxbrianr 0:c428f1d60316 457
foxbrianr 0:c428f1d60316 458 while(i < columns()-1)
foxbrianr 0:c428f1d60316 459 {
foxbrianr 0:c428f1d60316 460 if ( i < characterAmount )
foxbrianr 0:c428f1d60316 461 putc( text[i]);
foxbrianr 0:c428f1d60316 462 else
foxbrianr 0:c428f1d60316 463 putc ( ' ');
foxbrianr 0:c428f1d60316 464
foxbrianr 0:c428f1d60316 465 i++;
foxbrianr 0:c428f1d60316 466 }
foxbrianr 0:c428f1d60316 467 }
foxbrianr 0:c428f1d60316 468
foxbrianr 0:c428f1d60316 469 void LCD::writeCharacter(const char c, int row, int column) {
foxbrianr 0:c428f1d60316 470 locate(row, column);
foxbrianr 0:c428f1d60316 471 character(_row, _column, c);
foxbrianr 0:c428f1d60316 472 }
foxbrianr 0:c428f1d60316 473
foxbrianr 0:c428f1d60316 474 void LCD::writeCharacters(const char* text, int row, int column) {
foxbrianr 0:c428f1d60316 475 locate(row, column);
foxbrianr 0:c428f1d60316 476
foxbrianr 0:c428f1d60316 477 int characterAmount =0;
foxbrianr 0:c428f1d60316 478
foxbrianr 0:c428f1d60316 479 for(int i = 0; text[i] != '\0'; i++)
foxbrianr 0:c428f1d60316 480 characterAmount++;
foxbrianr 0:c428f1d60316 481
foxbrianr 0:c428f1d60316 482 for (int i = 0; i < characterAmount && i < columns() - column; i++){
foxbrianr 0:c428f1d60316 483 character(_row, _column, text[i]);
foxbrianr 0:c428f1d60316 484 }
foxbrianr 0:c428f1d60316 485 }
foxbrianr 0:c428f1d60316 486
foxbrianr 0:c428f1d60316 487 int LCD::address(int row, int column){
foxbrianr 0:c428f1d60316 488
foxbrianr 0:c428f1d60316 489 int a=0x80;
foxbrianr 0:c428f1d60316 490
foxbrianr 0:c428f1d60316 491 if(row < rows() && column < columns())
foxbrianr 0:c428f1d60316 492 {
foxbrianr 0:c428f1d60316 493 switch (row){
foxbrianr 0:c428f1d60316 494 case 0:
foxbrianr 0:c428f1d60316 495 case 1:
foxbrianr 0:c428f1d60316 496 a = 0x80 + (row * 0x40) + column;
foxbrianr 0:c428f1d60316 497 break;
foxbrianr 0:c428f1d60316 498 case 2:
foxbrianr 0:c428f1d60316 499 case 3:
foxbrianr 0:c428f1d60316 500 a = 0x80 + ((row-2) * 0x40) + column;
foxbrianr 0:c428f1d60316 501 break;
foxbrianr 0:c428f1d60316 502 default:
foxbrianr 0:c428f1d60316 503 break;
foxbrianr 0:c428f1d60316 504 }
foxbrianr 0:c428f1d60316 505 }
foxbrianr 0:c428f1d60316 506
foxbrianr 0:c428f1d60316 507 return a;
foxbrianr 0:c428f1d60316 508 }
foxbrianr 0:c428f1d60316 509
foxbrianr 0:c428f1d60316 510 void LCD::character(int row, int column, char c) {
foxbrianr 0:c428f1d60316 511 uint8_t a = address(row, column);
foxbrianr 0:c428f1d60316 512
foxbrianr 0:c428f1d60316 513 // ------------------------------------
foxbrianr 0:c428f1d60316 514 // write to internal buffer
foxbrianr 0:c428f1d60316 515 // ------------------------------------
foxbrianr 0:c428f1d60316 516 displayBuffer [row][column] = c;
foxbrianr 0:c428f1d60316 517
foxbrianr 0:c428f1d60316 518 if(row<2){
foxbrianr 0:c428f1d60316 519 writeCommand(a,1,0);
foxbrianr 0:c428f1d60316 520 writeChar(c,1,0);
foxbrianr 0:c428f1d60316 521 }
foxbrianr 0:c428f1d60316 522 else
foxbrianr 0:c428f1d60316 523 {
foxbrianr 0:c428f1d60316 524 writeCommand(a,0,1);
foxbrianr 0:c428f1d60316 525 writeChar(c,0,1);
foxbrianr 0:c428f1d60316 526 }
foxbrianr 0:c428f1d60316 527
foxbrianr 0:c428f1d60316 528 //Update position
foxbrianr 0:c428f1d60316 529 if(_column < columns())
foxbrianr 0:c428f1d60316 530 _column++;
foxbrianr 0:c428f1d60316 531 if (_column >= columns()){
foxbrianr 0:c428f1d60316 532 #if 0
foxbrianr 0:c428f1d60316 533 if (_row == 0){
foxbrianr 0:c428f1d60316 534 _column = 0;
foxbrianr 0:c428f1d60316 535 _row++;
foxbrianr 0:c428f1d60316 536 }
foxbrianr 0:c428f1d60316 537 else{
foxbrianr 0:c428f1d60316 538 _row = 0;
foxbrianr 0:c428f1d60316 539 _column = 0;
foxbrianr 0:c428f1d60316 540 }
foxbrianr 0:c428f1d60316 541 #endif
foxbrianr 0:c428f1d60316 542 }
foxbrianr 0:c428f1d60316 543 }
foxbrianr 0:c428f1d60316 544
foxbrianr 0:c428f1d60316 545 void LCD::setCursorMode( int mode )
foxbrianr 0:c428f1d60316 546 {
foxbrianr 0:c428f1d60316 547 if (mode)
foxbrianr 0:c428f1d60316 548 {
foxbrianr 0:c428f1d60316 549 if(_row < 2)
foxbrianr 0:c428f1d60316 550 writeCommand(LCD_setDisplayBlink,1,0);
foxbrianr 0:c428f1d60316 551 else
foxbrianr 0:c428f1d60316 552 writeCommand(LCD_setDisplayBlink,0,1);
foxbrianr 0:c428f1d60316 553
foxbrianr 0:c428f1d60316 554 }
foxbrianr 0:c428f1d60316 555 else
foxbrianr 0:c428f1d60316 556 {
foxbrianr 0:c428f1d60316 557 writeCommand(LCD_setDisplayOn,1,1);
foxbrianr 0:c428f1d60316 558 }
foxbrianr 0:c428f1d60316 559 }
foxbrianr 0:c428f1d60316 560
foxbrianr 0:c428f1d60316 561 void LCD::dump(Serial * pc) {
foxbrianr 0:c428f1d60316 562
foxbrianr 0:c428f1d60316 563 fprintf(stdout,"\n\r");
foxbrianr 0:c428f1d60316 564 fprintf(stdout,"----------------------------------------------\n\r");
foxbrianr 0:c428f1d60316 565 for (int i=0;i<rows();i++)
foxbrianr 0:c428f1d60316 566 {
foxbrianr 0:c428f1d60316 567 fprintf(stdout,"[%d]",i);
foxbrianr 0:c428f1d60316 568 for (int j=0;j<columns();j++)
foxbrianr 0:c428f1d60316 569 {
foxbrianr 0:c428f1d60316 570 fprintf(stdout,"%c",displayBuffer[i][j]);
foxbrianr 0:c428f1d60316 571 }
foxbrianr 0:c428f1d60316 572 fprintf(stdout,"\n\r");
foxbrianr 0:c428f1d60316 573 }
foxbrianr 0:c428f1d60316 574 fprintf(stdout,"\n\r");
foxbrianr 0:c428f1d60316 575 }