[TUT] LCD tutorials - updated

Go To Last Post
255 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LCD Tutorial

This tutorials is for people who like to use their LCD without understanding much about the working part of the LCD. This is my first attempt to write a tutorial so please bear my mistakes and point out if any occurs.

This tutorial explains how to program the LCD in an abstract view i.e. directly using the library functions without going into the depth of it.

Firstly I am using JHD162A LCD module which is compatible with HD44780. Now the LCD can come in various sizes. I am using a 16x2 LCD, which means 2 lines are there and each line can display a maximum of 16 characters. The LCD can be used in 4 bit mode or 8 bit mode depending upon your pin availability on our microcontroller or development board.
In 8 bit mode 8 data pins are there which sends a character(1 byte) directly to the LCD. In 4 bit mode the upper nibble is sent first and then lower nibble.
The 16 pins on JHD162A are

1  Vss
2  Vcc
3  Vee
4  RS
5  R/W
6  EN
7  DB0
8  DB1
9  DB2
10 DB3
11 DB4
12 DB5
13 DB6
14 DB7
15 LED+
16 LED-

DB0-DB7: Data Pins( use DB4-DB7 in 4 bit mode)

RS(Register Select) : 1 - Data is sent , 0 - Command is sent .

R/W(Read/Write) :1 - Read the LCD , 0 - Write to LCD. this pin is almost always low.

EN(Enable): to enable an operation . first make low(0) to send data and then set the other two control lines and when they are configured, bring EN high (1) and wait for the minimum amount of time required by the LCD and bring it low (0) again.

Vee: Contrast Adjust Pin. connect to a potentiometer.

Now to use it in 8 bit mode we need 8 data pins DB0-DB7 and 3 control pins. So total 11 pins are required. In 4 bit mode, we need DB4-DB7 and 3 control pins. So total 7 pins and required.Note that the data pins DB0-DB3 are left open and are not shorted to ground.The Vee pin is connected to the potentiometer and is varied from Vcc and Gnd. Pin 15 and 16 are used for backlight of LCD and can be connected to Vcc and Gnd respectively.

So till now, you have the basic idea to setup an LCD and connect it with your microcontroller. In this tutorial I am using LCD in 4 bit mode.

Before writing any code, first download the library written by Peter Fleury from his homepage - http://www.jump.to/fleury. The library written by him has excellent functions by which we can directly control the LCD without knowing what is going behind the scenes and using the built-in functions provided in the library.

First set the mode as 4 bit mode by writing LCD_IO_MODE=1 in the lcd.h file.

Change LCD_PORT if you want to use a different port for the LCD pins.

The four LCD data lines and the three control lines RS, R/W, EN can be on the same port or on different ports. Change LCD_RS_PORT, LCD_RW_PORT, LCD_E_PORT if you want the control lines on different ports.

int main(void)
{	
  lcd_init(LCD_DISP_ON);	/* initialize display, cursor off */
    while (1)					/* loop forever */ 
       {                           
       	 lcd_clrscr();                   /* clear the screen*/
	 lcd_puts("test program");  /* displays the string on lcd*/
 	_delay_ms(50);
        }	
}

This codes displays "test program" on your LCD.(without quotes) I have used delay because some time is required for the display to settle in.

The functions in the library which could be used are -

lcd_init (uint8_t dispAttr)-initialize display and select type of cursor.
lcd_putc(char c) -  to print a single charater on current cursor position
lcd_puts(const char* s) - to print a string without auto linefeed.
lcd_gotoxy(uint8_t x, uint8_t y) - xth position on the yth line. X varies from 0 to 15 and y is 0 or 1.
lcd_clrscr() - clears the screen .
lcd_home() "“ goes to the initial position, i.e. equivalent to lcd_gotoxy(0,0) 
lcd_puts_p (const char *progmem_s) - display string from program memory without auto linefeed. 
lcd_command (uint8_t cmd)-send LCD controller instruction command. 
lcd_data (uint8_t data)-send data byte to LCD controller

With the help of these basic functionalities you can display text on your LCD on both lines.

Please beware of delays. Proper delays are required for the text to properly display on screen or else it will blink or just displays once and go away. Play around with different types of functions given in the library.

e.g. lcd_command(LCD_MOVE_DISP_RIGHT) "“ moves the whole display one position right side.

Experiment it to display various kinds of symbols and text. One good code would be to show your text moving on the LCD. i.e. it enters from right and leaves from left "“ kind of rolling effect.

Problems which I have faced or mainly occurs with LCD:

The first problem to arise is you don't see anything on LCD although its on "“ change the value of contrast using potentiometer.

Black squares are seen "“ See that proper connection is there. i.e. if any two pins touch each other than this may occur. Also see the contrast pin.

Displays blinks- use a proper delay. Try out different values of delays

