Drawing a Custom Character on a 1602A LCD Display.

In previous articles we looked at how to initialize an LCD display and output characters to it using the character generator table (CGROM). Previous articles also mentioned a type of memory called CGRAM, in which you can store your own characters. I should say that its size is limited and it can hold up to 8 characters.

One character that can come in handy and that isn't in the standard character generator table (CGROM) is the backslash. Let's draw it.

First, we take a sheet of graph paper, outline a rectangle of 5 by 8 squares on it, and draw our character.
Drawing a Custom Character on a 1602A LCD Display.

Writing to CGRAM is done row by row, starting from the top row. 8 bytes are allocated for each character, and only the first five bits of each byte are used. Then our character can be represented as an array of eight elements.

     const char backslash[8] = {0x00 , 0x00, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00};


First, you need to specify the position in CGRAM to start writing from; after that, the address value will be incremented automatically. So a character can be written to CGRAM as follows.

      const char backslash[8] = {0x00 , 0x00, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00};
      Set_Cgram_Addres(0X08);
	for (uint8_t i = 0; i < 8; i++)
	{
		Write_Data(backslash[i]);
	}


Let's figure out why the CGRAM address is 0x08. As already written above, 8 bytes are allocated for each character, as shown in the picture below.
Drawing a Custom Character on a 1602A LCD Display.

So by starting to write at address 0x08, we wrote our character to the first column (column numbering starts from zero).

Great, the character is in memory; all that's left is to display it on the screen. To do this, specify the position on the screen where we want to display it and write the data stored in CGRAM to that position.

       Set_Cgram_Addres(0X08);
	for (uint8_t i = 0; i < 8; i++)
	{
		Write_Data(backslash[i]);
	}
	//set the position on the screen
	Set_Ddram_Address(0X00);
	//specify the very column we want to display
	Write_Data(0X01);

The code of the whole project for the Atmega16 can be expanded below.
Show / Hide text



#define F_CPU 8000000UL

#define LCD_PORT PORTA
#define LCD_DDR DDRA
#define LCD_PIN PINA

#define DATA_BUS 0XF0

#define RS 0
#define RW 1
#define E  2


#include <avr/io.h>
#include <avr/delay.h>

uint8_t Busy_Flag = 0;

void Check_Busy(void)
{
	//data bus - input with pull-up
	LCD_DDR &= ~DATA_BUS;
	LCD_PORT |= DATA_BUS;
	
	//we'll read from the lcd
	LCD_PORT |= (1<<RW);
	//commands
	LCD_PORT &= ~(1<<RS);
	
	do
	{
		LCD_PORT |= (1<<E);
		_delay_us(2);
		Busy_Flag = (LCD_PIN & 0X80);
		LCD_PORT  &= ~(1<<E);
		
		_delay_us(1);
		
		LCD_PORT |= (1<<E);
		_delay_us(2);
		LCD_PORT  &= ~(1<<E);
	}
	while (Busy_Flag);
	//data bus - output
	LCD_DDR |= DATA_BUS;
	//put the LCD into write mode
	LCD_PORT &= ~(1<<RW);
	

}

void Strob(void)
{
	LCD_PORT |= (1<<E);
	_delay_us(2);
	LCD_PORT &= ~(1<<E);
}

void Common_Write_Func(uint8_t data)
{
	uint8_t temp = (data & 0XF0);
	
	LCD_PORT &= ~(1<<RW);
	LCD_DDR |= DATA_BUS;
	
	//clear the previous transfer with zeros
	LCD_PORT &= ~DATA_BUS;
	LCD_PORT |= temp ;
	Strob();
	
	//clear the previous transfer with zeros
	LCD_PORT &= ~DATA_BUS;
	LCD_PORT |= (data << 4);
	Strob();	
}

void Write_Command(uint8_t data)
{
	
	Check_Busy();
	LCD_PORT &= ~(1<<RS);
	Common_Write_Func(data);
}

void Write_Data(uint8_t data)
{
	Check_Busy();
	LCD_PORT |= (1<<RS);
	Common_Write_Func(data);
}

void Write_Init_Command(uint8_t data)
{
	
	//pins used for transferring commands/data as outputs
	LCD_DDR |= DATA_BUS;
	//we're going to send a command
	LCD_PORT &= ~(1<<RS);
	LCD_PORT &= ~(1<<RW);
		
	//clear the previous transfer with zeros
	LCD_PORT &= ~DATA_BUS;
	//put the command on the bus
	LCD_PORT |= data;
	Strob();
	_delay_us(100);
		
}

void LCD_Init(void)
{
	_delay_ms(50);
	
	Write_Init_Command(0x20);
	Write_Init_Command(0x20);
	//turn on the display in 2-line mode
	Write_Init_Command(0xC0);
	
	_delay_us(50);
	
	Write_Init_Command(0x00);
	//turn on the blinking cursor
	Write_Init_Command(0xE0);
	
	_delay_us(50);
	
	Write_Init_Command(0x00);
	//clear the display
	Write_Init_Command(0x10);
	
	_delay_ms(2);
	
	Write_Init_Command(0x00);
	//DDRAM address increments, without display shift
	Write_Init_Command(0x60);
		
}

__inline void LCD_Clear(void)
{
	Write_Command(0X01);

}

void Draw_String(const char *str)
{
	uint8_t data = 0;
	while (*str)
	{
		data = *str++;
		Write_Data(data);
	}
}

__inline void Return_Home(void)
{
	Write_Command(0X02);
}

__inline void Set_Ddram_Address(uint8_t address)
{
	Write_Command(address | 0x80);
}

__inline void Set_Cgram_Addres(uint8_t address)
{
	Write_Command(address | 0x40);	
}

__inline void LCD_On(void)
{
	Write_Command(0X0E);
}

__inline void LCD_Off(void)
{
	Write_Command(0X08);

}

int main(void)
{
	const char backslash[8] = {0x00 , 0x00, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00};
	LCD_DDR = 0XFF;
	LCD_Init();
	Set_Ddram_Address(0X43);
	Draw_String("hubstub");
	Draw_String(".ru");
	
	
	Set_Cgram_Addres(0X08);
	for (uint8_t i = 0; i < 8; i++)
	{
		Write_Data(backslash[i]);
	}
	
	Set_Ddram_Address(0X00);
	
	Write_Data(0X01);
	
    while(1)
    {
        
    }
}


Drawing a Custom Character on a 1602A LCD Display.

That's all.