ILI9341 TFT-Display mit MSP430

ILI9341_00Bis jetzt finden sich hier auf der Seite nur Beispielprogramme für monochrome Displays bzw. LCDs. Da ich schon länger mal mal mit einem “richtigen” Display arbeiten wollte, also etwas in Farbe programmieren, habe ich mir auf Ebay ein kleines 2.2″ TFT-Display, mit einem ILI9341 Controller, bestellt. Das Display hat zu dem Kaufzeitpunkt mal gerade nur 5,69€ gekostet. Der aktuelle Preis von 6,00€ ist narülich auch noch super billig. Die Ansteuerung des Displays hat sich als ziemlich einfach gestaltet. Es kann direkt per SPI angesteuert werden und es gibt ein vernünftiges Datenblatt mit allen Informationen zur Initialisierung etc. In den letzten Tagen habe ich eine kleine Demo für das ILI9341 Display geschrieben (in Verbindung mit einem MSP430G2553).

Video

Display-Infos

Aus den Ebay-Angaben:

  • Maße: 2.2″
  • Punkt Matrix: QVGA 240 x 320 Punkte 
  • Schnittstelle: 4-Draht SPI
  • Farbtiefe: 262K/65K (24bit/16bit)
  • Display: 67mm(L)*40mm(B)*4mm(D)
  • Aktivbereich: 47.5mm(L)x36.5mm(B)
  • 4 LEDs

Download ILI9341 Documentation

Bilder

ILI9341_01ILI9341_02ILI9341_03ILI9341_04

Schaltplan

Alle 4 LEDs sind parallel geschaltet. Für eine LED gilt: If=15mA, Vf=3,2V. Somit beträgt der gesamte Strom 60mA. Da ich nicht die genauen Widerstandswerte auf Lager hatte, habe ich einfach einen 150ohm Vorwiderstand genommen, somit fließt ein Gesamtstrom von etwa 14,22mA (5V -> 150ohm -> LEDs). Die Helligkeit war für meine Zwecke ausreichend. Da sich ein richtiger Schaltplan hier nicht wirklich lohnt, gibt es also nur die Pinbelegung aus dem Code:

#define CS              0x0010        // chip select at P1.4
#define SDIN            0x0080        // data in at P1.7
#define SCLK            0x0020        // clk at P1.5
#define DC              0x0040        // Data/Cmd at P1.6
#define RES             0x0001        // reset at P1.0

Source-Code

Kompletter Source-Code (07.12.2014): SOURCE-CODE

Ein Ausschnitt der Initialisierung:

