EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Serial communication of ATMega128

Started by sindhu January 31, 2007
Hi all,
     Iam working on Atmega128 and STK500.Iam new to this.
I need to transmit a character from controller.I have given the code
below.

#include"iom128v.h"
#include "io.h"

unsigned char data;
unsigned int baud;
unsigned int i;
int transmit(unsigned char);

void USART0_Init( unsigned int baud)
{
UBRR0H=0x96;
UBRR0L=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
}
int transmit(unsigned char data)
{
PORTA=0x41;
DDRA=0xFF;
loop_until_bit_is_set(UCSR0A,UDRE);
UDR0=data;
return 0;
}
int main(void)
{
SREG=0x80;
SPL=0x61;
SPH=0x00;
USART0_Init(9600);
transmit('A');
return 0;
}
This code is working.PORTA outputs 41.Hyperterminal is not showing
character A.
Can anyone point out the mistake?

Bye.

"sindhu" <scinthia_245@rediffmail.com> wrote in message 
news:1170226154.395263.68070@p10g2000cwp.googlegroups.com...
> Hi all, > Iam working on Atmega128 and STK500.Iam new to this. > I need to transmit a character from controller.I have given the code > below. > > #include"iom128v.h" > #include "io.h" > > unsigned char data; > unsigned int baud; > unsigned int i; > int transmit(unsigned char); > > void USART0_Init( unsigned int baud) > { > UBRR0H=0x96; > UBRR0L=0x00; > UCSR0B=0x08; > UCSR0C=0x06; > } > int transmit(unsigned char data) > { > PORTA=0x41; > DDRA=0xFF; > loop_until_bit_is_set(UCSR0A,UDRE); > UDR0=data; > return 0; > } > int main(void) > { > SREG=0x80; > SPL=0x61; > SPH=0x00; > USART0_Init(9600); > transmit('A'); > return 0; > } > This code is working.PORTA outputs 41.Hyperterminal is not showing > character A. > Can anyone point out the mistake? > > Bye.
1) Are you sure you are outputting what you think? Look at the output on an oscilloscope - check the baud rate, number of stops and the parity (if used). 2) Are you sure hyperterminal is set up correctly with the right stop, start, baud etc? You are better off with Teraterm (free to download) because Hyperterminal is buggy. 3) Are you remembering to use an RS-232 level shifter and are not trying to drive the PC serial port with 5V and 0V? 4) Are you certain you have checked 1) and 2) as they are by far the most likely.
sindhu wrote:

> Hi all, > Iam working on Atmega128 and STK500.Iam new to this. > I need to transmit a character from controller.I have given the code > below.
[snip]
> } > This code is working.PORTA outputs 41.Hyperterminal is not showing > character A. > Can anyone point out the mistake?
I remmber having had a problem with the serial port and the Mega128 once. The SPI port for programming is on top of the Serial_0. Some external gates are necessary to resolve this conflict. Rene -- Ing.Buero R.Tschaggelar - http://www.ibrtses.com & commercial newsgroups - http://www.talkto.net
On Jan 31, 5:36 am, Rene Tschaggelar <n...@none.net> wrote:
> sindhu wrote: > > Hi all, > > Iam working on Atmega128 and STK500.Iam new to this. > > I need to transmit a character from controller.I have given the code > > below. > [snip] > > } > > This code is working.PORTA outputs 41.Hyperterminal is not showing > > character A. > > Can anyone point out the mistake? > > I remmber having had a problem with the serial port > and the Mega128 once. The SPI port for programming > is on top of the Serial_0. Some external gates are > necessary to resolve this conflict.
Another thing to check is the state of the M103C fuse. It comes from the factory set to emulate the M103, and that often causes problems. Never used the M128 myself, though, so I'm not certain it's pertinent... Regards, -=Dave
On Jan 31, 7:49 am, "sindhu" <scinthia_...@rediffmail.com> wrote:
> Hi all, > Iam working on Atmega128 and STK500.Iam new to this. > I need to transmit a character from controller.I have given the code > below. > > #include"iom128v.h" > #include "io.h" > > unsigned char data; > unsigned int baud; > unsigned int i; > int transmit(unsigned char); > > void USART0_Init( unsigned int baud) > { > UBRR0H=0x96; > UBRR0L=0x00; > UCSR0B=0x08; > UCSR0C=0x06;} > > int transmit(unsigned char data) > { > PORTA=0x41; > DDRA=0xFF; > loop_until_bit_is_set(UCSR0A,UDRE); > UDR0=data; > return 0;} > > int main(void) > { > SREG=0x80; > SPL=0x61; > SPH=0x00; > USART0_Init(9600); > transmit('A'); > return 0;} > > This code is working.PORTA outputs 41.Hyperterminal is not showing > character A. > Can anyone point out the mistake? > > Bye.
Looks like your baud rate is programmed to the wrong value. UBBR0H should be the most significant byte of the divisor, and UBRR0L the least significant byte. For example, if your CPU clock is 4 MHz, and your baudrate 9600, the divisor should be 4000000 / 16 / 9600 = 26.04, subtract one and round to get 25. Program 0 (MSB) into UBRR0H, and 25 (LSB) into UBRR0L. I always use something like this (with FCLK defined to be CPU clock): int ubrr = (FCLK / baud + 8) / 16 - 1; UBRR0H = (ubrr > 8); UBRR0L = ubrr;

Memfault Beyond the Launch