Typical symbols are displayed instead of text "“ see the connections again. If two pins are shorted, this might occur.

Hope you like and understood the tutorial and start programming the LCD and play around with it. Please give your feedback so that i can improve it.

Last Edited: Sun. Aug 2, 2009 - 06:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used your code

#include 
#include 
#include "lcd.h"

int main(void)
{
lcd_init(LCD_DISP_ON); 					/* initialize display, cursor off */
while (1) 								/* loop forever */
{
lcd_clrscr(); 							/* clear the screen*/
lcd_puts(“test program”); 				/* displays the string on lcd*/
_delay_ms(50);
}
} 

with my atmega128:


And it's not working :/ (no reaction on LCD), can you help me pls?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it's working now :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what was the error or problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I made an mistake setting pins for RS :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does anyone have a good link to the lcd lib???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

link It's just a . in the end of link in the first post.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, this is very good LCD library, library good work on my Atmega 8 or 16, bud when I use Atmega 128 don't work:( (Display don't initialize). Is any special setting in lcd.h or other setting for atmega128 ? I use port A,B,C,D or E (not F or G).
Thanks for answers:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
when I use Atmega 128

mega103 compatibility fuse?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

check the connections and modify the port connection list in lcd.h. i was using for PORTB. so if you want to use for other ports just change in .h file

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi, i just started manipulating my lcd for doing different works and i will share some with you and hope you share some with me so we can all learn

i tested few codes with my name and tried to shift it left or right i.e tried to make a rolling effect- text enters from one side and leaves from other. these codes i tried didn't work well althought i learnt new things and understood the working of lcd properly

// this program moves display right/left and some trouble shooting

#include 'lcd.h'
#include
#include
#include
#include
#include

void main(void)
{
DDRC=0xff;
int i;

lcd_init(LCD_DISP_ON);
lcd_clrscr();

while(1)
{
lcd_clrscr();
lcd_puts("DARSH SHAH");
_delay_ms(50);
for(i=0;i<16;i++)
{lcd_command(LCD_MOVE_DISP_RIGHT);
_delay_ms(50);
}

}

this code writes darsh shah as a string and then it is shifted right one character at a time and out of display.
-----------------------------------------------------------------------------------------------
to go in other direction
for(i=0;i<16;i++)
{
lcd_command(LCD_MOVE_DISP_LEFT);
_delay_ms(50);
}
this code writes darsh shah as a string and then it is shifted left one character at a time and out of display.
-----------------------------------------------------------------------------------------------

if i write i<20 then after 16 characters which is lenght of LCD , blank spaces are shown for 4 spaces.

-----------------------------------------------------------------------------------------------

for continuous display some algorithm is needed.
array[]= darsh shah
for(i=0;i<16;i++)
{lcd_putc(array[i]);
_delay_ms(50);
lcd_command(LCD_MOVE_DISP_RIGHT);
_delay_ms(50);
}

then after 10 characters(darsh shah) junk letters are printed.total 6 junk letters.
in this code what happens is that first d is printed and then shifted one space and then a.
so afer darsh sh the text moves out as 8 characters+8 shifts. hence we want the last letter to print first.

------------------------------------------------------------------------------------------------
array[]=darsh shah
for(i=0;i<10;i++)
{lcd_putc(array[9-i]);
_delay_ms(50);
lcd_command(LCD_MOVE_DISP_RIGHT);
_delay_ms(50);
}

here in this code, h is printed then space , then ha then space, then hah then space and similarly
hahs hsrad will be printed but only hahs hs is printed due to space i.e. 16 characters.

-----------------------------------------------------------------------------------------------
array[]=darsh shah
for(i=0;i<10;i++)
{lcd_putc(array[9-i]);
_delay_ms(50);
lcd_command(LCD_MOVE_CURSOR_LEFT);
_delay_ms(50);
lcd_command(LCD_MOVE_DISP_RIGHT);
_delay_ms(50);
lcd_command(LCD_MOVE_CURSOR_LEFT);
_delay_ms(50);
}

in this code h is printed in first line and rest of it comes in 2nd line. so i need to figure it how to make it stay only on first line.

-----------------------------------------------------------------------------------------
array[]=darsh shah
for(i=0;i<26;i++)
{i<10?lcd_putc(array[9-i]):lcd_putc(' ');
_delay_ms(50);
lcd_command(LCD_MOVE_CURSOR_LEFT);
_delay_ms(50);
lcd_command(LCD_MOVE_DISP_RIGHT);
_delay_ms(50);
lcd_command(LCD_MOVE_CURSOR_LEFT);
_delay_ms(50);

}

the above code writes darsh shah as stated in above programs and now it also puts blank spaces till all the text is shifted to right and goes out of display section.

----------------------------------------------------------------------------------------------

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

for a complete rolling effect, i got this code. i dont know whose it is but i found it on net. so i claim that it is not mine but works very well.

one thing is that if text is less than 16 characters(lenght of lcd) then the beauty of code is not seen as whole lcd is not covered. for more characters it works beautifully.

#define STRING_LENGTH 27
#define LCD_MAX 15

void main()
{char array[STRING_LENGTH] = { "darsh shah loves avrfreaks" };
int i,j;
int start;
int end;

lcd_init(LCD_DISP_ON_CURSOR);
lcd_clrscr();
while(1)
{
for ( j = 0; j < STRING_LENGTH; j++ )
{
if ( j >= LCD_MAX )
lcd_gotoxy( 0, 0 );
else
lcd_gotoxy( LCD_MAX - j, 0 );

start = (j <= LCD_MAX) ? 0 : (j - LCD_MAX);

end = (j <= LCD_MAX) ? j : (start + LCD_MAX);

if ( end >= STRING_LENGTH )
end = STRING_LENGTH - 1;

for ( i = start; i <= end; i++ )
lcd_putc( array[i] );

_delay_ms(100);
}

lcd_clrscr();
}
}

this code does it. the text can be of any length u want. one optimixation required is that after the text ends, the screen is cleaared and again the rolling text starts.
--------------------------------------------------------------------------------------------------------------

hope you also contribute your codes so we can learn and experiment!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ATMEL--Heart Beat
Nothing Impossible

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So.. m.b. i'm dumb, but it's not working. No reaction at all.

I download library written by Peter Fleury, open his test_lcd.c and load it into AVRStudio 4.
It's compiled.
After that i take WH1602b LCD display (HD44780 based) and connect it to At90usbkey as shown at "Home > AVR Microcontroller Projects > Interfacing a HD44780 Based LCD to an AVR" fig. 2 - using 4-bits interface.

Port A using.
Connections tested 3+ counts.
Vcc = usb +5v (+4.8v tested).
in lcd.h i change XTAL from 4000000 to 8000000, as on usbkey 8Mh quarts

It's not working. No response. (by the way, LCD is good - tested at another plate. not avr)

Why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I download library written by Peter Fleury, open his test_lcd.c and load it into AVRStudio 4.
It's compiled.

But did you set the defines in LCD.h to match your setup?

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Quote:
I download library written by Peter Fleury, open his test_lcd.c and load it into AVRStudio 4.
It's compiled.

But did you set the defines in LCD.h to match your setup?

Ports? Sure! So, i use port A as show on fig., and i dn't need to change anythng.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all.
I get the library from his website, I make the project in AvrStudio4, but i get a lots of errors, and warnings.
Code:

Build started 26.8.2009 at 10:41:50
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lcdTest.o -MF dep/lcdTest.o.d -c ../lcdTest.c
In file included from ../lcdTest.c:16:
../lcd.h:22: error: expected declaration specifiers or '...' before 'FILE'
../lcdTest.c: In function 'main':
../lcdTest.c:20: error: 'LCD_DISP_ON' undeclared (first use in this function)
../lcdTest.c:20: error: (Each undeclared identifier is reported only once
../lcdTest.c:20: error: for each function it appears in.)
../lcdTest.c:20: error: too many arguments to function 'lcd_init'
../lcdTest.c:23: warning: implicit declaration of function 'lcd_clrscr'
../lcdTest.c:24: warning: implicit declaration of function 'lcd_puts'
make: *** [lcdTest.o] Error 1
Build failed with 5 errors and 2 warnings...

Why did I get this errors?
P.S. : I have an atmega128 and a 2x12 lcd.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I get the library from his website

Which file? Who's webbsite?

One thing's for sure - those aren't Pete Fleury's files.

By the way, did you know that GCC comes with some LCD code?

http://www.nongnu.org/avr-libc/u...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Which file? Who's webbsite?
I took the library from Pete Fleury's website: http://www.jump.to/fleury
Quote:
By the way, did you know that GCC comes with some LCD code?

http://www.nongnu.org/avr-libc/u... ... odemo.html


Yes I know, but I don't have any examples, plus that I saw in the avr-gcc help that atmega128 is not included, and because of that I can't compile for atmega128, I got a lot of errors in the usart.c

rm -rf lcd.o hd44780.o lcd1.o uart.o lcd.elf dep/* lcd.hex lcd.eep lcd.lss lcd.map
Build succeeded with 0 Warnings...
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lcd.o -MF dep/lcd.o.d -c ../lcd.c
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT hd44780.o -MF dep/hd44780.o.d -c ../hd44780.c
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lcd1.o -MF dep/lcd1.o.d -c ../lcd1.c
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT uart.o -MF dep/uart.o.d -c ../uart.c
../uart.c: In function 'uart_init':
../uart.c:30: error: 'UCSRA' undeclared (first use in this function)
../uart.c:30: error: (Each undeclared identifier is reported only once
../uart.c:30: error: for each function it appears in.)
../uart.c:31: error: 'UBRRL' undeclared (first use in this function)
../uart.c:35: error: 'UCSRB' undeclared (first use in this function)
../uart.c: In function 'uart_putchar':
../uart.c:54: error: 'UCSRA' undeclared (first use in this function)
../uart.c:55: error: 'UDR' undeclared (first use in this function)
../uart.c: In function 'uart_getchar':
../uart.c:104: error: 'UCSRA' undeclared (first use in this function)
../uart.c:109: error: 'UDR' undeclared (first use in this function)
make: *** [uart.o] Error 1
Build failed with 9 errors and 0 warnings...

I have an 2*12 lcd, bought from this site: http://www.adelaida.ro/product_info.php?cPath=87_118_275&products_id=7586&osCsid=7b393d7d2884a1c8889d52e9425df74a. His datasheet is here: tp://www.adelaida.ro/adelaida/catalog/pub/TME/lcd/rc1202a-yhy-esx.pdf
When I saw that I got those errors I tried to male my own lcdinit() function.
I use the 8 bit interface(page 16).
Code:

#ifndef F_CPU
   #define F_CPU 4000000UL
#endif
#include 
#include 
#include 

#define DDR          DDRD // setare port
#define PORT         PORTD // setare bit
//definire pini de control
#define PIN          DDRG
#define PortPin      PORTG
#define RS           0
#define RW             1
#define E            2

#define N          0
#define F          0
#define B          1
#define C          1
#define D          1
#define S          1
#define ID          0

void initLCD(void);
void clearDisplay(void);

int main(void){
   
   DDRB |= (1<<PB5) | (1<<PB6);
   TCCR1A |= (1<<COM1A1) | (1<<COM1B1) | (1<<WGM10) | (1<<WGM11);
   TCCR1B |= (1<<CS00);
   OCR1A = 400; // alimentare contrast
   OCR1B = 400; // alimentare led
  
     initLCD();
     clearDisplay();
   PORT |= (1<<7);
a:

goto a;
  return 0;
}
void initLCD(void){
   DDR |= 0xff;
   PIN |= 0x07;
   _delay_ms(50);

   //PortPin |= 0x02;
   PORT |= (1<<5) | (1<<4) | (N<<3) | (F<<2);
   _delay_us(50);   

   PORT |= (1<<5) | (1<<4) | (N<<3) | (F<<2);
   _delay_us(50);

   PORT |= (1<<3) | (B<<2) | (C<<1) | (D<<0);
   _delay_us(50);

   PORT |= (1<<1);
   _delay_ms(5);

   PORT |= (1<<2) | (ID<<1) |(S<<0);
}

void clearDisplay(void){
   PORT |= (1<<0);
}

One row is totaly black:

P.S. : This atmega is new, and I don't set up the fuse bits for an external crystal(because I locked an atmega). I don't know what is the internal frequenci of the RC?

Attachment(s): 

Last Edited: Wed. Aug 26, 2009 - 10:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just took Fleury's code. Did nothing but edit the line in the Makefile to set "MCU = atmega128" and got:

D:\fleury>make

-------- begin --------
avr-gcc (WinAVR 20090313) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Compiling: test_lcd.c
avr-gcc -c -mmcu=atmega128 -I. -gdwarf-2   -Os -funsigned-char -funsigned-bitfie
lds -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=test_lcd.l
st  -std=gnu99 -Wp,-M,-MP,-MT,test_lcd.o,-MF,.dep/test_lcd.o.d test_lcd.c -o tes
t_lcd.o

Compiling: lcd.c
avr-gcc -c -mmcu=atmega128 -I. -gdwarf-2   -Os -funsigned-char -funsigned-bitfie
lds -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=lcd.lst  -
std=gnu99 -Wp,-M,-MP,-MT,lcd.o,-MF,.dep/lcd.o.d lcd.c -o lcd.o

Linking: test_lcd.elf
avr-gcc -mmcu=atmega128 -I. -gdwarf-2   -Os -funsigned-char -funsigned-bitfields
 -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=test_lcd.o  -
std=gnu99 -Wp,-M,-MP,-MT,test_lcd.o,-MF,.dep/test_lcd.elf.d test_lcd.o lcd.o   -
-output test_lcd.elf -Wl,-Map=test_lcd.map,--cref    -lm

Creating load file for Flash: test_lcd.hex
avr-objcopy -O ihex -R .eeprom test_lcd.elf test_lcd.hex

Creating load file for EEPROM: test_lcd.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
        --change-section-lma .eeprom=0 -O ihex test_lcd.elf test_lcd.eep
c:\WinAVR-20090313\bin\avr-objcopy.exe: --change-section-lma .eeprom=0x00000000
never used

Creating Extended Listing: test_lcd.lss
avr-objdump -h -S test_lcd.elf > test_lcd.lss

Creating Symbol Table: test_lcd.sym
avr-nm -n test_lcd.elf > test_lcd.sym

Size after:
test_lcd.elf  :
section           size      addr
.data               42   8388864
.text             1134         0
.debug_aranges      64         0
.debug_pubnames    212         0
.debug_info       1879         0
.debug_abbrev      847         0
.debug_line       1624         0
.debug_frame       256         0
.debug_str         401         0
.debug_loc         708         0
Total             7167



Errors: none
-------- end --------

So where exactly do you think the problem is?

In the files I downloaded Flury's lcd.h has this on line 22:

 4-bit IO port mode (LCD_IO_MODE=1). 8-bit IO port mode not supported.

So if yuou have one that has "FILE" on line 22 I'd suggest it is the WRONG copy of lcd.h (in fact it might be the one from the WinAVR examples)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I get the library from his website, I make the project in AvrStudio4, but i get a lots of errors, and warnings.
I said that I make the project with avrStudio4. I took only the ."c" and ".h" file.
Quote:
In the files I downloaded Flury's lcd.h has this on line 22:
Code:
4-bit IO port mode (LCD_IO_MODE=1). 8-bit IO port mode not supported.

So if yuou have one that has "FILE" on line 22 I'd suggest it is the WRONG copy of lcd.h (in fact it might be the one from the WinAVR examples)


I know, I did for the 4 bit, but when I saw that I have those errors I tried to make my own lcdinit() function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK if you want to use AVR Studio, I just did the following using that same set of untouched files I downloaded:

1) start Studio
2) new Project
3) AVR GCC
4) untick the create boxes (I already have directory and files)
5) set location to where the files are
6) next>>
7) AVR Simulator2 + atmega128
8) [finish]
9) In tree at left, right-click "Source files"
10) "Add existing source file(s)"
11) select both lcd.h and test_lcd.h in that directory then [open]
12) Build-rebuild all
13) err...
14) ... that's it....

Build started 26.8.2009 at 12:31:23
avr-gcc  -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lcd.o -MF dep/lcd.o.d  -c  ../lcd.c
avr-gcc  -mmcu=atmega128 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT test_lcd.o -MF dep/test_lcd.o.d  -c  ../test_lcd.c
avr-gcc -mmcu=atmega128 -Wl,-Map=fleury.map lcd.o test_lcd.o     -o fleury.elf
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature  fleury.elf fleury.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ihex fleury.elf fleury.eep || exit 0
avr-objdump -h -S fleury.elf > fleury.lss

AVR Memory Usage
----------------
Device: atmega128

Program:    1176 bytes (0.9% Full)
(.text + .data + .bootloader)

Data:         42 bytes (1.0% Full)
(.data + .bss + .noinit)


Build succeeded with 0 Warnings...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I could compile, but why, I did aprox. exactly the same thing.
I make a new project. I use the steps:

Quote:
9) In tree at left, right-click "Source files"
10) "Add existing source file(s)"
11) select both lcd.h and test_lcd.h in that directory then [open]

, and in my "main.c" I copy/paste the "test_lcd.c". But I don't copy the makefile as wel.
In the lcd.h file I modify:

#define LCD_PORT         PORTD        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
#define LCD_RS_PIN       4            /**< pin  for RS line         */
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
#define LCD_RW_PIN       5           /**< pin  for RW line         */
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
#define LCD_E_PIN        6            /**< pin  for Enable line     */

for port D, and in the test_lcd.c I comented these 2 lines:

    
    //DDRD &=~ (1 << PD2);        /* Pin PD2 input              */
    //PORTD |= (1 << PD2);        /* Pin PD2 pull-up enabled    */

and the wait_until_key_pressed() function, because I don't have a button on my bord.
I did all of that but is still doesn't worcking.
What did I do wrong?

Ok, forget all the thing I said above, now it's worcking. I don't belive. I own you :)
It wasn't worcking because i did not put any voltage on the led and contrast :( .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I own you

I'm not very good on "urban" but is this the point where I get to say "I pwn you" ? :lol:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:)))))))))))))))))))))), ok, if you say so.
Now one more question.
I blocked one of my atmega128, because I did not set the fuses how it should be(with pony prog2000).
I saw on the controler datasheet that it is compatible with atmega103. You have to set the M103C fuse. Bur I do't understand in wich mode it is atmega128, and in wicth mode is atmega103.
And I want to set an external crystal with two C of 22pF.
But I dont now how. I saw in the table from the pdf that fuses “1” means unprogrammed while “0” means programmed, and in the pony prog security bits checked means programmed and unchecked means unprogramed.
I am a litle bit confused. A print screed would be great.
Please send me a link or same documentation.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Bur I do't understand in wich mode it is atmega128, and in wicth mode is atmega103.

When a mega128 first arrives it has the M103C fuse enabled so behaves as a mega103. Usually the first thing anyone does (having paid all that money for a mega128 they might as well use it!) is to set the M103C fuse inactive. It's bit 1 of the extended fuse byte. By default that byte is 0xFD, set it to 0xFF

As for Ponyprog - I find it horrendously confusing to know which way up the fuse bits are (this is made worse by the fact that they have a tick box here you can invert the sense of the display/input). The best bet is to start by reading the fuse bytes. You know from the datasheet, for example, that the low fuse byte holds 0xE1 with SUT0, CKSEL3, CKSEL2, CKSEL1 active. So see whether Pony prog shows this as ticks against those entries or ticks against the inactive BODLEVEL, BODEN, SUT1 and CKSEL0

Cliff

[this is now off-topic for this LCD tutorial so you really should start a new thread with these questions in AVR Forum]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All I get is weird characters.(Asian)
All connections are good, contrast it working.
What could be wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if you are using a usart system and displaying output on
lcd then it might be the baud rate problem. else verify the connections and also the delay given. i think you should also modify the library according to your own need.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AH! It's all working!!!
2 all other.
Be sure, that pin 3 - contrast adj IS connect to GND
through 3k Ohm. Then all be good.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Be sure, that pin 3 - contrast adj IS connect to GND
through 3k Ohm. Then all be good.

Pulling it to 0V is not normally sufficient. Usually it needs a small positive voltage to get the contrast right so if Gnd worked for you then you were just lucky!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How many LCD libraries are there? Specifically, how does one display int/float values?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

How many LCD libraries are there? Specifically, how does one display int/float values?

There are many, many.

But it's not usually up to the library to display int/float values. Always assuming the library has some form of LCD_putstring() function you just need to use the C standard sprintf():

char buffer[20];

sprintf(buffer, "int=#d, float=#f", 54321, 3.1415926);
LCD_putstring(buffer);

(replace # with percent symbol)

If the LCD library doesn't have an LCD_putstring() but just LCD_putchar() then construct the string function with:

void LCD_putstring(char * str) {
 while (*str) {
   LCD_putchar(*str++);
 }
}

PS If you use GCC things are possibly even easier as you just associate the lowest level LCD_putchar() function with an output file stream, assign this to stdout and then printf() output goes directly via the LCD_putchar() routine. (see example in manual)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey thx for your toturial.

im trying to use and make this work, but it dosen't.

is it a must to use the avr gcc compiler? can't figure it out, so i try to use avr studio or code vision, by opening the lcd_test.c file but it kind of complains about everything... missing headers and other stuf. tryed to download headers like inttypes.h but it also complains about that.

how do your do it? i don't understand the tutorial :/

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i might occur that you would not have included the header file. Also use lcd.h header file. if these also not work then best way is to copy the contects of lcd.h and lcd_test.c in same the file and compile. i think that would work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What should i do to connect the legs of lcd to my desired legs of my AVR(atmega16)
when i tried to connect it to port c from thence two or three pins(in port C) are not working as output pins..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tilak!

Did you disable JTAG? (JTAG sits on 4 of the PORTC pins, and is enabled by default).

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys, im working with an HD44780 which is interface with an Atmega8 (L). i have downloaded peter fluery codes and setup the lcd accordingly. However, im not so sure how to set all the stuff in the library as i am planning to use the lcd in the 4-bit mode.

Could anyone help me? thanks so much for everything

Mark

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well it all hinges around his lcd.h.
The first thing to consider is:

/** 
 *  @name  Definitions for MCU Clock Frequency
 *  Adapt the MCU clock frequency in Hz to your target. 
 */
#define XTAL 4000000              /**< clock frequency in Hz, used to calculate delay timer */

Set the number here to be your CPU frequency in Hertz (and personally I'd suffix the letters "UL" onto the end of the constant too)

If yours is really an HD44780 this doesn't need to change:

/**
 * @name  Definition for LCD controller type
 * Use 0 for HD44780 controller, change to 1 for displays with KS0073 controller.
 */
#define LCD_CONTROLLER_KS0073 0  /**< Use 0 for HD44780 controller, 1 for KS0073 controller */

If your module is 16x2 then these lines should work unchanged, otherwise you may need to set the right values for your panel:

/** 
 *  @name  Definitions for Display Size 
 *  Change these definitions to adapt setting to your display
 */
#define LCD_LINES           2     /**< number of visible lines of the display */
#define LCD_DISP_LENGTH    16     /**< visibles characters per line of the display */
#define LCD_LINE_LENGTH  0x40     /**< internal line length of the display    */
#define LCD_START_LINE1  0x00     /**< DDRAM address of first char of line 1 */
#define LCD_START_LINE2  0x40     /**< DDRAM address of first char of line 2 */
#define LCD_START_LINE3  0x14     /**< DDRAM address of first char of line 3 */
#define LCD_START_LINE4  0x54     /**< DDRAM address of first char of line 4 */
#define LCD_WRAP_LINES      0     /**< 0: no wrap, 1: wrap at end of visibile line */

As you want to use 4-bit leave this as it is:

#define LCD_IO_MODE      1         /**< 0: memory mapped mode, 1: IO port mode */

And the following is the main place where you configure his code:

/**
 *  @name Definitions for 4-bit IO mode
 *  Change LCD_PORT if you want to use a different port for the LCD pins.
 *
 *  The four LCD data lines and the three control lines RS, RW, E can be on the 
 *  same port or on different ports. 
 *  Change LCD_RS_PORT, LCD_RW_PORT, LCD_E_PORT if you want the control lines on
 *  different ports. 
 *
 *  Normally the four data lines should be mapped to bit 0..3 on one port, but it
 *  is possible to connect these data lines in different order or even on different
 *  ports by adapting the LCD_DATAx_PORT and LCD_DATAx_PIN definitions.
 *  
 */
#define LCD_PORT         PORTA        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
#define LCD_RS_PIN       4            /**< pin  for RS line         */
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
#define LCD_RW_PIN       5            /**< pin  for RW line         */
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
#define LCD_E_PIN        6            /**< pin  for Enable line     */

As you can see it defaults to PORTA - you may want to change this?

One real nice thing about the Fleury code is that the PORT and bit for each of the 4 data bits and 3 control lines is spearately configurable so you can spread them around the ports of your AVR.

That's pretty much all there is to it.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Cliff,

thanks so much for the advice! haha i have changed all that is necessary already, though i am still unsure about the LCD_PORT- if i connect my avr_atmega8 (L) PORTD4..7 to DB4..7, would it mean that i have to write

#define LCD_PORT PORTD

Right now, i am experiencing problems with the building process- after building, it ends up with 3 errors:

Build started 26.11.2009 at 10:25:08
avr-gcc -mmcu=atmega8 -Wall -gdwarf-2 -std=gnu99 -DF_CPU=1000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT LCD_testing.o -MF dep/LCD_testing.o.d -c ../LCD_testing.c
avr-gcc -mmcu=atmega8 -Wl,-Map=LCD_testing.map LCD_testing.o -o LCD_testing.elf
LCD_testing.o: In function `main':
D:\AVR\LCD_testing\default/../LCD_testing.c:7: undefined reference to `lcd_init'
D:\AVR\LCD_testing\default/../LCD_testing.c:11: undefined reference to `lcd_clrscr'
D:\AVR\LCD_testing\default/../LCD_testing.c:12: undefined reference to `lcd_puts'
make: *** [LCD_testing.elf] Error 1
Build failed with 3 errors and 0 warnings...
Thanks so much!

Mark

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But those are just link errors meaning that the source file providing lcd_init(), lcd_clrscr() and lcd_puts() are not being built and linked into the project. I notice that the only input to your link is LCD_testing.o which suggests that you have not added lcd.c to the list of files to be built in the project.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Cliff,

haha so sorry, i was so stupid not to add the lcd.c. the building suceeded already, however, i am actually seeing a black screen here.

For my connections, i have:
PORTD1: Rs
PORTD2: R/W
PORTD3: E
PORTD4: DB4
PORTD5: DB5
PORTD6: DB6
PORTD7: DB7
- Do i need to change anything in lcd.h, i have left the commands alone, just only set

#define LCD_PORT PORTD

i am using a 4-bit mode! omgoodness C'MON LCD WORK!

Thanks so much again, Cliff. Sorry to be bothering you

Mark

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if ure seeing a black screen please check your connections meticulously thats what worked for me

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

help needed.... i have tried it all but don't want to accept that both the LCDs that i have is broken, is their any way to check the functionality of an LCD to see if its ok. i have ran it on PORTA and PORTC but nothing is working, i am using the fluery's library in 4-bit mode.

i am programming it in linux ,
tell me if i can compile with the given makefile that came with the library or is it only for winavr. although its compiling fine with no errors.
also i have compiled with standard makefile : same result :(
please reply soon
TIA

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how do you think you've broken in it?..have you soldered your connectors properly,some terminals might be shorted.Check that using a multimeter.

I don't have any idea about the linux part of your query,what i know is that if you supply 5V to the specified pins of the LCD ,it shows blackboxes even when not initalised properly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ya thats what i am getting black boxes in the first row
and nothing else. actually i have a new lcd with me right now but its behaving exactly the same.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

i am getting black boxes in the first row

A telltale sign of the display not being properly initialized (or not initialized to use both rows and the contrast control voltage not being correct).

Quote:
actually i have a new lcd with me right now but its behaving exactly the same

Which is another indicator that the LCD(s) are not faulty. Either you have the connections wrong, or the software isn't doing what it should.

You now need to work with one problem at any given time. Do not poke at everything at once.

1) What did you do for contrast control (often called Vee, and AFAIK always on pin 3 of the LCD module)? What does the data sheet say about this voltage? Some display modules require a negative voltage here. For most display modules an adjustable voltage around 0.5 to 1 Volts is what is required. Arrange a 10 or 20K pot as a voltage divider feeding this pin.

2) Double check your wiring.

3) Double check that you have altered everything required in the s/w "setup", eg #defines for port and pin names. Eg did you fix the #define of LCD_DATA0_PIN and the six others?

4) Check the timings of the software. One thing that might reveal broken timing constraints is to lower the AVR frequency if you have the provisions for that. The idea is not to lower it permanently, but temporarily as a test for this hypothesis. Eg if you are running your AVR at 8 MHz and lowering that to 1 MHz makes the LCD start working then you have just detected a probable timing problem.

Again, not everything at once. Be systematic. Stop and think. If you get an advice here to do a specific test - do that and only that and report the results.

We've seen sooooo many LCD woes here over the years and close to all have eventually been solved if given advice has been followed.

First advice: Patience.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks johan for keeping me trying.
at last it finally worked.
the most important thing i learned from this is , if nothing seems to be wrong then one of your basic assumptions must be rechecked. The mistake i was doing was really basic, may be stupid.
I read the library documentation and found that the interface pins for data pins in 4-bit mode are 11-14 pin no. rather than 7-10. since it was written DB0 for 7 to DB3 for 10, and i just assumed those connections.

i am just happy that it finally worked and now i can communicate between two atmegas without the need for a terminal.

thanks again all of you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.jump.to/fleury ----> is not there any more???

I cant find the library???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fleury's page/code is still visible at:

http://homepage.hispeed.ch/peter...

(top hit for "Fleury LCD" at Google)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can someone help me please?
I trying to init my LCD 16x2 in 4bit mode.
I have add to the source files the lcd.c and in to the headers file the lcd.h.
I am using the fleury cd lib
When i write this code for example :

#include
#include
#include "lcd.h"

DDRB=0x0F;
DDRD=0xF0;
int main(void)
{
lcd_init(LCD_DISP_ON);

for(;;)
{
lcd_clrscr();
lcd_puts(“program”);
_delay_ms(50);

}
it show me this erors:
../lcd.h:188: error: expected ')' before 'dispAttr'
../displej.c:5: error: expected identifier or '(' before 'volatile'
../displej.c:5: error: expected ')' before '(' token
../displej.c:6: error: expected identifier or '(' before 'volatile'
../displej.c:6: error: expected ')' before '(' token
../displej.c: In function 'main':
../displej.c:9: warning: implicit declaration of function 'lcd_init'
../displej.c:14: error: stray '\223' in program
../displej.c:14: error: stray '\224' in program
../displej.c:14: error: 'program' undeclared (first use in this function)
../displej.c:14: error: (Each undeclared identifier is reported only once
../displej.c:14: error: for each function it appears in.)
I spend my last 2 days about this and I cud found the problem.
Please can you help me and answer my?
If someone can help me,send me the answer in mail please. lacikeNZ@gmail.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The line in question in lcd.h is:

extern void lcd_init(uint8_t dispAttr);

so it sounds like it's the uint8_t it may be obecting to. Yet ultimately leads to (it includes sfr_defs.h which includes inttypes.h which includes stdint.h.

So I'd maybe check a small test program where uint8_t is used and make sure your system knows this typedef?

Alternatively if you edited lcd.h above line 188 to change either:

#define LCD_LINES           2     /**< number of visible lines of the display */
#define LCD_DISP_LENGTH    16     /**< visibles characters per line of the display */
#define LCD_LINE_LENGTH  0x40     /**< internal line length of the display    */
#define LCD_START_LINE1  0x00     /**< DDRAM address of first char of line 1 */
#define LCD_START_LINE2  0x40     /**< DDRAM address of first char of line 2 */
#define LCD_START_LINE3  0x14     /**< DDRAM address of first char of line 3 */
#define LCD_START_LINE4  0x54     /**< DDRAM address of first char of line 4 */
#define LCD_WRAP_LINES      0     /**< 0: no wrap, 1: wrap at end of visibile line */

or

#define LCD_IO_MODE      1         /**< 0: memory mapped mode, 1: IO port mode */
#if LCD_IO_MODE

#define LCD_PORT         PORTA        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
#define LCD_RS_PIN       4            /**< pin  for RS line         */
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
#define LCD_RW_PIN       5            /**< pin  for RW line         */
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
#define LCD_E_PIN        6            /**< pin  for Enable line     */

then I'd check those edits for anything that may have a knock on effect to line 188 just below.

Pages

Topic locked