Serial interrupt problem USART UDRE for ATMega328p

1

I'm having trouble using a USART serial interrupt in a program made in pure C and run on Arduino (Atmega 328p).

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

The following is a minimal representation of the program

The problem is interruption, I'm having a problem with it, I tried to use TX in the interrupt, but it generates inconsistent data in the output and, as I want to use circular vector method, the UDRE register seems the best way, adding to it, the example program found on link works best.

In short, the interrupt is not executed because the program gets stuck in while . How can I solve this? Can I disable the interruption in the middle of her?

The program is being compiled by AVR-GCC in IDE codeblocks and the example given in the previously referenced site works, but 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;

}
    
asked by anonymous 30.10.2017 / 02:12

0 answers