TFT Display Initialization for AVR, Using the ILI9341 as an Example.
About a year ago an article appeared on this site about how to initialize a TFT display controlled by the SSD1289, and about a month ago one of the site's visitors wrote to me. The gist of the letter was that he had ordered a display via the link in the article but couldn't get it working, and he offered to send me this display and several others, and I, in turn, would publish the code if I managed to get them working.
As it turned out, the display he was sent from AliExpress is controlled by the ILI9341 driver; the marking on it helped me figure that out.
The upper display has the SSD1289 driver, the lower one the ILI9341.

So as soon as this display arrived, I started studying the ILI9341 datasheet.
Unlike the SSD1289, the ILI9341 has no registers; commands are used to communicate with it. First you send a command and then a set of parameters, i.e. after we've sent a command, the display is already waiting for the parameters. I should also say that communication with the display uses one of two protocols: Intel's i8080 and Motorola's M6800; I described how they differ here. I didn't break with tradition and chose i8080. There are two implementations of this protocol; for more details you can read the datasheet.
First we need to implement the low-level functions, and here it's important to make sure we understand the datasheet correctly. The easiest way to check this is to read some information from the display, for example its ID.

The picture shows that if you send the controller the 0xD3h command and read data 4 times, the last two reads should return the driver ID (9341). But first we need to figure out how to send commands and read data; for this we'll need three pictures from the datasheet.
The first defines the control signals for the selected interface.

The second and third define their sequence for writing
and for reading

Then the function for sending a command will look like this.
And the function that reads data looks like this.
The screenshots show that the functions described above work; all that's left is to implement sending 8-bit and 16-bit data. The thing is, mostly only the lower 8 bits of the bus are used when sending data, but, for example, when writing data to the display memory, all 16 bits are used.
That's it, we've implemented all the necessary low-level functions; now we can move on to the initialization itself. Before moving on, I should say that the supply voltage of an LCD cell constantly reverses polarity; this is done to avoid hydrolysis and dissociation of the complex organic compounds the liquid crystal material consists of.
The initialization function contains the TFT_SetOrientation(2) function, which determines the drawing order; MAX_X and MAX_Y are declared globally and initialized to zero.
And here is what the display looks like after initialization.
To make sure it really initialized, I filled it with red.
I'll describe how to do this in the next article.
As for the wiring diagram: since the pinout of the 40-pin SSD1289 and ILI9341 displays is the same, I left the wiring diagram unchanged, and it looks like this.

This way you can make one development board for both displays. It's also important that the supply voltage of the MCU and the display is 3.3 volts; when powered from 5 volts, the display didn't start. I also powered the display backlight from 3.3 volts through a 47 Ohm resistor.
That's all; in the next article we'll look at how to display characters, and there will also be an Atmega16 project for AtmelStudio6.2.
For those who want to quickly check whether their display works, here is firmware for the Atmega16 that fills the display with different colors ili9341_i8080.hex.
I recently bought a couple of displays here.
As it turned out, the display he was sent from AliExpress is controlled by the ILI9341 driver; the marking on it helped me figure that out.
The upper display has the SSD1289 driver, the lower one the ILI9341.

So as soon as this display arrived, I started studying the ILI9341 datasheet.
Unlike the SSD1289, the ILI9341 has no registers; commands are used to communicate with it. First you send a command and then a set of parameters, i.e. after we've sent a command, the display is already waiting for the parameters. I should also say that communication with the display uses one of two protocols: Intel's i8080 and Motorola's M6800; I described how they differ here. I didn't break with tradition and chose i8080. There are two implementations of this protocol; for more details you can read the datasheet.
First we need to implement the low-level functions, and here it's important to make sure we understand the datasheet correctly. The easiest way to check this is to read some information from the display, for example its ID.

The picture shows that if you send the controller the 0xD3h command and read data 4 times, the last two reads should return the driver ID (9341). But first we need to figure out how to send commands and read data; for this we'll need three pictures from the datasheet.
The first defines the control signals for the selected interface.

The second and third define their sequence for writing

and for reading

