2

I am trying to interface a 16x4 LCD with an atmega32 chip. I wrote the following code which used to work for 16x2 LCD

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

#define dataport PORTC // LCD data port
#define commport PORTD
#define en PD6 // enable signal
#define wr PD5 // read/write signal
#define rs PD4 // register select signal


int wrcomm(unsigned int data)
{
  dataport=data;
  _delay_ms(10);
  commport &= ~(1 << rs);
  commport &= ~(1 << wr);
  commport |= (1 << en);
  commport &= ~(1 << en);
  _delay_ms(10);
  return 1;
}

int wrdata(unsigned int data)
{
  dataport=data;
  _delay_ms(10);
  commport |= (1 << rs);
  commport &= ~(1 << wr);
  commport |= (1 << en);
  commport &= ~(1 << en);
  _delay_ms(10);

  return 1;
}

int lcd_putchar(unsigned char x)
{
  wrdata(x);
  return 1;
}

void lcd_init(void)
{
  DDRC=0xff;
  DDRD=0x70;
  wrcomm(0x38);
  wrcomm(0x01); //turn on display and cursor
  wrcomm(0x0E);//cursor line 1 postn 1
  wrcomm(0x80);
}

int main(void)
{
    lcd_init();
    lcd_putchar('a');
    return 0;
}

I am guessing that it should work for 16x4 LCD too. But it doesn't. The LCD is a JHD539 164b.

The one thing I have made progress with is that if I do not send any command the 2nd and 4th line remain all black but if I send any command they are all off. The pot is not helping at all.

Josh
  • 133
  • 6
Rick_2047
  • 3,897
  • 5
  • 46
  • 68
  • I can't find a datasheet for your specific display -- do you have a link? Sometimes a display manufacturer uses an "odd" mapping from memory locations to screen locations. You might try writing a different data value to each location in the controller's memory to see if anything shows up on the screen, and figure out the mapping from that. – Dave Tweed Jan 21 '13 at 14:04
  • Why do you bother with the return values? For stuff like this it's mainly waste as there's not way you can detect an error. – Trygve Laugstøl Jan 22 '13 at 18:41

2 Answers2

1

The JHD539-164b is a standard KS0066 compatible character LCD. Nearly Identical to the HD44780 controlled lcds, and user end code would be completely compatible.

The difference between a 16x2 LCD and a 16x4 LCD is only that a 16x4 is really a logical 32x2 LCD, address wise.

If you are not getting it to work, double check the wiring, and see if the lcd is not damaged.

Passerby
  • 72,580
  • 7
  • 90
  • 202
0

It looks like you are using your LCD display in the 8-bit mode. Make sure in your lcd_init function that you have selected this mode, since LCD controllers have usually 4-bit mode set as a default.

Drazen Cika
  • 401
  • 2
  • 5