Hi Folks,
I am trying to combine the code that I found, I need to do buffer that will send like LIFO, and no success. Anyone can help me with that?
#include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #define USART_BAUDRATE 19200 #define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) //define max buffer size #define BUF_SIZE 200 //type definition of buffer structure typedef struct{ //Array of chars uint8_t buffer[BUF_SIZE]; //array element index uint8_t index; }u8buf; //declare buffer u8buf buf; //initialize buffer void BufferInit(u8buf *buf) { //set index to start of buffer buf->index=0; } //check buffer void BufferIsFull(u8buf *buf) { if ((buf->index)==(buf->index<sizeof(buf))) { return 0; } else return 1; } //write to buffer routine uint8_t BufferWrite(u8buf *buf, uint8_t u8data) { if (buf->index<BUF_SIZE) { buf->buffer[buf->index] = u8data; //increment buffer index buf->index++; return 0; } else return 1; } uint8_t BufferReadLIFO(u8buf *buf, volatile uint8_t *u8data) { if(buf->index>0) { buf->index--; //dicrement buffer index *u8data=buf->buffer[buf->index]; return 0; } else return 1; } uint8_t BufferReadFIFO(u8buf *buf, volatile uint8_t *u8data) { //soon } void USART0Init(void) { // Set baud rate UBRR0H = (uint8_t)(UBRR_VALUE>>8); UBRR0L = (uint8_t)UBRR_VALUE; // Set frame format to 8 data bits, no parity, 1 stop bit UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00); //enable reception and RC complete interrupt UCSR0B |= (1<<RXEN0)|(1<<RXCIE0); } //RX Complete interrupt service routine ISR(USART_RX_vect) { uint8_t u8temp; u8temp=UDR0; //check if period char or end of buffer if ((BufferWrite(&buf, u8temp)==1)||(u8temp==0x0D)) { //disable reception and RX Complete interrupt UCSR0B &= ~((1<<RXEN0)|(1<<RXCIE0)); //enable transmission and UDR0 empty interrupt UCSR0B |= (1<<TXEN0)|(1<<UDRIE0); } } //UDR0 Empty interrupt service routine ISR(USART_UDRE_vect) { //if index is not at start of buffer if (BufferReadLIFO(&buf, &UDR0)==1) { //start over //reset buffer BufferInit(&buf); //disable transmission and UDR0 empty interrupt UCSR0B &= ~((1<<TXEN0)|(1<<UDRIE0)); //enable reception and RC complete interrupt UCSR0B |= (1<<RXEN0)|(1<<RXCIE0); } } int main (void) { //Initialize USART0 USART0Init(); //Init buffer BufferInit(&buf); //enable global interrupts sei(); while(1) { _delay_ms(100); } }