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

📄 wireless_cc1100rx.c.bak

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

   isr_count1 = Q_VALUES[dpcm_code];
   isr_count1 = isr_count1;
   encoderpredsample = encoderpredsample + Q_VALUES[dpcm_code];

   if(!OutputByteReady)
   {
	  OutputByteReady = TRUE;
	  OutputByte = dpcm_code;
      OutputByte<<=4;
   }
   else
   {
      OutputByteReady = FALSE;
      OutputByte |= dpcm_code & 0x0F;
      EAstate = EA;
      EA = 0;
      TransmitFIFO_Push(OutputByte);
      EA = EAstate;
	  Compress_Bytes ++;
   }

}

//-----------------------------------------------------------------------------
// DPCM_Decode
//-----------------------------------------------------------------------------
//
// decode the 8-bit (MSBs of 10-bit sample) sample using DPCM compression.
// INPUTS:
//    old_prediction - the 8-bit unsigned predicted value from the current
//                     cycle which will be used to calculate the new predicted
//                     value
//    dpcm_code -   the 4-bit code indicating the quantized difference between
//                  the old_prediction and the current sample values
//                  (see DPCM_Encode for the dpcm_code values)
// OUTPUTS:
//    new_prediction - the 8-bit unsigned predicted value for the next cycle
//
void DPCM_Decompress (void)
{
	unsigned char CompressedByte, CompressedSample;
	static signed short UncompressedWord;
   bit EAstate;

   EAstate = EA;
   EA = 0;
	CompressedByte = ReceiveFIFO_Pull();
	
	//CompressedByte = TransmitFIFO_Pull();
	
	EA = EAstate;

	CompressedSample = CompressedByte>>4;

	UncompressedWord = UncompressedWord + Q_VALUES[CompressedSample];

   if (UncompressedWord > 3)
   {
      UncompressedWord--;
   }
   else
   if (UncompressedWord < 3)
   {
      UncompressedWord++;
   }

   if (UncompressedWord > 511)
   {
      UncompressedWord = 511;
   }
   else
   if (UncompressedWord < -512)
   {
      UncompressedWord = -512;
   }

   EAstate = EA;
   EA = 0;
   DACTXFIFO_Push(UncompressedWord + 512);
   EA = EAstate;

	CompressedSample = CompressedByte & 0x0F;

	UncompressedWord = UncompressedWord + Q_VALUES[CompressedSample];

   if (UncompressedWord > 3)
   {
      UncompressedWord--;
   }
   else
   if (UncompressedWord < 3)
   {
      UncompressedWord++;
   }

   if (UncompressedWord > 511)
   {
      UncompressedWord = 511;
   }
   else
   if (UncompressedWord < -512)
   {
      UncompressedWord = -512;
   }

	EAstate = EA;
	EA = 0;
   DACTXFIFO_Push(UncompressedWord + 512);
   EA = EAstate;
   
}
void WaitMS (unsigned int count)
{

   TCON &= ~0x30;                      // Stop Timer0; clear TF0
   TMOD &= ~0x0F;                      // Timer0 in 16-bit mode
   TMOD |=  0x01;
   CKCON |= 0x04;                      // Timer0 counts SYSCLKs

   TH0 = (-SYSCLK / 1000) >> 8;        // overflow in 1ms
   TL0 = (-SYSCLK / 1000);

   TR0 = 1;                            // Start Timer0
   do {

      while (!TF0);                    // wait for overflow
      TR0 = 0;                         // Stop Timer0
      TF0 = 0;                         // clear overflow flag

      TH0 = (-SYSCLK / 1000) >> 8;     // overflow in 1ms
      TL0 = (-SYSCLK / 1000);

      TR0 = 1;                         // Start Timer0

      count--;                         // update counter
   } while (count != 0);               // continue for <count> ms

   TR0 = 0;                            // Stop Timer0
}
//-------------------------------------------------------------------------------------------------------
// Macro to reset the CCxxx0 and wait for it to be ready
void RESET_CCxxx0(void) 
   {
     //   SPI0CN  = 0x08;                     // clear all flags
        NSSMD0 = 0; 
	while (SO_data);
//        {SO_data = 0;}
        SPI0DAT = CCxxx0_SRES;                //
        SPI_WAIT();
     //   while (SO_data)
     //   {SO_data = 0;}
        NSSMD0 = 1;
        SO_data = 0;
    }
//-------------------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------------------
// Macro to reset the CCxxx0 after power_on and wait for it to be ready
// IMPORTANT NOTICE:
// The file Wait.c must be included if this macro shall be used
// The file is located under: ..\Lib\Chipcon\Hal\CCxx00
//
//                 min 40 us
//                 <------------------>
// NSSMD0      |--|  |--------------------|            |-----
//          |  |  |                    |            |
//              --                      ------------
//
// MISO                                     |----|
//          -----------------------------|  |    |
//                                        --      ---------
//               Unknown / don't care
//                                       SRES     done
//
void POWER_UP_RESET_CCxxx0(void) 
 {
        NSSMD0 = 1;
        halWait(1);
        NSSMD0 = 0;
        halWait(1);
        NSSMD0 = 1;
        halWait(50);
        RESET_CCxxx0();
 }
//-------------------------------------------------------------------------------------------------------
void SPI_WAIT(void)
{ 
  while(!SPIF);
  SPIF = 0;
 // SPI0CN  = 0x08;                     // clear all flags
 // CSn=0;
}

