ATmega8A interface with JHD162A LCD Display in 4-bit mode - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: February 10, 2021, 07:29:12 pm
content: Hi All,I am interfacing ATmega8A microcontroller with JHD 162A LCD interface in 4-bit mode.The following are the codes of this interface.Code:#define F_CPU 8000000UL#include <avr/io.h>#include <util/delay.h>void I_O_Initialization();void LCD_Command(unsigned char cmnd);void LCD_char(unsigned char data);void LCD_Initialization();void I_O_Initialization() // IO Port Initialization{DDRD = 0x01; ///// PD0 -> Output DirectionDDRC = 0x7F; ///// PORTC -> Output Direction}void LCD_Command(unsigned char cmnd){PORTC = (PORTC & 0x0F) | (cmnd >> 4) ; ///// Higher Nibble bitPORTD = 0x00; ///// RS = 0 //////PORTD &= ~ (1<<PD0);PORTC |= (1<<PC4); ///// EN = 1_delay_us(1);PORTC &= ~(1<<PC4); ///// EN = 0_delay_ms(2);_delay_us(200);PORTC = (PORTC & 0xF0) | (cmnd & 0x0F) ; ////// Lower NibblePORTC |= (1<<PC4); ////// EN = 1_delay_us(1);PORTC &= ~(1<<PC4); ////// EN = 0_delay_ms(2);}void LCD_char(unsigned char data){//PORTD |= (1<<PD0);PORTC = (PORTC & 0x0F) | (data >> 4) ;PORTD = 0x01; //PORTD |= (1<<PD0);PORTC |= (1<<PC4);_delay_us(1);PORTC &= ~(1<<PC4);_delay_ms(2);_delay_us(200);PORTC = (PORTC & 0xF0) | (data& 0x0F) ;PORTC |= (1<<PC4);_delay_us(1);PORTC &= ~(1<<PC4);_delay_ms(2);}void LCD_Initialization(){_delay_ms(20);LCD_Command(0x20); // 4-bit mode_delay_ms(7);LCD_Command(0x20); //4-bit mode_delay_us(150);LCD_Command(0x20); //4-bit mode_delay_ms(200);LCD_Command(0x28); // 2 line 5x7matrixLCD_Command(0x0C); // Display no cursor no blinkLCD_Command(0x06); //automatic incrementLCD_Command(0x01); // CLear screen_delay_ms(200);}int main(void){I_O_Initialization();LCD_Initialization();while (1){LCD_char('W');_delay_ms(500);LCD_char('X');_delay_ms(500);LCD_char('Y');_delay_ms(500);}}In microcontroller, Pins (PC0 - PC3) are connected to pins (DB4-DB7) of JHD16A LCD display.When I send upper Nibble, used that bit shift operator and lower bit case, used AND/OR operation.I tried so many times, but i didn't get anything on my LCD screen.Kindly I request you , give your suggestions.(Note : that supply is +5V)Thanks...!

author: ozcar
author_rank: Frequent Contributor
timestamp: February 10, 2021, 10:04:00 pm
content: Have you provided a way to adjust the voltage on pin 3 ("VEE")? Maybe the contrast is so way-out that you can't see anything.

