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

📄 common.c

📁 cc2500 测试程序
💻 C
📖 第 1 页 / 共 2 页
字号:

unsigned char halRfReceivePacket(unsigned char *rxBuffer, unsigned char length)
{

        extern unsigned char PacketLength,Rssi,status[2];
        unsigned int j;


        halSpiStrobe(CCxxx0_SRX);

	Software_delay(2000);

	if(halSpiReadStatus(CCxxx0_RXBYTES)==0)  return 0;
	Software_delay(4000);

	j=length*360;
	while((halSpiReadStatus(CCxxx0_MARCSTATE)&0x1f)!=0x01)
	{
		
		Software_delay(2000);
		j--;
		if(!j)
		{
			halSpiStrobe(CCxxx0_SIDLE);
			halSpiStrobe(CCxxx0_SFRX);			
			return 0;
		}
	}

    	PacketLength = halSpiReadReg(CCxxx0_RXFIFO);

    	if((PacketLength<0x20) && (PacketLength>0))
	{
		halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, PacketLength);

        	// Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI)
                halSpiReadBurstReg(CCxxx0_RXFIFO, status, 2);
              //  Rssi = halSpiReadStatus(CCxxx0_RSSI);
        	return (status[1] & CRC_OK);
    	}
	else
	{
		halSpiStrobe(CCxxx0_SIDLE);
		halSpiStrobe(CCxxx0_SFRX);

        	return 0;
	}


}






//-------------------------------------------------------------
//	通过写NOP获得RF的状态
//-------------------------------------------------------------
unsigned char spiGetRxTxStatus(void)
{
	unsigned char RxTxstatus;

	P_CSn_L;
	while (P_SO);

	RxTxstatus=SPI_write(CCxxx0_SNOP);

	P_CSn_H;

        return(RxTxstatus);
}

//----------------------------------------------------------------
//	复位RF
//----------------------------------------------------------------
void POWER_UP_RESET_CCxxx0(void)
{

	halSpiStrobe(CCxxx0_SIDLE);

	P_CSn_H;

	Software_delay(30);
	P_CSn_L;

	Software_delay(30);
	P_CSn_H;

	Software_delay(100);
	P_CSn_L;

	while(P_SO);

	halSpiStrobe(CCxxx0_SRES);

        while(P_SO);

	P_CSn_H;

}



void halRfWriteRfSettings_CC2500(void)
{
        extern unsigned char systemerr;

	halSpiWriteReg(CCxxx0_FSCTRL1, 0x09);	
	halSpiWriteReg(CCxxx0_FSCTRL0, 0x00);	

	halSpiWriteReg(CCxxx0_FREQ2, 0x5D);
	halSpiWriteReg(CCxxx0_FREQ1, 0x93);
	halSpiWriteReg(CCxxx0_FREQ0, 0xB1);

	halSpiWriteReg(CCxxx0_MDMCFG4, 0x2D);
	halSpiWriteReg(CCxxx0_MDMCFG3, 0x3B);
	halSpiWriteReg(CCxxx0_MDMCFG2, 0x73);
	halSpiWriteReg(CCxxx0_MDMCFG1, 0xa2);// FEC
	halSpiWriteReg(CCxxx0_MDMCFG0, 0xF8);

	halSpiWriteReg(CCxxx0_CHANNR, 0x10);	//CHANNEL=0X10
	halSpiWriteReg(CCxxx0_DEVIATN, 0x01);

	halSpiWriteReg(CCxxx0_FREND1, 0xB6);
	halSpiWriteReg(CCxxx0_FREND0, 0x10);

	halSpiWriteReg(CCxxx0_MCSM1, 0x30);//0X00>NO CCA; 0X30>CCA
	halSpiWriteReg(CCxxx0_MCSM0, 0x18);

	halSpiWriteReg(CCxxx0_FOCCFG, 0x1D);
	halSpiWriteReg(CCxxx0_BSCFG, 0x1C);

	halSpiWriteReg(CCxxx0_AGCCTRL2, 0xC7);
	halSpiWriteReg(CCxxx0_AGCCTRL1, 0x00);//cca=0x10
	halSpiWriteReg(CCxxx0_AGCCTRL0, 0xB2);

	halSpiWriteReg(CCxxx0_FSCAL3, 0xEA);
	halSpiWriteReg(CCxxx0_FSCAL2, 0x0A);
	halSpiWriteReg(CCxxx0_FSCAL0, 0x11);

	halSpiWriteReg(CCxxx0_FSTEST, 0x59);

	halSpiWriteReg(CCxxx0_TEST2, 0x88);
	halSpiWriteReg(CCxxx0_TEST1, 0x31);
	halSpiWriteReg(CCxxx0_TEST0, 0x0B);

	halSpiWriteReg(CCxxx0_IOCFG2, 0x29);//CCA=0x09:1=free

	halSpiWriteReg(CCxxx0_IOCFG0, 0x06);
      //  halSpiWriteReg(CCxxx0_IOCFG0, 0x07);
      //  halSpiWriteReg(CCxxx0_IOCFG0, 0x80);

	halSpiWriteReg(CCxxx0_PKTCTRL1, 0x04);//CRC FLUSH,ADRESS CHECK
	halSpiWriteReg(CCxxx0_PKTCTRL0, 0x05);

	halSpiWriteReg(CCxxx0_ADDR, 0x01);	//ADDRESS=01 FOR RECEIVE 设备地址
	halSpiWriteReg(CCxxx0_PKTLEN, 0xFF);    // 数据包长度

	if(halSpiReadReg(CCxxx0_MDMCFG1)!=0xA2) systemerr=1;
	else systemerr=0;	
	
}