init_display.c

  1. /*******************************
  2.  *          __                   
  3.  *         /\ \                  
  4.  *  __  _  \_\ \     __    ___   
  5.  * /\ \/'\ /'_' \  /'__'\ /'___\
  6.  * \/>  <//\ \ \ \/\  __//\ \__/
  7.  *  /\_/\_\ \___,_\ \____\ \____\
  8.  *  \//\/_/\/__,_ /\/____/\/____/
  9.  *
  10.  *  Author: declis (xdec.de)
  11.  ********************************/
  12.  
  13. #include <msp430g2553.h>
  14. #include "typedef.h"
  15. #include "lib_lcd.h"
  16. #include "delay.h"
  17.  
  18. // see init_display.h for command description
  19. const uchar init_cmd[]={0xCB,0xCF,0xE8,0xEA,0xED,0xF7,0xC0,0xC1,0xC5,0xC7,0x36,0x3A,0xB1,0xB6,0xF2,0x26,0xE0,0xE1};
  20. const uchar init_data_count[]={5,3,3,2,4,1,1,1,2,1,1,1,2,3,1,1,15,15};
  21. const uchar init_data[]={0x39,0x2C,0x00,0x34,0x02,
  22.                         0x00,0xC1,0x30,
  23.                         0x85,0x00,0x78,
  24.                         0x00,0x00,
  25.                         0x64,0x03,0x12,0x81,
  26.                         0x20,
  27.                         0x23,
  28.                         0x10,
  29.                         0x3E,0x28,
  30.                         0x86,
  31.                         0x88,
  32.                         0x55,
  33.                         0x00,0x18,
  34.                         0x08,0x82,0x27,
  35.                         0x00,
  36.                         0x01,
  37.                         0x0F,0x31,0x2B,0x0C,0x0E,0x08,0x4E,0xF1,0x37,0x07,0x10,0x03,0x0E,0x09,0x00,
  38.                         0x00,0x0E,0x14,0x03,0x11,0x07,0x31,0xC1,0x48,0x08,0x0F,0x0C,0x31,0x36,0x0F};
  39.                        
  40. void init_LCD(uint b_color)
  41. {
  42.     uchar byte=0,bit_num=0,h_index=0,w_index=0;
  43.  
  44.     P1OUT|=RES;
  45.     wait_ms(10);
  46.     P1OUT&=~RES;
  47.     wait_ms(10);
  48.     P1OUT|=RES;
  49.     wait_ms(100);
  50.    
  51.     while(byte<sizeof(init_cmd))                        // byte is init_cmd index
  52.     {
  53.         set_instruction(0,init_cmd[byte++]);           
  54.         while(h_index<init_data_count[bit_num])         // h_index is size of init_data command part counter
  55.         {                                               // bit_num is init_data_count index
  56.             set_instruction(1,init_data[w_index++]);    // w_index is init_data index
  57.             h_index++;
  58.         }
  59.         bit_num++;
  60.         h_index=0;
  61.     }
  62.    
  63.     set_instruction(0,0x11);    // SLEEP OUT
  64.     wait_ms(120);               // 120ms needed (see description of sleep-out-cmd)
  65.    
  66.     MEM_WRITE;
  67.     fill_display(lcd_width,lcd_height,b_color);
  68.     DISPLAY_ON;
  69.     MEM_WRITE;
  70. }
  71.  
  72. void init_USCI(void)
  73. {
  74.     P1DIR|=RES+DC+CS;
  75.     P1OUT&=~RES+DC+CS;
  76.    
  77.     P1SEL|=SDIN+SCLK;
  78.     P1SEL2|=SDIN+SCLK;
  79.    
  80.     UCB0CTL1|=UCSWRST;                  // USCI in reset state
  81.     // SPI Master, 8bit, MSB first, synchronous mode
  82.     UCB0CTL0|=UCMST+UCSYNC+UCCKPH+UCMSB;       
  83.     UCB0CTL1|=UCSSEL_2;                 // USCI CLK-SRC=SMCLK=~8MHz
  84.     UCB0CTL1&=~UCSWRST;                 // USCI released for operation
  85.     IE2|=UCB0TXIE;                      // enable TX interrupt
  86.     IFG2&=~UCB0TXIFG;
  87.     _EINT();                            // enable interrupts
  88. }
  89.  
  90. #pragma INTERRUPT (USCI)
  91. #pragma vector=USCIAB0TX_VECTOR
  92. void USCI(void)
  93. {
  94.     P1OUT|=CS;              // transmission done
  95.     IFG2&=~UCB0TXIFG;       // clear TXIFG
  96. }  
/*******************************
 *          __                   
 *         /\ \                  
 *  __  _  \_\ \     __    ___   
 * /\ \/'\ /'_' \  /'__'\ /'___\ 
 * \/>  <//\ \ \ \/\  __//\ \__/ 
 *  /\_/\_\ \___,_\ \____\ \____\
 *  \//\/_/\/__,_ /\/____/\/____/
 * 
 *  Author: declis (xdec.de)
 ********************************/ 

#include <msp430g2553.h>
#include "typedef.h"
#include "lib_lcd.h"
#include "delay.h"

