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

📄 rf2401_demo.lst

📁 无线收发程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.12   RF2401_DEMO                                                           04/27/2008 19:15:43 PAGE 1   


C51 COMPILER V8.12, COMPILATION OF MODULE RF2401_DEMO
OBJECT MODULE PLACED IN rf2401_demo.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE rf2401_demo.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /////////////////////////////////////////////////////////////////////
   2          //  Desc:    Demo App for RF2401 Module
   3          //      Vender:  http://www.newmsg.com
   4          //  Date:    2007-3-12
   5          //  Update   2007-9-8
   6          ////////////////////////////////////////////////////////////////////
   7          
   8          #include <reg51.h>
   9          #include <string.h>
  10          #include <stdio.h>
  11          
  12          
  13          
  14          // 常量定义
  15          #define uchar unsigned char 
  16          #define uint  unsigned int
  17          
  18          
  19          #define BYTE_BIT0       0x01
  20          #define BYTE_BIT1       0x02
  21          #define BYTE_BIT2       0x04
  22          #define BYTE_BIT3       0x08
  23          #define BYTE_BIT4       0x10
  24          #define BYTE_BIT5       0x20
  25          #define BYTE_BIT6       0x40
  26          #define BYTE_BIT7       0x80
  27          
  28          
  29          
  30          // <RF2401_Pins 对应引脚> , 具体细节请参考相关电路图 NewMsg_RFDemo2401.SchDoc(用PortelDXP打开)
  31          sbit PWR_UP     = P1^6;
  32          sbit CE         = P1^2;
  33          //      sbit DR2        = P3^5;         //暂时没有用到
  34          //      sbit CLK2       = P3^4;
  35          //      sbit OUT2       = P3^3;
  36          sbit CS         = P1^1;
  37          sbit DR1        = P1^0;
  38          sbit CLK1       = P3^7;
  39          sbit DATA       = P3^3;
  40          
  41          sbit LED0       = P3^4;
  42          sbit LED1       = P3^5;
  43          sbit KEY0       = P3^0;
  44          sbit KEY1       = P3^1;
  45          
  46          
  47          
  48          /*
  49          *************************************************************
  50          *       RF2401 Configuration                                                                    *
  51          *       保存2401的配置信息                                                                              *
  52          *************************************************************
  53          */
  54          /*=====<RF-Configuration-Register 配置信息>=====*/
  55          //芯片测试用,无需修改
C51 COMPILER V8.12   RF2401_DEMO                                                           04/27/2008 19:15:43 PAGE 2   

  56          #define TEST_2          0x8E                    //MSB   D143~D136
  57          #define TEST_1          0x08                    //              D135~D128
  58          #define TEST_0          0x1C                    //              D127~D120
  59          
  60          /* 注意: DATAx_W + ADDRx_W + CRC 的值必须小于256 !  单个数据包的大小必须小于32字节(256位) */
  61          #define DATA2_W         0x10    //2字节     //频道2 数据长度(单位:Bit)
  62          //#define DATA1_W               0xE0    //28字节        //频道1 数据长度(单位:Bit)
  63          #define DATA1_W         0x10    //2字节 //频道1 数据长度(单位:Bit)
  64          
  65          //                                      0xE0 = 224
  66          //16bit Address + 224bit(28byte)Data + 16bit CRC = 256bit
  67          
  68          /* 注意:2401忽略ADDR中超过ADDR_W设定宽度的那些位,同时地址不能全部设置为0 */
  69          //频道2 接收地址(当前模块地址)
  70          #define ADDR2_4         0x00
  71          #define ADDR2_3         0x1c
  72          #define ADDR2_2         0xcc
  73          #define ADDR2_1         0xcc                                                            
  74          #define ADDR2_0         0xcc
  75          //频道1 接收地址
  76          #define ADDR1_4         0x00
  77          #define ADDR1_3         0xcc
  78          #define ADDR1_2         0xcc
  79          #define ADDR1_1         0xcc
  80          #define ADDR1_0         0xcc
  81          
  82          #define ADDR_W          0x10            //2字节 //接收地址宽度(单位:Bit)
  83          #define CRC_L           0x1                                     //CRC模式 0:8位 1:16
  84          #define CRC_EN          0x1                                     //CRC校验启用
  85          
  86          #define RX2_EN          0x0                                     //双频道功能启用
  87          #define CM                      0x1                                     //0:Direct mode 1:ShockBurst mode
  88          #define RFDR_SB         0x0                                     //0:250kbps             1:1Mbps
  89          #define XO_F            0x3             //16M           //nRF2401晶振频率
  90          #define RF_PWR          0x3                                     //信号发射功率
  91          
  92          #define RF_CH           0x2                                     //Channel RF 频率
  93          #define RXEN            0x0//DEF_RXEN           //0:Tx  1:Rx
  94                                                  //程序会重新设置此项参数
  95          
  96          //-----------------------------------------------------------
  97          
  98          
  99          
 100          //<将设置信息组合成每个字节的数据信息,此区域无需修改>
 101          #define RFConfig_Byte0  TEST_2
 102          #define RFConfig_Byte1  TEST_1
 103          #define RFConfig_Byte2  TEST_0
 104          #define RFConfig_Byte3  DATA2_W
 105          #define RFConfig_Byte4  DATA1_W
 106          #define RFConfig_Byte5  ADDR2_4
 107          #define RFConfig_Byte6  ADDR2_3
 108          #define RFConfig_Byte7  ADDR2_2
 109          #define RFConfig_Byte8  ADDR2_1
 110          #define RFConfig_Byte9  ADDR2_0
 111          #define RFConfig_Byte10 ADDR1_4
 112          #define RFConfig_Byte11 ADDR1_3
 113          #define RFConfig_Byte12 ADDR1_2
 114          #define RFConfig_Byte13 ADDR1_1
 115          #define RFConfig_Byte14 ADDR1_0
 116          #define RFConfig_Byte15 (ADDR_W<<2 | CRC_L<<1 | CRC_EN)
 117          #define RFConfig_Byte16 (RX2_EN<<7 | CM<<6    | RFDR_SB<<5 | XO_F <<2 | RF_PWR)