void SetupCC2500(void)
{
        extern unsigned char paTable_CC2500[8];

	POWER_UP_RESET_CCxxx0();

	halRfWriteRfSettings_CC2500();

	halSpiWriteBurstReg(CCxxx0_PATABLE, paTable_CC2500, sizeof(paTable_CC2500));

	halSpiStrobe(CCxxx0_SCAL);

	Software_delay(4000);

	halSpiStrobe(CCxxx0_SIDLE);
}


//------------------------------------------------------------
//  判断CC1100,CC2500是否接收到数据,如果有,在数码管显示出来
//
//-------------------------------------------------------------
unsigned char RxCCxx00(void)
{	
        extern unsigned char rxBuffer_CCxx00[0x20],status[2];
	unsigned char	length,i;

	length = sizeof(rxBuffer_CCxx00);

        if (halRfReceivePacket(rxBuffer_CCxx00, length))
	{
		i=rxBuffer_CCxx00[0];	
		send_uart0_a_byte( i );		//显示收到的数据

                send_uart0_a_byte( status[0] );

                send_uart0_a_byte(status[1]&0x7f );

		return 1;
			
	}
	else	return 0;
	
}

//------------------------------------------------------------
// TX OPTION CC2500,CC2500pa,cc2550,cc2550pa发射数据
//
//-------------------------------------------------------------
void TxCC2500(void)
{
	extern unsigned char txBuffer_CCxx00[0x21];
  	halRfSendPacket(txBuffer_CCxx00, (txBuffer_CCxx00[0]+1));
     //   Software_delay(1000);
	
}


//---------------------------------------------------------------
void RXCC2500(void)
{

        extern unsigned char rxBuffer_CCxx00[0x20],txBuffer_CCxx00[0x21],PacketLength;
        extern unsigned char uart_data_ok,rx_buf;
	unsigned char j;

	if(RxCCxx00())				//是否收到数据?
	{

		if(rxBuffer_CCxx00[1]==0x01) //有,判断FLAG
		{								//是对方发来的信息
								//返回收到的信息
			for(j=0;j<PacketLength;j++)	//save
			{
				txBuffer_CCxx00[j+1]=rxBuffer_CCxx00[j];
			}
			txBuffer_CCxx00[2]=0x07;	//SLAVE FLAG
			txBuffer_CCxx00[0]=PacketLength;

			halSpiStrobe(CCxxx0_SIDLE);

			Software_delay(20000);

			TxCC2500();	
		}
								

	
	}
	//else
	//{
		//if(uart_data_ok)
		//{

			//txBuffer_CCxx00[1]=rx_buf;

			//TxCC2500();					//发射信息

			//uart_data_ok=0;

		//}
	//}	
	
}



//**************************************************************************************//
//  函数名称: void ADC12_Initi(void)                                                   //
//  函数说明: AD初始化                                                                 //
//  参数说明: 无                                                                       //
//  返回值  : 无                                                                       //
//  调用函数: void Software_delay(unsigned int i)                                      //
//                                                                                      //
//  被调函数: unsigned char AD12_CONVER(void)                                          //
//  全局变量: 无                                                                       //
//**************************************************************************************//


void ADC12_Initi(void)
{


      P6SEL |= 0x02;                               // Enable A/D channel A1

      Software_delay(100);                        // 延时,等待P6口功能

      ADC12CTL0 = ADC12ON+SHT0_2+REFON+REF2_5V;   // Turn on and set up ADC12
      ADC12CTL1 = SHP+CSTARTADD_1;                // Use sampling timer
      ADC12MCTL1 = SREF_1+INCH_1;                 // Vr+=Vref+,channel A1

      Software_delay(8000);                       // 延时,等待AD设置完成

      ADC12CTL0 |= ENC;                           // Enable conversions


}


//**************************************************************************************//




//**************************************************************************************//
//  函数名称: unsigned char AD12_CONVER(void)                                          //
//  函数说明: AD1转换                                                                  //
//  参数说明: 无                                                                       //
//  返回值  : 0-->电池电压低于5V   1--> 电池电压高于5.1V   2-->电池电压低于4.9V        //
//  调用函数: void ADC12_Initi(void)                                                   //
//                                                                                      //
//  被调函数: void Battery_Low_Alarm(void)                                             //
//  全局变量: 无                                                                       //
//**************************************************************************************//


void AD12_CONVER(void)
{

      extern unsigned char temp_adc[2];
      unsigned char i;
      unsigned int x=0;

      ADC12_Initi();                                // AD初始化

      for (i=0;i<16;i++)                            // 16次转换结果
       {
            ADC12CTL0 |= ADC12SC;                   // Start conversion
            while ((ADC12IFG & BIT1)==0);

            x=x+ADC12MEM1;                          // add the result

            Software_delay(4000);
       }

      P6SEL &= ~0x02;

      x=x/16;                                       // 求AD转化结果

      temp_adc[0] = x&0x00ff;
      temp_adc[1] =(x>>8);



}

//**************************************************************************************//

⌨️ 快捷键说明

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