/* Display all possible 16-bit colors on the TFT with ILI9340 * * This program initializes the ILI9340 controller of a QVGA TFT * display then shows all possible 16-bit colors. * * Built and tested with MSP432P401R Rev. C, Keil MDK-ARM v5.24a and MSP432P4xx_DFP v3.1.0 */ #include "msp.h" #define TFTWIDTH 240 #define TFTHEIGHT 320 #define ILI9340_SLPOUT 0x11 #define ILI9340_GAMMASET 0x26 #define ILI9340_DISPON 0x29 #define ILI9340_CASET 0x2A #define ILI9340_PASET 0x2B #define ILI9340_RAMWR 0x2C #define ILI9340_MADCTL 0x36 #define ILI9340_MADCTL_MX 0x40 #define ILI9340_MADCTL_BGR 0x08 #define ILI9340_PIXFMT 0x3A #define ILI9340_FRMCTR1 0xB1 #define ILI9340_DFUNCTR 0xB6 #define ILI9340_PWCTR1 0xC0 #define ILI9340_PWCTR2 0xC1 #define ILI9340_VMCTR1 0xC5 #define ILI9340_VMCTR2 0xC7 #define ILI9340_GMCTRP1 0xE0 #define ILI9340_GMCTRN1 0xE1 #define ILI9340_setCommand() P3->OUT &= ~(1 << 0); /* make DC P3.0 low */ #define ILI9340_setData() P3->OUT |= 1 << 0; /* make DC P3.0 high */ #define ILI9340_assertSS() P6->OUT &= ~(1 << 7); /* assert /SS P6.7 */ #define ILI9340_deassertSS() P6->OUT |= 1 << 7; /* deassert /SS P6.7 */ #define ILI9340_reset() P2->OUT &= ~(1 << 6); /* make RESET P2.6 low */ #define ILI9340_dereset() P2->OUT |= 1 << 6; /* make RESET P2.6 high */ void SPI_init(void); void SPI_write(unsigned char data); void ILI9340_hardReset(void); void ILI9340_command(uint8_t command); void ILI9340_data(uint8_t data); void ILI9340_blockData(int32_t n, uint16_t color); void delayMs(int32_t n); void ILI9340_setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void ILI9340_init(void); // define block size #define W 10 #define H 10 int32_t main(void) { uint32_t i, j; uint16_t color; ILI9340_init(); while(1) { for (i = 0; i < TFTHEIGHT / H; i++) { for (j = 0; j < TFTWIDTH / W; j++) { ILI9340_setWindow(j * W, i * H, j * W + W - 1, i * H + H - 1); ILI9340_blockData(W * H, color++); } } } } // Initialize SPI for TFT controller void SPI_init(void) { EUSCI_B0->CTLW0 = 0x0001; /* soft reset */ EUSCI_B0->CTLW0 = 0xE9C1; /* SPI master PH=1, PL=1 */ EUSCI_B0->CTLW0 &= ~0x0001; /* out of reset */ P1->SEL0 |= (1 << 5) | (1 << 6); /* P1.5, P1.6 for UCB0 */ P1->SEL1 &= ~((1 << 5) | (1 << 6)); /* P6.7 as slave select */ P6->DIR |= 1 << 7; /* P6.7 set as output */ P6->OUT |= 1 << 7; /* /SS idle high */ /* P3.0 as DC */ P3->DIR |= 1 << 0; /* P3.0 set as output */ P3->OUT |= 1 << 0; /* /SS idle high */ /* P2.6 as RESET */ P2->DIR |= 1 << 6; /* P2.6 set as output */ P2->OUT |= 1 << 6; /* /SS idle high */ } // write data to SPI void SPI_write(unsigned char data) { ILI9340_assertSS(); /* assert slave select */ EUSCI_B0->TXBUF = data; /* write data */ while(EUSCI_B0->STATW & 0x01) ; /* wait for transmit done */ ILI9340_deassertSS(); /* deassert slave select */ } // hardware reset of TFT controller void ILI9340_hardReset(void) { delayMs(2000); ILI9340_reset(); delayMs(1); ILI9340_dereset(); delayMs(2000); } // send a command to TFT controller void ILI9340_command(uint8_t command) { ILI9340_setCommand(); ILI9340_assertSS(); SPI_write(command); ILI9340_deassertSS(); } // send a data to TFT controller void ILI9340_data(uint8_t data) { ILI9340_setData(); ILI9340_assertSS(); SPI_write(data); ILI9340_deassertSS(); } // send a block of data to TFT controller void ILI9340_blockData(int32_t n, uint16_t color) { ILI9340_setData(); ILI9340_assertSS(); while (n--) { SPI_write(color >> 8); SPI_write(color & 0xFF); } ILI9340_deassertSS(); } // set a window in the display to receive data void ILI9340_setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { ILI9340_command(ILI9340_CASET); // Column addr set ILI9340_data(x0 >> 8); // XSTART ILI9340_data(x0 & 0xFF); ILI9340_data(x1 >> 8); // XEND ILI9340_data(x1 & 0xFF); ILI9340_command(ILI9340_PASET); // Row addr set ILI9340_data(y0>>8); // YSTART ILI9340_data(y0); ILI9340_data(y1>>8); // YEND ILI9340_data(y1); ILI9340_command(ILI9340_RAMWR); // write to RAM } // initialize the TFT controller void ILI9340_init(void) { SPI_init(); ILI9340_hardReset(); ILI9340_command(0xEF); ILI9340_data(0x03); ILI9340_data(0x80); ILI9340_data(0x02); ILI9340_command(0xCF); ILI9340_data(0x00); ILI9340_data(0xC1); ILI9340_data(0x30); ILI9340_command(0xED); ILI9340_data(0x64); ILI9340_data(0x03); ILI9340_data(0x12); ILI9340_data(0x81); ILI9340_command(0xE8); ILI9340_data(0x85); ILI9340_data(0x00); ILI9340_data(0x78); ILI9340_command(0xCB); ILI9340_data(0x39); ILI9340_data(0x2C); ILI9340_data(0x00); ILI9340_data(0x34); ILI9340_data(0x02); ILI9340_command(0xF7); ILI9340_data(0x20); ILI9340_command(0xEA); ILI9340_data(0x00); ILI9340_data(0x00); ILI9340_command(ILI9340_PWCTR1); //Power control ILI9340_data(0x23); //VRH[5:0] ILI9340_command(ILI9340_PWCTR2); //Power control ILI9340_data(0x10); //SAP[2:0];BT[3:0] ILI9340_command(ILI9340_VMCTR1); //VCM control ILI9340_data(0x3e); ILI9340_data(0x28); ILI9340_command(ILI9340_VMCTR2); //VCM control2 ILI9340_data(0x86); ILI9340_command(ILI9340_MADCTL); // Memory Access Control ILI9340_data(ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); ILI9340_command(ILI9340_PIXFMT); ILI9340_data(0x55); ILI9340_command(ILI9340_FRMCTR1); ILI9340_data(0x00); ILI9340_data(0x18); ILI9340_command(ILI9340_DFUNCTR); // Display Function Control ILI9340_data(0x08); ILI9340_data(0x82); ILI9340_data(0x27); ILI9340_command(0xF2); // 3Gamma Function Disable ILI9340_data(0x00); ILI9340_command(ILI9340_GAMMASET); //Gamma curve selected ILI9340_data(0x01); ILI9340_command(ILI9340_GMCTRP1); //Set Gamma ILI9340_data(0x0F); ILI9340_data(0x31); ILI9340_data(0x2B); ILI9340_data(0x0C); ILI9340_data(0x0E); ILI9340_data(0x08); ILI9340_data(0x4E); ILI9340_data(0xF1); ILI9340_data(0x37); ILI9340_data(0x07); ILI9340_data(0x10); ILI9340_data(0x03); ILI9340_data(0x0E); ILI9340_data(0x09); ILI9340_data(0x00); ILI9340_command(ILI9340_GMCTRN1); //Set Gamma ILI9340_data(0x00); ILI9340_data(0x0E); ILI9340_data(0x14); ILI9340_data(0x03); ILI9340_data(0x11); ILI9340_data(0x07); ILI9340_data(0x31); ILI9340_data(0xC1); ILI9340_data(0x48); ILI9340_data(0x08); ILI9340_data(0x0F); ILI9340_data(0x0C); ILI9340_data(0x31); ILI9340_data(0x36); ILI9340_data(0x0F); ILI9340_command(ILI9340_SLPOUT); //Exit Sleep delayMs(120); ILI9340_command(ILI9340_DISPON); //Display on } /* system clock at 3 MHz, MSP432P401R Rev. C, Start-up v2.2.1 */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* Delay */ }