Lcd 4 bit initialization problem after MCU reset - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: November 16, 2014, 05:16:27 pm
content: 1. I have interfaced 16x2 lcd in 4 bit mode. D0-D3 & R/w of lcd are grounded.2. If system is power on, then lcd works properly. No issue. I have tried many times.3. But when I press reset button on MCU so that it get reset & reinit the lcd. It don't works most of the time.I don't understand why. Sometimes some random chinese number, some cursor only & sometimes random numbers.4. All other pins interfaced worked fine when MCU get reset like led's & adc module except lcd.5. Below is my code for lcd.Code:void main(void){const uint8_t str1[] = "******";const uint8_t str2[] = "999999";configure_lcd();lcd_string_print(&str1 , 0x80U);lcd_string_print(&str2 , 0xc0U);while(1);}void configure_lcd(void){initialize_lcd_pins();init_lcd();}void lcd_string_print(const uint8_t *p_str , uint8_t address){uint8_t cnt = 0U; /* max 16 chars to be printed */write_lcd_command(address);while( (*p_str != '\0') && (cnt < 16U) ){write_lcd_data(*p_str);p_str++;}}void write_lcd_data(uint8_t value){uint8_t cnt;LCD_RS_HIGH();shift_data_on_lcd_pins(value & 0xf0U);toggle_lcd_en_pin();/* delay for 12ms */__delay_us(12000);shift_data_on_lcd_pins( (value << 4U) & 0xf0U );toggle_lcd_en_pin( );/* delay for 12ms */__delay_us(12000);}void write_lcd_command(uint8_t value){uint8_t cnt;LCD_RS_LOW();shift_data_on_lcd_pins(value & 0xf0U);toggle_lcd_en_pin();/* delay for 12ms */__delay_us(12000);shift_data_on_lcd_pins( (value << 4U) & 0xf0U );toggle_lcd_en_pin( );/* delay for 12ms */__delay_us(12000);}static void init_lcd(void){uint8_t cnt;/* delay for 300ms */__delay_ms(300);/* rs shoud be low */LCD_RS_LOW();/* write 0x30 on port */shift_data_on_lcd_pins(0x30U);__delay_us(12000);/* write 0x30 on port */shift_data_on_lcd_pins(0x30U);__delay_us(12000);/* write 0x30 on port */shift_data_on_lcd_pins(0x30U);__delay_us(12000);/* write 0x20 on port */shift_data_on_lcd_pins(0x20U);toggle_lcd_en_pin();write_lcd_command(0x28U);__delay_us(12000);write_lcd_command(0x0cU);__delay_us(12000);write_lcd_command(0x01U);__delay_us(12000);write_lcd_command(0x06U);__delay_us(12000);write_lcd_command(0x80U);__delay_us(12000);write_lcd_command(0x0eU);__delay_us(12000);}static void shift_data_on_lcd_pins(uint8_t value){if(value & REG8_BIT_7){LCD_D7_HIGH();}else{LCD_D7_LOW();}if(value & REG8_BIT_6){LCD_D6_HIGH();}else{LCD_D6_LOW();}if(value & REG8_BIT_5){LCD_D5_HIGH();}else{LCD_D5_LOW();}if(value & REG8_BIT_4){LCD_D4_HIGH();}else{LCD_D4_LOW();}}void initialize_lcd_pins(void){LCD_EN_OUTPUT_LOW();LCD_RS_OUTPUT_LOW();LCD_D7_OUTPUT_LOW();LCD_D6_OUTPUT_LOW();LCD_D5_OUTPUT_LOW();LCD_D4_OUTPUT_LOW();}static void toggle_lcd_en_pin(void){LCD_EN_HIGH();__delay_us(500);LCD_EN_LOW();__delay_us(500);}

author: alank2
author_rank: Super Contributor
timestamp: November 16, 2014, 08:04:39 pm
content: D0-D3 should be left disconnected per the datasheet...

author: Rufus
author_rank: Super Contributor
timestamp: November 16, 2014, 08:33:13 pm
content: The 0x30s sent to set 4 bit mode are not being strobed into the LCD.

author: Ansel
author_rank: Newbie
timestamp: November 17, 2014, 07:34:04 am
content: Below is my code for lcd.But when I press reset button on MCU so that it get reset & reinit the lcd. It don't works most of the time.I don't understand why. Sometimes some random chinese number, some cursor only & sometimes random numbers.If system is power on, then lcd works properly. No issue. I have tried many times.I have interfaced 16x2 lcd in 4 bit mode. D0-D3 & R/w of lcd are grounded.All other pins interfaced worked fine when MCU get reset like led's & adc module except lcd.translation services uk

author: lapm
author_rank: Frequent Contributor
timestamp: November 17, 2014, 01:41:03 pm
content: Read datasheet of display very very carefully... Some displays needs to initialized to 4 bit mode twice... had few of 1x16 displays that require this...

author: Rufus
author_rank: Super Contributor
timestamp: November 17, 2014, 02:34:03 pm
content: Quote from: lapm on November 17, 2014, 01:41:03 pmYou need to write 8 bit mode 3 times. If the display is 'on' the second half of a 4 bit transfer the first write will be lost, if the display is in 4 bit mode it takes two writes to set 8 bit mode. If the display is in 8 bit mode you just set it again 3 times.After writing 8 bit mode 3 times with the appropriate delays you are sure it is in 8 bit mode and can then set it to 4 bit mode. I have never seen a requirement to set 4 bit mode twice.

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: November 17, 2014, 04:19:02 pm
content: Tomorrow I will try to init the lcd twice to see if that works

author: mikerj
author_rank: Super Contributor
timestamp: November 17, 2014, 06:55:36 pm
content: Quote from: Vindhyachal.takniki on November 17, 2014, 04:19:02 pmBut take note of what Rufus said in his first post! In init_lcd() you set 0x30 on the LCD data pins three times, but you never toggle the Enable pin so this won't be doing anything.

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: November 21, 2014, 06:18:48 am
content: Thanks @Rufus, problem solved.Lcd is working fine after reset