Wednesday, 15 February 2012

avr - atmega8 UART- doesn't show character in realterm [Solved] -



avr - atmega8 UART- doesn't show character in realterm [Solved] -

hi i'm new , need help. it's suppose show 's' in realterm instead gives 'null'. problem? register? or code itself?

#include <avr/io.h> #include <util/delay.h> void uart_init(unsigned int ubrr) { ubrrh=(unsigned int)(ubrr>>8); ubrrl=(unsigned int)ubrr; ucsra=0x00; ucsrb=(1<<txen)|(1<<rxen); ucsrc=(0<<usbs)|(1<<ucsz0)|(1<<ucsz1); } void uart_tx(unsigned char chr) { while (bit_is_clear(ucsra,udre)){} udr=chr; } int main(void) { uart_init(95); ddrd|=0b11111111; portd|=0b11111111; while(1){ _delay_ms(10); uart_tx('s'); } }

system running on xtal 14745600 hz. speed on host 9600 baud. settings should 8n1.

you need set ursel when writing ucsrc register.

change

ucsrc=(0<<usbs)|(1<<ucsz0)|(1<<ucsz1);

to

ucsrc=(1<<ursel)|(0<<usbs)|(1<<ucsz0)|(1<<ucsz1);

from info sheet:

the ubrrh register shares same i/o location ucsrc register. hence special consideration must taken when accessing i/o location. when doing write access of i/o location, high bit of value written, usart register select (ursel) bit, controlswhich 1 of 2 registers written. if ursel 0 during write operation, ubrrh value updated. if ursel one, ucsrc setting updated.

the rest of code looks fine me.

avr uart baud-rate

No comments:

Post a Comment