// see init_display.h for command description
const uchar init_cmd[]={0xCB,0xCF,0xE8,0xEA,0xED,0xF7,0xC0,0xC1,0xC5,0xC7,0x36,0x3A,0xB1,0xB6,0xF2,0x26,0xE0,0xE1};
const uchar init_data_count[]={5,3,3,2,4,1,1,1,2,1,1,1,2,3,1,1,15,15};
const uchar init_data[]={0x39,0x2C,0x00,0x34,0x02,
						0x00,0xC1,0x30,
						0x85,0x00,0x78,
						0x00,0x00,
						0x64,0x03,0x12,0x81,
						0x20,
						0x23,
						0x10,
						0x3E,0x28,
						0x86,
						0x88,
						0x55,
						0x00,0x18,
						0x08,0x82,0x27,
						0x00,
						0x01,
						0x0F,0x31,0x2B,0x0C,0x0E,0x08,0x4E,0xF1,0x37,0x07,0x10,0x03,0x0E,0x09,0x00,
						0x00,0x0E,0x14,0x03,0x11,0x07,0x31,0xC1,0x48,0x08,0x0F,0x0C,0x31,0x36,0x0F};
						
void init_LCD(uint b_color)
{
	uchar byte=0,bit_num=0,h_index=0,w_index=0;

	P1OUT|=RES;
	wait_ms(10);
	P1OUT&=~RES;
	wait_ms(10);
	P1OUT|=RES;
	wait_ms(100);
	
	while(byte<sizeof(init_cmd))						// byte is init_cmd index
	{
		set_instruction(0,init_cmd[byte++]);			
		while(h_index<init_data_count[bit_num])			// h_index is size of init_data command part counter
		{												// bit_num is init_data_count index
			set_instruction(1,init_data[w_index++]);	// w_index is init_data index
			h_index++;
		}
		bit_num++;
		h_index=0;
	}
    
	set_instruction(0,0x11); 	// SLEEP OUT
	wait_ms(120);				// 120ms needed (see description of sleep-out-cmd)
	
	MEM_WRITE;
    fill_display(lcd_width,lcd_height,b_color);
    DISPLAY_ON;
    MEM_WRITE;
}

void init_USCI(void)
{
	P1DIR|=RES+DC+CS;
	P1OUT&=~RES+DC+CS;
	
	P1SEL|=SDIN+SCLK;
	P1SEL2|=SDIN+SCLK;
	
  	UCB0CTL1|=UCSWRST;					// USCI in reset state
  	// SPI Master, 8bit, MSB first, synchronous mode
  	UCB0CTL0|=UCMST+UCSYNC+UCCKPH+UCMSB;		
  	UCB0CTL1|=UCSSEL_2;					// USCI CLK-SRC=SMCLK=~8MHz
  	UCB0CTL1&=~UCSWRST;					// USCI released for operation
  	IE2|=UCB0TXIE;						// enable TX interrupt
  	IFG2&=~UCB0TXIFG;
  	_EINT();							// enable interrupts
}

#pragma INTERRUPT (USCI)
#pragma vector=USCIAB0TX_VECTOR
void USCI(void)
{
	P1OUT|=CS;				// transmission done
	IFG2&=~UCB0TXIFG;		// clear TXIFG
}	

