⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wireless_cc1100rx.c.bak

📁 CC1100做呼叫器的程序(C语言)用8051与CC1100接口,语音压缩,频率在916.5MHZ
💻 BAK
📖 第 1 页 / 共 5 页
字号:

      // wrap FIFO pointer if necessary
      if (ReceiveFIFO_FIRST==ReceiveFIFO_FIFOSIZE-1) ReceiveFIFO_FIRST = 0;
      else (ReceiveFIFO_FIRST)++;

      // pull value from fifo
      output = ReceiveFIFO_FIFO[ReceiveFIFO_FIRST];

      // set empty indicator if necessary
      if (--(ReceiveFIFO_COUNT) == 0) ReceiveFIFO_EMPTY = 1;

      return output;
   }
}


void ReceiveFIFO_Push(unsigned char num)
{

   ReceiveFIFO_EMPTY = 0;                  // FIFO is no longer empty
   if (!ReceiveFIFO_FULL) {

      (ReceiveFIFO_LAST)++;                // increment, wrap if necessary
      if (ReceiveFIFO_LAST == ReceiveFIFO_FIFOSIZE)
      {
         ReceiveFIFO_LAST = 0;
      }

      ReceiveFIFO_FIFO[ReceiveFIFO_LAST]=num;  // push value to FIFO

      ReceiveFIFO_COUNT++;

      if ( ReceiveFIFO_COUNT == ReceiveFIFO_FIFOSIZE)
      {
         ReceiveFIFO_FULL = 1;
      }
   // buffer is full
   } else {
      // only set overflow flag and exit
      ReceiveFIFO_OF = 1;
   }
}

void ADCRXFIFO_Push(unsigned short num)
{
   ADCRXFIFO_EMPTY = 0;                // buffer is no longer empty

   if (!ADCRXFIFO_FULL) {
      (ADCRXFIFO_LAST)++;              // increment, wrap around if necessary
      if (ADCRXFIFO_LAST == ADCRXFIFO_FIFOSIZE)
      {
         ADCRXFIFO_LAST = 0;
      }


      ADCRXFIFO_FIFO[ADCRXFIFO_LAST]=num;

      // update count, check for an overflow
      ++ADCRXFIFO_COUNT;

      if(ADCRXFIFO_COUNT == ADCRXFIFO_FIFOSIZE)
      {
         ADCRXFIFO_FULL = 1;
      }
   } else {
      ADCRXFIFO_OF = 1;
   }
}

unsigned short ADCRXFIFO_Pull(void)
{
   unsigned short output;

   // if a function tries to pull a value from an empty buffer, set the
   // underflow flag and exit the function
   if(ADCRXFIFO_EMPTY)
   {
      ADCRXFIFO_UF = 1;
      return 0;
   }

   // else the buffer is not empty, pull the value and return it
   else
   {
      ADCRXFIFO_FULL = 0;
      // update the first pointer, wrap around if necessary
      if (ADCRXFIFO_FIRST==ADCRXFIFO_FIFOSIZE-1) ADCRXFIFO_FIRST = 0;
      else (ADCRXFIFO_FIRST)++;

      // save output value
      output = ADCRXFIFO_FIFO[ADCRXFIFO_FIRST];
	  DAC_Send ++;
      // decrement buffer size
      if (--(ADCRXFIFO_COUNT) == 0) ADCRXFIFO_EMPTY = 1;

      return output;
   }
}


