Baseline for testing

Committer:
foxbrianr
Date:
Thu Sep 12 11:27:29 2019 +0000
Revision:
1:0f1dffc6103a
Parent:
0:c428f1d60316
Child:
2:73721d9e0056
beta1;

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