Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Cannot Initialize LCD From Function

Status
Not open for further replies.

boze

Newbie level 1
Joined
Feb 19, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,294
I am using a PIC16F684, with the HiTech PICC Lite (ver 9.83), to interface to a Hitachi compatible 16x2 LCD display (P/N: JHD 162A). I have code that will initialize the LCD and display 1 of 2 messages depending on if a button is pressed or not. This code works fine when the LCD initialization sequence is done in Main, but when I tried to move the initialization to a function that is called from Main, the LCD fails to initialize. I do not get any errors, and everything seems fine when I step through the code with MPLAB Sim. In an effort to find my problem, I began moving the initialization commands to a function, one at a time. My problem seems to be the step where the LCD is cleared. If I clear the LCD inside the function, I get a blank display and the button does nothing. However, if I begin the initialization sequence in the function, then clear the LCD in Main and finish the sequence in Main, everything works like it should and text is displayed on the screen.

When simulated in MPLAB Sim, the Port output values are the same in either case, and there is not a big difference in the timing. I have tried adding delays in various places, but nothing seems to make a difference.

If anyone can tell me what I am doing wrong, or why I can't move the entire initialization sequence to a function, I would greatly appreciate it. I am stumped.

My code is posted below:

Code:
//LCD - 4 bit interface
#include <pic.h>

//"NEW" Configuration Bit Symbols
__CONFIG(FOSC_INTOSCCLK & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF \
  & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF);

#define E               RC4               //Set LCD Enable pin
#define RS              RC5               //Set LCD Register Select pin
#define LCD_DATA	    PORTC             //Set LCD data pins
#define	LCD_STROBE()	((E = 1),(E=0))   //Strobe E pin to clock data in
#define _XTAL_FREQ      4000000           //Define freq for Delay routines

//LCD Write////////////////////////////////////////////////////////////////////////////////////////
void LCDWrite(int LCDData, int RSValue)
{
  LCD_DATA = (LCDData >> 4) & 0x0F;              //Get High 4 Bits for Output
  RS = RSValue;
  LCD_STROBE();                                  //Toggle the High 4 Bits Out
  LCD_DATA = LCDData & 0x0F;                     //Get Low 4 Bits for Output
  RS = RSValue;
  LCD_STROBE();                                  //Toggle the Low 4 Bits Out

  if ((0 == (LCDData & 0xFC)) && (0 == RSValue))
    __delay_ms(5);                               //Set delay for commands
  else
    __delay_us(200);                             //Set delay for data
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Move Cursor//////////////////////////////////////////////////////////////////////////////////////
void lcd_goto(unsigned char pos)
{
//  RS = 0;
  LCDWrite(0x80+pos, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Write a string of Chars to LCD///////////////////////////////////////////////////////////////////
void lcd_puts(const char * s)
{
//  RS = 1;
  while(*s)
    LCDWrite(*s++, 1);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Initialize LCD///////////////////////////////////////////////////////////////////////////////////
void lcd_init(void)
{
  RS = 0;                         //Set RS to 0 for sending commands
  __delay_ms(20);                 //Wait 20ms for LCD to Power Up
  LCD_DATA = 3;                   //Start Initialization Process
  LCD_STROBE();                   //Toggle Command In
  __delay_ms(5);                  //Wait 5ms
  LCD_STROBE();                   //Toggle Command In Again
  __delay_us(200);                //Wait 200us
  LCD_STROBE();                   //Toggle Command In A Third Time
  __delay_us(200);                //Wait 200us
  LCD_DATA = 2;                   //Initialize LCD 4 Bit Mode
  LCD_STROBE();                   //Toggle Command In
  __delay_us(200);                //Wait 200us
  LCDWrite(0b00101000, 0);        //LCD is 4 Bit I/F, 2 Line
  LCDWrite(0b00001000, 0);        //Turn Display off
//  LCDWrite(0b00000001, 0);        //Clear LCD 
//  LCDWrite(0b00000110, 0);        //Move Cursor After Each Character
//  LCDWrite(0b00001110, 0);        //Turn On LCD and Enable Cursor
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

//Main/////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
//Set Fosc = 4 MHz//
  IRCF0 = 0;      //
  IRCF1 = 1;      //
  IRCF2 = 1;      //
////////////////////
  PORTC  = 0b00000000;            //Start with Everything Low
  TRISC  = 0b00000000;            //All of PORTC are Outputs
  PORTA  = 0b00000000;            //Start with Everything Low
  TRISA  = 0b00001000;            //Only RA3 (button) is an input
  CMCON0 = 0b00000111;            //Turn off Comparators
  ANSEL  = 0b00000000;            //Turn off ADC

  lcd_init();
//Initialize LCD///////////////////////////////////////////////////////////////////////////////////
////  RS = 0;                         //Set RS to 0 for sending commands
////  __delay_ms(20);                 //Wait 20ms for LCD to Power Up
////  LCD_DATA = 3;                   //Start Initialization Process
////  LCD_STROBE();                   //Toggle Command In
////  __delay_ms(5);                  //Wait 5ms
////  LCD_STROBE();                   //Toggle Command In Again
////  __delay_us(200);                //Wait 200us
////  LCD_STROBE();                   //Toggle Command In A Third Time
////  __delay_us(200);                //Wait 200us
////  LCD_DATA = 2;                   //Initialize LCD 4 Bit Mode
////  LCD_STROBE();                   //Toggle Command In
////  __delay_us(200);                //Wait 200us
////  LCDWrite(0b00101000, 0);        //LCD is 4 Bit I/F, 2 Line
  LCDWrite(0b00000001, 0);        //Clear LCD 
  LCDWrite(0b00000110, 0);        //Move Cursor After Each Character
  LCDWrite(0b00001110, 0);        //Turn On LCD and Enable Cursor
///////////////////////////////////////////////////////////////////////////////////////////////////

  while(1 == 1)                        //Loop Forever
  {
    if (RA3 == 0)                      //If button is pressed
    {
//      RA5 = 1;
//        LCDWrite(0b00000001, 0);
//        lcd_clear();
      lcd_goto(0x00);                  //Move cursor to 1st line of LCD
      lcd_puts("      Hello     ");    //First line of LCD
      lcd_goto(0x40);                  //Move cursor to 2nd line of LCD
      lcd_puts("      World     ");    //Second line of LCD	
    }
    else                               //When button is NOT pressed
    {
//      RA5 = 0;
      lcd_goto(0x00);                  //Move cursor to 1st line of LCD
      lcd_puts("     This is    ");    //First line of LCD
      lcd_goto(0x40);                  //Move cursor to 2nd line of LCD
      lcd_puts("     an LCD     ");    //Second line of LCD	
    }

  }//End Forever Loop

}//End Main
///////////////////////////////////////////////////////////////////////////////////////////////////
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top