usart interrupt problem

Discussion in 'Embedded Systems and Microcontrollers' started by baba_bhuri, Jul 2, 2013.

  1. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    i am newbie trying to use usart by interrupts in the echo program.
    i us atmega 328 and c language.
    i have successfully implemented the echo program without using interrupts.
    while using interrupt, microcontroller echoes back no character.
    here's my program
    Code ( (Unknown Language)):
    1.  
    2. // program to echo using interrupts
    3. #include<avr/io.h>
    4. #include<util/delay.h>
    5. #include<interrupt.h>
    6. #define BAUD_RATE 9600
    7. #define BAUD_PRESCALE (((F_CPU / (BAUD_RATE * 16UL))) - 1)
    8. void initialise_serial(); //initialise USART
    9. char c;
    10. main()
    11. {
    12.   initialise_serial();
    13.  sei();
    14.  UCSR0B |=(1<<RXCIE0);//enabling recieve complete interrupt
    15.   while(1);
    16.  
    17. }
    18.  
    19. void initialise_serial()
    20. {
    21.   UCSR0B |=(1<<RXEN0)|(1<<TXEN0);
    22.   UCSR0C |=(1<<UCSZ01)|(1<<UCSZ00);
    23.   UBRR0H |=(BAUD_PRESCALE>>8);
    24.   UBRR0L |=BAUD_PRESCALE;
    25.   }
    26. ISR(USART_RXC_vect)
    27. {
    28.   c = UDR0;
    29.   UDR0 =c;
    30.  
    31. }
    32.  
    please help sorting out the problem .
    thanks in advance:)
     
    Last edited by a moderator: Jul 3, 2013
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Can you run the debugger, set a breakpoint in the ISR and see if the ISR is being reached?
     
    baba_bhuri likes this.
  3. mitko89

    Member

    Sep 20, 2012
    123
    19
    Did you read the datasheet? Check page 184.
     
    baba_bhuri likes this.
  4. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    i am making a transition from arduino ide to programming in pure c.
    so, i don't know much about debugging.
    could you please offer an alternative??
     
  5. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    i rechecked the datasheet . i don't seem to find any mistake.
     
  6. mitko89

    Member

    Sep 20, 2012
    123
    19
    A piece of advice. Do things one at a time with increasing difficulty so you know where the mistake might be if things go wrong. First, write simple functions such as putchar and getchar. Then try the echo without interrupt and after that use the interrupts. At page 184 there is this example code:
    Code ( (Unknown Language)):
    1. void USART_Transmit( unsigned char data )
    2. {
    3. /* Wait for empty transmit buffer */
    4. while ( !( UCSRnA & (1<<UDREn)) )
    5. ;
    6. /* Put data into buffer, sends the data */
    7. UDRn = data;
    8. }
    9.  
    Did you try this one out? I want you to read the complete section for UART0 in the datasheet attached. Everything is clearly explained there.
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    In the first post the OP did say that he tested the echo program successfully without using interrupts.

    To be honest, I have looked over the code and cannot find anything obviously amiss.
     
  8. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    I did read USART0 chapter.
    Still fail to see any problem.
    Could there be something wrong in the ISR ??
     
  9. mitko89

    Member

    Sep 20, 2012
    123
    19
    "Writing this bit to one enables interrupt on the RXCn Flag. A USART Receive Complete interrupt
    will be generated only if the RXCIEn bit is written to one, the Global Interrupt Flag in SREG is
    written to one and the RXCn bit in UCSRnA is set."
    Did you do all of these?
     
  10. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    The sei() macro enables the global interrupt.
    The RXCIE0 bit of UCSROB is also enabled.
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Check to see if you are, indeed, hitting the interrupt vector. I usually use an output initialized to output 0 and set it only in the ISR., this way, a LED can be used to check.

    If you do hit the ISR, you know that the error us elsewhere...
     
    baba_bhuri likes this.
  12. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    I followed your advice.
    The LED didn't glow.

    Replacing RXC by RX in ISR made it work. The source from where I studied it used RXC ,but the datasheet mentioned RX.
    But ,now I have another doubt, regarding UDR0 register, which I mention in the next thread.
     
Loading...