AVR-GCC LCD library – mixed pin support using Atmega328P

Some time ago we have posted alphanumeric AVR-GCC LCD library. It works fine in 8-bit and 4-bit modes. But it has some limitations that some people may find annoying. One of them is requirement that LCD pins has to be byte aligned for instance in 8 bit mode LCD_D0 … LCD_D7 pins has to be connected to AVR single AVR port. Similar situation is with 4-bit mode where LCD data pins has to be connected to single port 4, 5, 6 and 7 pins. For both modes control pins RS, RW and E has to be connected to single port.
LCD library
And this is how most LCD libraries work when you try to look for one in the internet. In reality things may be a bit different each microcontroller pin has at least several alternative functions available like ADC, INT, I2C, USART and if project requires using one or another and you still need LCD standard libraries won’t work as most likely you wont be able to get all particular port pins connected to LCD. You gotta use whats left. This is why I decided to find a little time and modify LCD library to support these cases. Didn’t want to write everything from scratch or change its functionality – just wanted it work with existing projects but have more freedom with new ones. So basically I left standard 8-bit and 4-bit same. The main change is adding two more modes: 8-bit mix and 4-bit mix. These modes allow connected LCD to any free pins of microcontroller.

Configuring LCD for 4-bit mixed pin mode

Lets look what you need to start using these features. We can do this by selecting a simple example. This time 4-bit mixed mode:
As you can see LCD pins are connected to Atmega328P in following way:
RS – > PB5
RW – > PD1
E – > PD2
D4 – > PD4
D5 – > PC2
D6 – > PD6
D7 – > PB0

So we get unaligned situation. Firs of all we need to edit pin assignments in lcd_lib.h. First of all uncomment one of following defines that indicate the mode chosen

//LCD 4 bit interface is used (single port pins)
//#define LCD_4BIT
//LCD 8 bit interface is used (single port pins)
//#define LCD_8BIT
//LCD 4 bit interface is used (mixed port pins)
#defineLCD_4BIT_M
//LCD 8 bit interface is used (mixed port pins)
//#define LCD_8BIT_M
This time we use LCD_4BIT_M

Then we need to associate LCD pins with port pin numbers. If LCD_RS is connected to PB5 pin then we write 5:

#defineLCD_RS5//define MCU pin connected to LCD RS
#defineLCD_RW1//define MCU pin connected to LCD R/W
#defineLCD_E2//define MCU pin connected to LCD E
#defineLCD_D00//define MCU pin connected to LCD D0
#defineLCD_D11//define MCU pin connected to LCD D1
#defineLCD_D22//define MCU pin connected to LCD D2
#defineLCD_D33//define MCU pin connected to LCD D3
#defineLCD_D44//define MCU pin connected to LCD D4
#defineLCD_D52//define MCU pin connected to LCD D5
#defineLCD_D66//define MCU pin connected to LCD D6
#defineLCD_D70//define MCU pin connected to LCD D7

And now we have to define port and data direction register for each pin. As pins may be connected to different ports – we need to work with individual pins. We edit this part:

#ifdefLCD_4BIT_M||LCD_8BIT_M//8- bit or 4 – bit mode
#defineLDPRSPORTB//RS pin assignment
#defineLDDRSDDRB
#defineLDPRWPORTD//RW pin assignment
#defineLDDRWDDRD
#defineLDPEPORTD//E pin assignment
#defineLDDEDDRD
#defineLDPD0PORTD//D0 pin assignment
#defineLDDD0DDRD
#defineLDPD1PORTD//D1 pin assignment
#defineLDDD1DDRD
#defineLDPD2PORTD//D2 pin assignment
#defineLDDD2DDRD
#defineLDPD3PORTD//D3 pin assignment
#defineLDDD3DDRD
#defineLDPD4PORTD//D4 pin assignment
#defineLDDD4DDRD
#defineLDPD5PORTC//D5 pin assignment
#defineLDDD5DDRC
#defineLDPD6PORTD//D6 pin assignment
#defineLDDD6DDRD
#defineLDPD7PORTB//D7 pin assignment
#defineLDDD7DDRB
#endif

This is practically it. We can start using LCD as we did in old library version.

Configuring LCD for 8-bit mixed pin mode

To make sure things are working correctly lets set up project for 8-bit mixed mode. To do so we connect LCD as follows:
 
For more detail: AVR-GCC LCD library – mixed pin support using Atmega328P


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top