Then the function for sending a command will look like this.
void TFT_Send_Cmd(uint8_t cmd)
{
COMMAND_PORT &= ~(1<<lcd_dc); //we're going to send a command
COMMAND_PORT |= (1<<lcd_rd); //set the read pin to 1
COMMAND_PORT &= ~(1<<lcd_cs); //activate the chip
COMMAND_PORT1 &= ~(1<<lcd_wr); //strobe the write bit
DATA_PORT_0 = cmd;
_delay_us(5);
COMMAND_PORT1 |= (1<<lcd_wr);
COMMAND_PORT |= (1<<lcd_cs); //deactivate the chip
}And the function that reads data looks like this.
uint8_t TFT_Read_Data(void)
{
uint8_t data = 0;
DATA_DDR_0 = 0X00; //port as input with pull-down
DATA_PORT_0 = 0x00;
COMMAND_PORT |= (1<<lcd_dc); //we're going to read DATA
COMMAND_PORT1 |= (1<<lcd_wr); //set the write pin to 1
COMMAND_PORT &= ~(1<<lcd_cs); //activate the chip
COMMAND_PORT &= ~(1<<lcd_rd); //strobe the read bit
_delay_us(5);
data = PINA;
COMMAND_PORT |= (1<<lcd_rd);
COMMAND_PORT |= (1<<lcd_cs); //deactivate the chip
DATA_DDR_0 = 0XFF; //port as output
return data;
}The screenshots show that the functions described above work; all that's left is to implement sending 8-bit and 16-bit data. The thing is, mostly only the lower 8 bits of the bus are used when sending data, but, for example, when writing data to the display memory, all 16 bits are used.
void TFT_Write_Data(uint8_t data)
{
COMMAND_PORT |= (1<<lcd_dc); //we're going to send DATA
COMMAND_PORT |= (1<<lcd_rd); //set the read pin to 1
COMMAND_PORT &= ~(1<<lcd_cs); //activate the chip
COMMAND_PORT1 &= ~(1<<lcd_wr); //strobe the write bit
DATA_PORT_0 = data;
_delay_us(5);
COMMAND_PORT1 |= (1<<lcd_wr);
COMMAND_PORT |= (1<<lcd_cs); //deactivate the chip
}
void TFT_Write_Data16(uint16_t data)
{
COMMAND_PORT |= (1<<lcd_dc); //we're going to send DATA
COMMAND_PORT |= (1<<lcd_rd); //set the read pin to 1
COMMAND_PORT &= ~(1<<lcd_cs); //activate the chip
COMMAND_PORT1 &= ~(1<<lcd_wr); //strobe the write bit
DATA_PORT_0 = (data & 0x00ff);
DATA_PORT_8 = (data >> 8);
_delay_us(5);
COMMAND_PORT1 |= (1<<lcd_wr);
COMMAND_PORT |= (1<<lcd_cs); //deactivate the chip
}
That's it, we've implemented all the necessary low-level functions; now we can move on to the initialization itself. Before moving on, I should say that the supply voltage of an LCD cell constantly reverses polarity; this is done to avoid hydrolysis and dissociation of the complex organic compounds the liquid crystal material consists of.
void Init_ILI9341(void)
{
//configure all used pins as outputs
DATA_DDR_0 = 0XFF;
DATA_DDR_8 = 0XFF;
COMMAND_DDR = 0XFF;
COMMAND_DDR1 = 0X01;
COMMAND_PORT |= (1<<lcd_res);
TFT_Send_Cmd(0x01); //Software Reset
_delay_ms(1000);
//Power Control 1
TFT_Send_Cmd(0xC0); //set the grayscale level
TFT_Write_Data(0x25);
//Power Control 2
TFT_Send_Cmd(0xC1); //configure the step-up converter
TFT_Write_Data(0x11);
//VCOM Control 1
TFT_Send_Cmd(0xC5); //contrast is determined by the difference VCOMH - VCOML = 5.2V
TFT_Write_Data(0x2B); //VCOMH = 3.825
TFT_Write_Data(0x2B); //VCOML = -1.375
//VCOM Control 2
TFT_Send_Cmd(0xC7); //Vcom is essentially PWM, and here we set its offset
TFT_Write_Data(0x86); //VML=58 VMH=58
//Memory Access Control
TFT_SetOrientation(orient); //select the display orientation
//COLMOD: Pixel Format Set
TFT_Send_Cmd(0x3A); //one pixel will be encoded with 16 bits
TFT_Write_Data(0x05);
//Frame Rate Control
TFT_Send_Cmd(0xB1);
TFT_Write_Data(0x00);
TFT_Write_Data(0x18); //Frame Rate 79Hz
//Display Function Control
TFT_Send_Cmd(0xB6);
TFT_Write_Data(0x0A);
TFT_Write_Data(0x82);//the eighth bit determines the normal crystal color: white - 1, black - 0,
TFT_Write_Data(0x27);
// Sleep Out
TFT_Send_Cmd(0x11);
_delay_ms(120);
//Display On
TFT_Send_Cmd(0x29);
}
The initialization function contains the TFT_SetOrientation(2) function, which determines the drawing order; MAX_X and MAX_Y are declared globally and initialized to zero.
void TFT_SetOrientation(uint8_t orient)
{
TFT_Send_Cmd(0x36);
switch (orient)
{
case 0: TFT_Write_Data(0x48);
break;
case 1: TFT_Write_Data(0x28);
break;
case 2: TFT_Write_Data(0x88);
break;
case 3: TFT_Write_Data(0xE8);
break;
}
if (orient == 0 || orient == 2)
{
MAX_X = 239;
MAX_Y = 319;
}
else
{
MAX_X = 319;
MAX_Y = 239;
}
}
And here is what the display looks like after initialization.
To make sure it really initialized, I filled it with red.
I'll describe how to do this in the next article.
As for the wiring diagram: since the pinout of the 40-pin SSD1289 and ILI9341 displays is the same, I left the wiring diagram unchanged, and it looks like this.

This way you can make one development board for both displays. It's also important that the supply voltage of the MCU and the display is 3.3 volts; when powered from 5 volts, the display didn't start. I also powered the display backlight from 3.3 volts through a 47 Ohm resistor.
That's all; in the next article we'll look at how to display characters, and there will also be an Atmega16 project for AtmelStudio6.2.
For those who want to quickly check whether their display works, here is firmware for the Atmega16 that fills the display with different colors ili9341_i8080.hex.
I recently bought a couple of displays here.