C51 COMPILER V8.12   RF2401_DEMO                                                           04/27/2008 19:15:43 PAGE 3   

 118          #define RFConfig_Byte17 (RF_CH<<1  | RXEN)
 119          
 120          //------------------------------------------------------------
 121          
 122          
 123          //通过宏定义将18字节的寄存器参数按照各个功能分解,以便于参数的调整
 124          
 125          
 126          unsigned char code nRF2401_Conf[18] ={
 127                  RFConfig_Byte0,         
 128                  RFConfig_Byte1,         
 129                  RFConfig_Byte2,         
 130                  RFConfig_Byte3,         
 131                  RFConfig_Byte4,
 132                  RFConfig_Byte5,         
 133                  RFConfig_Byte6,         
 134                  RFConfig_Byte7,         
 135                  RFConfig_Byte8,         
 136                  RFConfig_Byte9,
 137                  RFConfig_Byte10, 
 138                  RFConfig_Byte11, 
 139                  RFConfig_Byte12, 
 140                  RFConfig_Byte13, 
 141                  RFConfig_Byte14,
 142                  RFConfig_Byte15, 
 143                  RFConfig_Byte16, 
 144                  RFConfig_Byte17
 145          };
 146          
 147          //------------------------------------------------------------
 148          
 149          /*
 150          *************************************************************
 151          *       nRF2401 Tx/Rx functions                                                                 *
 152          *
 153          *       void Delay100(void);
 154          *       void Config2401(void);          //配置2401,写入初始化设置
 155          *       void SetTxMode(void);           //设置为发送模式
 156          *       void SetRxMode(void);           //设置为接收模式
 157          *       void nRF2401_TxPacket(unsigned char TxBuf[]);
 158          *                                                               //发送TxBuf[]内的数据 长度由 DATA1_W 决定
 159          *       unsigned char nRF2401_RxPacket(unsigned char *RxBuf);
 160          *                                                               //检查是否有数据需要接受 如果有,则保存至RxBuf[]
 161          *                                                               //返回值 0:没有接收到数据       1:接收到数据
 162          *************************************************************
 163          */
 164          
 165          //16M晶振 600us左右
 166          void Delay100(void)
 167          { 
 168   1              unsigned int i; 
 169   1              for(i=0;i<100;i++);
 170   1      }
 171          
 172          
 173          void Delay(uchar n)
 174          {
 175   1              uint i;
 176   1              while(n--)
 177   1              for(i=0;i<80;i++);      
 178   1      }
 179          
C51 COMPILER V8.12   RF2401_DEMO                                                           04/27/2008 19:15:43 PAGE 4   

 180          
 181          bdata unsigned  char DATA_BUF;
 182          #define DATA7   ((DATA_BUF & BYTE_BIT7) != 0)
 183          #define DATA0   ((DATA_BUF & BYTE_BIT0) != 0)
 184          
 185          /**********************************************************
 186          *       2401数据传输接口
 187          *       详细内容参见2401手册<Configuration mode timing>章节
 188          **********************************************************/
 189          unsigned char Spi_ByteRead(void)
 190          {
 191   1              unsigned char i = 0;
 192   1              for (i=0; i<8; i++)
 193   1              {
 194   2                      DATA_BUF = DATA_BUF << 1;
 195   2                      CLK1 = 1;
 196   2                      DATA = 1;       //设置为输入状态
 197   2                      if (DATA)       //读取最高位,保存至最末尾,通过左移位完成整个字节
 198   2                      {
 199   3                              DATA_BUF |= BYTE_BIT0;
 200   3                      }
 201   2                      else
 202   2                      {
 203   3                              DATA_BUF &= ~BYTE_BIT0;
 204   3                      }
 205   2                      CLK1 = 0;
 206   2               }
 207   1      
 208   1               return DATA_BUF;
 209   1      }
 210          
 211          void Spi_ByteWrite(unsigned char send)
 212          {
 213   1              unsigned char i;
 214   1      
 215   1              DATA_BUF = send;
 216   1      
 217   1              CLK1 = 0;
 218   1      
 219   1              for (i=0; i<8; i++)
 220   1              {
 221   2                      
 222   2                      if (DATA7)      //总是发送最高位
 223   2                      {
 224   3                              DATA = 1;
 225   3                      }
 226   2                      else
 227   2                      {
 228   3                              DATA = 0;
 229   3                      }
 230   2                      CLK1 = 1;
 231   2                      DATA_BUF = DATA_BUF << 1;
 232   2                      CLK1 = 0;
 233   2              }
 234   1      }
 235          
 236          
 237          
 238          
 239          /*      <RF2401配置寄存器的写入方式>
 240          NOTE.
 241          On the falling edge of CS, the nRF2401A updates the number of bits actually shifted
C51 COMPILER V8.12   RF2401_DEMO                                                           04/27/2008 19:15:43 PAGE 5   

 242          in during the last configuration.
 243          Ex:
 244          If the nRF2401A is to be configured for 2 channel RX in ShockBurst., a total of 120
 245          bits must be shifted in during the first configuration after VDD is applied.
 246          Once the wanted protocol, modus and RF channel are set, only one bit (RXEN) is
 247          shifted in to switch between RX and TX.
 248          */
 249          void Config2401(void)

⌨️ 快捷键说明

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