Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

In the previous article we looked at how to initialize an LCD display over a 4-bit bus, but instead of checking the busy flag we used a delay. Let's start by implementing that.
Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.


To check the busy flag, you need to do the following:
  • set the data port as an input with pull-up
  • RS = 0
  • R/W = 1
  • raise the strobe E=1
  • delay
  • read the upper nibble and note the value of bit 7
  • lower the strobe E=0
  • delay
  • raise the strobe E=1
  • delay; we won't read the lower nibble
  • lower the strobe E=0
  • if bit 7 is one (the controller is busy), repeat the operation again; if it's zero, set the data port as an output and exit

Let's put this into code.


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);
	LCD_PORT &= ~(1<<RS);
	
	do
	{
                //raise the strobe
		LCD_PORT |= (1<<E);
		_delay_us(2);
                //note the value of bit 7 
		Busy_Flag = (LCD_PIN & 0X80);
                //lower the strobe
		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);

}


The busy flag must be polled before starting any operation; that's all about the busy flag.
 
The sequence of actions for sending commands/data was described in the previous article and differs only in the RS bit: to send commands RS=0, to send data RS=1, so let's move the common part into a separate function.

void Common_Write_Func(uint8_t data)
{
       //variable for storing the upper nibble
	uint8_t temp = (data & 0XF0);
	
        //we're going to write
	LCD_PORT &= ~(1<<RW);
        //data bus as output
	LCD_DDR |= DATA_BUS;
	
	//clear the previous transfer with zeros
	LCD_PORT &= ~DATA_BUS;
        //put the upper nibble on the bus
	LCD_PORT |= temp ;
	Strob();
	
	//clear the previous transfer with zeros
	LCD_PORT &= ~DATA_BUS;
        //put the upper nibble on the bus
	LCD_PORT |= (data << 4);
	Strob();	
}


Then sending commands will look like this

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

And sending data

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

 
The controller documentation has a command table that lets you work with it
Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

Using this table, let's figure out how to display a character on the screen.

To display a character on an LCD display, you need to:
  • specify its position on the screen
  • write the character code to that position

 
The position on the screen can be specified with the command shown below.
Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

Bits AC0 – AC6 encode the address of the position on the screen.
 
And data can be written to this position with the following instruction.
Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

Bits D0 – D7 encode the address of the character in the table
 
Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

The encoding of English letters in the character generator table matches the ASCII codes, which is very convenient: you can pass string literals to the function.
 
Displaying a single character is rarely needed, so let's write a function for displaying a string.

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

As you know, a string in C ends with an escaped zero ('\0'); the escape means that the character itself, not its encoding, is written to memory.
Below you can see how the following strings are stored in memory.

        Draw_String("hubstub");
	Draw_String(".ru");

Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.

The function outputs the string to the display character by character until it encounters this zero.

And now let's display the site name.

#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 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 Set_Ddram_Address(uint8_t address)
{
	Write_Command(address | 0x80);
}


int main(void)
{
	
	LCD_DDR = 0XFF;
	LCD_Init();

	Set_Ddram_Address(0X43);
	Draw_String("hubstub");
	Draw_String(".ru");

    while(1)
    {
        
    }
}


Displaying Characters and Strings on a 1602A LCD Display with a KS0066U Controller.


That's all; in the next article we'll try to draw our own character on the display.