Displaying Characters and Strings on a TFT Display, Using the ILI9341 as an Example.
In the previous article we looked at how to initialize a TFT display controlled by the ILI9341 driver; in this one we'll learn to display characters and strings.
But first we need to clarify one thing: whereas with the SSD1289 you need to specify two coordinates and the color of a point to draw it on the display, with the ILI9341 you specify 4 coordinates that bound the working area, and then the color of each newly drawn point, while the order in which points are drawn is set during initialization.
As for the order in which points are drawn, it is set after sending the 36h command.

To understand what is written there, you need to mentally draw two axes, the X axis horizontally and the Y axis vertically; then:

Changing the ML and MH values had no effect; that's something I still have to figure out.
To draw anything on the screen, you need to perform the following sequence of actions:
Then the code for drawing a single pixel will look like this
And now, as promised in the previous article, let's look at how to fill the screen with a chosen color.
I hope the comments make it clear how the function works.
Now let's look at how to draw characters on the display. Each 8x8 letter is an array of 8 elements; the figure below shows how it is formed.
To display the letter A, you need to output the array elements row by row. For convenience, all the letters are put together into one big two-dimensional array stored in the controller's flash, as indicated by the PROGMEM attribute.
The function for drawing characters looks like this.
It works as follows: using the function
pgm_read_byte(&simbols[ascii-0x20][i])
the i-th byte of the required character is fetched from flash. As we remember, a character is encoded with 8 bytes, and each byte, in turn, stores information about how to fill a row. We shift the bit we are interested in to the rightmost position of this byte
(pgm_read_byte(&simbols[ascii-0x20][i])>>(7-f))
and use a bitwise AND operation to determine its value
(pgm_read_byte(&simbols[ascii-0x20][i])>>(7-f))&0x01
If it is one, we draw a point in the character color; otherwise, a point in the background color. This is how characters are drawn row by row.
And here is the function for displaying strings.
I think the comments also make it clear how this function works.

In the next article we'll learn to draw geometric shapes.
The Atmega16 project for Atmel Studio 6.2 is in the archive ili9341_i8080_example.rar.
But first we need to clarify one thing: whereas with the SSD1289 you need to specify two coordinates and the color of a point to draw it on the display, with the ILI9341 you specify 4 coordinates that bound the working area, and then the color of each newly drawn point, while the order in which points are drawn is set during initialization.
As for the order in which points are drawn, it is set after sending the 36h command.

To understand what is written there, you need to mentally draw two axes, the X axis horizontally and the Y axis vertically; then:
- MX – determines on which side of the display X will be zero, right or left; accordingly, on the other side of the display X will be at its maximum
- MY – the same, but for the Y axis
- MV – swaps the X and Y axes
- ML – determines the vertical refresh order of the selected area
- MH – determines the horizontal refresh order of the selected area

