SerLCD not working?

my setup: ATMega8 on an Olimex dev board. Trying to get LCD working, no luck so far. I’m using code from the Olimex website for an UART example, and I get the same problem I had with my original code. When I try to send a single character (in this case a ‘3’), the LCD displays a space followed by two strange characters: http://www.flickr.com/photos/24242034@N08/3556730889/

The code I’m using is the UART example from: http://olimex.com/dev/avr-p28.html except I’ve changed it to only transmit ‘3’ instead of echoing back input.

any ideas? Is my cystal not working right? My next thing to try is hook up the RS232 to hyperterminal and see if that works with the original example.

thanks for any help,

James

ok, so I went out and built myself a serial cable and hooked up the 232 IC so I could do some testing using hyperterminal. Using the Olimex UART code out of the box did not work, was supposed to echo back characters, but the screen was blank as I typed. Tried hooking the SerLCD straight to the 232 chip with no luck either. I kept testing, and changed the code so it would output ‘3’ when I pushed the button. at 9600 in hyperterminal, it was just junk, so I started playing with the speed settings in hyperterminal and got different junk at different speeds except one.

Turns out that my ATMega8 is transmitting perfectly at 1200baud. WTH? I screencapped a string of characters I txd: http://www.flickr.com/photos/24242034@N08/3565101771/ I’ve tried a couple of multiplications of the factor 8 in different spots to get it up to 9600 cleanly, but so far no luck. I also tried the original echo code at 1200baud, but it was a blank screen just like before. Anyone ever run into a problem like this? I don’t understand what could be causing this. I’ve attached below the modified code I used for the screencap:

/*	Sample program for Olimex AVR-P28 with ATMega8 processor
 *	Echoes back the received characters on the uart. In order to work,
 *	connect the TX pad with PD1(pin 3) and RX pad with PD0(pin 2)
 *	Compile with AVRStudio+WinAVR (gcc version 3.4.6)
 */

#define	__AVR_ATmega8__	1
#define OSCSPEED	8000000		/* in Hz */

#include "avr/io.h"

void Initialize(void)
{
	PORTB = 0x0;
	PORTC = 1<<5;	/* turn the LED off */
	PORTD = 0x0;

	DDRB = 0x0;
	DDRC = 1<<5;	/* PC5 as output - the LED is there */
	DDRD = 0x0;

}

void InitUART(uint32_t baud)	/* here a simple int will not suffice*/	
{
	int baudrate=((OSCSPEED/(16*baud))-1); /* as per pg. 133 of the user manual */
	/* Set baud rate */
	UBRRH = (unsigned char)(baudrate>>8);
	UBRRL = (unsigned char)baudrate;
	/* Enable Receiver and Transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8data, 1stop bit */
	UCSRC = (1<<URSEL)|(3<<UCSZ0);
	
}

unsigned char UARTReceive(void)
{
	if (UCSRA & (1<<RXC))
		return UDR;
	else	/* no data pending */
		return 0;
}

void UARTTransmit(unsigned char data)
{
	while (!( UCSRA & (1<<UDRE)) );
	/* Put data into buffer, sends the data */
	UDR = data;
}

void sendword(char strg[])
{
int a=0;
while(strg[a]!=0){
	UARTTransmit(strg[a]);
	a++;
	}
}

/*	state = 0 -> Led Off
 *	state = 1 -> Led On
 *	state !=[0,1] -> Led Toggle 
 */
void LedSet(unsigned char state)
{
	switch (state)
	{
		case 0:
			PORTC &= ~(1<<5);
			break;
		case 1:
			PORTC |= 1<<5;
			break;
		case 2:
			if (PORTC & 1<<5){ 
				PORTC &= ~(1<<5);
			//	sendword("LED on\r\n");
				}
			else{
				PORTC |= 1<<5;
		//		sendword("LED off\r\n");
				}
	}
	
}


int main(void)
{
	unsigned char ch;
	Initialize();
	InitUART(9600);
	int i;

	while (1)
	{
		ch=UARTReceive();

		if (ch)
		{
			UARTTransmit(ch+1);
		}

		if (!(PIND & 1<<2))	/* PIND2 is LOW when the button is pressed*/
		{
			LedSet(2);
			for (i=65535;i;i--);
			{
			__asm__ __volatile__ ("nop");
			__asm__ __volatile__ ("nop");
			__asm__ __volatile__ ("nop");
			__asm__ __volatile__ ("nop");
			}
			sendword("Partial success\r\n");

		}

	}
	return 0;
}

Did you change the fuses to use the external crystal? If not, you are running on the internal oscillator at 1 MHz instead of the 8 MHz you are expecting…

/mike

thanks, that sounds about right. I took a look through the datasheet, but it’s really unclear how to write to these fuse bits? I didn’t see a register where I could change CKSEL3…0 to use the external source. How do I do this in my code?

thanks,

kc5zfz

edit: nevermind, found the option in ponyprog, thanks!

You can’t change them in the code, you have to use the Fuse Settings tab in AVR Studio during the programming process.

Leon