Author Topic: Lcd 4 bit initialization problem after MCU reset  (Read 5146 times)

0 Members and 2 Guests are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 488
Lcd 4 bit initialization problem after MCU reset
« on: November 16, 2014, 05:16:27 pm »
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: [Select]
void main(void)
{
    const uint8_t str1[] = "******";
    const uint8_t str2[] = "999999";

    configure_lcd();
    lcd_string_print(&str1[0] , 0x80U);
    lcd_string_print(&str2[0] , 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);
   

 

Offline alank2

  • Super Contributor
  • ***
  • Posts: 2196
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #1 on: November 16, 2014, 08:04:39 pm »
D0-D3 should be left disconnected per the datasheet...
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #2 on: November 16, 2014, 08:33:13 pm »
The 0x30s sent to set 4 bit mode are not being strobed into the LCD.
 

Offline Ansel

  • Newbie
  • Posts: 1
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #3 on: November 17, 2014, 07:34:04 am »
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
« Last Edit: November 19, 2014, 07:04:03 am by Ansel »
 

Offline lapm

  • Frequent Contributor
  • **
  • Posts: 564
  • Country: fi
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #4 on: November 17, 2014, 01:41:03 pm »
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...
Electronics, Linux, Programming, Science... im interested all of it...
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #5 on: November 17, 2014, 02:34:03 pm »
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...

You 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.
 

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 488
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #6 on: November 17, 2014, 04:19:02 pm »
Tomorrow I will try to init the lcd twice to see if that works
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3322
  • Country: gb
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #7 on: November 17, 2014, 06:55:36 pm »
Tomorrow I will try to init the lcd twice to see if that works

But 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.
 

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 488
Re: Lcd 4 bit initialization problem after MCU reset
« Reply #8 on: November 21, 2014, 06:18:48 am »
Thanks @Rufus, problem solved.
Lcd is working fine after reset
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf