Problem with serial interruption USART UDRE for Atmega328p

Asked

Viewed 146 times

1

I’m having trouble using USART serial transmission interrupt in a program made in pure C and run in Rduino (Atmega 328p).

My application requires pure C for reasons of efficiency and speed, but still want to maintain serial communication for debug and printing information in real time.

The following is a minimal representation of the program

The problem is the interruption, I have a problem with it, I tried to use TX in the interruption, but it generates inconsistent data in the output and, as I want to use the circular vector method, the UDRE register seems the best way, adding to this, the sample program found on the site http://jamesgregson.blogspot.com.br/2012/07/sample-code-for-atmega328p-serial.html works best.

In short, the interruption is not executed, because the program is stuck in the while. How can I fix this? I can disable the interruption in the middle hers?

The program is being compiled by AVR-GCC in the codeblocks IDE and the example given on the previously referenced site works, but it is not what the application requires.

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

//void intToStr(unsigned long, char*);
#define USART_BAUDRATE 57600
#define BAUD_PRESCALE (((F_CPU/(USART_BAUDRATE*16UL)))-1)

char ok = 0;


ISR(UART_UDRE_vect)
{
    ok = 1;
    UCSR0B &= ~(1<<5);
}

int main(void){


    UBRR0H  = (BAUD_PRESCALE >> 8);
    UBRR0L  = BAUD_PRESCALE;
    UCSR0B |= (1<<TXEN0);
    UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
    sei();

    UCSR0B |= (1<<5);
    while(1){

        // write the byte to the serial port
        UDR0 = '0';
        UCSR0B |= (1<<5);
        while(ok != 1){}

        UDR0 = '\n';
        UCSR0B |= (1<<5);
        while(ok != 1){}
    }
    return 0;

}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.