40 #include "hal_MSP-EXP430F5438.h"
41 #include "hal_lcd_fonts.h"
43 unsigned char LcdInitMacro[] = {
44 0x74, 0x00, 0x00, 0x76, 0x00, 0x01,
45 0x74, 0x00, 0x01, 0x76, 0x00, 0x0D,
46 0x74, 0x00, 0x02, 0x76, 0x00, 0x4C,
47 0x74, 0x00, 0x03, 0x76, 0x12, 0x14,
48 0x74, 0x00, 0x04, 0x76, 0x04, 0x66,
49 0x74, 0x00, 0x05, 0x76, 0x00, 0x10,
50 0x74, 0x00, 0x06, 0x76, 0x00, 0x00,
51 0x74, 0x00, 0x07, 0x76, 0x00, 0x15,
52 0x74, 0x00, 0x08, 0x76, 0x00, 0x03,
53 0x74, 0x00, 0x09, 0x76, 0x00, 0x00,
54 0x74, 0x00, 0x0A, 0x76, 0x00, 0x15,
55 0x74, 0x00, 0x0B, 0x76, 0x00, 0x03,
56 0x74, 0x00, 0x0C, 0x76, 0x00, 0x03,
57 0x74, 0x00, 0x0D, 0x76, 0x00, 0x00,
58 0x74, 0x00, 0x0E, 0x76, 0x00, 0x15,
59 0x74, 0x00, 0x0F, 0x76, 0x00, 0x03,
60 0x74, 0x00, 0x10, 0x76, 0x00, 0x15,
61 0x74, 0x00, 0x11, 0x76, 0x00, 0x03,
64 unsigned char Read_Block_Address_Macro[] = {0x74, 0x00, 0x12, 0x77, 0x00, 0x00};
65 unsigned char Draw_Block_Value_Macro[] = {0x74, 0x00, 0x12, 0x76, 0xFF, 0xFF};
66 unsigned char Draw_Block_Address_Macro[] = {0x74, 0x00, 0x11, 0x76, 0x00, 0x00};
68 unsigned int LcdAddress = 0, LcdTableAddress = 0;
69 unsigned char contrast = 0x66;
70 unsigned char backlight = 8;
71 int LCD_MEM[110 * 17];
86 void halLcdSendCommand(
unsigned char Data[])
90 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
91 for (i = 0; i < 6; i++)
93 while (!(UCB2IFG & UCTXIFG)) ;
98 while (UCB2STAT & UCBUSY) ;
99 LCD_CS_RST_OUT |= LCD_CS_PIN;
100 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
103 while (UCB2STAT & UCBUSY) ;
104 LCD_CS_RST_OUT |= LCD_CS_PIN;
118 void halLcdInit(
void)
120 LCD_CS_RST_OUT |= LCD_CS_PIN | LCD_RESET_PIN;
121 LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN;
123 LCD_BACKLT_SEL |= LCD_BACKLIGHT_PIN;
125 LCD_CS_RST_OUT &= ~LCD_RESET_PIN;
127 LCD_CS_RST_OUT |= LCD_RESET_PIN;
130 LCD_SPI_SEL |= LCD_MOSI_PIN + LCD_CLK_PIN;
131 LCD_SPI_SEL &= ~LCD_MISO_PIN;
132 LCD_SPI_DIR &= ~(LCD_MISO_PIN + LCD_MOSI_PIN);
138 UCB2CTL0 = UCMST + UCSYNC + UCCKPL + UCMSB;
139 UCB2CTL1 |= UCSSEL_2;
142 UCB2CTL1 &= ~UCSWRST;
149 halLcdSendCommand(&LcdInitMacro[1 * 6]);
150 halLcdSendCommand(&LcdInitMacro[2 * 6]);
151 halLcdSendCommand(&LcdInitMacro[4 * 6]);
152 halLcdSendCommand(&LcdInitMacro[5 * 6]);
153 halLcdSendCommand(&LcdInitMacro[6 * 6]);
154 halLcdSendCommand(&LcdInitMacro[7 * 6]);
166 void halLcdShutDown(
void)
170 LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN;
171 LCD_CS_RST_OUT &= ~(LCD_CS_PIN | LCD_RESET_PIN);
172 LCD_CS_RST_OUT &= ~LCD_RESET_PIN;
174 LCD_SPI_SEL &= ~(LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN);
175 LCD_CS_RST_DIR |= LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN;
176 LCD_CS_RST_OUT &= ~(LCD_MOSI_PIN + LCD_CLK_PIN + LCD_MISO_PIN);
190 void halLcdBackLightInit(
void)
192 LCD_BACKLT_DIR |= LCD_BACKLIGHT_PIN;
193 LCD_BACKLT_OUT |= LCD_BACKLIGHT_PIN;
194 LCD_BACKLT_SEL |= LCD_BACKLIGHT_PIN;
197 TA0CCR3 = TA0CCR0 >> 1;
201 TA0CTL = TASSEL_2 + MC_1;
213 unsigned int halLcdGetBackLight(
void)
226 void halLcdSetBackLight(
unsigned char BackLightLevel)
228 unsigned int dutyCycle = 0, i, dummy;
230 if (BackLightLevel > 0)
233 dummy = (TA0CCR0 >> 4);
235 for (i = 0; i < BackLightLevel; i++)
249 backlight = BackLightLevel;
262 void halLcdShutDownBackLight(
void)
264 LCD_BACKLT_DIR |= LCD_BACKLIGHT_PIN;
265 LCD_BACKLT_OUT &= ~(LCD_BACKLIGHT_PIN);
266 LCD_BACKLT_SEL &= ~LCD_BACKLIGHT_PIN;
282 void halLcdSetContrast(
unsigned char ContrastLevel)
284 if (ContrastLevel > 127) ContrastLevel = 127;
285 if (ContrastLevel < 70) ContrastLevel = 70;
286 LcdInitMacro[0x04 * 6 + 5] = ContrastLevel;
287 halLcdSendCommand(&LcdInitMacro[0x04 * 6]);
298 unsigned char halLcdGetContrast(
void)
300 return LcdInitMacro[0x04 * 6 + 5];
311 void halLcdCursor(
void)
313 LcdInitMacro[8 * 6 + 5] ^= BIT2;
314 halLcdSendCommand(&LcdInitMacro[8 * 6]);
316 LcdInitMacro[0x0B * 6 + 5] = ((LcdAddress & 0x1F) << 3);
317 LcdInitMacro[0x0B * 6 + 4] = ((LcdAddress & 0x1F) << 3) + 3;
318 LcdInitMacro[0x0C * 6 + 5] = (LcdAddress >> 5);
319 LcdInitMacro[0x0C * 6 + 4] = (LcdAddress >> 5) + 7;
320 halLcdSendCommand(&LcdInitMacro[0x0B * 6]);
321 halLcdSendCommand(&LcdInitMacro[0x0C * 6]);
323 halLcdSetAddress(LcdAddress);
334 void halLcdCursorOff(
void)
336 LcdInitMacro[8 * 6 + 5] &= ~BIT2;
337 halLcdSendCommand(&LcdInitMacro[8 * 6]);
348 void halLcdReverse(
void)
350 LcdInitMacro[7 * 6 + 5] ^= BIT1;
351 halLcdSendCommand(&LcdInitMacro[7 * 6]);
362 void halLcdStandby(
void)
364 LcdInitMacro[3 * 6 + 5] &= (~BIT3) & (~BIT2);
365 LcdInitMacro[3 * 6 + 5] |= BIT0;
366 halLcdSendCommand(&LcdInitMacro[3 * 6]);
377 void halLcdActive(
void)
379 halLcdSendCommand(LcdInitMacro);
385 for(i = 0; i < 5; ++i) {
390 LcdInitMacro[3 * 6 + 5] |= BIT3;
391 LcdInitMacro[3 * 6 + 5] &= ~BIT0;
392 halLcdSendCommand(&LcdInitMacro[3 * 6]);
407 void halLcdSetAddress(
int Address)
411 Draw_Block_Address_Macro[4] = Address >> 8;
412 Draw_Block_Address_Macro[5] = Address & 0xFF;
413 halLcdSendCommand(Draw_Block_Address_Macro);
414 LcdAddress = Address;
416 temp = temp + (temp << 4);
418 LcdTableAddress = temp + (Address & 0x1F);
435 void halLcdDrawBlock(
unsigned int Address,
unsigned int Value)
437 halLcdSetAddress(Address);
438 halLcdDrawCurrentBlock(Value);
451 void halLcdDrawCurrentBlock(
unsigned int Value)
455 Draw_Block_Value_Macro[4] = Value >> 8;
456 Draw_Block_Value_Macro[5] = Value & 0xFF;
457 LCD_MEM[LcdTableAddress] = Value;
459 halLcdSendCommand(Draw_Block_Value_Macro);
462 temp = LcdAddress >> 5;
463 temp = temp + (temp << 4);
465 LcdTableAddress = temp + (LcdAddress & 0x1F);
468 if ((LcdAddress & 0x1F) > 0x11)
469 halLcdSetAddress((LcdAddress & 0xFFE0) + 0x20);
470 if (LcdAddress == LCD_Size)
482 int halLcdReadBlock(
unsigned int Address)
484 int i = 0, Value = 0, ReadData[7];
486 halLcdSetAddress(Address);
487 halLcdSendCommand(Read_Block_Address_Macro);
489 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
492 while (!(UCB2IFG & UCTXIFG)) ;
493 while (UCB2STAT & UCBUSY) ;
496 LCD_SPI_SEL &= ~LCD_MOSI_PIN;
497 LCD_SPI_SEL |= LCD_MISO_PIN;
499 for (i = 0; i < 7; i++)
503 while (!(UCB2IFG & UCRXIFG)) ;
504 ReadData[i] = UCB2RXBUF;
506 LCD_CS_RST_OUT |= LCD_CS_PIN;
508 LCD_SPI_SEL |= LCD_MOSI_PIN;
509 LCD_SPI_SEL &= ~LCD_MISO_PIN;
510 LCD_CS_RST_DIR |= LCD_MOSI_PIN + LCD_CLK_PIN;
511 LCD_CS_RST_DIR &= ~LCD_MISO_PIN;
513 Value = (ReadData[5] << 8) + ReadData[6];
530 void halLcdPixel(
int x,
int y,
unsigned char GrayScale)
533 unsigned char offset;
536 if ((x >= 0) && (x < LCD_COL) && (y >= 0) && (y < LCD_ROW))
538 Address = (y << 5) + (x >> 3);
540 Value = LCD_MEM[(y << 4) + y + (x >> 3)];
542 offset = (x & 0x07) << 1;
543 Value &= ~(3 << offset);
544 Value |= GrayScale << offset;
546 halLcdDrawBlock(Address, Value);
558 void halLcdClearScreen(
void)
560 int i, j, k, Current_Location = 0;
564 for (i = 0; i < 110; i++)
567 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
568 for (k = 0; k < 3; k++)
570 while (!(UCB2IFG & UCTXIFG)) ;
571 UCB2TXBUF = Draw_Block_Value_Macro[k];
573 while (UCB2STAT & UCBUSY) ;
574 LCD_CS_RST_OUT |= LCD_CS_PIN;
575 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
576 while (!(UCB2IFG & UCTXIFG)) ;
577 UCB2TXBUF = Draw_Block_Value_Macro[3];
580 for (j = 0; j < 17; j++)
582 LCD_MEM[LcdTableAddress++] = 0x00;
583 while (!(UCB2IFG & UCTXIFG)) ;
585 while (!(UCB2IFG & UCTXIFG)) ;
589 while (!(UCB2IFG & UCTXIFG)) ;
591 while (!(UCB2IFG & UCTXIFG)) ;
593 while (UCB2STAT & UCBUSY) ;
594 LCD_CS_RST_OUT |= LCD_CS_PIN;
596 Current_Location += 0x20;
597 halLcdSetAddress(Current_Location);
620 void halLcdImage(
const unsigned int Image[],
int Columns,
int Rows,
int x,
int y)
622 int i, CurrentLocation;
624 CurrentLocation = (y << 5) + (x >> 3);
625 halLcdSetAddress(CurrentLocation);
626 for (i = 0; i < Rows; i++)
628 halLcdDrawCurrentLine(Image, Columns);
630 CurrentLocation += 0x20;
631 halLcdSetAddress(CurrentLocation);
645 void halLcdDrawCurrentLine(
const unsigned int *value,
int Columns)
650 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
651 for (i = 0; i < 3; i++)
653 while (!(UCB2IFG & UCTXIFG)) ;
654 UCB2TXBUF = Draw_Block_Value_Macro[i];
656 while (UCB2STAT & UCBUSY) ;
657 LCD_CS_RST_OUT |= LCD_CS_PIN;
658 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
659 while (!(UCB2IFG & UCTXIFG)) ;
660 UCB2TXBUF = Draw_Block_Value_Macro[3];
663 for (i = 0; i < Columns; i++)
666 if (LcdTableAddress >=
sizeof(LCD_MEM)){
669 LCD_MEM[LcdTableAddress++] = *value;
670 while (!(UCB2IFG & UCTXIFG)) ;
671 UCB2TXBUF = (*value) >> 8;
672 while (!(UCB2IFG & UCTXIFG)) ;
673 UCB2TXBUF = (*value++) & 0xFF;
676 while (UCB2STAT & UCBUSY) ;
677 LCD_CS_RST_OUT |= LCD_CS_PIN;
694 void halLcdClearImage(
int Columns,
int Rows,
int x,
int y)
696 int i, j, k, Current_Location;
698 Current_Location = (y << 5) + (x >> 3);
699 halLcdSetAddress(Current_Location);
701 for (i = 0; i < Rows; i++)
704 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
705 for (k = 0; k < 3; k++)
707 while (!(UCB2IFG & UCTXIFG)) ;
708 UCB2TXBUF = Draw_Block_Value_Macro[k];
710 while (UCB2STAT & UCBUSY) ;
711 LCD_CS_RST_OUT |= LCD_CS_PIN;
712 LCD_CS_RST_OUT &= ~LCD_CS_PIN;
713 while (!(UCB2IFG & UCTXIFG)) ;
714 UCB2TXBUF = Draw_Block_Value_Macro[3];
717 for (j = 0; j < Columns; j++)
719 LCD_MEM[LcdTableAddress++] = 0x00;
720 while (!(UCB2IFG & UCTXIFG)) ;
722 while (!(UCB2IFG & UCTXIFG)) ;
725 while (UCB2STAT & UCBUSY) ;
726 LCD_CS_RST_OUT |= LCD_CS_PIN;
728 Current_Location += 0x20;
729 halLcdSetAddress(Current_Location);
742 void halLcdDrawTextBlock(
unsigned int Value)
746 Draw_Block_Value_Macro[4] = Value >> 8;
747 Draw_Block_Value_Macro[5] = Value & 0xFF;
748 LCD_MEM[LcdTableAddress] = Value;
750 halLcdSendCommand(Draw_Block_Value_Macro);
753 temp = LcdAddress >> 5;
754 temp = temp + (temp << 4);
756 LcdTableAddress = temp + (LcdAddress & 0x1F);
759 if ((LcdAddress & 0x1F) > 0x10)
760 halLcdSetAddress((LcdAddress & 0xFFE0) + 0x20);
762 if (LcdAddress >= LCD_Size)
782 void halLcdPrint(
char String[],
unsigned char TextStyle)
784 int i, j, Counter = 0, BlockValue;
785 int Address, LCD_MEM_Add, ActualAddress;
789 ActualAddress = LcdAddress;
790 Counter = LcdAddress & 0x1F;
793 while (String[i] != 0)
795 LookUpChar = fonts_lookup[(
unsigned char)String[i]];
797 for (j = 0; j < FONT_HEIGHT; j++)
799 Address = ActualAddress + j * 0x20;
803 LCD_MEM_Add = temp + (Address & 0x1F);
805 BlockValue = LCD_MEM[LCD_MEM_Add];
807 if (TextStyle & GRAYSCALE_TEXT)
809 if (TextStyle & INVERT_TEXT)
810 if (TextStyle & OVERWRITE_TEXT)
811 BlockValue = 0xAAAA - GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
813 BlockValue |= 0xAAAA - GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
815 if (TextStyle & OVERWRITE_TEXT)
816 BlockValue = GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
818 BlockValue |= GrayScale_fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
822 if (TextStyle & INVERT_TEXT)
823 if (TextStyle & OVERWRITE_TEXT)
824 BlockValue = 0xFFFF - fonts[LookUpChar * 13 + j];
826 BlockValue |= 0xFFFF - fonts[LookUpChar * 13 + j];
829 if (TextStyle & OVERWRITE_TEXT)
830 BlockValue = fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
832 BlockValue |= fonts[LookUpChar * (FONT_HEIGHT + 1) + j];
834 halLcdDrawBlock(Address, BlockValue);
841 ActualAddress += 0x20 * FONT_HEIGHT - 16;
842 if (ActualAddress > LCD_Last_Pixel - 0x20 * FONT_HEIGHT)
849 halLcdSetAddress(ActualAddress);
871 void halLcdPrintXY(
char String[],
int x,
int y,
unsigned char TextStyle)
874 halLcdSetAddress((y << 5) + (x >> 3));
875 halLcdPrint(String, TextStyle);
893 void halLcdPrintLine(
char String[],
unsigned char Line,
unsigned char TextStyle)
897 temp = Line * FONT_HEIGHT;
898 halLcdSetAddress(temp << 5);
899 halLcdPrint(String, TextStyle);
919 void halLcdPrintLineCol(
char String[],
unsigned char Line,
unsigned char Col,
920 unsigned char TextStyle)
924 temp = Line * FONT_HEIGHT;
928 halLcdSetAddress(temp);
929 halLcdPrint(String, TextStyle);
946 void halLcdHLine(
int x1,
int x2,
int y,
unsigned char GrayScale)
957 halLcdPixel(x, y, GrayScale);
976 void halLcdVLine(
int x,
int y1,
int y2,
unsigned char GrayScale)
987 halLcdPixel(x, y, GrayScale);
1010 void halLcdLine(
int x1,
int y1,
int x2,
int y2,
unsigned char GrayScale)
1012 int x, y, deltay, deltax, d;
1016 halLcdVLine(x1, y1, y2, GrayScale);
1020 halLcdHLine(x1, x2, y1, GrayScale);
1032 deltay = ABS(y2 - y1);
1033 deltax = ABS(x2 - x1);
1035 if (deltax >= deltay)
1037 d = (deltay << 1) - deltax;
1040 halLcdPixel(x, y, GrayScale);
1045 d += ((deltay - deltax) << 1);
1053 d = (deltax << 1) - deltay;
1056 halLcdPixel(x, y, GrayScale);
1061 d += ((deltax - deltay) << 1);
1085 void halLcdCircle(
int x,
int y,
int Radius,
int GrayScale)
1087 int xx, yy, ddF_x, ddF_y, f;
1090 ddF_y = -(2 * Radius);
1095 halLcdPixel(x + xx, y + yy, GrayScale);
1096 halLcdPixel(x + xx, y - yy, GrayScale);
1097 halLcdPixel(x - xx, y + yy, GrayScale);
1098 halLcdPixel(x - xx, y - yy, GrayScale);
1099 halLcdPixel(x + yy, y + xx, GrayScale);
1100 halLcdPixel(x + yy, y - xx, GrayScale);
1101 halLcdPixel(x - yy, y + xx, GrayScale);
1102 halLcdPixel(x - yy, y - xx, GrayScale);
1114 halLcdPixel(x + xx, y + yy, GrayScale);
1115 halLcdPixel(x + xx, y - yy, GrayScale);
1116 halLcdPixel(x - xx, y + yy, GrayScale);
1117 halLcdPixel(x - xx, y - yy, GrayScale);
1118 halLcdPixel(x + yy, y + xx, GrayScale);
1119 halLcdPixel(x + yy, y - xx, GrayScale);
1120 halLcdPixel(x - yy, y + xx, GrayScale);
1121 halLcdPixel(x - yy, y - xx, GrayScale);
1137 void halLcdScrollRow(
int y)
1139 int i, Address, LcdTableAddressTemp;
1144 halLcdSetAddress(Address);
1147 LcdTableAddressTemp = y + (y << 4);
1148 temp = ((LCD_MEM[LcdTableAddressTemp] & 0x0003) << 14);
1150 for (i = 0; i < 0x10; i++)
1151 halLcdDrawCurrentBlock(((LCD_MEM[LcdTableAddressTemp + i] & 0xFFFC) >> 2) \
1152 + ((LCD_MEM[LcdTableAddressTemp + i + 1] & 0x0003) << 14));
1154 halLcdDrawCurrentBlock(((LCD_MEM[LcdTableAddressTemp + 0x10] & 0xFFFC) >> 2) + temp);
1172 void halLcdHScroll(
int yStart,
int yEnd)
1176 for (i = yStart; i < yEnd + 1; i++)
1188 void halLcdScrollLine(
int Line)
1192 Row = Line * FONT_HEIGHT;
1194 for (i = Row; i < Row + FONT_HEIGHT; i++)
Default definitions of C compiler quirk work-arounds.
#define __delay_cycles(x)
__delay_cycles() is an intrinsic IAR call; however, we have explicity disallowed it since it is too s...