Changing the ML and MH values had no effect; that's something I still have to figure out.
To draw anything on the screen, you need to perform the following sequence of actions:
- send the 2A command, then the start and end coordinates of the area horizontally
- send the 2B command, then the start and end coordinates of the area vertically
- send the 2C command, i.e. say: "Now we're going to write to video RAM"
- send the code of the color we want to display in the current cell
- send the color code again; the coordinates will change automatically according to the algorithm selected during initialization
Then the code for drawing a single pixel will look like this
void TFT_Send_Data(uint16_t data)
{
uint8_t data1 = data>>8;
uint8_t data2 = data&0xff;
TFT_Write_Data(data1);
TFT_Write_Data(data2);
}
/*function limits the coordinates of the working area along the X axis*/
void TFT_Set_Column(uint16_t start_column,uint16_t end_colunm)
{
TFT_Send_Cmd(0x2A);
TFT_Send_Data(start_column);
TFT_Send_Data(end_colunm);
}
/*function limits the coordinates of the working area along the Y axis*/
void TFT_Set_Page(uint16_t start_page,uint16_t end_page)
{
TFT_Send_Cmd(0x2B);
TFT_Send_Data(start_page);
TFT_Send_Data(end_page);
}
/*function limits the coordinates of the working area*/
void TFT_Set_XY(uint16_t x, uint16_t y)
{
TFT_Set_Column(x, x);
TFT_Set_Page(y, y);
}
/*function draws a pixel at the given coordinates*/
void TFT_Draw_Pixel(uint16_t x, uint16_t y,uint16_t color)
{
TFT_Set_XY(x, y);
TFT_Send_Cmd(0x2c);
TFT_Write_Data16(color);
}
And now, as promised in the previous article, let's look at how to fill the screen with a chosen color.
uint16_t constrain(uint16_t a, uint16_t b, uint16_t c)
{
if (a < b)
{
return b;
}
if (c < a)
{
return c;
}
else return a;
}
void TFT_Fill_Screen(uint16_t x_left, uint16_t x_right, uint16_t y_up, uint16_t y_down, uint16_t color)
{
unsigned long xy=0;
unsigned long i=0;
if(x_left > x_right)
{
x_left = x_left^x_right; //if the left edge coordinate is greater than
x_right = x_left^x_right; //the right edge coordinate, they will be swapped:
x_left = x_left^x_right; //it was x_left = 5 x_right = 3
//it became x_left = 3 x_right = 5
}
if(y_up > y_down)
{
y_up = y_up^y_down; //the same for the y axis
y_down = y_up^y_down; //this operation is called
y_up = y_up^y_down; //"swap without a temporary variable"
}
//make sure that the coordinates passed to the function
//are within the valid range
x_left = constrain(x_left, MIN_X,MAX_X);
x_right = constrain(x_right, MIN_X,MAX_X);
y_up = constrain(y_up, MIN_Y,MAX_Y);
y_down = constrain(y_down, MIN_Y,MAX_Y);
xy = (x_right - x_left+1); //calculate the number of points
xy = xy*(y_down - y_up+1); //that need to be filled
TFT_Set_Column(x_left,x_right); //set the working area along x
TFT_Set_Page(y_up, y_down); //set the working area along y
TFT_Send_Cmd(0x2c); //we'll write to video RAM
for(i=0; i < xy; i++)
{
TFT_Write_Data16(color); //send the color code
}
}
I hope the comments make it clear how the function works.
Now let's look at how to draw characters on the display. Each 8x8 letter is an array of 8 elements; the figure below shows how it is formed.
To display the letter A, you need to output the array elements row by row. For convenience, all the letters are put together into one big two-dimensional array stored in the controller's flash, as indicated by the PROGMEM attribute.
Show / Hide text
const unsigned char simbols[][8] PROGMEM ={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},// space
{0x04,0x04,0x04,0x04,0x00,0x04,0x00,0x00},// !
{0x0A,0x0A,0x0A,0x00,0x00,0x00,0x00,0x00},// "
{0x0A,0x0A,0x1F,0x0A,0x1F,0x0A,0x0A,0x00},// #
{0x04,0x0F,0x14,0x0E,0x05,0x1E,0x04,0x00},// $
{0x18,0x19,0x02,0x04,0x08,0x13,0x03,0x00},// %
{0x0C,0x12,0x14,0x08,0x14,0x12,0x0D,0x00},// &
{0x0C,0x04,0x08,0x00,0x00,0x00,0x00,0x00},// '
{0x02,0x04,0x08,0x08,0x08,0x04,0x02,0x00},// (
{0x08,0x04,0x02,0x02,0x02,0x04,0x08,0x00},// )
{0x00,0x04,0x15,0x0E,0x15,0x04,0x00,0x00},// *
{0x00,0x04,0x04,0x1F,0x04,0x04,0x00,0x00},// +
{0x00,0x00,0x00,0x00,0x0C,0x04,0x08,0x00},// ,
{0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00},// -
{0x00,0x00,0x00,0x00,0x00,0x0C,0x0C,0x00},// .
{0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00},// /
{0x0E,0x11,0x13,0x15,0x19,0x11,0x0E,0x00},// 0
{0x04,0x0C,0x04,0x04,0x04,0x04,0x0E,0x00},// 1
{0x0E,0x11,0x01,0x02,0x04,0x08,0x1F,0x00},// 2
{0x1F,0x02,0x04,0x02,0x01,0x11,0x0E,0x00},// 3
{0x02,0x06,0x0A,0x12,0x1F,0x02,0x02,0x00},// 4
{0x1F,0x10,0x1E,0x01,0x01,0x11,0x0E,0x00},// 5
{0x06,0x08,0x10,0x1E,0x11,0x11,0x0E,0x00},// 6
{0x1F,0x01,0x02,0x04,0x08,0x08,0x08,0x00},// 7
{0x0E,0x11,0x11,0x0E,0x11,0x11,0x0E,0x00},// 8
{0x0E,0x11,0x11,0x0F,0x01,0x02,0x0C,0x00},// 9
{0x00,0x01,0x02,0x04,0x08,0x10,0x00,0x00},// :
{0x00,0x0C,0x0C,0x00,0x0C,0x04,0x08,0x00},// ;
{0x02,0x04,0x08,0x10,0x08,0x04,0x02,0x00},// <
{0x00,0x00,0x1F,0x00,0x1F,0x00,0x00,0x00},// =
{0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x00},// >
{0x0E,0x11,0x01,0x02,0x04,0x00,0x04,0x00},// ?
{0x0E,0x11,0x01,0x0D,0x15,0x15,0x0E,0x00},// @
{0x0E,0x11,0x11,0x11,0x1F,0x11,0x11,0x00},// A
{0x1E,0x11,0x11,0x1E,0x11,0x11,0x1E,0x00},// B
{0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00},// C
{0x1C,0x12,0x11,0x11,0x11,0x12,0x1C,0x00},// D
{0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00},// E
{0x1F,0x10,0x10,0x1E,0x10,0x10,0x10,0x00},// F
{0x0E,0x11,0x10,0x17,0x11,0x11,0x0E,0x00},// G
{0x11,0x11,0x11,0x1F,0x11,0x11,0x11,0x00},// H
{0x0E,0x04,0x04,0x04,0x04,0x04,0x0E,0x00},// I
{0x07,0x02,0x02,0x02,0x02,0x12,0x0C,0x00},// J
{0x11,0x12,0x14,0x18,0x14,0x12,0x11,0x00},// K
{0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0x00},// L
{0x11,0x1B,0x15,0x15,0x11,0x11,0x11,0x00},// M
{0x11,0x11,0x19,0x15,0x13,0x11,0x11,0x00},// N
{0x0E,0x11,0x11,0x11,0x11,0x11,0x0E,0x00},// O
{0x1E,0x11,0x11,0x1E,0x10,0x10,0x10,0x00},// P
{0x0E,0x11,0x11,0x11,0x15,0x12,0x0D,0x00},// Q
{0x1E,0x11,0x11,0x1E,0x14,0x12,0x11,0x00},// R
{0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E,0x00},// S
{0x1F,0x04,0x04,0x04,0x04,0x04,0x04,0x00},// T
{0x11,0x11,0x11,0x11,0x11,0x11,0x0E,0x00},// U
{0x11,0x11,0x11,0x11,0x11,0x0A,0x04,0x00},// V
{0x11,0x11,0x11,0x11,0x15,0x15,0x0E,0x00},// W
{0x11,0x11,0x0A,0x04,0x0A,0x11,0x11,0x00},// X
{0x11,0x11,0x11,0x0A,0x04,0x04,0x04,0x00},// Y
{0x1F,0x01,0x02,0x04,0x08,0x10,0x1F,0x00},// Z
{0x0E,0x08,0x08,0x08,0x08,0x08,0x0E,0x00},// [
{0x11,0x0A,0x1F,0x04,0x1F,0x04,0x04,0x00},//
{0x0E,0x02,0x02,0x02,0x02,0x02,0x0E,0x00},// ]
{0x04,0x0A,0x11,0x00,0x00,0x00,0x00,0x00},// ^
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x00},// _
{0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00},// '
{0x00,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00},// a
{0x10,0x10,0x1E,0x11,0x11,0x11,0x1E,0x00},// b
{0x00,0x00,0x0E,0x10,0x10,0x11,0x0E,0x00},// c
{0x01,0x01,0x0D,0x13,0x11,0x11,0x0F,0x00},// d
{0x00,0x00,0x0E,0x11,0x1F,0x10,0x0E,0x00},// e
{0x06,0x09,0x08,0x1C,0x08,0x08,0x08,0x00},// f
{0x00,0x0F,0x11,0x11,0x0F,0x01,0x0E,0x00},// g
{0x10,0x10,0x16,0x19,0x11,0x11,0x11,0x00},// h
{0x04,0x00,0x0C,0x04,0x04,0x04,0x0E,0x00},// i
{0x02,0x00,0x06,0x02,0x02,0x12,0x0C,0x00},// j
{0x10,0x10,0x12,0x14,0x18,0x14,0x12,0x00},// k
{0x18,0x08,0x08,0x08,0x08,0x08,0x1C,0x00},// l
{0x00,0x00,0x1A,0x15,0x15,0x11,0x11,0x00},// m
{0x00,0x00,0x16,0x19,0x11,0x11,0x11,0x00},// n
{0x00,0x00,0x0E,0x11,0x11,0x11,0x0E,0x00},// o
{0x00,0x00,0x1E,0x11,0x1E,0x10,0x10,0x00},// p
{0x00,0x00,0x0F,0x11,0x0F,0x01,0x01,0x00},// q
{0x00,0x00,0x16,0x19,0x10,0x10,0x10,0x00},// r
{0x00,0x00,0x0E,0x10,0x0E,0x01,0x1E,0x00},// s
{0x08,0x08,0x1C,0x08,0x08,0x09,0x06,0x00},// t
{0x00,0x00,0x11,0x11,0x11,0x13,0x0D,0x00},// u
{0x00,0x00,0x11,0x11,0x11,0x0A,0x04,0x00},// v
{0x00,0x00,0x11,0x11,0x11,0x15,0x0A,0x00},// w
{0x00,0x00,0x11,0x0A,0x04,0x0A,0x11,0x00},// x
{0x00,0x00,0x11,0x11,0x0F,0x01,0x0E,0x00},// y
{0x00,0x00,0x1F,0x02,0x04,0x08,0x1F,0x00},// z
{0x17,0x15,0x15,0x15,0x17,0x00,0x00,0x00},// 10
{0x17,0x11,0x17,0x14,0x17,0x00,0x00,0x00},// 12
{0x17,0x14,0x17,0x11,0x17,0x00,0x00,0x00},// 15
{0x01,0x05,0x09,0x1F,0x08,0x04,0x00,0x00},// arrow
{0x00,0x07,0x0E,0x1C,0x11,0x1E,0x00,0x00}};// squiggle
The function for drawing characters looks like this.
void TFT_Draw_Char(uint16_t x, uint16_t y, uint16_t color, uint16_t phone, uint8_t ascii, uint8_t size)
{
for (int i = 0; i < FONT_Y; i++ )
{
for(uint8_t f = 0; f < FONT_X; f++)
{
if((pgm_read_byte(&simbols[ascii-0x20][i])>>(7-f))&0x01)
{
TFT_Fill_Rectangle(x+f*size, y+i*size, size, size, color);
}
else
{
TFT_Fill_Rectangle(x+f*size, y+i*size, size, size, phone);
}
}
}
}
It works as follows: using the function
pgm_read_byte(&simbols[ascii-0x20][i])
the i-th byte of the required character is fetched from flash. As we remember, a character is encoded with 8 bytes, and each byte, in turn, stores information about how to fill a row. We shift the bit we are interested in to the rightmost position of this byte
(pgm_read_byte(&simbols[ascii-0x20][i])>>(7-f))
and use a bitwise AND operation to determine its value
(pgm_read_byte(&simbols[ascii-0x20][i])>>(7-f))&0x01
If it is one, we draw a point in the character color; otherwise, a point in the background color. This is how characters are drawn row by row.
And here is the function for displaying strings.
void TFT_Draw_String(uint16_t x, uint16_t y, uint16_t color,uint16_t phone,char *string, uint8_t size)
{
//finding the end of a string is very easy if you know that it ALWAYS ends with zero
while(*string)
{
//check whether we go beyond the screen when drawing the next character;
// if so, move to the next line
if((x + FONT_X) > MAX_X)
{
x = 1;
y = y + FONT_X*size;
}
TFT_Draw_Char(x, y, color, phone,*string, size);//draw the character
x += FONT_X*size; //change the coordinate for drawing the next character
*string++; //increment the pointer so that it points to the next character
}
}
I think the comments also make it clear how this function works.

In the next article we'll learn to draw geometric shapes.
The Atmega16 project for Atmel Studio 6.2 is in the archive ili9341_i8080_example.rar.
