CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Ring buffer example

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

Ring buffer example
PostPosted: Thu Aug 12, 2004 8:46 am     Reply with quote

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
}

zspikes



Joined: 10 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Thu Jan 10, 2013 7:39 am     Reply with quote

Thank you!!! Very Happy
very useful!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group