tyassin
Advanced Member level 1
Hello,
I have a PC sending 9 bytes to my uP. These are received with an interrupt function and saved in a buffer. When the buffer is full the program add some data to some of the bytes and then call the USART Transmit Function shown below. This send the 9 bytes back to the PC.
However this is where I get some strange behavoir. After I have received the 9 bytes I set the buffer index to zero(highlighted in red). As shown in the code below this is done in the transmit function. Now this scenario seems to work fine and I receive the data back with the bytes in the right order and some of hem modified correctly.
But when I set the buffer index to zero inside the Receive ISR of the UART, then when I send data back, the data has been left shifted randomly.
I have tried making variable static volatile but nothing seems to work. Only by having the index sat to zero inside the transmit function seem to work.
Please if anybody has an idea let me know.
Thank you
I have a PC sending 9 bytes to my uP. These are received with an interrupt function and saved in a buffer. When the buffer is full the program add some data to some of the bytes and then call the USART Transmit Function shown below. This send the 9 bytes back to the PC.
However this is where I get some strange behavoir. After I have received the 9 bytes I set the buffer index to zero(highlighted in red). As shown in the code below this is done in the transmit function. Now this scenario seems to work fine and I receive the data back with the bytes in the right order and some of hem modified correctly.
But when I set the buffer index to zero inside the Receive ISR of the UART, then when I send data back, the data has been left shifted randomly.
I have tried making variable static volatile but nothing seems to work. Only by having the index sat to zero inside the transmit function seem to work.
Please if anybody has an idea let me know.
Thank you
Code:
/*USART transmit function*/
#include <mega164a.h>
#include <delay.h>
#include "PA_USART.h"
#include "PA_DataSatByPC.h"
extern unsigned char PCReadRequest;
extern unsigned char PCWriteRequest;
extern unsigned char rx_buffer0[9];
extern volatile unsigned char PC_Buffer[9];
unsigned char rx_wr_index0;
//-----------------------------------------------------USART Transmit function-------------------------------------------------------------------------
void USART_Transmit( unsigned char data[] )
{
unsigned char x,y;
PORTD.6=1;
delay_ms(10);
for (x=0; x<=8; x++)
{
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0=data[x];
}
delay_ms(10);
PORTD.6=0;
delay_ms(10);
PCReadRequest=0;
for(x=0; x<=8; x++){
rx_buffer0[x]=0;
}
[COLOR="#FF0000"]rx_wr_index0=0;[/COLOR]
}
//-----------------------------------------------------USART Receive Interrupt Routine-------------------------------------------------------------------------
interrupt [USART0_RXC] void usart0_rx_isr(void) //Datastring is received from PC
{
static unsigned char status,x,data;
static unsigned char RS485_address=0x01; //Address of the RS 485 module. Skal ændres til at være hardware eller PC bestemt.
status=UCSR0A;
data=UDR0; //IMPORTANT that "UDR0" is read otherwise the interrupt flag is never reset.
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer0[rx_wr_index0++]=data; //Data from PC is saved in buffer/array
if(rx_wr_index0==9)
{
[COLOR="#FF0000"] //rx_wr_index0=0;[/COLOR]
PCReadRequest=1;
}
}
}