author: ozcar
author_rank: Frequent Contributor
timestamp: February 11, 2021, 04:29:26 pm
content: I had another look at your code, not the detail of how you output the data, but just the overall sequence.I notice that you start off the initialisation by setting the interface data length to 4 bits (repeat for total of three times). The data sheet for the very first of these sort of displays that I used (25 years ago!) advised to start off by setting the interface data length to 8 bits (three times), then finally set it to 4 bit mode (once only). Since then, that is what I have always done.The data sheet I can find for JHD162A (https://drive.google.com/file/d/1Pjc3C2ksZwIndoZtflCykHWC_UVc7aVE/view) also suggests first setting 8-bit mode (three times), before setting 4-bit mode (but, if I am reading it correctly, says set it to 4-bit mode twice, not only once).Maybe what you are doing should also work, it is not something I ever investigated or tried. However, if you can't find anything else wrong, maybe you should try setting 8-bit mode first.

author: madires
author_rank: Super Contributor
timestamp: February 11, 2021, 04:54:28 pm
content: Quote from: arul on February 10, 2021, 07:29:12 pm"PORTC & 0x0F" will keep the lower four bits which are the data lines. Shouldn't it be 0xF0 to keep the higher four bits and clear the lower four?

author: arul
author_rank: Regular Contributor
timestamp: February 12, 2021, 12:28:59 pm
content: Controller pins of PC0 to PC3 are data lines. Because controller pins of PC0 to PC3 are connected to DB4 to DB7 of JHD 162A display pins.

author: arul
author_rank: Regular Contributor
timestamp: February 12, 2021, 12:31:18 pm
content: After varying the voltage on VEE pin, I could see some junk characters on that display. But still I couldn't get proper characters on that display.

author: arul
author_rank: Regular Contributor
timestamp: February 12, 2021, 01:00:25 pm
content: Hi @ozcar,These are my initialization commands,Code:_delay_ms(20);LCD_Command(0x30); // 8-bit mode_delay_ms(7);LCD_Command(0x30); //8-bit mode_delay_us(150);LCD_Command(0x30); //8-bit mode_delay_ms(200);_delay_ms(20);LCD_Command(0x20); // 4-bit mode_delay_ms(7);LCD_Command(0x20); //4-bit mode_delay_us(150);LCD_Command(0x20); //4-bit modeLCD_Command(0x00);LCD_Command(0x80);LCD_Command(0x00);LCD_Command(0x10);LCD_Command(0x00);_delay_ms(200);LCD_Command(0x28); // 2 line 5x7matrixLCD_Command(0x0C); // Display no cursor no blinkLCD_Command(0x06); //automatic incrementLCD_Command(0x01); // CLear screen_delay_ms(200);Still got junk characters only, please let me know, what I have been doing wrong

author: mon2
author_rank: Frequent Contributor
timestamp: February 12, 2021, 02:43:06 pm
content: Start with the basics.Output a value of 0000 binary on your cpu and enter a permanent loop. Confirm with a meter that the pattern is there...all lows.Then change to value of 0101b and confirm again.Then 1010b and confirm again.Post your results.

author: iMo
author_rank: Super Contributor
timestamp: February 12, 2021, 07:03:36 pm
content: Quote from: mon2 on February 12, 2021, 02:43:06 pm@arul: Or do use the arduino environment - you will certainly find the LCD library there..

author: mon2
author_rank: Frequent Contributor
timestamp: February 12, 2021, 07:31:02 pm
content: https://www.electronicwings.com/avr-atmega/interfacing-lcd-16x2-in-4-bit-mode-with-atmega-16-32-https://blog.circuits4you.com/2015/04/atmega8-interfacing-with-16x2-lcd-in-4.html

author: ozcar
author_rank: Frequent Contributor
timestamp: February 12, 2021, 07:35:47 pm
content: Quote from: arul on February 12, 2021, 12:28:59 pmHave you got them connected in the correct order? The diagram you posted shows them the wrong way (eg PC0 should connect to DB4, not DB7).Otherwise follow the advice to output static patterns so you can check that they are as expected (for RS and E as well as data).

author: arul
author_rank: Regular Contributor
timestamp: February 25, 2021, 09:33:32 am
content: Hi All,After I changed to individual bitwise shift{void LCD_Port(char port) - function}and commands, I got the output on screen. The following is the codeCode:#ifndef F_CPU#define F_CPU 16000000UL // 16 MHz clock speed#endif#include <avr/io.h>#include <util/delay.h>#include <string.h>void I_O_Initialization();void LCD_Port(char port);void LCD_Command(unsigned char cmnd);void LCD_char(unsigned char ch);void LCD_string(char *str);void Lcd4_Set_Cursor(char a, char b);void LCD_Clear();void I_O_Initialization() // IO Port Initialization{DDRD = 0xFF; ///// PORTD -> Output DirectionDDRC = 0x7F; ///// PORTC -> Output Direction}void LCD_Port(char port){if(port & 1)PORTC |= (1<<PC0);elsePORTC &= ~(1<<PC0);if(port & 2)PORTC |= (1<<PC1);elsePORTC &= ~(1<<PC1);if(port & 4)PORTC |= (1<<PC2);elsePORTC &= ~(1<<PC2);if(port & 8)PORTC |= (1<<PC3);elsePORTC &= ~(1<<PC3);}void LCD_Command(unsigned char cmnd){PORTD &= ~(1<<PD0);LCD_Port(cmnd);PORTC |= (1<<PC4);_delay_ms(1);PORTC &= ~(1<<PC4);_delay_ms(1);}void LCD_char(unsigned char data){char temp,y;temp = data & 0x0F;y = data & 0xF0;PORTD |= (1<<PD0); // => RS = 1LCD_Port(y>>4); //Data transferPORTC |= (1<<PC4);_delay_ms(1);PORTC &= ~(1<<PC4);_delay_ms(1);LCD_Port(temp);PORTC |= (1<<PC4);_delay_ms(1);PORTC &= ~(1<<PC4);_delay_ms(1);}void LCD_string(char *str){int i;for(i=0; str != '\0'; i++){LCD_char(str);}}void Lcd4_Set_Cursor(char a, char b){char temp,z,y;if(a == 1){temp = 0x80 + b;z = temp>>4;y = (0x80+b) & 0x0F;LCD_Command(z);LCD_Command(y);}else if(a == 2){temp = 0xC0 + b;z = temp>>4;y = (0xC0+b) & 0x0F;LCD_Command(z);LCD_Command(y);}}void LCD_Initialization(){LCD_Command(0x00);_delay_ms(20);///////////// Reset process from datasheet /////////LCD_Command(0x03);_delay_ms(5);LCD_Command(0x03);_delay_ms(11);LCD_Command(0x03);/////////////////////////////////////////////////////LCD_Command(0x02);LCD_Command(0x02);LCD_Command(0x08);LCD_Command(0x00);LCD_Command(0x0C);LCD_Command(0x00);LCD_Command(0x06);}void LCD_Clear(){LCD_Command(0);LCD_Command(1);}int main(void){I_O_Initialization();LCD_Initialization();while (1){//Lcd4_Set_Cursor(1,1);//LCD_char('W'); LCD_char('i'); LCD_char('W'); LCD_char('0'); LCD_char(' '); LCD_char('T'); LCD_char('E'); LCD_char('C'); LCD_char('H');//_delay_ms(500);//Lcd4_Set_Cursor(2,1);//LCD_char('S'); LCD_char('Y'); LCD_char('S'); LCD_char(' '); LCD_char('P'); LCD_char('V'); LCD_char('T'); LCD_char(' '); LCD_char('L');//LCD_char('T');LCD_char('D');//_delay_ms(500); _delay_ms(500);//LCD_Clear();Lcd4_Set_Cursor(1,1);LCD_string("Hello World"); _delay_ms(500);Lcd4_Set_Cursor(2,1);LCD_string("JHD 162A"); _delay_ms(500); _delay_ms(500);LCD_Clear();}}Thank you so much all for your kind support...!