Initializing a 1602A LCD Display with a KS0066U Controller.
This display had been lying around unused for a while.
And then I wanted to hook it up to one of my projects. Of course, you could try to find a library with ready-made functions, but then you won't get the complete picture of how the display works, and that doesn't suit us. Once you've figured out how an LCD display works, it won't be hard to write your own library for the required display if there isn't one or the existing one isn't satisfactory.
So, let's begin.
The first thing to do is find the pinout, i.e. which pin does what; the second is to find the name of the controller that drives the display. To do this, download the datasheet for this LCD and open it at the first page.
The pins are counted from left to right; the first one is marked with a red arrow. The supply voltage is 5 volts, and the controller is the S6A0069 or an equivalent, for example the ks0066U.
Why did we look for the name of the controller? The thing is, the display datasheet contains timing diagrams and describes the command set, but has no basic initialization procedure, and you can't do without it.
Next, open the second page and you'll see a table describing what each pin does.

DB7…DB0 – data/address bus.
R/W - determines what we're going to do, read (R/W=1) or write (R/W=0)
R/S – determines whether we're going to send a command (RS=0) or data (RS=1)
E – the strobe input; by changing the signal on this input we allow the display to read/write data.
LED± – backlight control.
I should say that on the display I got, the backlight won't turn on just like that; you need to solder in the resistor marked R7 on the board. But we don't need it yet.
Download the datasheet for the controller and find the initialization instructions. You can enlarge the pictures by clicking on them.
It turns out there are two such instructions, for 8-bit and 4-bit mode. What are these modes? They determine how many wires the data is transferred over: four or eight. Let's look at transfer over 4 wires; in this case the display will work more slowly, but we'll save 4 microcontroller pins, and the 8-bit mode implementation isn't much different anyway.
The data line wiring diagram looks like this.
Contrast can be adjusted by connecting a potentiometer between the supply pins.

I'd like to point out that during initialization R/S and R/W are always zero, i.e. we'll be sending commands.
During initialization you can configure:
Let's look at the last two items in more detail.
The picture below shows which address to write data to so that it appears in a certain position; for example, if we want to display a character at the first position of the second line, we need to write to address 0x40.

After that, the counter value changes automatically, either increasing or decreasing, and the cursor position changes along with it.
By the way, the memory we write to is called DDRAM; everything we write to this memory is shown on the display. There is also CGROM, which stores the character generator table.
This table can't be changed, but you can take ready-made characters from it. Another type of memory is CGRAM; it's also a character generator table, but we draw the characters in it ourselves.

Now a couple of words about display shifting: usually we don't see all of DDRAM on the display, only a certain part, as shown in the picture below.
We can also write to the invisible part, but what we write won't be visible until we move the display window to that location.
That's it for the theory; let's move on to practice.
Communication with an LCD display in 4-bit mode looks like this.

Data is sent in bytes, but since we're in 4-bit mode, sending a byte takes 2 transfers, MSB first. In the picture the first transfer is labeled D7 (upper nibble) and the second D3 (lower nibble). Before the next transfer we must check the busy flag: if it isn't set, we can send again; if it is set, we wait until the controller driving the LCD finishes its work.
Having the overall picture of a transfer, let's figure out how to implement the send operation.

To send over an 8-bit bus, you need to:
The read operation is implemented similarly:
Where does the 2us delay come from?
Above the timing diagrams there is a table listing the values of the delays shown in the graph; the strobe pulse width tw must be 230nS or 450nS depending on the supply voltage, and we took it with a little margin. Why did we only take this delay into account? Because the other delays are very small.
To send over a 4-bit bus:
To read over a 4-bit bus:
Raising the strobe and putting the command/data on the bus can be swapped. Now it won't be hard to initialize the display. To simplify the initialization, we'll replace reading the busy flag with a delay and look at working with the flag later.
It should be noted that during initialization in 4-bit mode, 4-bit commands are used, and after initialization the 8-bit command set, so for initialization we'll implement a separate command-sending function void Write_Init_Command(uint8_t data).
A cheerfully blinking cursor shows that initialization was successful. In the next article we'll learn to display characters on the LCD display.
And then I wanted to hook it up to one of my projects. Of course, you could try to find a library with ready-made functions, but then you won't get the complete picture of how the display works, and that doesn't suit us. Once you've figured out how an LCD display works, it won't be hard to write your own library for the required display if there isn't one or the existing one isn't satisfactory.
So, let's begin.
The first thing to do is find the pinout, i.e. which pin does what; the second is to find the name of the controller that drives the display. To do this, download the datasheet for this LCD and open it at the first page.
The pins are counted from left to right; the first one is marked with a red arrow. The supply voltage is 5 volts, and the controller is the S6A0069 or an equivalent, for example the ks0066U.
Why did we look for the name of the controller? The thing is, the display datasheet contains timing diagrams and describes the command set, but has no basic initialization procedure, and you can't do without it.
Next, open the second page and you'll see a table describing what each pin does.

