// TFT Functions #include #include #include "p24FJ64GA102.h" #include "timer_functions.h" // :: Define Pins :: #define RESET_TFT LATBbits.LATB8 // Reset Pin #define DCX LATBbits.LATB9 // Data+Command Pin #define CS_TFT LATBbits.LATB13 // Chip-Select of TFT #define LCD_COM 1 // DCX Bit for Command #define LCD_DATA 0 // DCX Bit for Data #define LCD_WIDTH 320 #define LCD_HEIGHT 240 // :: SPI Communication :: // Sends Data via SPI to Slave and receives data from Slave unsigned char spi_com_d(char value) { unsigned char SPI_COM; SPI1BUF = value; // Write Data in SPI1 Buffer while(SPI1STATbits.SPITBF); // Wait until Transmit is done. while(!SPI1STATbits.SPIRBF); // Wait until Receive is done. SPI_COM = SPI1BUF; return SPI_COM; } // :: TFT Send :: // Send Command-Char or Data-Char to TFT Controller void tft_send(char dcx, char data) { char spi_data; DCX = dcx; // Set D/CX --> 0=Command, 1=Data CS_TFT = 0; // Set CS to 0 (Low Active) spi_data = spi_com_d(data); // SPI communication if(dcx == 0) CS_TFT = 1; } // :: TFT Get :: // Send Command-Char or Data-Char to TFT Controller and returns Feedback char tft_get(char dcx, char data) { char spi_data; DCX = dcx; // Set D/CX --> 0=Command, 1=Data CS_TFT = 0; // Set CS to 0 (Low Active) spi_data = spi_com_d(data); // SPI communication return spi_data; } int lcd_set_cursor_x(int x) { if( x >= LCD_WIDTH ) return EXIT_FAILURE; tft_send(LCD_COM, 0x2B); tft_send(LCD_DATA, x >> 8); tft_send(LCD_DATA, x & 0xFF); tft_send(LCD_COM, 0x2c); return EXIT_SUCCESS; } int lcd_set_cursor_y(int y) { if( y >= LCD_HEIGHT ) return EXIT_FAILURE; tft_send(LCD_COM, 0x2A); tft_send(LCD_DATA, y >> 8); tft_send(LCD_DATA, y & 0xFF); tft_send(LCD_COM, 0x2c); return EXIT_SUCCESS; } int lcd_set_cursor(int x, int y) { if( lcd_set_cursor_x(x) || lcd_set_cursor_y(y) ) return EXIT_FAILURE; return EXIT_SUCCESS; } int lcd_draw_pixel(int color) { tft_send(LCD_DATA, color >> 8); tft_send(LCD_DATA, color & 0xFF); CS_TFT = 1; return EXIT_SUCCESS; } void lcd_fill(int bg_color) { int width = LCD_WIDTH, height = LCD_HEIGHT; if(lcd_set_cursor(0,0)) return; while(width--) { while(height--) lcd_draw_pixel(bg_color); height = LCD_HEIGHT; } } // :: Initialization Process of TFT Display :: // According to Documentation delivered with display module void init_tft(void) { RESET_TFT = 1; // Set Reset Pin high to initialize the Reset delay_ms(10); RESET_TFT = 0; tft_send(0,0xCB); // Power control A tft_send(1,0x39); tft_send(1,0x2C); tft_send(1,0x00); tft_send(1,0x34); tft_send(1,0x02); CS_TFT = 1; tft_send(0,0xCF); // Power Control B tft_send(1,0x00); tft_send(1,0xC1); tft_send(1,0x30); CS_TFT = 1; tft_send(0,0xE8); // Driver timing control A tft_send(1,0x85); tft_send(1,0x00); tft_send(1,0x78); CS_TFT = 1; tft_send(0,0xEA); // Driver timing control B tft_send(1,0x00); tft_send(1,0x00); CS_TFT = 1; tft_send(0,0xED); // Power on sequence control tft_send(1,0x64); tft_send(1,0x03); tft_send(1,0x12); tft_send(1,0x81); CS_TFT = 1; tft_send(0,0xF7); // Pump ratio control tft_send(1,0x20); CS_TFT = 1; tft_send(0,0xC0); // Power Control 1 tft_send(1,0x23); CS_TFT = 1; tft_send(0,0xC1); // Power Control 2 tft_send(1,0x10); CS_TFT = 1; tft_send(0,0xC5); // VCOM Control 1 tft_send(1,0x3e); tft_send(1,0x28); CS_TFT = 1; tft_send(0,0xC7); // VCOM Control 2 tft_send(1,0x86); CS_TFT = 1; tft_send(0,0x36); // Memory Access Control tft_send(1,0x48); CS_TFT = 1; tft_send(0,0x3A); // COLMOD: Pixel Format Set tft_send(1,0x55); CS_TFT = 1; tft_send(0,0xB1); // Frame Rate Control tft_send(1,0x00); tft_send(1,0x18); CS_TFT = 1; tft_send(0,0xB6); // Display Function Control tft_send(1,0x08); tft_send(1,0x82); tft_send(1,0x27); CS_TFT = 1; tft_send(0,0xF2); // Enable 3Gamma tft_send(1,0x00); CS_TFT = 1; tft_send(0,0x26); // Gamma Set tft_send(1,0x01); CS_TFT = 1; tft_send(0,0xE0); // Positive Gamma Correction tft_send(1,0x0F); tft_send(1,0x31); tft_send(1,0x2B); tft_send(1,0x0C); tft_send(1,0x0E); tft_send(1,0x08); tft_send(1,0x4E); tft_send(1,0xF1); tft_send(1,0x37); tft_send(1,0x07); tft_send(1,0x10); tft_send(1,0x03); tft_send(1,0x0E); tft_send(1,0x09); tft_send(1,0x00); CS_TFT = 1; tft_send(0,0xE1); // Negative Gamma Correction tft_send(1,0x00); tft_send(1,0x0E); tft_send(1,0x14); tft_send(1,0x03); tft_send(1,0x11); tft_send(1,0x07); tft_send(1,0x31); tft_send(1,0xC1); tft_send(1,0x48); tft_send(1,0x08); tft_send(1,0x0F); tft_send(1,0x0C); tft_send(1,0x31); tft_send(1,0x36); tft_send(1,0x0F); CS_TFT = 1; tft_send(0,0x11); // Sleep Out CS_TFT = 1; delay_ms(200); // Wait for 120ms tft_send(0,0x29); // Display On CS_TFT = 1; //tft_send(0,0x2c); // Memory Write CS_TFT = 1; lcd_fill(0x0000); // Fill TFT Background Black }