>> My current projects, though, are using the state machine and FIFO >> techniques on PIC24H chips. > > > I am currently going through the same loop with 24FJ USB chips. > > The FIFO code I am using I found at > http://www.cs.bu.edu/teaching/c/queue/array/types.html and modified so it > wasn't using dynamic allocation. > Here's my current FIFO code. Many FIFO implementations try to calculate how much is in a FIFO or how much space is left by comparing the input and output pointers. This is complicated with wrap-around. So, I just count stuff as it goes in and out. Use of indices makes it easy to detect wrap-around. Since the FIFOs are used in both main and interrupt code, I generally disable interrupts when putting something in a fifo or pulling something out. It gets messy when an interrupt changes stuff in the middle of these operations. Harold struct FifoStruct{ uint8_t buffer[FifoSize]; uint16_t IndexIn; // Put next byte at this index, then increment uint16_t IndexOut; // take next byte from this index, then increment uint16_t BytesInBuf;// how many bytes we have in the fifo uint16_t BytesFree;// how many bytes we have left }; // Function Prototypes void InitFifo(struct FifoStruct *fifo); // Initialize the Fifo uint8_t PutFifo(struct FifoStruct *fifo, uint8_t data); // Put data in the specified fifo. Return 0 on success. Nonzero on error. uint8_t GetFifo(struct FifoStruct *fifo); // Get a byte from the specified fifo. Returns 0 if nothing available void InitFifo(struct FifoStruct *fifo){ // Initialize the Fifo uint16_t n; fifo->IndexIn=0; // set indices to 0 fifo->IndexOut=0; fifo->BytesInBuf=0; // nothing here yet fifo->BytesFree=FifoSize; // full size is available } uint8_t PutFifo(struct FifoStruct *fifo, uint8_t data){ uint8_t result; // Put data in the specified fifo. Return 0 on success. Nonzero on error. if(fifo->BytesFree){ // we have room fifo->buffer[fifo->IndexIn++]=data; // save data and bump index fifo->BytesInBuf++; // add one to buffer contents fifo->BytesFree--; // and subtract one from how many are free if(fifo->IndexIn>=FifoSize){ // went off end of fifo fifo->IndexIn=0; // go back to start } result=0;// exit error free }else{ // no room result=1; } return(result); } uint8_t GetFifo(struct FifoStruct *fifo){ // Get a byte from the specified fifo. Returns 0 if nothing available uint8_t result; if(fifo->BytesInBuf){ result=fifo->buffer[fifo->IndexOut++]; // return the character fifo->BytesInBuf--; // subtract one from contents fifo->BytesFree++; // add one to how many are free if(fifo->IndexOut>=FifoSize){ fifo->IndexOut=0;// ran off end, reset index }else{ // fifo is empty result=0; // give back nothing and change nothing } return(result); } -- FCC Rules Updated Daily at http://www.hallikainen.com - Advertising opportunities available! -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist