00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00016
00017 #include <msp430xG461x.h>
00018 #include <stdio.h>
00019 #include <signal.h>
00020 #include <string.h>
00021 #include "lcd.h"
00022 #include "ball.h"
00023 #include "bits.h"
00024 #include "font.h"
00025 #include "time.h"
00026
00027 #define LCD_RESET_LOW() P7OUT &= ~BIT4;
00028 #define LCD_RESET_HIGH() P7OUT |= BIT4;
00029
00030
00031 #define CS_LOW() P4OUT &= ~BIT2;
00032 #define CS_HIGH() P4OUT |= BIT2;
00033
00034 static uint8_t xBase = 0;
00035 static uint8_t x = 0;
00036 static uint8_t y = 0;
00037 static uint16_t fgColor = LCD_DEFAULT_FG_COLOR;
00038 static uint16_t bgColor = LCD_DEFAULT_BG_COLOR;
00039
00040 void LCD_writeSpiCommand (unsigned char data);
00041 void LCD_writeSpiData (unsigned char data);
00042 void LCD_setup (void);
00043
00044 void LCD_init (void)
00045 {
00046
00047
00048 U1CTL |= SWRST;
00049
00050
00051 U1ME |= USPIE1;
00052
00053 U1CTL &= ~SWRST;
00054
00055
00056
00057 U1CTL = CHAR + SYNC + MM;
00058
00059
00060
00061 U1TCTL = SSEL_SMCLK + STC + CKPL;
00062
00063
00064
00065 U1BR0 = 0x02;
00066 U1BR1 = 0x00;
00067
00068
00069 U1MCTL = 0x00;
00070
00071
00072 P4SEL |= (BIT3 | BIT5);
00073
00074 P4SEL &= ~(BIT4);
00075 P4OUT |= BIT2;
00076 P4DIR |= BIT2;
00077
00078
00079 P7SEL &= ~BIT4;
00080 P7OUT |= BIT4;
00081 P7DIR |= BIT4;
00082
00083 LCD_setup ();
00084 }
00085
00086 void LCD_writeSpiCommand (unsigned char data)
00087 {
00088
00089 P4SEL &= ~(BIT3 + BIT5);
00090 P4OUT |= BIT5;
00091 P4OUT &= ~BIT3;
00092 P4DIR |= BIT3 + BIT5;
00093
00094
00095 CS_LOW ();
00096
00097 P4OUT &= ~BIT5;
00098 P4OUT |= BIT5;
00099
00100 P4SEL |= BIT3 + BIT5;
00101
00102
00103
00104 U1TXBUF = data;
00105
00106
00107 while ((U1TCTL & TXEPT) == 0);
00108
00109
00110
00111 }
00112
00113 void LCD_writeSpiData (unsigned char data)
00114 {
00115
00116 P4SEL &= ~ (BIT3 + BIT5);
00117 P4OUT |= BIT3 + BIT5;
00118 P4DIR |= BIT3 + BIT5;
00119
00120
00121 CS_LOW ();
00122
00123 P4OUT &= ~BIT5;
00124 P4OUT |= BIT5;
00125
00126 P4SEL |= BIT3 + BIT5;
00127
00128
00129
00130 U1TXBUF = data;
00131
00132
00133 while ((U1TCTL & TXEPT) == 0);
00134
00135
00136
00137 }
00138
00139 void LCD_setBacklight (unsigned char state)
00140 {
00141 if (!state)
00142 {
00143 P7OUT |= BIT6;
00144 }
00145 else
00146 {
00147 P7OUT &= ~BIT6;
00148 }
00149 }
00150
00151 void LCD_setContrast (unsigned char contrast)
00152 {
00153 #ifdef GE12
00154 LCD_writeSpiCommand (CONTRAST);
00155 LCD_writeSpiData (0x20 + contrast);
00156 #endif
00157 }
00158
00159 void LCD_setup (void)
00160 {
00161 #ifdef GE12
00162
00163
00164
00166
00167
00168 LCD_writeSpiCommand (SOFTRST);
00169 DelayS (2000);
00170
00171
00172 LCD_writeSpiCommand (INITESC);
00173 DelayS (2000);
00174
00176
00178
00179
00180 LCD_writeSpiCommand (REFSET);
00181 LCD_writeSpiData (0);
00182
00183
00184 LCD_writeSpiCommand (DISPCTRL);
00185 LCD_writeSpiData (128);
00186 LCD_writeSpiData (128);
00187 LCD_writeSpiData (134);
00188 LCD_writeSpiData (84);
00189 LCD_writeSpiData (69);
00190 LCD_writeSpiData (82);
00191 LCD_writeSpiData (67);
00192
00193
00194
00195 LCD_writeSpiCommand (GRAYSCALE0);
00196 LCD_writeSpiData (1);
00197 LCD_writeSpiData (2);
00198 LCD_writeSpiData (4);
00199 LCD_writeSpiData (8);
00200 LCD_writeSpiData (16);
00201 LCD_writeSpiData (30);
00202 LCD_writeSpiData (40);
00203 LCD_writeSpiData (50);
00204 LCD_writeSpiData (60);
00205 LCD_writeSpiData (70);
00206 LCD_writeSpiData (80);
00207 LCD_writeSpiData (90);
00208 LCD_writeSpiData (100);
00209 LCD_writeSpiData (110);
00210 LCD_writeSpiData (127);
00211
00212
00213 LCD_writeSpiCommand (GAMMA);
00214 LCD_writeSpiData (1);
00215
00216
00217
00218 LCD_writeSpiCommand (COMMONDRV);
00219 LCD_writeSpiData (0);
00220
00221
00222
00223 LCD_writeSpiCommand (NORMALMODE);
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 LCD_writeSpiCommand (ACCESSCTRL);
00242 LCD_writeSpiData (0x40);
00243
00244
00246
00247
00249
00250
00251 LCD_writeSpiCommand (PWRCTRL);
00252 LCD_writeSpiData (4);
00253
00254
00255
00256 LCD_writeSpiCommand (SLEEPOUT);
00257
00258
00259
00260 LCD_writeSpiCommand (VOLTCTRL);
00261
00262
00263
00264
00265 LCD_writeSpiCommand (CONTRAST);
00266 LCD_writeSpiData (0x30);
00267
00268 DelayS (2000);
00269
00270
00271 LCD_writeSpiCommand (TEMPGRADIENT);
00272 for (i = 0; i < 14; i++)
00273 {
00274 LCD_writeSpiData (0);
00275 }
00276
00277
00278 LCD_writeSpiCommand (BOOSTVON);
00279
00280 #else // GE12
00281
00282
00283
00284
00285 LCD_RESET_LOW ();
00286 mdelay (100);
00287
00288 LCD_RESET_HIGH ();
00289 mdelay (100);
00290
00291
00292
00293 LCD_writeSpiCommand (DISCTL);
00294
00295
00296
00297 LCD_writeSpiData (0x00);
00298 LCD_writeSpiData (0x20);
00299 LCD_writeSpiData (0x0a);
00300
00301
00302 LCD_writeSpiCommand (COMSCN);
00303 LCD_writeSpiData (0x00);
00304
00305
00306 LCD_writeSpiCommand (IOSCON);
00307
00308
00309 mdelay (100);
00310
00311
00312
00313 LCD_writeSpiCommand (SLPOUT);
00314
00315
00316 LCD_writeSpiCommand (VOLCTR);
00317 LCD_writeSpiData (0x1F);
00318 LCD_writeSpiData (0x03);
00319
00320
00321 LCD_writeSpiCommand (TMPGRD);
00322 LCD_writeSpiData (0x00);
00323
00324
00325 LCD_writeSpiCommand (PWRCTR);
00326 LCD_writeSpiData (0x0f);
00327
00328
00329 LCD_writeSpiCommand (DISNOR);
00330
00331
00332 LCD_writeSpiCommand (DISINV);
00333
00334
00335 LCD_writeSpiCommand (PTLOUT);
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 LCD_writeSpiCommand (DATCTL);
00350 LCD_writeSpiData (0x00);
00351 LCD_writeSpiData (0x03);
00352 LCD_writeSpiData (0x02);
00353
00354
00355 LCD_writeSpiCommand (CASET);
00356 LCD_writeSpiData (LCD_X_OFFSET);
00357 LCD_writeSpiData (LCD_X_OFFSET + LCD_WIDTH - 1);
00358
00359
00360 LCD_writeSpiCommand (PASET);
00361 LCD_writeSpiData (LCD_Y_OFFSET);
00362 LCD_writeSpiData (LCD_Y_OFFSET + LCD_HEIGHT - 1);
00363
00364 #endif // GE12
00365 LCD_clear ();
00366
00367 LCD_writeSpiCommand (DISPLAYON);
00368 }
00369
00370 void LCD_write130x130bmpStart ()
00371 {
00372 #ifdef GE12
00373
00374 LCD_writeSpiCommand (DISPLAYOFF);
00375 #endif
00376
00377 LCD_writeSpiCommand (CASET);
00378 LCD_writeSpiData (LCD_X_OFFSET);
00379 LCD_writeSpiData (LCD_X_OFFSET + LCD_WIDTH - 1);
00380
00381
00382 LCD_writeSpiCommand (PASET);
00383 LCD_writeSpiData (LCD_Y_OFFSET);
00384 LCD_writeSpiData (LCD_Y_OFFSET + LCD_HEIGHT - 1);
00385
00386
00387 LCD_writeSpiCommand (MEMWRITE);
00388 }
00389
00390 void LCD_write130x130bmpData16 (const uint16_t *data)
00391 {
00392 LCD_writeSpiData ((*data) >> 8);
00393 LCD_writeSpiData ((*data) & 0xFF);
00394 }
00395
00396 void LCD_write130x130bmpData8 (const uint8_t *data)
00397 {
00398 LCD_writeSpiData (*data);
00399 }
00400 void LCD_write130x130bmpEnd ()
00401 {
00402 #ifndef GE12
00403
00404 mdelay (100);
00405 #endif
00406
00407
00408 LCD_writeSpiCommand (DISPLAYON);
00409 }
00410
00411 void LCD_write130x130bmp (const unsigned char *bmp)
00412 {
00413 unsigned int j;
00414
00415 LCD_write130x130bmpStart ();
00416
00417
00418 for (j = 0; j < LCD_MEM_SIZE; j++)
00419 {
00420 LCD_writeSpiData (bmp[j]);
00421 }
00422
00423 LCD_write130x130bmpEnd ();
00424 }
00425
00426 void LCD_clear (void)
00427 {
00428 unsigned int j;
00429 #if 0
00430
00431 LCD_writeSpiCommand (DISPLAYOFF);
00432 #endif
00433
00434 LCD_writeSpiCommand (CASET);
00435 LCD_writeSpiData (LCD_X_OFFSET);
00436 LCD_writeSpiData (LCD_X_OFFSET + LCD_WIDTH - 1);
00437
00438
00439 LCD_writeSpiCommand (PASET);
00440 LCD_writeSpiData (LCD_Y_OFFSET);
00441 LCD_writeSpiData (LCD_Y_OFFSET + LCD_HEIGHT - 1);
00442
00443
00444 LCD_writeSpiCommand (MEMWRITE);
00445
00446
00447 for (j = 0; j < LCD_MEM_SIZE; j++)
00448 {
00449 LCD_writeSpiData (0xFF);
00450 }
00451
00452 #if 0
00453 #ifndef GE12
00454
00455 Delay (1000);
00456
00457 #endif
00458
00459
00460 LCD_writeSpiCommand (DISPLAYON);
00461 #endif
00462 LCD_setXY (0, 0);
00463 LCD_setColor (LCD_DEFAULT_FG_COLOR, LCD_DEFAULT_BG_COLOR);
00464 }
00465
00466 void LCD_clearBall (unsigned char x, unsigned char y)
00467 {
00468 unsigned int j;
00469
00470 LCD_writeSpiCommand (PAGEADDRSET);
00471 LCD_writeSpiData (y);
00472 LCD_writeSpiData (y + 19);
00473
00474
00475 LCD_writeSpiCommand (COLADDRSET);
00476 LCD_writeSpiData (x);
00477 LCD_writeSpiData (x + 19);
00478
00479
00480 LCD_writeSpiCommand (MEMWRITE);
00481
00482 for (j = 0; j < 600; j++)
00483 {
00484 LCD_writeSpiData (0xff);
00485 }
00486
00487 #if 0
00488 #ifndef GE12
00489
00490 Delay (1000);
00491
00492 #endif
00493
00494
00495 LCD_writeSpiCommand (DISPLAYON);
00496 #endif
00497 }
00498
00499 void LCD_writeBall (unsigned char x, unsigned char y)
00500 {
00501 unsigned int j;
00502
00503 LCD_writeSpiCommand (PAGEADDRSET);
00504 LCD_writeSpiData (y);
00505 LCD_writeSpiData (y + 19);
00506
00507
00508 LCD_writeSpiCommand (COLADDRSET);
00509 LCD_writeSpiData (x);
00510 LCD_writeSpiData (x + 19);
00511
00512
00513 LCD_writeSpiCommand (MEMWRITE);
00514
00515 for (j = 0; j < 600; j++)
00516 {
00517 LCD_writeSpiData (ball[j]);
00518 }
00519
00520
00521
00522 }
00523
00524 void LCD_writeChar (unsigned char Ascii,
00525 unsigned char x,
00526 unsigned char y,
00527 unsigned short FG_Colour, unsigned short BG_Colour)
00528 {
00529 unsigned int i, j;
00530
00531 unsigned char Byte_1 = 0;
00532 unsigned char Byte_2 = 0;
00533 unsigned char Byte_3 = 0;
00534 unsigned char Pixel = 0;
00535
00536 LCD_writeSpiCommand (COLADDRSET);
00537 LCD_writeSpiData (LCD_X_OFFSET + x);
00538 LCD_writeSpiData (LCD_X_OFFSET + x + FONT_WIDTH - 1);
00539
00540 LCD_writeSpiCommand (PAGEADDRSET);
00541 LCD_writeSpiData (LCD_Y_OFFSET + y);
00542 LCD_writeSpiData (LCD_Y_OFFSET + y + FONT_HEIGHT - 1);
00543
00544 LCD_writeSpiCommand (MEMWRITE);
00545
00546 for (j = 0; j < FONT_HEIGHT; j++)
00547 {
00548 Pixel = font_digits[j + (FONT_HEIGHT * Ascii)];
00549 for (i = 0; i < FONT_WIDTH; i++)
00550 {
00551 if (i % 2)
00552 {
00553 if (Pixel & 0x80)
00554 {
00555 Byte_2 &= 0xF0;
00556 Byte_2 |= FG_Colour >> 8;
00557 Byte_3 = FG_Colour & 0x00FF;
00558 }
00559 else
00560 {
00561 Byte_2 &= 0xF0;
00562 Byte_2 |= BG_Colour >> 8;
00563 Byte_3 = BG_Colour & 0x00FF;
00564 }
00565 LCD_writeSpiData (Byte_1);
00566 LCD_writeSpiData (Byte_2);
00567 LCD_writeSpiData (Byte_3);
00568 }
00569 else
00570 {
00571 if (Pixel & 0x80)
00572 {
00573 Byte_1 = FG_Colour >> 4;
00574 Byte_2 = FG_Colour << 4;
00575 }
00576 else
00577 {
00578 Byte_1 = BG_Colour >> 4;
00579 Byte_2 = 0;
00580 Byte_2 |= BG_Colour << 4;
00581 }
00582 }
00583 Pixel <<= 1;
00584 }
00585 }
00586 }
00587
00588 inline void LCD_setX (uint8_t _x)
00589 {
00590 xBase = x = _x;
00591 }
00592
00593 inline void LCD_setY (uint8_t _y)
00594 {
00595 y = _y;
00596 }
00597
00598 inline void LCD_setXY (uint8_t _x, uint8_t _y)
00599 {
00600 xBase = x = _x;
00601 y = _y;
00602 }
00603
00604 inline uint8_t LCD_getX ()
00605 {
00606 return x;
00607 }
00608
00609 inline uint8_t LCD_getY ()
00610 {
00611 return y;
00612 }
00613
00614 inline void LCD_setFGColor (uint16_t _fgColor)
00615 {
00616 fgColor = _fgColor;
00617 }
00618
00619 inline void LCD_setBGColor (uint16_t _bgColor)
00620 {
00621 bgColor = _bgColor;
00622 }
00623
00624 inline void LCD_setColor (uint16_t _fgColor, uint16_t _bgColor)
00625 {
00626 fgColor = _fgColor;
00627 bgColor = _bgColor;
00628 }
00629
00630 void LCD_printf (const char *fmt, ...)
00631 {
00632 va_list valist;
00633 char buf[LCD_PRINTF_BUF_SIZE];
00634
00635 va_start (valist, fmt);
00636 vsnprintf (buf, sizeof (buf), fmt, valist);
00637 va_end (valist);
00638 LCD_write (buf, strlen (buf));
00639 }
00640
00641 void LCD_write (const char *buf, uint16_t length)
00642 {
00643 uint16_t i;
00644
00645 for (i = 0; i < length; i++)
00646 {
00647 if (buf[i] != '\n')
00648 {
00649
00650 LCD_writeChar (buf[i], x, y, fgColor, bgColor);
00651 x += FONT_WIDTH;
00652 if (x >= LCD_WIDTH - FONT_WIDTH)
00653 {
00654 x = xBase;
00655 y += FONT_HEIGHT;
00656 }
00657 }
00658 else
00659 {
00660
00661 x = xBase;
00662 y += FONT_HEIGHT;
00663 }
00664 }
00665 }
00666