Problem initializing 4-bit mode LCD(DEM 20486 SBH-PW-N)

avrlcd

I've got an ATMEGA16 with a LCD display DEM 20486 SBH-PW-N (datasheet) connected to port C like so

  • C0 – RS
  • C1 – RW
  • C2 – E
  • C3 – not Set
  • C4 – D4
  • C5 – D5
  • C6 – D6
  • C7 – D7
    D0-D3 are grounded

I haven't been able to find much detailed information on how to work with the LCD and I've been trying this for a good two days, so any information as to what I've been doing wrong would be helpful.

#define LCDPORT PORTC
#define LCDPIN PINC
#define LCDDDR DDRC
#define RS 0
#define RW 1
#define EN 2

#define D4 4
#define D5 5
#define D6 6
#define D7 7

#include <avr/io.h>
#include <util/delay.h>

void LcdFlash() {  // Flash enable flag
    LCDPORT |=  (1<<EN);
    _delay_ms(2);
    LCDPORT &= ~(1<<EN);
    _delay_ms(2);
}

void LcdSendNibble(char cmd, char rs) {
    LCDPORT &= ~(1<<RW); // Write mode
    if(rs)
        LCDPORT &= ~(1<<RS); // Command mode
    else
        LCDPORT |=  (1<<RS); // Data mode
    LCDPORT |=  (cmd<<D4);
    LcdFlash();
}

void LcdInit() {

    LCDDDR |=  (1<<RS);     // SET(LCDDDR, RS);
    LCDDDR |=  (1<<RW);     // SET(LCDDDR, RW);
    LCDDDR |=  (1<<EN);     // SET(LCDDDR, EN);
    LCDDDR |=  (0x0F<<D4);  // ASSIGN(LCDDDR, D4, 0x0F);

    _delay_ms(150); //Wait for boot
    //Function set
    LcdSendNibble(0b0010, 0);
    LcdSendNibble(0b0010, 0);
    LcdSendNibble(0b0100, 0); //N = 0, F = 1
    _delay_ms(50);

    // Display on/off control
    LcdSendNibble(0b0000, 0);
    LcdSendNibble(0b1100, 0); //D = 1, C = 0, B = 0
    _delay_ms(50);

    // Display clear
    LcdSendNibble(0b0000, 0);
    LcdSendNibble(0b0001, 0);
    _delay_ms(50);

    //Entry mode set
    LcdSendNibble(0b0000, 0);
    LcdSendNibble(0b0110, 0); //I/D = 1 SH = 0
    _delay_ms(50);
}

int main() {
    LcdInit();
    while(1) {}
}

I would also appreciate information as to what some of the different flags mean during the initialization.
specifically the line-mode flag N and the shift flag SH.

My goal is simply to get the screen to light up, and then to write a character to the first position though this is not attempted in the first place

Best Answer

Assuming it's Hitachi HD44780-compatible you need to study the data sheet very carefully and follow it to the letter and it will work. There should be plenty of stuff on the net for an AVR if you want to copy, but I think it's best to figure it out (tm).

Also, make sure you've got the contrast voltage input set to a reasonable value (near ground for normal temperature range units). The datasheet you linked is a bit confusing as it shows 4.5V for Vo. Note that voltage is relative to Vdd, so for a 5V supply it should be about 0.5V with respect to ground.