SteveL89
Posts: 32
Joined: Sun Dec 30, 2012 3:20 pm

ATmega 328 Running LCD

Mon Jan 21, 2013 2:00 pm

Has anyone tried to run a 16x2 LCD with the ATmega 328 on the Gertboard using the "Hello World" example sketch in the Arduino IDE?

I have wired my ATmega to the LCD using the wiring suggested in the example, converting the Arduino pin designations to the proper Gertboard equivalents. I am powering the LCD from the 5V pin on the Gertboard.

The LCD lights up fine, put instead of displaying "Hello World" and the timer, it just displays a few characters of gibberish on the top line that change.

Anyone have any thoughts?

Steve

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Mon Jan 21, 2013 5:55 pm

Here's my code. I use a shift register to reduce the pins needed to run the display, but you should be able to adjust the code in the lcd_byte subroutine to not use the shift register fairly easily. Also, I'm creating and displaying two custom characters along with my text (might not be the best way to do it, but it works).

Code: Select all

const int LCD_RS=16;
const int LCD_E=32;
const int LCD_D4=1;
const int LCD_D5=2;
const int LCD_D6=4;
const int LCD_D7=8;

const int LCD_WIDTH=50;
const boolean LCD_CHR=true;
const boolean LCD_CMD=false;

const int LCD_LINE_1=0x80;
const int LCD_LINE_2=0xC0;
const int LCD_CHARS[8]={0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78};

const int E_PULSE=1;
const int E_DELAY=1;

const int DATA_PIN=5;
const int CLOCK_PIN=6;
const int CLEAR_PIN=7;

void clearShifter(){
 digitalWrite(CLEAR_PIN,LOW);
 digitalWrite(CLOCK_PIN,HIGH);
 delay(E_PULSE);
 digitalWrite(CLOCK_PIN,LOW);
 digitalWrite(CLEAR_PIN,HIGH);
}

void lcd_byte(int bits, boolean mode){
  int value=0;
  if (mode==LCD_CHR){
   value=LCD_RS;
  }
  value=value+((bits&0xF0)/0x0F);
  value=value+LCD_E;
  delay(E_DELAY);
  shiftOut(DATA_PIN,CLOCK_PIN,MSBFIRST,value);
  delay(E_PULSE);
  clearShifter();
  delay(E_DELAY);

  value=0;
  if (mode==LCD_CHR){
   value=LCD_RS;
  };
  value=value+(bits&0x0F);

  value=value+LCD_E;
  delay(E_DELAY);
  shiftOut(DATA_PIN,CLOCK_PIN,MSBFIRST,value);
  delay(E_PULSE);
  clearShifter();
  delay(E_DELAY);

}

void lcd_string(String message){
    for (int i=0;i<message.length();i++){
      lcd_byte(message.charAt(i),LCD_CHR);
    }
}

void lcd_init(){
  lcd_byte(0x33,LCD_CMD);
  lcd_byte(0x32,LCD_CMD);
  lcd_byte(0x28,LCD_CMD);
  lcd_byte(0x0C,LCD_CMD);
  lcd_byte(0x06,LCD_CMD);
  lcd_byte(0x01,LCD_CMD);
}

void lcd_custom(int charPos, int charDef[8]){
  lcd_byte(LCD_CHARS[charPos],LCD_CMD);
  for (int i=0;i<8;i++){
    lcd_byte(charDef[i],LCD_CHR);
  }
}
void setup(){
  pinMode(DATA_PIN,OUTPUT);
  pinMode(CLOCK_PIN,OUTPUT);
  pinMode(CLEAR_PIN,OUTPUT);
  digitalWrite(CLOCK_PIN,LOW);
  digitalWrite(CLEAR_PIN,HIGH);

  clearShifter();
  lcd_init();
  int char0[8]={0x04,0x02,0x0F,0x12,0x14,0x10,0x10,0x10};
  int char1[8]={0x04,0x0A,0x0A,0x0A,0x11,0x1F,0x06,0x00};
  lcd_custom(2,char0);
  lcd_custom(1,char1);
  lcd_byte(LCD_LINE_1,LCD_CMD);
  String output="  HELLO WORLD!  ";
  output.setCharAt(0,2);
  output.setCharAt(15,1);
  lcd_string(output);
}

void loop(){


}
There are 10 types of people in this world. Those that understand binary, and those that don't.

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Mon Jan 21, 2013 6:02 pm

Here's the circuit that I use to drive my display with the shift register. I just built a little board out of it, and it stays plugged into my display at all times.

Pins, from top to bottom, on the left hand part of the board are:

--VCC (+5V)
--Ground
--Data (Note that this is hooked to both pin one and pin two as my shift register is a "ANDed" input)
--Clock
--Clear

The Potentiometer is a 10K pot.
There are 10 types of people in this world. Those that understand binary, and those that don't.

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Mon Jan 21, 2013 6:05 pm

