Briez
Member level 5
- Joined
- Nov 30, 2012
- Messages
- 83
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 1,288
- Activity points
- 1,835
hi,
I am interfacing Atmega32 with PC via RS232 Protocol. I have made a hardware with max232 and made simple echo back received program, but i am facing a problem in that.
When i send character 'A' from PC it echo backs with Garbage character. I am using 4MHZ external crystal.
What should be the problem?
I am attaching my code below,
I am interfacing Atmega32 with PC via RS232 Protocol. I have made a hardware with max232 and made simple echo back received program, but i am facing a problem in that.
When i send character 'A' from PC it echo backs with Garbage character. I am using 4MHZ external crystal.
What should be the problem?
I am attaching my code below,
Code:
#define F_CPU 4000000UL
#include<avr/io.h>
#include<avr/interrupt.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE ((( F_CPU / ( USART_BAUDRATE * 16UL))) - 1)
int main(void)
{
UCSRB |= (1 << RXEN ) | (1 << TXEN ); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 ); // Use 8- bit character sizes
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
UCSRB |= (1 << RXCIE ); // Enable the USART Recieve Complete interrupt ( USART_RXC )
sei (); // Enable the Global Interrupt Enable flag so that interrupts can be processed
for (;;) // Loop forever
{
// Do nothing - echoing is handled by the ISR instead of in the main loop
}
}
ISR (USART_RXC_vect)
{
char ReceivedByte ;
ReceivedByte = UDR ; // Fetch the received byte value into the variable " ByteReceived "
UDR = ReceivedByte ; // Echo back the received byte back to the computer
}