/**********************************************************/ /* Pins and outputs */ /**********************************************************/ // Chip Select line pin1.0 #define LCD_CS BIT7 #define LCD_CS_DIR P1DIR #define LCD_CS_OUT P1OUT // Hardware Reset pin1.1 #define LCD_RESET BIT6 #define LCD_RESET_DIR P1DIR #define LCD_RESET_OUT P1OUT // Command/Data mode line pin1.4 #define LCD_CD BIT3 #define LCD_CD_DIR P1DIR #define LCD_CD_OUT P1OUT // SPI #define SPI UCA0TXBUF
void LCD_SendCmd(unsigned char Cmd) { LCD_CS_OUT |= LCD_CS; // set CS pin to High LCD_CD_OUT &= ~LCD_CD; // set CD pin to Low LCD_CS_OUT &= ~LCD_CS; SPI = Cmd; } void LCD_SendDat(unsigned char Data) { LCD_CD_OUT |= LCD_CD; // set CD pin to High SPI = Data; }
void LCD_Init() { // Set pins to output direction LCD_CS_DIR |= LCD_CS; LCD_RESET_DIR |= LCD_RESET; LCD_CD_DIR |= LCD_CD; LCD_CS_OUT &= ~LCD_CS; LCD_RESET_OUT &= ~LCD_RESET; LCD_CD_OUT &= ~LCD_CD; __delay_cycles(160000); //wait 100ms (F_CPU 16MHz) LCD_RESET_OUT |= LCD_RESET; __delay_cycles(160000); LCD_SendCmd(0x01); //reset sw __delay_cycles(80000); LCD_SendCmd(0xc6); //initial escape LCD_SendCmd(0xb9); //Refresh set LCD_SendDat(0x00); __delay_cycles(160000); LCD_SendCmd(0xb6); //Display control LCD_SendDat(0x80); // LCD_SendDat(0x04); // LCD_SendDat(0x0a); // LCD_SendDat(0x54); // LCD_SendDat(0x45); // LCD_SendDat(0x52); // LCD_SendDat(0x43); // LCD_SendCmd(0xb3); //Gray scale position set 0 LCD_SendDat(0x02); // LCD_SendDat(0x0a); // LCD_SendDat(0x15); // LCD_SendDat(0x1f); // LCD_SendDat(0x28); // LCD_SendDat(0x30); // LCD_SendDat(0x37); // LCD_SendDat(0x3f); // LCD_SendDat(0x47); // LCD_SendDat(0x4c); // LCD_SendDat(0x54); // LCD_SendDat(0x65); // LCD_SendDat(0x75); // LCD_SendDat(0x80); // LCD_SendDat(0x85); // LCD_SendCmd(0xb5); //Gamma curve LCD_SendDat(0x01); // LCD_SendCmd(0xbd); //Common driver output select LCD_SendDat(0x00); // LCD_SendCmd(0xbe); //Power control LCD_SendDat(0x54); //0x58 before LCD_SendCmd(0x11); //sleep out __delay_cycles(800000); LCD_SendCmd(0xba); //Voltage control LCD_SendDat(0x2f); // LCD_SendDat(0x03); // LCD_SendCmd(0x25); //Write contrast LCD_SendDat(0x60); // LCD_SendCmd(0xb7); //Temperature gradient LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendDat(0x00); // LCD_SendCmd(0x03); //Booster voltage ON __delay_cycles(800000); LCD_SendCmd(0x36); //Memory access control LCD_SendDat(0x48); // LCD_SendCmd(0x2d); //Color set LCD_SendDat(0x00); // LCD_SendDat(0x03); // LCD_SendDat(0x05); // LCD_SendDat(0x07); // LCD_SendDat(0x09); // LCD_SendDat(0x0b); // LCD_SendDat(0x0d); // LCD_SendDat(0x0f); // LCD_SendDat(0x00); // LCD_SendDat(0x03); // LCD_SendDat(0x05); // LCD_SendDat(0x07); // LCD_SendDat(0x09); // LCD_SendDat(0x0b); // LCD_SendDat(0x0d); // LCD_SendDat(0x0f); // LCD_SendDat(0x00); // LCD_SendDat(0x05); // LCD_SendDat(0x0b); // LCD_SendDat(0x0f); // LCD_SendCmd(0x3a); //interface pixel format LCD_SendDat(0x03); // 0x02 for 8-bit 0x03 for 12bit __delay_cycles(1600000); LCD_SendCmd(0x29); //Display ON }
LCD_SendCmd(0x2A); // X (x0 - , x1 - ) LCD_SendDat(x0); LCD_SendDat(x1); LCD_SendCmd(0x2B); // Y (y0 - , y1 - ) LCD_SendDat(y0+1); // Y 1, 0 LCD_SendDat(y1+1); LCD_SendCmd(0x2C); //
void LCD_Flush(unsigned char R, unsigned char G, unsigned char B) { volatile int i = 4040; volatile char B0, B1, B2; B0 = ((R << 4) & 0xF0) + (G & 0x0F); B1 = ((B << 4) & 0xF0) + (R & 0x0F); B2 = ((G << 4) & 0xF0) + (B & 0x0F); LCD_SendCmd(0x2A); LCD_SendDat(0); LCD_SendDat(100); LCD_SendCmd(0x2B); LCD_SendDat(1); LCD_SendDat(80); LCD_SendCmd(0x2C); while (i--) { LCD_CD_OUT |= LCD_CD; SPI = B0; SPI = B1; SPI = B2; } }
void drawHorizontal(char type, unsigned char x, unsigned char y) { volatile unsigned char i = 22, B2, B1, B0; if (type) { B0 = greenBright; B1 = 0; B2 = (greenBright << 4) & 0xF0; } else { B0 = greenDim; B1 = 0; B2 = (greenDim << 4) & 0xF0; } LCD_SendCmd(0x2A); LCD_SendDat(x); LCD_SendDat(x+11); LCD_SendCmd(0x2B); LCD_SendDat(y+1); LCD_SendDat(y+4); LCD_SendCmd(0x2C); __delay_cycles(4); LCD_CD_OUT |= LCD_CD; SPI = BG0; SPI = (BG1 << 4) & 0xF0; __delay_cycles(2); SPI = B2; while(i--) { if (i == 17) { SPI = B0; __delay_cycles(2); SPI = (BG0 >> 4) & 0x0F; __delay_cycles(2); SPI = (BG0 << 4) & 0xF0 + (BG1 << 4) & 0x0F; continue; } if (i == 4) { SPI = BG0; SPI = (BG1 << 4) & 0xF0; __delay_cycles(2); SPI = B2; continue; } SPI = B0; SPI = B1; SPI = B2; } SPI = B0; __delay_cycles(2); SPI = (BG0 >> 4) & 0x0F; __delay_cycles(2); SPI = (BG0 << 4) & 0xF0 + (BG1 << 4) & 0x0F; }
/******************************************************************************************** * Array for Clock * ____ * _|__1_|_ * |6| |2| * |_|____|_| * _|__7_|_ * |5| |3| * |_|____|_| * |__4_| * ********************************************************************************************/ static const char HH[10][7] = { {1,1,1,1,1,1,0}, // 0 {0,1,1,0,0,0,0}, // 1 {1,1,0,1,1,0,1}, // 2 {1,1,1,1,0,0,1}, // 3 {0,1,1,0,0,1,1}, // 4 {1,0,1,1,0,1,1}, // 5 {1,0,1,1,1,1,1}, // 6 {1,1,1,0,0,0,0}, // 7 {1,1,1,1,1,1,1}, // 8 {1,1,1,1,0,1,1} // 9 };
void drawClock(char hh, char mm, char dots) { volatile char h0, h1, m0, m1; h0 = hh / 10; h1 = hh - (h0 * 10); m0 = mm / 10; m1 = mm - (m0 * 10); drawHorizontal(HH[h0][0], 9, 25); drawHorizontal(HH[h1][0], 31, 25); drawHorizontal(HH[m0][0], 58, 25); drawHorizontal(HH[m1][0], 80, 25); drawVertical(HH[h0][5], 6, 29); drawVertical(HH[h0][1], 20, 29); drawVertical(HH[h1][5], 28, 29); drawVertical(HH[h1][1], 42, 29); drawVertical(HH[m0][5], 55, 29); drawVertical(HH[m0][1], 69, 29); drawVertical(HH[m1][5], 77, 29); drawVertical(HH[m1][1], 91, 29); drawHorizontal(HH[h0][6], 9, 38); drawHorizontal(HH[h1][6], 31, 38); drawHorizontal(HH[m0][6], 58, 38); drawHorizontal(HH[m1][6], 80, 38); drawVertical(HH[h0][4], 6, 42); drawVertical(HH[h0][2], 20, 42); drawVertical(HH[h1][4], 28, 42); drawVertical(HH[h1][2], 42, 42); drawVertical(HH[m0][4], 55, 42); drawVertical(HH[m0][2], 69, 42); drawVertical(HH[m1][4], 77, 42); drawVertical(HH[m1][2], 91, 42); drawHorizontal(HH[h0][3], 9, 51); drawHorizontal(HH[h1][3], 31, 51); drawHorizontal(HH[m0][3], 58, 51); drawHorizontal(HH[m1][3], 80, 51); drawDots(dots); }
Source: https://habr.com/ru/post/159011/
All Articles