init_display.h

  1. /*******************************
  2.  *          __                   
  3.  *         /\ \                  
  4.  *  __  _  \_\ \     __    ___   
  5.  * /\ \/'\ /'_' \  /'__'\ /'___\
  6.  * \/>  <//\ \ \ \/\  __//\ \__/
  7.  *  /\_/\_\ \___,_\ \____\ \____\
  8.  *  \//\/_/\/__,_ /\/____/\/____/
  9.  *
  10.  *  Author: declis (xdec.de)
  11.  ********************************/
  12.  
  13. #ifndef INIT_DISPLAY_H_
  14. #define INIT_DISPLAY_H_
  15.  
  16. void init_LCD(uint);
  17. void init_USCI(void);
  18.  
  19. /*
  20.     set_instruction(0,0xCB);    // POWER CONTROL A
  21.     set_instruction(1,0x39);    // 1. (default)
  22.     set_instruction(1,0x2C);    // 2. (default)
  23.     set_instruction(1,0x00);    // 3. (default)
  24.     set_instruction(1,0x34);    // 4. (Vcore Controll: 1.55V, default)
  25.     set_instruction(1,0x02);    // 5. (DDVDH: 5.8V, default)
  26.    
  27.     set_instruction(0,0xCF);    // POWER CONTROL B
  28.     set_instruction(1,0x00);    // 1. (default)
  29.     set_instruction(1,0xC1);    // 2. (PC&EQ operation for power saving enabled), 0x81 def.
  30.     set_instruction(1,0x30);    // 3. (default)
  31.    
  32.     set_instruction(0,0xE8);    // DRIVER TIMING CONTROL A
  33.     set_instruction(1,0x85);    // 1. (gate driver non-overlap timing control), 0x84 def.
  34.     set_instruction(1,0x00);    // 2. (EQ timing control), 0x11 def.
  35.     set_instruction(1,0x78);    // 3. (pre-charge timing control), 0x7A def.
  36.    
  37.     set_instruction(0,0xEA);    // DRIVER TIMING CONTROL A
  38.     set_instruction(1,0x00);    // 1. (gate driver timing control), 0x66 def.
  39.     set_instruction(1,0x00);    // 2. (default)
  40.    
  41.     set_instruction(0,0xED);    // POWER ON SEQUENCE CONTROL
  42.     set_instruction(1,0x64);    // 1. (soft start control), 0x55 def.
  43.     set_instruction(1,0x03);    // 2. (power on sequence control), 0x01 def.
  44.     set_instruction(1,0x12);    // 3. (power on sequence control), 0x23 def.
  45.     set_instruction(1,0x81);    // 4. (DDVDH enhance mode(only for 8 external capacitors)), enabled, 0x01 def.
  46.    
  47.     set_instruction(0,0xF7);    // PUMP RATION CONTROL
  48.     set_instruction(1,0x20);    // 1. (ratio control)
  49.    
  50.     set_instruction(0,0xC0);    // POWER CONTROL 1
  51.     set_instruction(1,0x23);    // 1. (set the GVDD level, 4.6V), 0x21 def.
  52.    
  53.     set_instruction(0,0xC1);    // POWER CONTROL 2
  54.     set_instruction(1,0x10);    // 1. (sets the factor used in the step-up circuits) default
  55.    
  56.     set_instruction(0,0xC5);    // VCOM CONTROL 1
  57.     set_instruction(1,0x3e);    // 1. (VCOMH voltage = 5.85V), 0x31 def.
  58.     set_instruction(1,0x28);    // 2. (VCOML voltage = -1.50V), 0x3C def.
  59.    
  60.     set_instruction(0,0xC7);    // VCOM CONTROL 2
  61.     set_instruction(1,0x86);    // 1. (VCOM offset voltage), 0xC0 def.
  62.    
  63.     set_instruction(0,0x36);    // MEMORY ACCESS CONTROL
  64.     set_instruction(1,0x80);    // 1. ( ), 0x00 def.
  65.    
  66.     set_instruction(0,0x3A);    // COLMOD: PIXEL FORMAT SET
  67.     set_instruction(1,0x55);    // 1. (sets the pixel format for the RGB image data used by the interface, 16bits/pixel)
  68.    
  69.     set_instruction(0,0xB1);    // FRAME RATE CONTROL (IN NORMAL MODE / FULL COLORS)
  70.     set_instruction(1,0x00);    // 1. (division ratio for internal clocks when Normal mode), default
  71.     set_instruction(1,0x18);    // 2. (frame frequency, 79Hz), 0x1B def. (70Hz)
  72.    
  73.     set_instruction(0,0xB6);    // DISPLAY FUNCTION CONTROL
  74.     set_instruction(1,0x08);    // 1. (), 0x0A def.
  75.     set_instruction(1,0x82);    // 2. (default)
  76.     set_instruction(1,0x27);    // 3. (default)
  77.    
  78.     set_instruction(0,0xF2);    // 3GAMMA CONTROL
  79.     set_instruction(1,0x00);    // 1. (disabled), 0x02 def.
  80.    
  81.     set_instruction(0,0x26);    // GAMMA SET
  82.     set_instruction(1,0x01);    // 1. (default)
  83.    
  84.     set_instruction(0,0xE0);    // POSITIVE GAMMA CORRECTION
  85.     set_instruction(1,0x0F);    // 1.
  86.     set_instruction(1,0x31);    // 2.
  87.     set_instruction(1,0x2B);    // 3.
  88.     set_instruction(1,0x0C);    // 4.
  89.     set_instruction(1,0x0E);    // 5.
  90.     set_instruction(1,0x08);    // 6.
  91.     set_instruction(1,0x4E);    // 7.
  92.     set_instruction(1,0xF1);    // 8.
  93.     set_instruction(1,0x37);    // 9.
  94.     set_instruction(1,0x07);    // 10.
  95.     set_instruction(1,0x10);    // 11.
  96.     set_instruction(1,0x03);    // 12.
  97.     set_instruction(1,0x0E);    // 13.
  98.     set_instruction(1,0x09);    // 14.
  99.     set_instruction(1,0x00);    // 15.
  100.    
  101.     set_instruction(0,0xE1);    // NEGATIVE GAMMA CORRECTION
  102.     set_instruction(1,0x00);    // 1.
  103.     set_instruction(1,0x0E);    // 2.
  104.     set_instruction(1,0x14);    // 3.
  105.     set_instruction(1,0x03);    // 4.
  106.     set_instruction(1,0x11);    // 5.
  107.     set_instruction(1,0x07);    // 6.
  108.     set_instruction(1,0x31);    // 7.
  109.     set_instruction(1,0xC1);    // 8.
  110.     set_instruction(1,0x48);    // 9.
  111.     set_instruction(1,0x08);    // 10.
  112.     set_instruction(1,0x0F);    // 11.
  113.     set_instruction(1,0x0C);    // 12.
  114.     set_instruction(1,0x31);    // 13.
  115.     set_instruction(1,0x36);    // 14.
  116.     set_instruction(1,0x0F);    // 15.
  117. */
  118.  
  119. #endif /*INIT_DISPLAY_H_*/
