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

📄 cc1100.c

📁 STC89C51单片机与cc1100组合构建的短距离无线通信
💻 C
📖 第 1 页 / 共 2 页
字号:


#include<reg51.h> 
#include "config.h" 
#include <intrins.h>
#include <io.h>

char flag;

/*发送数据************************************************/ 
     unsigned char Tx_data[] =  
              {0x02 ,0x59, 0xD6, 0x29, 0x0E, 0x0E, 0x77, 0x88}; 

/*接收数据************************************************/ 
     unsigned char Rx_data[10];         
          
/*功率配置************************************************/ 
    
unsigned char PaTabel[] = 
              {0xC0, 0xC8, 0x85, 0x51, 0x3A, 0x06, 0x1C, 0x6C}; 

			  
static void delay(unsigned int s)
{
	unsigned int i;
	for(i=0; i<s; i++);
	for(i=0; i<s; i++);
}        

void halWait(unsigned int timeout) {
    do {
        _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_(); 
    } while (--timeout);
}

void delay_ns(unsigned char n)                   //纳秒延时  
 { 
     while (n--); 
 }  
          
/*********************************************************/ 
/*函数名字:delay_ms                                     */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:延时1 毫秒                                   */  
/*                              */   
/*********************************************************/ 
void delay_ms(void)                              //毫秒延时  
 {     
     unsigned int i; 
          
     for (i=0; i<1500; i++)  
         { 
         _nop_();
         } 
 } 
  
/*********************************************************/ 
/*函数名字:delay_nms                                    */ 
/*输入参数:延时周期参数据                               */  
/*输出参数:无                                           */ 
/*功能描述:延时程序                                     */  
/*                               */   
/*********************************************************/ 
void delay_nms(unsigned int n)                   //延时周期 
 { 
     unsigned int i; 
          
     for (i=0; i<n; i++) 
          { 
           delay_ms(); 
          } 
 }     

 
void Send_char(unsigned char sdata)
{
    SBUF=sdata;
   while(!TI);
   TI  =0;
}
  
/*********************************************************/ 
/*函数名字:PORT_Init                                    */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:单片机端口初始化                             */  
/*                               */   
/*********************************************************/ 

void PORT_Init(void) 
 { 
    
    CSN=0;
	SCK=0;
	CSN=1;
	delay(5000);
 }          

/*********************************************************/ 
/*函数名字:Led_Light                                    */ 
/*输入参数:无                                           */  
/*输出参数:无                                           */ 
/*功能描述:LED1闪烁三次                                 */  
/*                               */   
/*********************************************************/   
void Led_Light(void) 
 { 
     unsigned char i; 
          
         for (i=0;i<3;i++)                           //闪烁三次 
          { 
          // LED1_ON;                                  //输出指示 
		   LED2_ON;
           delay_nms(50);                            // 
         //  LED1_OFF;                                 //关闭指示 
           LED2_OFF;
		   delay_nms(50);                            // 
          } 
 }   

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

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




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

/*********************************************************/ 
/*函数名字:Spi_Read_Byte                                */ 
/*输入参数:寄存器地址                                   */  
/*输出参数:寄存器配置                                   */ 
/*功能描述:单字节读取寄存器                             */  
/*                               */ 
/*********************************************************/  
unsigned char Spi_Read_Byte(unsigned char addr) 
 { 
     unsigned char value;             
                 
     CSN_L;                                      //片选使能     
     while (MISO_H);                             //等待响应  
     SpiTxRx_Byte((addr|Read_Byte));             //送出地址  
     value = SpiTxRx_Byte(0);                    //读寄存器 
     CSN_H;                                      //结束使能                           
          
     return value;                               //返回数据 
 } 

/**********************************************************/ 
/*函数名字: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_Read_Burst                                */ 
/*输入参数:寄存器地址,接收缓冲区首址,接收字节数        */  
/*输出参数:结果存储在缓冲区地址                          */ 
/*功能描述:连续读取寄存器数据                            */  
/*                               */ 
/**********************************************************/  
void Spi_Read_Burst (unsigned char addr,unsigned char *buffer,unsigned char count)  
 { 
     unsigned char i; 
          
     CSN_L;                                      //片选使能 
     while (MISO_H);                             //等待响应  
         SpiTxRx_Byte((addr|Read_Burst));            //连续读取 
          
     for (i = 0; i < count; i++)                 // 
          { 
       buffer[i] = SpiTxRx_Byte(0);              //存储数据 
      } 
     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));           //连续写入 
          
     for (i = 0; i < count; i++)                 // 
           { 
       SpiTxRx_Byte(buffer[i]);                  //送出数据 
      } 
     CSN_H;                                      //结束使能                            
 } 
   
/*********************************************************/ 
/*函数名字:Spi_Write_Packet                             */ 
/*输入参数:发送缓冲区首址,数组长度                      */  
/*输出参数:无                                           */ 
/*功能描述:发送缓冲区数据                               */  
/*                              */   
/*********************************************************/  
void Spi_Write_Packet(unsigned char *Tx_buffer,unsigned char size)  
 {    
     unsigned char i; 
           
         Spi_Write_Byte(CC_TXFIFO,size);             //先送长度 
         Spi_Write_Burst(CC_TXFIFO,Tx_buffer,size);  //发送数据 
         Spi_Write_Strobe(CC_STX);                   //发送模式 
          
         i = 0; 
         while (GDO0_L)                              //等待送出 
          { 
           if (i > 10) break;                        //限时等待 
           delay_nms(2);                             // 
           i++;                                      // 
          } 
         i = 0;  
         while (GDO0_H)                              //送出完毕 

⌨️ 快捷键说明

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