void DACTXFIFO_Push(unsigned short num)
{
   DACTXFIFO_EMPTY = 0;                // buffer is no longer empty

   if (!DACTXFIFO_FULL) {
      (DACTXFIFO_LAST)++;              // increment, wrap around if necessary
      if (DACTXFIFO_LAST == DACTXFIFO_FIFOSIZE)
      {
         DACTXFIFO_LAST = 0;
      }

      DACTXFIFO_FIFO[DACTXFIFO_LAST]=num;
      Decompress_Bytes ++;


      // update count, check for an overflow
      ++DACTXFIFO_COUNT;
      if (DACTXFIFO_COUNT == DACTXFIFO_FIFOSIZE - 1)
      {
         // the decompression routine will push two decompressed
         // audio samples per call to the routine, and
         // DACTXFIFO_DECOMPRESS_HALT is set when the buffer can only
         // store 1 more decompressed sample.
         // The flag will be cleared when at least one buffer byte is pulled
         DACTXFIFO_DECOMPRESS_HALT = 1;
      }
      else if (DACTXFIFO_COUNT == DACTXFIFO_FIFOSIZE)
      {
         DACTXFIFO_DECOMPRESS_HALT = 1;
         DACTXFIFO_FULL = 1;
      }
      else if ( DACTXFIFO_COUNT == DACTXFIFO_FIFOSIZE+1)
      {
         DACTXFIFO_DECOMPRESS_HALT = 1;
         DACTXFIFO_FULL = 1;
         DACTXFIFO_OF = 1;
      }
   } else {
      DACTXFIFO_OF = 1;
   }
}

unsigned short DACTXFIFO_Pull(void)
{
   unsigned short output;

   // if a function tries to pull a value from an empty buffer, set the
   // underflow flag and exit the function
   if(DACTXFIFO_EMPTY)
   {
      DACTXFIFO_UF = 1;
      return 0;
   }

   // else the buffer is not empty, pull the value and return it
   else
   {
      DACTXFIFO_FULL = 0;
      // update the first pointer, wrap around if necessary
      if (DACTXFIFO_FIRST==DACTXFIFO_FIFOSIZE-1) DACTXFIFO_FIRST = 0;
      else (DACTXFIFO_FIRST)++;

      // save output value
      output = DACTXFIFO_FIFO[DACTXFIFO_FIRST];

      // decrement buffer size
      if (--(DACTXFIFO_COUNT) == 0) DACTXFIFO_EMPTY = 1;

      if((DACTXFIFO_DECOMPRESS_HALT) &&
         (DACTXFIFO_COUNT <= DACTXFIFO_FIFOSIZE - 2))
      {
         DACTXFIFO_DECOMPRESS_HALT = 0;
      }

      return output;
   }
}
/*
//-----------------------------------------------------------------------------
// ADCRXFIFO_Newest()
//-----------------------------------------------------------------------------
//
// This function returns the newest received ADC sample, which can be used
// to loop back the local audio signal during communication.
//
unsigned short ADCRXFIFO_Newest(void)
{
   return ADCRXFIFO_FIFO[ADCRXFIFO_LAST];
}
*/
//-----------------------------------------------------------------------------
// CLEAR_FIFOS()
//-----------------------------------------------------------------------------
//
// Routine resets all buffers; index values and flags to initial values.
//
void CLEAR_FIFOS(void)
{
   // reset the ADCRX FIFO
   ADCRXFIFO_EMPTY = 1;
   ADCRXFIFO_FIRST = 0;
   ADCRXFIFO_LAST = 0;
   ADCRXFIFO_COUNT = 0;
   ADCRXFIFO_OF = 0;
   ADCRXFIFO_FULL = 0;

   // reset the DACTX FIFO
   DACTXFIFO_EMPTY = 1;
   DACTXFIFO_FIRST = 0;
   DACTXFIFO_LAST = 0;
   DACTXFIFO_COUNT = 0;
   DACTXFIFO_OF = 0;
   DACTXFIFO_FULL = 0;

   // reset the UART TX FIFO
   TransmitFIFO_EMPTY = 1;
   TransmitFIFO_FIRST = 0;
   TransmitFIFO_LAST = 0;
   TransmitFIFO_COUNT = 0;
   TransmitFIFO_OF = 0;
   TransmitFIFO_FULL = 0;

   // reset the UART RX FIFO
   ReceiveFIFO_EMPTY = 1;
   ReceiveFIFO_FIRST = 0;
   ReceiveFIFO_LAST = 0;
   ReceiveFIFO_COUNT = 0;
   ReceiveFIFO_OF = 0;
   ReceiveFIFO_FULL = 0;

}

