Connect with us

2 x 16 lcd 4 bit mode

Discussion in 'Microcontrollers, Programming and IoT' started by Rajinder, Jun 25, 2017.

  1. Rajinder

    Rajinder

    419
    7
    Jan 30, 2016
    hi all,
    I am using a pic to connect to a 2 x 16 lcd in 4 bit mode.
    I can get the cursor to blink on and off, also move to any position on line 1 or 2. however when I come to display character the screen goes blank.
    I am using a pic18f4550 and pic dem plus (microchip demo board).
    here is my code:
    (written in Mikro C)


    #define TRUE 1
    #define LCD_LINE_1 0x80
    #define LCD_LINE_2 0x40


    sbit RS at RA3_bit;
    sbit EN at RA1_bit;
    sbit RW at RA2_bit;

    /* function prototype */
    void lcdInit(void);
    void lcdWrite(unsigned char);
    void lcdOut(char c);
    void lcdLine(unsigned char);
    void configurePort(void);
    void lcdStrobe(void);
    void lcdWriteData(char);

    char Hello [ ] = "Hello"; /*char string*/

    void main()
    {
    int index = 0;
    char outchar;
    lcdInit();
    lcdLine(0x01); // select first line, second line 0x40
    //for (index = 0; index <5; index++)
    while(TRUE)
    {
    outchar = Hello[0];
    lcdWriteData(outchar); // write character data to LCD
    }
    }

    void lcdInit()
    {
    ADCON1 = 0x07;
    TRISA = 0x00;
    TRISD = 0x00;
    RS = 0; /* RS =0 - instruction to LCD, RS=1 character to lcd */
    EN = 0; /* EN=0 lcd disabled, EN=1 enabled */
    RW = 0; /* RW = 0 - data written to lcd, RW = high data read from lcd */

    delay_ms(15); /* wait 15mSec after power applied, */
    PORTD = 0x03;
    lcdStrobe();

    delay_ms(5); //5
    PORTD =0x03;
    lcdStrobe();

    Delay_us(120); //120us
    PORTD = 0x03;
    lcdStrobe();

    Delay_ms(5);
    PORTD =0x02; //Four bit mode
    lcdStrobe();

    delay_us(40);

    lcdWrite(0x28);
    lcdWrite(0x0f); /* 0x0f D=1 display on, c=1 cursor on, b=1 cursor blink */
    lcdWrite(0x06); /* 0x06 I/D = 1 inc cursor position, s = 0 - no display shift*/
    lcdWrite(0x01); /* 0x01*/
    }

    void lcdWrite(unsigned char c)
    {
    unsigned char copy;
    copy = c;
    RS=0;
    RW=0;
    delay_ms(50);
    PORTD = ( ( c >> 4 ) & 0x0F );
    lcdStrobe();
    PORTD = ( copy & 0x0F );
    lcdStrobe();
    }


    /* Go to the specified position */
    void lcdLine(unsigned char pos)
    {
    RS = 0; //0
    lcdWrite(LCD_LINE_1+pos);
    }

    void lcdStrobe()
    {
    EN = 1;
    delay_us(10); /* us */
    EN = 0;
    }

    void lcdWriteData(char d)
    {
    unsigned char copy;
    copy = d;
    RS = 1;
    RW = 0;

    PORTD = ( ( d >> 4 ) & 0x0F );
    lcdStrobe();
    PORTD = ( copy & 0x0F );
    lcdStrobe();
    }
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-