tr_test1.c

来自「CC1100无线收发模块开发程序」· C语言 代码 · 共 546 行 · 第 1/2 页

C
546
字号
	TIMSK2 = 0x00; //timer 2 interrupt sources
	
	PCMSK0 = 0x00; //pin change mask 0 
	PCMSK1 = 0x00; //pin change mask 1 
	PCMSK2 = 0x00; //pin change mask 2
	PCICR  = 0x00; //pin change enable 
	PRR    = 0x00; //power controlle
	
	//_SEI(); 	   //re-enable interrupts
}


/*********************************************************/ 
/*函数名字:SpiTxRx_Byte                                 */ 
/*输入参数:写入寄存器的数据                             */  
/*输出参数:读取寄存器的数据                             */ 
/*功能描述:通过SPI 串口读写一字节数据                   */    
/*********************************************************/   
unsigned char SpiTxRx_Byte(unsigned char data) 
 { 
     unsigned char i, temp = 0;              
          
     for (i=0; i<8; i++)                         //读写八位 
      {                                      
       if (data & 0x80) 
            { 
             MOSI_H;                                 //写出高位    
                }                 
           else  
            { 
             MOSI_L;                                 //写出低位 
            } 
           
       SCLK_H;                                   //发出时钟 
       NOP(); 
           NOP();
            
           data <<= 1; 
           temp <<= 1;                               //数据左移 
                                  
       if (MISO_H) 
            { 
             temp += 1 ;                             //记录高位   
                }              
           else   
            { 
             temp += 0;                              //记录低位 
                } 

       SCLK_L;                                   //结束时钟                         
       NOP(); 
           NOP();
           
      } 
     return temp;                                //返回数据 
 } 



/*********************************************************/ 
/*函数名字:RESET_CC1100                                 */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:写入复位滤波命                               */    
/*********************************************************/ 
void RESET_CC1100(void)  
 { 
         CSN_L;                                      //片选使能  
         while (MISO_H);                             //等待响应 
         SpiTxRx_Byte(CC_SRES);                      //复位命令 
         while (MISO_H);                             //等待响应 
         CSN_H;                                      //结束使能 
 } 

/*********************************************************/ 
/*函数名字:POWER_UP_RESET_CC1100                        */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:模块上电初始化                               */  
/*********************************************************/   
void POWER_UP_RESET_CC1100(void)  
 { 
     CSN_H;                                      //上电拉高 
	 SCLK_H;
         MOSI_L;                              // 
	 delay_ns(1);                                // 
	 CSN_L;                                      //片选使能 
	 delay_ns(1);                                //  
	 CSN_H;                                      //选择拉高 
	 delay_ns(80);                               // 
	 SCLK_L;                                     //拉低时钟 
	 RESET_CC1100();                                        //复位命令 
 }  

/*********************************************************/ 
/*函数名字:Init_cc1100                                  */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:初始化模块                                   */    
/*********************************************************/  
void Init_cc1100(void)  
 { 
     POWER_UP_RESET_CC1100();   //上电复位 
     delay_ns(40);
     WriteRfSettings();                          //写入配置 
     Spi_Write_Burst(CC_PATABLE,PaTabel,8);      //功率配置 
     Spi_Write_Strobe(CC_STX);                 //进入空闲 
     //Spi_Write_Strobe(CC_SFRX);                  //清缓冲区       

 } 
  
/*********************************************************/ 
/*函数名字:Spi_Write_Strobe                             */ 
/*输入参数:滤波命令                                     */  
/*输出参数:无                                           */ 
/*功能描述:写入滤波命令                                 */   
/*********************************************************/   
void Spi_Write_Strobe(unsigned char strobe)  
 { 
     CSN_L;                                      //片选使能                             
     while (MISO_H);                             //等待响应 
     SpiTxRx_Byte(strobe);                                 //写入命令 
     CSN_H;                                      //结束使能                          
 } 




/**********************************************************/ 
/*函数名字:Spi_Write_Byte                                */ 
/*输入参数:寄存器地址,配置                              */  
/*输出参数:无                                            */ 
/*功能描述:单字节写入寄存器                              */  
/**********************************************************/  
void Spi_Write_Byte(unsigned char addr,unsigned char value) 
 { 
     CSN_L;                                      //片选使能 
     while (MISO_H);                             //等待响应  
     SpiTxRx_Byte(addr);                         //送出地址 
     SpiTxRx_Byte(value);                        //写入配置 
     CSN_H;                                      //结束使能                          
 } 
  

  
/**********************************************************/ 
/*函数名字:Spi_Write_Burst                               */ 
/*输入参数:寄存器地址,发射缓冲区首址,发送字节数        */  
/*输出参数:无                                            */ 
/*功能描述:连续写入寄存器数据                            */  
/**********************************************************/  
void Spi_Write_Burst(unsigned char addr,unsigned char *buffer,unsigned char count)  
{ 
     unsigned char i; 
         
     CSN_L;                                      //片选使能 
     while (MISO_H);                             //等待响应 
     SpiTxRx_Byte((addr|Write_Burst));           //连续写入 
     SpiTxRx_Byte(count);//长度
     for (i = 0; i < count; i++)                 // 
           { 
       SpiTxRx_Byte(buffer[i]);                  //送出数据 
      } 
     CSN_H;                                      //结束使能                            
 } 
   



