treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
Ring buffer example |
Posted: Thu Aug 12, 2004 8:46 am |
|
|
Ring buffer example
circular buffer
Code: |
#define TXSIZE 64 //ie:buffer is 2^6=64 bytes deep
#define TXMASK TXSIZE-1
int8 TX_BUF[TXSIZE];
//==========tx_buffer===================//
//For transmitting data via USART and using interrupts
//must have #INT_TBE defined
void tx_buffer(char *data,int8 size)
{
int8 x;
for (x=0;x<size;x++,data++)
{
//chksum=chksum+*data;
TX_BUF[tx_indx_i] = *data;
tx_indx_i++; //when filling the buffer, increment the index
tx_indx_i &= TXMASK;//mask off the index,.. this makes it circular
if(tx_indx_o==tx_indx_i) //if the indexs match we are full
{
bit_set(ERRORS,1);//set error flag bit 1 for tx overflow
disable_interrupts(INT_TBE);
tx_indx_o++;
tx_indx_o &= TXMASK;
}
}
RS485TX();//enable transmit
delay_ms(2);//2ms is as fast as it can go,... error on 1ms delay
enable_interrupts(INT_TBE);
return;
}
//============ Empty buffer================//
//=======================TXisr============================//
//Takes data from buffer and puts it into a TX stream (RS485)
//uses interrupts to empty buffer at proper rate
#INT_TBE
void TXisr(void)
{
CREN = 0;
fputc(TX_BUF[tx_indx_o],RS485);//empty the buffer of a byte
tx_indx_o++;//increment the index
tx_indx_o &= TXMASK;//mask off the index,.. this makes it circular
if(tx_indx_o==tx_indx_i)//if they are == buffer is empty, turn off int_tbe
{
disable_interrupts(INT_TBE);
CREN = 1;
}
set_timer1(TIMER1_INIT);//reset the timer
//enable_interrupts(INT_RB);// don't have the keypad INT bother the RX INT
}
|
|
|