usart interrupt problem

Thread Starter

baba_bhuri

Joined Jul 2, 2013
17
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
Rich (BB code):
// program to echo using interrupts
#include<avr/io.h>
#include<util/delay.h>
#include<interrupt.h>
#define BAUD_RATE 9600
#define BAUD_PRESCALE (((F_CPU / (BAUD_RATE * 16UL))) - 1)
void initialise_serial(); //initialise USART
char c;
main()
{
  initialise_serial();
 sei(); 
 UCSR0B |=(1<<RXCIE0);//enabling recieve complete interrupt
  while(1);
  
}

void initialise_serial()
{
  UCSR0B |=(1<<RXEN0)|(1<<TXEN0);
  UCSR0C |=(1<<UCSZ01)|(1<<UCSZ00);
  UBRR0H |=(BAUD_PRESCALE>>8);
  UBRR0L |=BAUD_PRESCALE;
  }
ISR(USART_RXC_vect)
{
  c = UDR0;
  UDR0 =c;
  
}
please help sorting out the problem .
thanks in advance:)
 
Last edited by a moderator:

Thread Starter

baba_bhuri

Joined Jul 2, 2013
17
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??
 

mitko89

Joined Sep 20, 2012
127
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:
Rich (BB code):
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRnA & (1<<UDREn)) )
;
/* Put data into buffer, sends the data */
UDRn = data;
}
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.
 

MrChips

Joined Oct 2, 2009
30,806
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.
 

mitko89

Joined Sep 20, 2012
127
"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?
 

tshuck

Joined Oct 18, 2012
3,534
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...
 

Thread Starter

baba_bhuri

Joined Jul 2, 2013
17
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.
 
Top