DB7…DB0 – data/address bus.
R/W - determines what we're going to do, read (R/W=1) or write (R/W=0)
R/S – determines whether we're going to send a command (RS=0) or data (RS=1)
E – the strobe input; by changing the signal on this input we allow the display to read/write data.
LED± – backlight control.
I should say that on the display I got, the backlight won't turn on just like that; you need to solder in the resistor marked R7 on the board. But we don't need it yet.
Download the datasheet for the controller and find the initialization instructions. You can enlarge the pictures by clicking on them.
It turns out there are two such instructions, for 8-bit and 4-bit mode. What are these modes? They determine how many wires the data is transferred over: four or eight. Let's look at transfer over 4 wires; in this case the display will work more slowly, but we'll save 4 microcontroller pins, and the 8-bit mode implementation isn't much different anyway.
The data line wiring diagram looks like this.
Contrast can be adjusted by connecting a potentiometer between the supply pins.

I'd like to point out that during initialization R/S and R/W are always zero, i.e. we'll be sending commands.
During initialization you can configure:
- N - the number of displayed lines
- C - turn the cursor on or off
- B - make the cursor blink
- I/D - increment or decrement the address counter
- SH - shift the display window
Let's look at the last two items in more detail.
The picture below shows which address to write data to so that it appears in a certain position; for example, if we want to display a character at the first position of the second line, we need to write to address 0x40.

After that, the counter value changes automatically, either increasing or decreasing, and the cursor position changes along with it.
By the way, the memory we write to is called DDRAM; everything we write to this memory is shown on the display. There is also CGROM, which stores the character generator table.
This table can't be changed, but you can take ready-made characters from it. Another type of memory is CGRAM; it's also a character generator table, but we draw the characters in it ourselves.

Now a couple of words about display shifting: usually we don't see all of DDRAM on the display, only a certain part, as shown in the picture below.
We can also write to the invisible part, but what we write won't be visible until we move the display window to that location.
That's it for the theory; let's move on to practice.
Communication with an LCD display in 4-bit mode looks like this.

Data is sent in bytes, but since we're in 4-bit mode, sending a byte takes 2 transfers, MSB first. In the picture the first transfer is labeled D7 (upper nibble) and the second D3 (lower nibble). Before the next transfer we must check the busy flag: if it isn't set, we can send again; if it is set, we wait until the controller driving the LCD finishes its work.
Having the overall picture of a transfer, let's figure out how to implement the send operation.

To send over an 8-bit bus, you need to:
- make sure the controller is free
- set RS to 0 (command) or 1 (data), depending on what we're going to send
- set R/W to 0
- raise the strobe E (set it to 1)
- put the command code/data on the bus
- delay 2us
- lower the strobe E
The read operation is implemented similarly:
- make sure the controller is free
- set the data port as an input with pull-up
- set RS to 0 (command) or 1 (data), depending on what we're going to read
- set R/W to 1
- raise the strobe E (at this moment the LCD will put data on the bus)
- delay 2us
- read what the LCD has output
- lower the strobe E
Where does the 2us delay come from?
Above the timing diagrams there is a table listing the values of the delays shown in the graph; the strobe pulse width tw must be 230nS or 450nS depending on the supply voltage, and we took it with a little margin. Why did we only take this delay into account? Because the other delays are very small.
To send over a 4-bit bus:
- make sure the controller is free
- set RS to 0 (command) or 1 (data), depending on what we're going to send
- set R/W to 0
- raise the strobe E (set it to 1)
- put the upper nibble on the bus
- delay 2us
- lower the strobe E
- delay 1us
- raise the strobe E (set it to 1)
- put the lower nibble on the bus
- delay 2us
- lower the strobe E
To read over a 4-bit bus:
- make sure the controller is free
- set the data port as an input with pull-up
- set RS to 0 (command) or 1 (data), depending on what we're going to read
- set R/W to 1
- raise the strobe E (set it to 1)
- delay 2us
- read the upper nibble
- lower the strobe E
- delay 1us
- raise the strobe E (set it to 1)
- delay 2us
- read the lower nibble
- lower the strobe E
Raising the strobe and putting the command/data on the bus can be swapped. Now it won't be hard to initialize the display. To simplify the initialization, we'll replace reading the busy flag with a delay and look at working with the flag later.
It should be noted that during initialization in 4-bit mode, 4-bit commands are used, and after initialization the 8-bit command set, so for initialization we'll implement a separate command-sending function void Write_Init_Command(uint8_t data).
//Initialization code for the Atmega16
#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>
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 Strob(void)
{
LCD_PORT |= (1<<E);
_delay_us(2);
LCD_PORT &= ~(1<<E);
}
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(0xF0);
_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);
}
int main(void)
{
LCD_DDR = 0XFF;
LCD_Init();
while(1)
{
}
}
A cheerfully blinking cursor shows that initialization was successful. In the next article we'll learn to display characters on the LCD display.