//-----------------------------------------------------------------------------
// FIFO_ManagementRoutine()
//-----------------------------------------------------------------------------
//
//
//
void FIFO_ManagementRoutine(void)
{
   bit EAState;
 /*
   EAState = EA;
   EA = 0;
   //PCA0CPH0 = ReceiveFIFO_COUNT;
   while (!ADCRXFIFO_EMPTY)               // The compression algorithm should run
      DPCM_Compress();                 // any time samples exist to compress
   EA = EAState;
*/
   EAState = EA;
   EA = 0;
   while ((!ReceiveFIFO_EMPTY) && (!DACTXFIFO_DECOMPRESS_HALT))
   {
                                       // Decompress received samples when
      DPCM_Decompress();               // available
   }
   EA = EAState;

   EAState = EA;
   EA = 0;
    if(ReceiveFIFO_FULL)
   {

      ReceiveFIFO_Pull();
   }
   EA = EAState;

   EAState = EA;
   EA = 0;
   if(DACTXFIFO_OF)
   {
      DACTXFIFO_Pull();
   }
   EA = EAState;


}
//-----------------------------------------------------------------------------
// DPCM_Encode
//-----------------------------------------------------------------------------
//
// Encode the 8-bit (MSBs of 10-bit sample) sample using DPCM compression.
// INPUTS:
//    sample_diff -  8-bit difference between the predicted value and the 8
//                   MSBs of the sample from the ADC
// OUTPUTS:
//    dpcm_code - the 4-bit quantized DPCM code
//
//    The difference will be rounded down if positive and rounded up if
//    negative (i.e. 41 => 32, and -41 => -32).
//
    // volatile short data encoderpredsample;
   static short encoderpredsample;
void DPCM_Compress (void)
{
   short isr_count1 = TMR2;
   short sample_diff;
   unsigned short sample_diff_us;
   unsigned char dpcm_code;
   static unsigned char OutputByte;

   bit EAstate;

   EAstate = EA;
   EA = 0;
   sample_diff = ADCRXFIFO_Pull();
   EA = EAstate;

   sample_diff -= encoderpredsample;

   // determine if the difference is positive or negative
   if (sample_diff < 0)
   {
      sample_diff_us = -sample_diff;   // use the absolute value
   }
   else
   {
      sample_diff_us = sample_diff;
   }

   // narrow down which bits need to be set to use the proper
   // quantization code using a binary search algorithm (divide in halves)
   // the sign of the difference no longer matters, so only use the
   // lower 8 bits
   if (sample_diff_us >= middle)
   {
      if (sample_diff_us >= high_mid)
      {
         if (sample_diff_us >= high_high)
         {
            dpcm_code = 15;
         }
         else
         {
            dpcm_code = 14;
         }
      }
      else
      {
         if (sample_diff_us >= high_low)
         {
            dpcm_code = 13;
         }
         else
         {
            dpcm_code = 12;
         }
      }
   }
   else
   {
      if (sample_diff_us >= low_mid)
      {
         if (sample_diff_us >= low_high)
         {
            dpcm_code = 11;
         }
         else
         {
            dpcm_code = 10;
         }
      }
      else
      {
         if (sample_diff_us >= low_low)
         {
            dpcm_code = 9;
         }
         else
         {
            dpcm_code = 8;
         }
      }
   }


   if (sample_diff < 0)
   {
      dpcm_code = ~dpcm_code + 1;      // use the 2's compliment of
                                       // the dpcm code
      dpcm_code &= 0x0F;               // use only the 4 LSBs for the
                                       // dpcm code
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -