Atmega8 and LCD routines

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I've been trying to get a string to show on an LCD (16x2 in 4 bit mode) but just keep getting gibberish. I'm using peter fleurs lcd library and the lcd_puts(string).

am i missing something? do i need to do somesort of conversion? Just a noob trying to learn.
i'm also getting an error when trying to use the delay_us() function anything i've forgotten? the error is : undefined reference to delay_us()

I'm using avr gcc in avr studio 4.

Thanks.

#include
#include
#include
#include "lcd.h"

#define F_CPU 16000000
#include

/// Typedefs //////////
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;

int main(void) {

lcd_init(LCD_DISP_ON); //Turn on LCD, No cursor
lcd_clrscr(); // Clear screen

int i;
char lcd_buffer;

for (i = 1; 1 <= 10; i++) {
lcd_clrscr();
lcd_puts(i);
delay_us(10000);

}
}

Dave Harrod

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

I'm not familiar with Peter's delay. But if you wish to delay 10000 us (10ms), then you'd need to use the following for avr-gcc:

// top of the file
#include 
... your code
...
_delay_us(10000);  // or better, _delay_ms(10)

But, if you are only delaying 10ms, then you'll go through the numbers 1 to 10 in 0.1 seconds; far too fast to read. 10ms is faster than the refresh rate of the LCD controller, too, I'd expect. That's maybe why it looks like "gibberish" because characters are overlaying each other.

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

Quote:
lcd_puts(i);

This function puts the character i on to the LCD. It is up to the programmer to ensure that i means something. You are sending the integers 1 - 10. These are not printable ASCII characters.

Regards,
Steve A.

The Board helps those that help themselves.

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

thanks for the quick replies.

Ok, so first thanks for clearing up the _delay_ms(x) things. works fine.

Next, without changing the fusebits it works sending a string as lcd_puts("text"). I changed the fusebits to show 16mhz crystal, (unchecked them all in ponyprog), now i get lots of white and nothing else

As per the code i showed above, how would i change the integer i to show an ascii character? There must be a simpler way than to use a case for each int???

Thanks again

Dave Harrod

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

Use itoa().

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char tmpstr[6];

sprintf(tmpstr,"%d",i);
lcd_puts(tmpstr);

something like this might work (I hope)

Imagecraft compiler user

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

It'd be safer to declare tmpstr as size 7 (rather than 6) in case a wide negative number like -32767 was passed which takes 7 ASCII characters to store (including the zero termination byte).

Edit: I think you code does look right, Bob. But, to prevent buffer overruns, I always use snprintf, like:

char tmpstr[7]; 

snprintf(tmpstr,sizeof(tmpstr),"%d",i);
tmpstr[sizeof(tmpstr)-1] = 0; //always ensure zero-termination byte (see snprintf documentation) 
lcd_puts(tmpstr);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For reference here is Fleury's home page. I'm not familiar with this particular LCD library, just read trough the header file quickly. Did you notice that you need to edit lcd.h? You must edit the XTAL define so that it holds your clock speed.

EDIT: Oh, I guess you did, because you must also have set the port definitions in the same file...

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

yeah i had seen that! it was all looking good untill i changed the fusebits and now it wont program... gonna have to dig out my m128... :(

Any ideas on how to recover my m8?

Dave Harrod

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

harrod7159 wrote:
Any ideas on how to recover my m8?
What do you mean by recover?

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

It wont allow me to program it, ponyprog says that write failed, and i try to change the fusebits back and it makes no difference.

Dave Harrod

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

What fuses did you change to cause the problem? Can you read the fuses settings and tell us what they are? Do you have a high-voltage programmer like a Dragon or STK500? (high-voltage programmers allow you to reset any fuses)

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

I dont have an stk500, i'm using ISP on a bread board.
I changed the clock fuses to try and get it to use the 16Mhz crystal.

It was telling me what they were set to, but now ponyprog is simply saying the device cant be found (-24).

Edit:

I hit ignore and it read, it says all the fuses are ticked ( at 0 i think?)

Dave Harrod

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

What did you change the clock fuses to? Probably not the internal RC oscillator or it would be working. Did you change it to an external crystal? If so, did you already have the crystal installed? If it wasn't installed then, did you install the crystal and capacitors after making the change?

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

yeah i tried to change it to 16mhz crystal, they were already installed, with 22pf caps.

i think i might have set it to clock 1111 and sut 10.

Dave Harrod

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

And what about CKOPT?

And why SUT 10, and not 11?

And you have a mega8, not a mega8L?

You did calculate the capacitors from the data sheet of your crystal?

You are sure the oscillator oscillates?

You did try with smaller capacitors, e.g. 10pF or 15pF for a start?

You did try with a lower frequency crystal (8 MHz or below)?
Also note that Atmel is very tight lips about the maximum frequency of the crystal oscillator. Using such nonsense wording like "optimized frequency range" for crystal frequencies below 8 MHz, and not mentioning crystals beyond 8 MHz at all. Although it should work.

Stealing Proteus doesn't make you an engineer.

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

I haven't as of yet, but these shall be next on my list. unfortunately i have a very limited amount of kit here.

On the chip it states that it is an atmega8-16pu if this makes any difference.

Dave Harrod

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

You may accidentaly have set fuse bits to "external oscillator" if you checked CKSEL 0-3 in Ponyprog. (Checked means 0, not 1.)

Try to connect an oscillator about 1 MHz to XTAL1 pin.

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

Sometimes oscilator doesn't run when xtal connections are long, so try to put xtal close to AVR.

Another case could be that you programmed fuses for external RC oscilator. Look at datasheet page 29 how to connect external RC. In case you've programmed the CKOPT fuse then you only need a resistor of a value 3k-100k ohms, else you will need to connect a capacitor of at least 22pf. The frequency is roughly given from f=1/(3RC).

awen

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

Quote:
I dont have an stk500, i'm using ISP on a bread board.
I changed the clock fuses to try and get it to use the 16Mhz crystal.

It was telling me what they were set to, but now ponyprog is simply saying the device cant be found (-24).

Been a while since I worked with one, but don't you need to use high voltage to change the fuse bits?

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

Not until you screw them up! ;)

(mega8 is fine for ISP fuse programming unless you "lose contact")

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

ok thanks. Gonna buy an avr dragon, see if i can recover them. It'll be useful to have the USB programming capability too.

Thanks for all your help guys!!!

Dave Harrod