Sample code for 2.4" TFT - ILI9340 controller.
//--------------------------------------------------------- /* 2.4in_TFT.c Program snippets for writing to Newhaven Display 2.4" TFT with ILI9340 controller (c)2010 Newhaven Display International, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ //------------------------------------------------------- /******************************************************************************* * Function Name : TFT_24S_Write_Command * Description : writes a 1 byte command to 2.4" TFT. * Input : command = one byte command (register address) * Output : None * Return : None *******************************************************************************/ void TFT_24S_Write_Command(unsigned int command) { GPIO_ResetBits(GPIOC, CS1); GPIO_ResetBits(GPIOC, RS); GPIO_SetBits(GPIOC, nRD); GPIO_ResetBits(GPIOC, nWR); GPIO_Write(GPIOB, command); GPIO_SetBits(GPIOC, nWR); TFT_delay(10); } /******************************************************************************* * Function Name : TFT_24S_Write_Data * Description : writes 1 byte of data to 2.4" TFT. * Input : data1 = one byte of display data or command parameter * Output : None * Return : None *******************************************************************************/ void TFT_24S_Write_Data(unsigned int data1) { GPIO_Write(GPIOB, data1); GPIO_SetBits(GPIOC, RS); GPIO_ResetBits(GPIOC, nWR); GPIO_SetBits(GPIOC, nWR); } /******************************************************************************* * Function Name : TFT_24_9340_demo * Description : Loads bmp from SD card and writes to NHD-2.4-240320SF-CTXI#-1. * Input : None * Output : None * Return : 1-end of function reached *******************************************************************************/ int TFT_24_9340_demo(void) { char filename[18] = "2_4_240320SF_"; char filetype[4] = ".bmp"; char filenumber[1] = "0"; int i,n,result; char image = 0x30; unsigned short RGB16[25600]; unsigned char red,green,blue; UINT blen; GPIO_ResetBits(GPIOC, IM0); //16-bit mode memset(RGB16,0x0000,sizeof(RGB16)); TFT_delay(100); TFT_24_9340_Init(); TFT_delay(100); filename[13]='\0'; filenumber[0] = image; strncat(filename,filenumber,1); strncat(filename,filetype,4); while(1){ TFT_24_9340_Init(); UILCD_SetCursor(0,0);UILCD_DisplayString("Reading SD Card ");UILCD_SetCursor(1,0);UILCD_DisplayString(" "); UILCD_SetCursor(1,0);UILCD_DisplayString(filename); if(openSD(filename)==0x30) {while(1);} seekSD(54); TFT_24S_Write_Command(0x002C); //Memory write for (n=0;n<3;n++){ memset(RGB16,0x0000,sizeof(RGB16)); for (i=0;i<25600;i++) //for each 24-bit pixel... { f_read(&File1, &blue, 1, &blen); //read the blue 8-bits f_read(&File1, &green, 1, &blen); //read the green 8-bits f_read(&File1, &red, 1, &blen); //read the red 8-bits red=red>>3; //shift down to 5-bits green=green>>2; //shift down to 6-bits blue=blue>>3; //shift down to 5-bits RGB16[i]= (RGB16[i] | red); //put red 5-bits into int RGB16[i]= (RGB16[i] << 6); //move red bits over, make room for green RGB16[i]= (RGB16[i] | green); //put green 6-bits into int RGB16[i]= (RGB16[i] << 5); //move red and green bits over, make room for blue RGB16[i]= (RGB16[i] | blue); //put blue 5-bits into int } GPIO_SetBits(GPIOC, RS); for(i=0;i<25600;i++){ GPIO_Write(GPIOB, (RGB16[i])); GPIO_ResetBits(GPIOC, nWR); GPIO_SetBits(GPIOC, nWR); } } TFT_24S_Write_Command(0x0029); //display ON result = f_close(&File1); // close file if (result != FR_OK) { UILCD_SetCursor(0,0);UILCD_DisplayString("File not Closed ");while(1); return result;} UILCD_SetCursor(0,0);UILCD_DisplayString("Press SEL button");UILCD_SetCursor(1,0);UILCD_DisplayString("for next file. "); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)!=0){;} image++; if(image==0x3A){image=0x41;} filename[13]='\0'; filenumber[0] = image; strncat(filename,filenumber,1); strncat(filename,filetype,4); if(openSD(filename)==0x30) {image=0x30;} filename[13]='\0'; filenumber[0] = image; strncat(filename,filenumber,1); strncat(filename,filetype,4); } return 1; } /******************************************************************************* * Function Name : TFT_24_9340_Init * Description : Initializes LCD with built-in ILI9340 controller. * Input : None * Output : None * Return : None *******************************************************************************/ void TFT_24_9340_Init(void) { GPIO_ResetBits(GPIOC, CS1); GPIO_SetBits(GPIOC, nRD); GPIO_ResetBits(GPIOC, nWR); GPIO_WriteBit(GPIOC, RES, Bit_RESET); TFT_delay(100); GPIO_WriteBit(GPIOC, RES, Bit_SET); TFT_delay(100); TFT_24S_Write_Command(0x0028);//display OFF TFT_24S_Write_Command(0x0011);TFT_24S_Write_Data(0x0000); //exit SLEEP mode TFT_24S_Write_Command(0x00C0);TFT_24S_Write_Data(0x0026);TFT_24S_Write_Data(0x0004); //power control 1 TFT_24S_Write_Command(0x00C1);TFT_24S_Write_Data(0x0004); //power control 2 TFT_24S_Write_Command(0x00C5);TFT_24S_Write_Data(0x0034);TFT_24S_Write_Data(0x0040); //VCOM control 1 TFT_24S_Write_Command(0x0036);TFT_24S_Write_Data(0x0088); //memory access control = BGR TFT_24S_Write_Command(0x00B1);TFT_24S_Write_Data(0x0000);TFT_24S_Write_Data(0x0018); //frame rate control TFT_24S_Write_Command(0x00B6);TFT_24S_Write_Data(0x000A);TFT_24S_Write_Data(0x00A2); //display function control TFT_24S_Write_Command(0x00C7);TFT_24S_Write_Data(0x00C0); //VCOM control 2 TFT_24S_Write_Command(0x003A);TFT_24S_Write_Data(0x0055); //pixel format = 16 bit per pixel TFT_24S_Write_Command(0x00E0);TFT_24S_Write_Data(0x001F); //positive gamma correction TFT_24S_Write_Data(0x001B); TFT_24S_Write_Data(0x0018); TFT_24S_Write_Data(0x000B); TFT_24S_Write_Data(0x000F); TFT_24S_Write_Data(0x0009); TFT_24S_Write_Data(0x0046); TFT_24S_Write_Data(0x00B5); TFT_24S_Write_Data(0x0037); TFT_24S_Write_Data(0x000A); TFT_24S_Write_Data(0x000C); TFT_24S_Write_Data(0x0007); TFT_24S_Write_Data(0x0007); TFT_24S_Write_Data(0x0005); TFT_24S_Write_Data(0x0000); TFT_24S_Write_Command(0x00E1);TFT_24S_Write_Data(0x0000); //negative gamma correction TFT_24S_Write_Data(0x0024); TFT_24S_Write_Data(0x0027); TFT_24S_Write_Data(0x0004); TFT_24S_Write_Data(0x0010); TFT_24S_Write_Data(0x0006); TFT_24S_Write_Data(0x0039); TFT_24S_Write_Data(0x0074); TFT_24S_Write_Data(0x0048); TFT_24S_Write_Data(0x0005); TFT_24S_Write_Data(0x0013); TFT_24S_Write_Data(0x0038); TFT_24S_Write_Data(0x0038); TFT_24S_Write_Data(0x003A); TFT_24S_Write_Data(0x001F); TFT_24S_Write_Command(0x002A);TFT_24S_Write_Data(0x0000); //column address set TFT_24S_Write_Data(0x0000); //start 0x0000 TFT_24S_Write_Data(0x0000); TFT_24S_Write_Data(0x00EF); //end 0x00EF TFT_24S_Write_Command(0x002B);TFT_24S_Write_Data(0x0000); //page address set TFT_24S_Write_Data(0x0000); //start 0x0000 TFT_24S_Write_Data(0x0001); TFT_24S_Write_Data(0x003F); //end 0x013F } /*-------------------------------------------------------*/