hello ..
I know This is repeated ...
I'm using ATmega8 , The problem is that when i set the clock to internal RC 1MHZ (in fuse bits and code) with buad rate set to 2400 (error rate is 0.2% according to datasheet) it works perfectly !
But when i set the clock to Internal 8MHz (both in fuse bits and code) with baud rate to 9600,4800,2400,19200 (I tried all 4) (error rate is 0.2% according to datasheet) it outputs none sense on Terminal screen
I ofcourse checked that the baud rate is the same in my serial terminal And the code ..
My Code:
#define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> //------------------------------------------------------------------------------------------------- void Usart_Init(unsigned long USART_BAUDRATE) { unsigned int BAUD_PRESCALE; BAUD_PRESCALE = (((F_CPU / (USART_BAUDRATE * 16UL))) - 1); UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register }; //------------------------------------------------------------------------------------------------- void Usart_Write(char Data2Write) { while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it UDR = Data2Write; }; //------------------------------------------------------------------------------------------------- void Usart_Write_Text(char *text_){ unsigned char c; while ((c = *text_++) != 0) { Usart_Write(c); } Usart_Write(0); } int main(void) { DDRB=0xff; PORTB=0; Usart_Init(9600); Usart_Write_Text("Hello"); Usart_Write(0x0d); // new line Usart_Write_Text("-------------"); Usart_Write(0x0d); for(char i =0 ; i<10 ; i++) // quick led flash Just to check that the atmega has started up { PORTB=255; _delay_ms(50); PORTB=0; _delay_ms(50); } Usart_Write_Text("StartingUp... \r"); while (1) { Usart_Write_Text("still alive : )\r"); PORTB=255; _delay_ms(500); PORTB=0; _delay_ms(500); } }
Edit: ((Spelling))