1
\$\begingroup\$

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" .

\$\endgroup\$
4
  • \$\begingroup\$ Well, you've commented out the line which would have polled for data from your UART - hoping that your interrupt works. If you use the polling approach instead of interrupts do you have any success? \$\endgroup\$
    – brhans
    Commented Dec 18, 2014 at 14:05
  • \$\begingroup\$ @brhans It looks like he's trying to use an interrupt for it, but it's hard to tell with that badly formatted code, and also it looks truncated with that if(data==1) } at the end there... \$\endgroup\$
    – Majenko
    Commented Dec 18, 2014 at 14:06
  • \$\begingroup\$ @Majenko Yes - the code formatting fooled me at first and that bit on the end doesn't make any sense either. \$\endgroup\$
    – brhans
    Commented Dec 18, 2014 at 14:09
  • \$\begingroup\$ @brhans I have passed it through astyle to clean it up a little. Really highlights the broken code portion... \$\endgroup\$
    – Majenko
    Commented Dec 18, 2014 at 14:10

1 Answer 1

2
\$\begingroup\$

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:

  1. Make sure your code is actually running and the serial itself is working: Make it print a "Hello" message at the start of your main() after configuring the UART.
  2. Make sure the interrupt is actually firing - make it light an LED when the interrupt is triggered.
  3. Make it blink another LED as a "heartbeat" in your main 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:

    USART_RXC_vect 

After changing the name to that I can see from a decompilation that the ISR gets installed properly in the vector table:

00000000 <__vectors>:
   0:   0c 94 2a 00     jmp 0x54    ; 0x54 <__ctors_end>
   4:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
   8:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
   c:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  10:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  14:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  18:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  1c:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  20:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  24:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  28:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  2c:   0c 94 5c 00     jmp 0xb8    ; 0xb8 <__vector_11>   // <-- ISR
  30:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  34:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  38:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  3c:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  40:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  44:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  48:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  4c:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>
  50:   0c 94 47 00     jmp 0x8e    ; 0x8e <__bad_interrupt>

... which it didn't before.

\$\endgroup\$
3
  • \$\begingroup\$ I am also new to coding formats as you have suggested about the artistic style, I have installed it in atmel studio 5.2 but there seems to be no difference at all in the format. \$\endgroup\$
    – arun crazz
    Commented Dec 20, 2014 at 5:37
  • \$\begingroup\$ I have never used Artistic Style from Atmel Studio. I don't know if it's supported at all in it. I only use it as a command line tool, or from my own IDE (UECIDE, which I configured specially to try out your code). \$\endgroup\$
    – Majenko
    Commented Dec 20, 2014 at 10:08
  • \$\begingroup\$ The uart interrupt which I have mentioned is working in atmel studio 5.2 for some unknown reason.If i give the interrupt as you have mentioned it seems to give me an error as "MISSTYPED HANDLER".. and I am able to receive both TX/RX ,tested it using int instead of char ..it seems I have to use (strcmp(x,y)==0) ... thank you for your trouble friend.. \$\endgroup\$
    – arun crazz
    Commented Dec 21, 2014 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.