Displaying Characters and Strings on a TFT Display, Using the SSD1289 as an Example.
First of all, I'd like to say that the display's shorter side is X and the longer side is Y.
In the previous article we looked at how to initialize the display; in this one we'll learn to display characters and strings. Let's start with characters.
Characters are a set of points filled in a certain order.

To display a point, you need to perform the following sequence of actions:
So the simplest code for displaying a point looks like this:
Next, to display the next point, we send only the code of the color we want for the next cell, and the SSD1289 will step through the cells by itself, according to the algorithm we selected earlier. To refresh your memory, here's a screenshot of the datasheet; the mode we selected is marked in green.
The point we displayed is very small and hard to see; it would be good to increase its size. For example, to make it look like this.

The main thing to understand is that a large point is a square. Let's write the code to display a point of a given size.
For later use, it's convenient to put the code that selects a point in video RAM into a function.
Now that we've learned to draw a point, it won't be hard for us to fill the screen with one big point))) Just kidding: the screen is rectangular and the point is square, so it won't work.
But we can fill the screen with many small points: to do this, set the cursor to the start of the screen and send the color we want to fill the screen with 76800 times in a loop. The SSD1289 will fill the whole screen by itself according to the algorithm we selected earlier, and 76800 = 320x240, i.e. the number of points on the screen.
Now we can move on to letters. Each 8x8 letter is an array of 8 elements; the figure below shows how the array is formed.
To display the letter A, you need to output the elements of the array line by line. For convenience, all the letters are combined into one big array and stored in the controller's flash, as indicated by the PROGMEM attribute.
In its simplest form, the function that draws characters on the screen looks like this.
Let's figure out how it works. On the first iteration, when j=0 and i=0, the cursor is set to the coordinates we passed to the function. The iteration variable j determines the cursor offset along the X axis, i.e. downward. The iteration variable i determines the cursor offset along the Y axis, i.e. to the right. Next, using
(pgm_read_byte(&sim[j])
the zeroth element of the array is read from flash. We shift the bit we're interested in to the last position of this element
((pgm_read_byte(&sim[j]))>>(7-i))
now, using the "bitwise AND" operation, we determine whether it equals zero or one.
0x01&((pgm_read_byte(&sim[j]))>>(7-i))
If it equals one, we draw a point in the character color; otherwise a point in the background color. Remember how we drew a big point by replacing it with a square? We can do the same here: instead of one point, draw a square of arbitrary size. Then the final function for drawing a character on the display will look like this.
Next, it's no trouble to put letters together into words.

And here is the function for displaying strings.
There is one nuance in this function: finding the required character in the table, which is handled by the following piece of code.
simbols+((pgm_read_byte(&string[k]))-32)*8
First we get the character we're interested in from the string, using the following construct, where k is the character's number in the string.
&string[k]
Suppose we got the letter A. Since characters in a string are stored as ASCII codes, for the letter 'A' it equals 65. In our array the letter 'A' is on line 34, so to find the letter we need, we subtract 32 from 65, getting 33, the offset in lines from the start of the array, and multiply it by the number of elements per line, 8. Now if we add the calculated offset to the start of the array, we get the first element for drawing the letter A.

That's probably all; in the next article we'll learn to draw geometric shapes.
The Atmega16 project for Atmel Studio 6.2 is in the archive ssd1289_i8080.rar
In the previous article we looked at how to initialize the display; in this one we'll learn to display characters and strings. Let's start with characters.
Characters are a set of points filled in a certain order.

To display a point, you need to perform the following sequence of actions:
- set the cell's X coordinate in register R4E;
- set the cell's Y coordinate in register R4F;
- select register R22, that is, say: “Now we're going to write to video RAM”;
- send the code of the color we want to display in this cell;
So the simplest code for displaying a point looks like this:
Lcd_Write_Reg(0x004e, x);
Lcd_Write_Reg(0x004f, y);
Lcd_Write_Index(0x0022);
Lcd_Write_Data(color);
Next, to display the next point, we send only the code of the color we want for the next cell, and the SSD1289 will step through the cells by itself, according to the algorithm we selected earlier. To refresh your memory, here's a screenshot of the datasheet; the mode we selected is marked in green.
The point we displayed is very small and hard to see; it would be good to increase its size. For example, to make it look like this.

The main thing to understand is that a large point is a square. Let's write the code to display a point of a given size.
// size is the point size, x and y are the coordinates of the point's top-left corner, color is the point's color.
void Draw_Point(uint8_t size,uint16_t x,uint16_t y,uint16_t color)
{
uint8_t j, i=0; //iteration variables
for(j=0; j<size; j++) // this loop sets the start coordinates of the horizontal line
{
Lcd_Write_Reg(0x004e, x+j);
Lcd_Write_Reg(0x004f, y);
Lcd_Write_Index(0x0022);
for(i=0; i<size; i++) // this loop draws the horizontal line
{
Lcd_Write_Data(color);
}
}
}
For later use, it's convenient to put the code that selects a point in video RAM into a function.
void Set_Cursor(uint16_t x_kur, uint16_t y_kur)
{
Lcd_Write_Reg(0x004e, x_kur);
Lcd_Write_Reg(0x004f, y_kur);
Lcd_Write_Index(0x0022);
}
Now that we've learned to draw a point, it won't be hard for us to fill the screen with one big point))) Just kidding: the screen is rectangular and the point is square, so it won't work.
But we can fill the screen with many small points: to do this, set the cursor to the start of the screen and send the color we want to fill the screen with 76800 times in a loop. The SSD1289 will fill the whole screen by itself according to the algorithm we selected earlier, and 76800 = 320x240, i.e. the number of points on the screen.
void TFT_Clear(uint16_t color)
{
uint32_t index=0;
Set_Cursor(0,0);
for(index=0; index<76800; index++)
{
Lcd_Write_Data(color);
}
}
Now we can move on to letters. Each 8x8 letter is an array of 8 elements; the figure below shows how the array is formed.
To display the letter A, you need to output the elements of the array line by line. For convenience, all the letters are combined into one big array and stored in the controller's flash, as indicated by the PROGMEM attribute.
Show / Hide text
const unsigned char simbols [] 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
In its simplest form, the function that draws characters on the screen looks like this.
void Draw_Simbol(uint16_t x, uint16_t y, uint16_t color, uint16_t phone, const uint8_t *sim)
{
volatile uint8_t j, i; //row and column dimension variables
for(j=0; j<8; j++) //for 8 rows along x
{
Set_Cursor(x+j,y); //set the cursor
for(i=0; i<8; i++) //for 8 columns along y
{
if(0x01&((pgm_read_byte(&sim[j]))>>(7-i))) //if 1, write a point in the character color
{
Lcd_Write_Data(color);
}
else //if 0, write a point in the background color
{
Lcd_Write_Data(phone);
}
}
}
}
Let's figure out how it works. On the first iteration, when j=0 and i=0, the cursor is set to the coordinates we passed to the function. The iteration variable j determines the cursor offset along the X axis, i.e. downward. The iteration variable i determines the cursor offset along the Y axis, i.e. to the right. Next, using
(pgm_read_byte(&sim[j])
the zeroth element of the array is read from flash. We shift the bit we're interested in to the last position of this element
((pgm_read_byte(&sim[j]))>>(7-i))
now, using the "bitwise AND" operation, we determine whether it equals zero or one.
0x01&((pgm_read_byte(&sim[j]))>>(7-i))
If it equals one, we draw a point in the character color; otherwise a point in the background color. Remember how we drew a big point by replacing it with a square? We can do the same here: instead of one point, draw a square of arbitrary size. Then the final function for drawing a character on the display will look like this.
void Draw_Simbol ( uint16_t x, uint16_t y, uint16_t color, uint16_t phone, const uint8_t *sim,
uint8_t size)
{
volatile uint8_t j, i, k, p ;
for( j=0; j<8; j++) //output 8 rows along x
{
for( k=0; k< size; k++) //size is the point size
{
//set the cursor to the start of the row we'll draw
Set_Cursor(x+(j*size)+k, y);
for( i=0; i<8; i++) //output 8 columns along y
{
//if the character array has 1, write a point in the character color size times
if(0x01 & ((pgm_read_byte(&sim[j])) >> (7 - i)))
{
for( p=0; p< size; p++) Lcd_Write_Data(color);
}
//if the character array has 0, write a point in the background color size times
else
{
for(p=0; p< size; p++) Lcd_Write_Data(phone);
}
}
}
}
}
Next, it's no trouble to put letters together into words.

And here is the function for displaying strings.
void Draw_String (uint8_t size,uint16_t start_x,uint16_t start_y,uint16_t color,uint16_t phone,const uint8_t *string)
{
// maximum number of character cells along x and y, intermediate coordinates, character counter in the string
uint16_t max_x, max_y, x1, y1, k = 0;
if(size > 5) size=5; //a point size above 5x5 makes no sense
max_y = 320/(size*8); //max number of character cells along y
max_x = 240/(size*8); //max number of character cells along x
for(x1 =start_x; x1 < max_x; x1++)
{
for(y1 =start_y; y1 < max_y; y1++)
{
//output a character with the given parameters from the string
Draw_Simbol(x1*8*size,y1*8*size,color,phone,simbols+((pgm_read_byte(&string[k]))-32)*8,size);
//if the number of characters exceeds the maximum in a line, exit the function
if(k++ > (strlen_P (string))-2) return;
}
}
}
There is one nuance in this function: finding the required character in the table, which is handled by the following piece of code.
simbols+((pgm_read_byte(&string[k]))-32)*8
First we get the character we're interested in from the string, using the following construct, where k is the character's number in the string.
&string[k]
Suppose we got the letter A. Since characters in a string are stored as ASCII codes, for the letter 'A' it equals 65. In our array the letter 'A' is on line 34, so to find the letter we need, we subtract 32 from 65, getting 33, the offset in lines from the start of the array, and multiply it by the number of elements per line, 8. Now if we add the calculated offset to the start of the array, we get the first element for drawing the letter A.

That's probably all; in the next article we'll learn to draw geometric shapes.
The Atmega16 project for Atmel Studio 6.2 is in the archive ssd1289_i8080.rar

