Electronic – Atmega168 USART RX Interrupt does not trigger

atmelatmel-studioc

I have been trying to get the USART RX interrupt trigger working on my ATmega168A using the tutorial here (updating my registers as per the datasheet): Tutorials

I have setup the system to use a baud of 4800 at a freq of 1MHz (8Mhz internal oscillator div/8), prescale value of 12 came from the microchip datasheet.

Using the normal serial communications (inside my main loop), the system works as expected and echos the transmitted char back to the pc console. I then added the 2 lines at the end of the function USART_Init() which should enable the RX interrupt and the global interrpt but when typing over the same serial connection which worked, noting happens which suggests to me that either its not triggering or theres an error.

I have searched google and the stack overflow for other instances and I havent been able to find anything which matches my issue or the many links i have found suggest doing things which I already have in my code/tried before.

Have I missed something or have I got a fault in something?

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define F_CPU 1000000UL
#define BAUD 4800
#define BAUD_PRESCALE 12


void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}

unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) )
;
/* Get and return received data from buffer */
return UDR0;
}

void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (0<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01);
/*enable RX interrupt */
UCSR0B |= (1 << RXCIE0);
sei();
}

int main(void)
{
USART_Init(BAUD_PRESCALE);
while (1) 
{
    //a = USART_Receive();
    //USART_Transmit(a);
    _delay_ms(100);
}
}

ISR(USART_RX_vect, ISR_BLOCK){
char ReceivedByte ;
ReceivedByte = UDR0 ; // Fetch the received byte value into the variable " ByteReceived "
UDR0 = ReceivedByte ; // Echo back the received byte back to the computer

}

I am using ATmel Studio 7 with WinAvr to code, compile and program the chip.

Best Answer

You need to change ISR(USART_RX_vect, ISR_BLOCK) to ISR(USART0_RX_vect, ISR_BLOCK)

Beware when following code examples online. With the newer chips they started adding numbers to a lot of the registers/macros.

Related Topic