richardlaishram
Member level 4
- Joined
- Jan 6, 2013
- Messages
- 77
- Helped
- 6
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 1,298
- Location
- Planet Earth
- Activity points
- 1,804
Every time I power on the board, the LCD shows some garbage characters and I have to press MCLR button to make it work. Following are the details of my code. Please let me know if there is any error with the code. It works fine with Proteus.
Configuration:
LCD Initialization Code (4 bit Mode):
I want the code to display the proper lines when I power up the board. Please help and thanks in advance.
Configuration:
__CONFIG(FOSC_HS & WDTE_ON & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
LCD Initialization Code (4 bit Mode):
Code:
void ToggleEpinOfLCD(void)
{
LCD_E = 1; // Give a pulse on E pin
__delay_us(E_Delay); // so that LCD can latch the
LCD_E = 0; // data from data bus
__delay_us(E_Delay);
}
void WriteCommandToLCD(unsigned char Command)
{
LCD_RS = 0; // Command
__delay_ms(40);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= (Command&0xF0); // Write Upper nibble of data
ToggleEpinOfLCD(); // Give pulse on E pin
PORTD &= 0x0F; // Make Data pins zero
PORTD |= ((Command<<4)&0xF0); // Write Lower nibble of data
ToggleEpinOfLCD(); // Give pulse on E pin
}
void WriteDataToLCD(char LCDChar)
{
LCD_RS = 1; // It is data
__delay_ms(40);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= (LCDChar&0xF0); // Write Upper nibble of data
ToggleEpinOfLCD(); // Give pulse on E pin
PORTD &= 0x0F; // Make Data pins zero
PORTD |= ((LCDChar<<4)&0xF0); // Write Lower nibble of data
ToggleEpinOfLCD(); // Give pulse on E pin
}
void InitLCD(void)
{
// Firstly make all pins output
LCD_E = 0; // E = 0
LCD_RS = 0; // RS = 0
LCD_Data_Bus_D4 = 0; // Data bus = 0
LCD_Data_Bus_D5 = 0; // Data bus = 0
LCD_Data_Bus_D6 = 0; // Data bus = 0
LCD_Data_Bus_D7 = 0; // Data bus = 0
LCD_E_Dir = 0; // Make Output
LCD_RS_Dir = 0; // Make Output
LCD_Data_Bus_Dir_D4 = 0; // Make Output
LCD_Data_Bus_Dir_D5 = 0; // Make Output
LCD_Data_Bus_Dir_D6 = 0; // Make Output
LCD_Data_Bus_Dir_D7 = 0; // Make Output
__delay_ms(40);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= 0x30; // Write 0x3 value on data bus
ToggleEpinOfLCD(); // Give pulse on E pin
__delay_ms(20);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= 0x30; // Write 0x3 value on data bus
ToggleEpinOfLCD(); // Give pulse on E pin
__delay_ms(20);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= 0x30; // Write 0x3 value on data bus
ToggleEpinOfLCD(); // Give pulse on E pin
__delay_ms(20);
PORTD &= 0x0F; // Make Data pins zero
PORTD |= 0x20; // Write 0x2 value on data bus
ToggleEpinOfLCD(); // Give pulse on E pin
__delay_ms(20);
WriteCommandToLCD(0x28); //function set
__delay_ms(20);
WriteCommandToLCD(0x0c); //display on,cursor off,blink off
__delay_ms(20);
WriteCommandToLCD(0x01); //clear display
__delay_ms(20);
WriteCommandToLCD(0x06); //entry mode, set increment
}
void WriteStringToLCD(const char *s)
{
while(*s)
WriteDataToLCD(*s++); // print first character on LCD
}
void ClearLCDScreen(void) // Clear the Screen and return cursor to zero position
{
WriteCommandToLCD(0x01); // Clear the screen
__delay_ms(20); // Delay for cursor to return at zero position
}
I want the code to display the proper lines when I power up the board. Please help and thanks in advance.