//-------------------------------------------------------------------------------------------------------
//  void halWait(int timeout)
//
//  DESCRIPTION:
//      Runs an idle loop for [timeout] microseconds.
//
//  ARGUMENTS:
//      unsigned char timeout
//          The timeout in microseconds
//-------------------------------------------------------------------------------------------------------
void halWait(int timeout) {
    do {
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
    } while (--timeout);
}// halWait
//-------------------------------------------------------------------------------------------------------
//  void halSpiStrobe(BYTE strobe)
//
//  DESCRIPTION:
//      Function for writing a strobe command to the CCxxx0
//
//  ARGUMENTS:
//      BYTE strobe
//          Strobe command
//-------------------------------------------------------------------------------------------------------
void halSpiStrobe(unsigned char strobe) {
  //  SPI0CN  = 0x08;                     // clear all flags
    NSSMD0 = 0;
    while (SO_data);
 //   {SO_data = 0;}
    SPI0DAT = strobe;
    SPI_WAIT();
    NSSMD0 = 1;
    SO_data = 0;
}// halSpiStrobe

//-----------------------------------------------------------------------------------
//	 Timer1_Loader()
//-----------------------------------------------------------------------------------

void Timer1_Loader(unsigned char time_ms)
{
  USHORT temp_value;

  temp_value.S = 256*256-time_ms*500;

  TH1 = temp_value.C[0];
  TL1 = temp_value.C[1]; 
}

//-------------------------------------------------------------------------------------------------------
//  void halSpiReadBurstReg(BYTE addr, BYTE *buffer, BYTE count)
//
//  DESCRIPTION:
//      This function reads multiple CCxxx0 register, using SPI burst access.
//
//  ARGUMENTS:
//      BYTE addr
//          Address of the first CCxxx0 register to be accessed.
//      BYTE *buffer
//          Pointer to a byte array which stores the values read from a
//          corresponding range of CCxxx0 registers.
//      BYTE count
//          Number of bytes to be written to the subsequent CCxxx0 registers.
//-------------------------------------------------------------------------------------------------------
void halSpiReadBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count) {
    unsigned char i;
  //  SPI0CN  = 0x08;                     // clear all flags
    NSSMD0 = 0;
    while (SO_data);
 //   {SO_data = 0;}
    SPI0DAT = (addr | READ_BURST);
    SPI_WAIT();
    for (i = 0; i < count; i++) {
        SPI0DAT = 0;
        SPI_WAIT();
        buffer[i] = SPI0DAT;
    }
    NSSMD0 = 1;
    SO_data = 0;
}// halSpiReadBurstReg

unsigned char ReceivePocket_Wait(void)
{
  
  halSpiStrobe(CCxxx0_SRX);
    // Wait for GDO0 to be set -> sync received

  while (!GDO0);

    // Wait for GDO0 to be cleared -> end of packet
  while (GDO0);
     return TRUE;
}
//-------------------------------------------------------------------------------------------------------
//  BOOL halRfReceivePacket(BYTE *RxTxBuffer, UINT8 length)
//
//  DESCRIPTION: 
//      This function can be used to receive a packet of variable packet length (first byte in the packet
//      must be the length byte). The packet length should not exceed the RX FIFO size.
//      To use this function, GD00 must be configured to be asserted when sync word is sent and 
//      de-asserted at the end of the packet => halSpiWriteReg(CCxxx0_IOCFG0, 0x06);
//      Also, APPEND_STATUS in the PKTCTRL1 register must be enabled.
//      The function implements polling of GDO0. First it waits for GD00 to be set and then it waits
//      for it to be cleared.
//      After the GDO0 pin has been de-asserted, the RXBYTES register is read to make sure that there
//      are bytes in the FIFO. This is because the GDO signal will indicate sync received even if the
//      FIFO is flushed due to address filtering, CRC filtering, or packet length filtering. 
//  
//  ARGUMENTS:
//      BYTE *RxTxBuffer
//          Pointer to the buffer where the incoming data should be stored
//      UINT8 length
//          Pointer to a variable containing the size of the buffer where the incoming data should be
//          stored. After this function returns, that variable holds the packet length.
//          
//  RETURN VALUE:
//      BOOL
//          TRUE:   CRC OK
//          FALSE:  CRC NOT OK (or no packet was put in the RX FIFO due to filtering)
//-------------------------------------------------------------------------------------------------------
unsigned char halRfReceivePacket(unsigned char *RxTxBuffer, unsigned char *length)
{	unsigned char value;
    unsigned char status[2];
    unsigned char packetLength;
    halSpiStrobe(CCxxx0_SIDLE);
   if ((value=halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) {

        // Read length byte
        packetLength = halSpiReadReg(CCxxx0_RXFIFO);
    
  
        if(packetLength ==62)
		      {LEVEL3=0;
		       LEVEL4=0;}
		    else
		      {LEVEL3=1;
		       LEVEL4=1;}
 
  
    
        // Read data from RX FIFO and store in RxTxBuffer
        if (packetLength <= *length) {
            halSpiReadBurstReg(CCxxx0_RXFIFO, RxTxBuffer, packetLength); 
            *length = packetLength;
            // Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
            halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2); 
        	halSpiStrobe(CCxxx0_SIDLE);
            // MSB of LQI is the CRC_OK bit
       //     return (status[LQI] & CRC_OK);
            
             return 1;
        }
		else 
		{
            *length = packetLength;
		
             //Flush RX FIFO
             halSpiStrobe(CCxxx0_SFRX);
             halSpiStrobe(CCxxx0_SIDLE);
             return FALSE;
        }
    }
	else
	  {
	    halSpiStrobe(CCxxx0_SIDLE);
            return FALSE;
      }

}// halRfReceivePacket
//-------------------------------------------------------------------------------------------------------
//  void h

⌨️ 快捷键说明

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