Can't init more than one line on LCD

Thread Starter

FroceMaster

Joined Jan 28, 2012
702
Hi
My init for LCD seems to malfunction,( but do proberly what i have told it to do :)
Get only line 1 active.
Here is LCD.c code
Code:
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif


#include    <htc.h>
#include    "lcd.h"

#define    LCD_RS RC0
#define LCD_EN RC1
#define LCD_D4 RB4    // Data bits
#define LCD_D5 RB5    // Data bits
#define LCD_D6 RB6    // Data bits
#define LCD_D7 RB7    // Data bits

#define    LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{   static unsigned char temp;
     temp=c;
    __delay_us(320);
   
    if(c & 0x80) LCD_D7=1; else LCD_D7=0;
    if(c & 0x40) LCD_D6=1; else LCD_D6=0;
    if(c & 0x20) LCD_D5=1; else LCD_D5=0;
    if(c & 0x10) LCD_D4=1; else LCD_D4=0;
    LCD_STROBE();
    if(c & 0x08) LCD_D7=1; else LCD_D7=0;
    if(c & 0x04) LCD_D6=1; else LCD_D6=0;
    if(c & 0x02) LCD_D5=1; else LCD_D5=0;
    if(c & 0x01) LCD_D4=1; else LCD_D4=0;

    LCD_STROBE();
}

/*
*     Clear and home the LCD
*/

void
lcd_clear(void)
{
    LCD_RS = 0;
    lcd_write(0x1);
    __delay_ms(16);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
    LCD_RS = 1;    // write characters
    while(*s)
        lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
    LCD_RS = 1;    // write characters
    lcd_write( c );
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
    LCD_RS = 0;
    lcd_write(0x80+pos);
}
   
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
       
//NY RUTINE       
        LCD_RS = 0;    // write control bytes

__delay_ms(280);// power on delay

    LCD_D4 = 1;    // init!   
    LCD_D5 = 1; //
    LCD_STROBE();   
    __delay_ms(40);

    LCD_STROBE();    // init!   
    __delay_us(800);

    LCD_STROBE();    // init!   
    __delay_us(40);

    LCD_D4 = 0;    // set 4 bit mode
    LCD_STROBE();   
    __delay_us(320);
   
    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    __delay_ms(1);
    lcd_write(0x0C);// display on
    __delay_ms(1);
    lcd_write(0x06);// entry mode advance cursor
    __delay_ms(1);
    lcd_write(0x01);// clear display and reset cursor
__delay_ms(1);
}
In main, I setup the ports / timers / ect, and init lcd at last.
added delay for 0.5 sec after init before rest code is run,
 

JSCV

Joined Oct 3, 2015
23
Hello FroceMAster,
You should give us some more information to help you,
MCU type + used clockspeed
LCD type (controller type.)
 

DNA Robotics

Joined Jun 13, 2014
649
You need to set the cursor position to the second line before printing.

lcd.setCursor( 0, 1 ); //second line left for Arduino

LCD_goto(2,0); // for some varieties of PIC

Edited: lcd_goto(unsigned char pos) on yours.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Hi
My init for LCD seems to malfunction,( but do proberly what i have told it to do :)
Get only line 1 active.
Here is LCD.c code
Code:
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 8MHz system frequency is assumed
#define _XTAL_FREQ 8000000
#endif


#include    <htc.h>
#include    "lcd.h"

#define    LCD_RS RC0
#define LCD_EN RC1
#define LCD_D4 RB4    // Data bits
#define LCD_D5 RB5    // Data bits
#define LCD_D6 RB6    // Data bits
#define LCD_D7 RB7    // Data bits

#define    LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))

/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{   static unsigned char temp;
     temp=c;
    __delay_us(320);
  
    if(c & 0x80) LCD_D7=1; else LCD_D7=0;
    if(c & 0x40) LCD_D6=1; else LCD_D6=0;
    if(c & 0x20) LCD_D5=1; else LCD_D5=0;
    if(c & 0x10) LCD_D4=1; else LCD_D4=0;
    LCD_STROBE();
    if(c & 0x08) LCD_D7=1; else LCD_D7=0;
    if(c & 0x04) LCD_D6=1; else LCD_D6=0;
    if(c & 0x02) LCD_D5=1; else LCD_D5=0;
    if(c & 0x01) LCD_D4=1; else LCD_D4=0;

    LCD_STROBE();
}

/*
*     Clear and home the LCD
*/

void
lcd_clear(void)
{
    LCD_RS = 0;
    lcd_write(0x1);
    __delay_ms(16);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
    LCD_RS = 1;    // write characters
    while(*s)
        lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(char c)
{
    LCD_RS = 1;    // write characters
    lcd_write( c );
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
    LCD_RS = 0;
    lcd_write(0x80+pos);
}
  
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
      
//NY RUTINE      
        LCD_RS = 0;    // write control bytes

__delay_ms(280);// power on delay

    LCD_D4 = 1;    // init!  
    LCD_D5 = 1; //
    LCD_STROBE();  
    __delay_ms(40);

    LCD_STROBE();    // init!  
    __delay_us(800);

    LCD_STROBE();    // init!  
    __delay_us(40);

    LCD_D4 = 0;    // set 4 bit mode
    LCD_STROBE();  
    __delay_us(320);
  
    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    __delay_ms(1);
    lcd_write(0x0C);// display on
    __delay_ms(1);
    lcd_write(0x06);// entry mode advance cursor
    __delay_ms(1);
    lcd_write(0x01);// clear display and reset cursor
__delay_ms(1);
}
In main, I setup the ports / timers / ect, and init lcd at last.
added delay for 0.5 sec after init before rest code is run,
Post ALL of your code relevent to the LCD. I don't see any where you are actually wring to the LCD.
 

geekoftheweek

Joined Oct 6, 2013
1,216
A couple things I noticed from my limited experience (I've only worked with three displays so far...)

Code:
    LCD_D4 = 1;    // init!   
    LCD_D5 = 1; //
LCD_D4 should be 0 in this step. You seem to tell it 8 bit to start then switch to 4 bit in the next command.

Code:
    lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
I have a Vishay display that it seems it wants to see this line twice.

A specific display would help. There are at least two common controller ic's that are used and each takes slightly different initialization routines.
 
Last edited:
Top