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 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);
    LCDPORT &= ~(1<<EN);

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

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

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

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

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

int main() {
    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

  • \$\begingroup\$ This LCD looks like it's compatible with the HD44780. You should be able to find plenty of information online about that part… \$\endgroup\$ – duskwuff -inactive- Nov 16 '14 at 18:46

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.

| improve this answer | |
  • \$\begingroup\$ V0 is set fine, I've got a test hex where the thing works but I can't figure out how to get the initialization part done. How do I know if it's HD44780 compatible? \$\endgroup\$ – Anton Christensen Nov 16 '14 at 19:12
  • \$\begingroup\$ Almost surely it is, but you can compare the datasheet for the module with the chip datasheet I linked above. If it's a clone the module manufacturer may prefer not to mention it. Be generous (and confirm) timing delays to begin with. \$\endgroup\$ – Spehro Pefhany Nov 16 '14 at 19:17
  • 1
    \$\begingroup\$ I recently studied the HD44780 and I had a question about a the include file I wrote with some help of tutorials. I think it's quite good commented and that it might be helpful. electronics.stackexchange.com/questions/138680/… \$\endgroup\$ – ChThy Nov 16 '14 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.