OOPS! Helps if you hit Add the file when adding an attachment!
Attachments
display.jpg
display.jpg (51.56 KiB) Viewed 3217 times
There are 10 types of people in this world. Those that understand binary, and those that don't.

SteveL89
Posts: 32
Joined: Sun Dec 30, 2012 3:20 pm

Re: ATmega 328 Running LCD

Mon Jan 21, 2013 8:27 pm

Thanks for sharing this. This worked from your Gertboard?

Steve

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Mon Jan 21, 2013 8:42 pm

Absolutely.
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
brgibson
Posts: 4
Joined: Tue Jan 15, 2013 6:29 pm

Re: ATmega 328 Running LCD

Tue Jan 29, 2013 3:43 pm

I'm having the same issue Steve. Did you ever find a solution? I tried Rick's sketch but it didn't work - just different jibberish. His picture doesn't show where or how his board interfaces to the pins on a Gertboard so that was of little help. The only way I've been able to make a 5v display work is by running it on the 3.3v supply, but they can be difficult (if not impossible) to read on that supply voltage. I've even tried running the 6 outputs from the Gertboard through a 74LS244 to shift the levels to 5V but still no workie.

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Tue Jan 29, 2013 4:02 pm

brgibson wrote:I'm having the same issue Steve. Did you ever find a solution? I tried Rick's sketch but it didn't work - just different jibberish. His picture doesn't show where or how his board interfaces to the pins on a Gertboard so that was of little help. The only way I've been able to make a 5v display work is by running it on the 3.3v supply, but they can be difficult (if not impossible) to read on that supply voltage. I've even tried running the 6 outputs from the Gertboard through a 74LS244 to shift the levels to 5V but still no workie.
Sorry for the confusion. My sketch is meant to run with a shift register to save pins when running the display. The code tells you the pin out from the Gertboard (Data is pin 5 or PD4, Clock is pin 6, or PD5, and Clear is pin 7, or PD6). The illustration shows the pins connector on the left hand side. From top to bottom it's

-5V (which I get from the Gertboard)
-Ground
-Data (connected to Pin 5)
-Clock (connected to Pin 6)
-Clear (connected to pin 7)


Hope this helps at least a little.
Rick
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
brgibson
Posts: 4
Joined: Tue Jan 15, 2013 6:29 pm

Re: ATmega 328 Running LCD

Tue Jan 29, 2013 7:08 pm

Thanks for the reply Rick. Looking a little closer at your picture explained a lot. The 74HC164 shifts the data sent serially and sends it into D4-D7 on the display. Cool way to conserve pins! As far as a beginning "Hello World" program though, utilizing the standard LiquidCrystal library simplifies things for me. I'm still not sure exactly the cause of the original problem that Steve and I are having. I'm having to run the logic supply to the LCD at 3.3V to make it work, otherwise it shows only jibberish. On the brighter side, I'm getting more fluent in that tongue ;)

Thanks for your help!

SteveL89
Posts: 32
Joined: Sun Dec 30, 2012 3:20 pm

Re: ATmega 328 Running LCD

Tue Jan 29, 2013 9:43 pm

I was not able to get it working, most likely due to my own ineptness!! However, the reason I was trying to get it to work was to run a temperature sensor and display the temperature on the LCD using the ATmega. What I ended up doing was running the temperature sensor off the ATmega and the LCD off the Pi and printing the temperature to the LCD using a Python code that prints the serial output to the LCD. I ended up including temp in F and C which alternate readouts with the time from the Pi. It is pretty cool.

I will now try running the LCD again off the ATmega with the suggestions above.

I run the LCD off the 5v pin on the Gertboard and wire up the rest of the GPIOs with jumpers from the Gertboard to the LCD on a breadboard.

Steve

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Sat Feb 02, 2013 7:51 pm

Don't know if you guys got it working or not yet, but I just put together an instructable on how I got it working without the shift register. Hope this helps.
There are 10 types of people in this world. Those that understand binary, and those that don't.

SteveL89
Posts: 32
Joined: Sun Dec 30, 2012 3:20 pm

Re: ATmega 328 Running LCD

Sat Feb 02, 2013 8:40 pm

How can I access your instructable?

Steve

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Sat Feb 02, 2013 9:00 pm

Click the "Hope this helps" link, or go here: http://www.instructables.com/id/Running ... -on-a-Ger/
There are 10 types of people in this world. Those that understand binary, and those that don't.

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: ATmega 328 Running LCD

Mon Feb 04, 2013 3:26 am

WELL!?!?! Did you try it? Did it work? I'm dying to know!
There are 10 types of people in this world. Those that understand binary, and those that don't.

SteveL89
Posts: 32
Joined: Sun Dec 30, 2012 3:20 pm

Re: ATmega 328 Running LCD

Mon Feb 04, 2013 4:39 am

Tried it and wired it as per instructions but just got a blank screen with no text. I will try it again tomorrow and report back.

S

Return to “Gertboard classic”

Who is online

Users browsing this forum: No registered users and 1 guest