I am new to the avr (8bit uc) and recently I ran into a problem using serial communication (uart) with PC. Microcontroller fails to responsd in putty terminal where it should echo back whatever I have sent.
Thank you for your response everyone .
I am sorry for the corrupt code I had no idea what I was doing since I am newbie to this code indention in stack exchange.
The new code is attached below please kindly suggest
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
volatile char data;
void USARTInit(uint16_t ubrr_value) {
UBRRL = ubrr_value;
UBRRH = (ubrr_value >> 8);
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
UCSRB = (1 << RXEN) | (1 << TXEN);
}
void USARTWriteChar(char data) {
while (!(UCSRA & (1 << UDRE)))
{ }
//Now write the data to USART buffer
UDR = data;
}
void main() {
sei();
UCSRB |= (1 << RXCIE);
USARTInit(51);
while (1) {
if (data == 'a') {
USARTWriteChar(data);
}
}
}
ISR(USARTRXC_vect) {
data = UDR;
}
so here foo just represents my data='a', unfortunately avr is not responding to my inputs .
when I type "a" .
Best Answer
Firstly, that code won't compile. The last portion of it (the ISR) looks corrupt - you might want to re-post that bit.
Secondly, it's not surprising that if you type "FOO" it doesn't respond, since the code is waiting for you to press "a" at which point it would respond with "[a][a][a][a][a][a]...." until you press something else.
Thirdly, you should learn to format your code properly, or at least use one of the good auto-formatters, like Artistic Style.
Unfortunately I don't have an ATMega16A here to test your code with, and I am not up on Atmel interrupts, so all I can do is suggest some things to try:
main()
after configuring the UART.while(1)
loop to make sure it's not getting stuck.Edit: After some digging and reading (I do find that Atmel datasheets are appallingly badly written) I have found that your interrupt vector is named wrong.
This page lists them all: http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html
There, after some scouring, you can find that for the ATMega16 the RX vector should be named:
After changing the name to that I can see from a decompilation that the ISR gets installed properly in the vector table:
... which it didn't before.