/*********************************************************/ 
/*函数名字:WriteRfSettings                              */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:模块寄存器配置                               */    
/*********************************************************/  
void WriteRfSettings(void) 
 { 
    Spi_Write_Byte (CC_IOCFG2,IOCFG2);          // 
     Spi_Write_Byte (CC_IOCFG0,IOCFG0);          // 
     Spi_Write_Byte (CC_FIFOTHR,FIFOTHR);        //
     Spi_Write_Byte (CC_SYNC1,SYNC1);          // 
     Spi_Write_Byte (CC_SYNC0,SYNC0);          // 
     Spi_Write_Byte (CC_PKTLEN,PKTLEN);          // 
     Spi_Write_Byte (CC_PKTCTRL1,PKTCTRL1);      // 
     Spi_Write_Byte (CC_PKTCTRL0,PKTCTRL0);      // 
     Spi_Write_Byte (CC_ADDR,ADDR);              // 
     Spi_Write_Byte (CC_CHANNR,CHANNR);          // 
     Spi_Write_Byte (CC_FSCTRL1,FSCTRL1);        // 
     Spi_Write_Byte (CC_FSCTRL0,FSCTRL0);        // 
     Spi_Write_Byte (CC_FREQ2,FREQ2);            // 
     Spi_Write_Byte (CC_FREQ1,FREQ1);            // 
     Spi_Write_Byte (CC_FREQ0,FREQ0);            // 
     Spi_Write_Byte (CC_MDMCFG4,MDMCFG4);        // 
     Spi_Write_Byte (CC_MDMCFG3,MDMCFG3);        // 
     Spi_Write_Byte (CC_MDMCFG2,MDMCFG2);        // 
     Spi_Write_Byte (CC_MDMCFG1,MDMCFG1);        // 
     Spi_Write_Byte (CC_MDMCFG0,MDMCFG0);        // 
     Spi_Write_Byte (CC_DEVIATN,DEVIATN);        //
     Spi_Write_Byte (CC_MCSM2,MCSM2);            //
     Spi_Write_Byte (CC_MCSM1,MCSM1);            //
     Spi_Write_Byte (CC_MCSM0,MCSM0);            // 
     Spi_Write_Byte (CC_FOCCFG,FOCCFG);          // 
     Spi_Write_Byte (CC_BSCFG,BSCFG);            // 
     Spi_Write_Byte (CC_AGCCTRL2,AGCCTRL2);      // 
     Spi_Write_Byte (CC_AGCCTRL1,AGCCTRL1);      // 
     Spi_Write_Byte (CC_AGCCTRL0,AGCCTRL0);      //    
     Spi_Write_Byte (CC_WOREVT1,WOREVT1);        //
     Spi_Write_Byte (CC_WOREVT0,WOREVT0);        //
     Spi_Write_Byte (CC_WORCTRL,WORCTRL);        //  
     Spi_Write_Byte (CC_FREND1,FREND1);          // 
     Spi_Write_Byte (CC_FREND0,FREND0);          // 
     Spi_Write_Byte (CC_FSCAL3,FSCAL3);          // 
     Spi_Write_Byte (CC_FSCAL2,FSCAL2);          // 
     Spi_Write_Byte (CC_FSCAL1,FSCAL1);          // 
     Spi_Write_Byte (CC_FSCAL0,FSCAL0);          // 
    // Spi_Write_Byte (CC_FSTEST,FSTEST);          // 
     Spi_Write_Byte (CC_TEST2,TEST2);            // 
     Spi_Write_Byte (CC_TEST1,TEST1);            // 
     Spi_Write_Byte (CC_TEST0,TEST0);     //
     //Spi_Write_Byte (0x3e,0xc0);
    // Spi_Write_Byte (0x3f,0xc0);
     //Spi_Write_Byte (0x40,0xc0);
    // Spi_Write_Byte (0x41,0xc0);
     //Spi_Write_Byte (0x42,0xc0);
     //Spi_Write_Byte (0x43,0xc0);
     //Spi_Write_Byte (0x44,0xc0);
    // Spi_Write_Byte (0x45,0xc0);
 }  
  


unsigned char buffer[]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
/*********************************************************/ 
/*函数名字:main                                         */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:主程序                                       */     
/*********************************************************/
void main(void) 
 {  
     PORT_Init();
     Init_Devices();  
     Init_cc1100();      
     
     __delay_cycles(200000);//Init finished
    
     
     //===============================以下为发送程序=====================  
     while(1)                                    //循环程序 
     {   
       GDO0_H;
       delay_ns(10);
       GDO0_L;
       delay_ns(10);
       
       
        
        //Spi_Write_Byte (CC_FREND0,0x16);            //功率配置      
        //Spi_Write_Burst(CC_TXFIFO,buffer,sizeof(buffer));
        //Spi_Write_Strobe(CC_STX);
        //while(GDO0_L);
        //while(GDO0_H);
        //__delay_cycles(200000);//
         //Spi_Write_Strobe(CC_SFTX);                  //清缓冲区 
         //Spi_Write_Strobe(CC_SIDLE);                 //进入空闲 
        //__delay_cycles(300000);//
     } 
    //===============================以下为发送程序完毕=====================  
    
 }        

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

⌨️ 快捷键说明

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