/*******************************
 *          __                   
 *         /\ \                  
 *  __  _  \_\ \     __    ___   
 * /\ \/'\ /'_' \  /'__'\ /'___\ 
 * \/>  <//\ \ \ \/\  __//\ \__/ 
 *  /\_/\_\ \___,_\ \____\ \____\
 *  \//\/_/\/__,_ /\/____/\/____/
 * 
 *  Author: declis (xdec.de)
 ********************************/ 

#ifndef INIT_DISPLAY_H_
#define INIT_DISPLAY_H_

void init_LCD(uint);
void init_USCI(void);

/*
	set_instruction(0,0xCB);	// POWER CONTROL A
	set_instruction(1,0x39);	// 1. (default)
	set_instruction(1,0x2C);	// 2. (default)
	set_instruction(1,0x00);	// 3. (default)
	set_instruction(1,0x34);	// 4. (Vcore Controll: 1.55V, default)
	set_instruction(1,0x02);	// 5. (DDVDH: 5.8V, default)
    
	set_instruction(0,0xCF);	// POWER CONTROL B
	set_instruction(1,0x00);	// 1. (default)
	set_instruction(1,0xC1);	// 2. (PC&EQ operation for power saving enabled), 0x81 def.
	set_instruction(1,0x30);	// 3. (default)
    
	set_instruction(0,0xE8);	// DRIVER TIMING CONTROL A
	set_instruction(1,0x85);	// 1. (gate driver non-overlap timing control), 0x84 def.
	set_instruction(1,0x00);	// 2. (EQ timing control), 0x11 def.
	set_instruction(1,0x78);	// 3. (pre-charge timing control), 0x7A def.
    
	set_instruction(0,0xEA);	// DRIVER TIMING CONTROL A
	set_instruction(1,0x00);	// 1. (gate driver timing control), 0x66 def.
	set_instruction(1,0x00);	// 2. (default)
    
	set_instruction(0,0xED);	// POWER ON SEQUENCE CONTROL
	set_instruction(1,0x64);	// 1. (soft start control), 0x55 def.
	set_instruction(1,0x03);	// 2. (power on sequence control), 0x01 def.
	set_instruction(1,0x12);	// 3. (power on sequence control), 0x23 def.
	set_instruction(1,0x81);	// 4. (DDVDH enhance mode(only for 8 external capacitors)), enabled, 0x01 def.
    
	set_instruction(0,0xF7);	// PUMP RATION CONTROL
	set_instruction(1,0x20);	// 1. (ratio control)
    
	set_instruction(0,0xC0); 	// POWER CONTROL 1
	set_instruction(1,0x23); 	// 1. (set the GVDD level, 4.6V), 0x21 def.
    
	set_instruction(0,0xC1); 	// POWER CONTROL 2
	set_instruction(1,0x10); 	// 1. (sets the factor used in the step-up circuits) default
    
	set_instruction(0,0xC5); 	// VCOM CONTROL 1
	set_instruction(1,0x3e); 	// 1. (VCOMH voltage = 5.85V), 0x31 def.
	set_instruction(1,0x28);	// 2. (VCOML voltage = -1.50V), 0x3C def.
    
	set_instruction(0,0xC7); 	// VCOM CONTROL 2
	set_instruction(1,0x86); 	// 1. (VCOM offset voltage), 0xC0 def.
    
	set_instruction(0,0x36); 	// MEMORY ACCESS CONTROL
	set_instruction(1,0x80); 	// 1. ( ), 0x00 def.
    
	set_instruction(0,0x3A);	// COLMOD: PIXEL FORMAT SET
	set_instruction(1,0x55);	// 1. (sets the pixel format for the RGB image data used by the interface, 16bits/pixel)
    
	set_instruction(0,0xB1);	// FRAME RATE CONTROL (IN NORMAL MODE / FULL COLORS)
	set_instruction(1,0x00);	// 1. (division ratio for internal clocks when Normal mode), default
	set_instruction(1,0x18);	// 2. (frame frequency, 79Hz), 0x1B def. (70Hz)
    
	set_instruction(0,0xB6); 	// DISPLAY FUNCTION CONTROL
	set_instruction(1,0x08);	// 1. (), 0x0A def.
	set_instruction(1,0x82);	// 2. (default)
	set_instruction(1,0x27);	// 3. (default)
    
	set_instruction(0,0xF2); 	// 3GAMMA CONTROL
	set_instruction(1,0x00);	// 1. (disabled), 0x02 def.
    
	set_instruction(0,0x26);	// GAMMA SET
	set_instruction(1,0x01);	// 1. (default)
    
	set_instruction(0,0xE0); 	// POSITIVE GAMMA CORRECTION
	set_instruction(1,0x0F);	// 1.
	set_instruction(1,0x31);	// 2.
	set_instruction(1,0x2B);	// 3.
	set_instruction(1,0x0C);	// 4.
	set_instruction(1,0x0E);	// 5.
	set_instruction(1,0x08);	// 6.
	set_instruction(1,0x4E);	// 7.
	set_instruction(1,0xF1);	// 8.
	set_instruction(1,0x37);	// 9.
	set_instruction(1,0x07);	// 10.
	set_instruction(1,0x10);	// 11.
	set_instruction(1,0x03);	// 12.
	set_instruction(1,0x0E);	// 13.
	set_instruction(1,0x09);	// 14.
	set_instruction(1,0x00);	// 15.
    
	set_instruction(0,0xE1); 	// NEGATIVE GAMMA CORRECTION
	set_instruction(1,0x00);	// 1.
	set_instruction(1,0x0E);	// 2.
	set_instruction(1,0x14);	// 3.
	set_instruction(1,0x03);	// 4.
	set_instruction(1,0x11);	// 5.
	set_instruction(1,0x07);	// 6.
	set_instruction(1,0x31);	// 7.
	set_instruction(1,0xC1);	// 8.
	set_instruction(1,0x48);	// 9.
	set_instruction(1,0x08);	// 10.
	set_instruction(1,0x0F);	// 11.
	set_instruction(1,0x0C);	// 12.
	set_instruction(1,0x31);	// 13.
	set_instruction(1,0x36);	// 14.
	set_instruction(1,0x0F);	// 15.
*/

#endif /*INIT_DISPLAY_H_*/

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.