10- AVR ATmega 16 Tutorials- Interfacing an LCD || Part II || Creating the LCD Library Leave a comment

Hello everyone and welcome back to the Blog.

In the last post on this tutorial series, we talked about how to interface an LCD with the AVR ATmega 16 microcontroller. We also showed you how to use the library file to print numbers, letters and characters on the LCD. Therefore, in this post, we will delve into the details of the code present in the LCD library and unravel all of its mysteries…so to speak. But I warn you in advance that this post will be heavily code oriented, in case that is not your cup of tea.

Before we talk about the code let us first quickly recap the Pin Description of a 16*2 Alphanumeric LCD.

Pin Description:

LCD Pin Description

As you can see in the picture to the left, most 16*2 Alphanumeric LCD has 16 Pins in it. Some might not have the last two pins which correspond to the backlight LED, but in my experience most of them do.

  • The first pin VSS is actually the Ground pin.
  • The second pin VDD is the Power pin to which a positive voltage of 5Volts needs to be given.
  • The third pin VEE is used for contrast setting. A 10K Potentiometer(variable resistor) as shown in the picture to the right needs to be connected with it. By turning the coil of the potentiometer the contrast of the LCD can be set. (Note: For more details refer to the circuit diagram and video below)
  • Next up are three control pins RS, RW and E. They are used to control all the operations of the LCD. They are to be directly connected to the microcontroller. Here RS stands for Resistor Select pin, RW stands for Read/Write pin and E stand for Enable pin. I will talk about them in more details in the next Post.
  • Then we have eight data line pins from D0 to D7. The data that needs to be displayed on the LCD is passed through this eight-bit data bus. These eight pins are also to be directly connected to the microcontroller.
  • Finally, we have two more pins which correspond to the backlight LED in the LCD. The VDD is the Positive pin while VSS is the Ground pin.

Control Pins:

As mentioned above we have three Control Pins in the LCD that we can use to control all the functionalities of an LCD. These pins are:

  • RS: This stands for Resistor Select pin. We have got two modes that our LCD can work in. They are the Command Mode and the Data Mode(we will talk about them in details below). RS pin helps us in selecting these modes. When the pin is set at LOW, the LCD enters the Command Mode while when the pin is set at HIGH the LCD enters the Data Mode. Every time we change modes while working with the LCD, we need to toggle this pin.
  • RW: This stands for Read/Write pin. As the name suggests this pin is used to let the LCD know, whether we want to Read Data from it or we want to Write data to it. We cannot do both of these simultaneously and the pin helps us to specify what we want to do. While writing data on the LCD this pin needs to be at LOW and whenever we want to read data from the LCD this pin needs to be at HIGH.
  • E: This stands for the Enable pin. It is a special pin that needs to be toggled every time we read data from the LCD or write data to it. Think of it as an On/Off switch. We need to flash the LCD every time we perform a data transfer. We do this by turning this pin HIGH, waiting for some time and then turning it LOW.

Modes:

As mentioned above our LCD works in two modes and we need to specify them everytime we use them. These modes are:

Command Mode:

As the name suggests, we send commands to the LCD in this mode. These Commands are predefined by the manufacturer of the LCD and mentioned in the datasheet. We can use them to do things like, clear the LCD screen of all old data, make the cursor blink or not, position the cursor at the location of our choice and many more things. To work in the Command Mode and send commands to the LCD the below steps must be followed.

  1. RS pin must be set to LOW for choosing the Command Mode.
  2. RW pin should also be set to LOW for writing the Commands on the LCD.
  3. The command is passed to the Data pins of the LCD.
  4. Enable pin is turned HIGH for flashing the LCD.
  5. We wait for 500 microseconds for the LCD to process the command.
  6. Enable pin is turned LOW to finish flashing the LCD.

Data Mode:

Again as the name suggests we send actual data that we want to be printed on the LCD, in this mode. We can print numbers, letters and characters on the LCD using this mode. Basically, we use this mode any time we want something to be printed on the LCD. To work in the Command Mode and print data on the LCD, the below steps must be followed.

  1. RS pin must be set to HIGH for choosing the Data Mode.
  2. RW pin must be set to LOW for writing the Data on the LCD.
  3. The data is passed to the Data pins of the LCD.
  4. Enable pin is turned HIGH for flashing the LCD.
  5. We wait for 500 microseconds for the LCD to process the data.
  6. Enable pin is turned LOW to finish flashing the LCD.

I would now ask you to go through the video below where I explain the Code in details:

Video:

 

Download the Makefile needed for compilation and copy it into the folder where you have saved the source file.

Code:

Shop Smart with us for Best Prices:

If you want to continue on with this series I would ask you to visit our YouTube channel and subscribe to receive continuous notifications. You can also subscribe to our Newsletter below to get email notification of each post as and when they are posted.

You can also follow us on social media platforms such as  Facebook and Twitter.

Thanks a lot for reading through this post. Feel free to follow through this series and comment your queries in the comment box below. Also feel free to contact us regarding any out of stock product and we will try to get back to you as soon as possible.

Leave a Reply