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

📄 nrf24l01.lst

📁 NRF24L01中文资料和程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 197          
 198          /**************************************************
 199          Function: SPI_Write_Buf();
 200          
 201          Description:
 202            Writes contents of buffer '*pBuf' to nRF24L01
 203            Typically used to write TX payload, Rx/Tx address
 204          /**************************************************/
 205          uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
 206          {
 207   1              uchar status,byte_ctr;
 208   1      
 209   1              CSN = 0;                   // Set CSN low, init SPI tranaction
 210   1              status = SPI_RW(reg);    // Select register to write to and read status byte
 211   1              for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
 212   1              SPI_RW(*pBuf++);
 213   1              CSN = 1;                 // Set CSN high again
 214   1              return(status);          // return nRF24L01 status byte
 215   1      }
 216          /**************************************************/
 217          
 218          /**************************************************
 219          Function: RX_Mode();
 220          
 221          Description:
 222            This function initializes one nRF24L01 device to
 223            RX Mode, set RX address, writes RX payload width,
 224            select RF channel, datarate & LNA HCURR.
 225            After init, CE is toggled high, which means that
 226            this device is now ready to receive a datapacket.
 227          /**************************************************/
 228          void RX_Mode(void)
 229          {
 230   1              CE=0;
 231   1              SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX devi
             -ce as the TX device
 232   1      
 233   1              SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 234   1              SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 235   1              SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 236   1              SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
 237   1              SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 238   1              SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
             -ed..
 239   1      
C51 COMPILER V8.05a   NRF24L01                                                             08/21/2009 10:54:35 PAGE 5   

 240   1              CE = 1; // Set CE pin high to enable RX device
 241   1      
 242   1        //  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
             -ss
 243   1        //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
 244   1      
 245   1      }
 246          /**************************************************/
 247          
 248          /**************************************************
 249          Function: TX_Mode();
 250          
 251          Description:
 252            This function initializes one nRF24L01 device to
 253            TX mode, set TX address, set RX address for auto.ack,
 254            fill TX payload, select RF channel, datarate & TX pwr.
 255            PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
 256          
 257            ToDo: One high pulse(>10us) on CE will now send this
 258            packet and expext an acknowledgment from the RX device.
 259          /**************************************************/
 260          void TX_Mode(void)
 261          {
 262   1              CE=0;
 263   1              
 264   1              SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
 265   1              SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ac
             -k
 266   1              SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
 267   1      
 268   1              SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 269   1              SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 270   1              SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
 271   1              SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 272   1              SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 273   1              SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX
             -_DS enabled..
 274   1              CE=1;
 275   1      
 276   1      }
 277          /**************************************************/
 278          
 279          /**************************************************
 280          Function: check_ACK();
 281          
 282          Description:
 283            check if have "Data sent TX FIFO interrupt",if TX_DS=1,
 284            all led light and after delay 100ms all led close
 285          /**************************************************/
 286          void check_ACK()
 287          {
 288   1              uchar test;
 289   1              test=SPI_Read(READ_REG+STATUS); // read register STATUS's
 290   1              test=test&0x20;                                 // check if have Data sent TX FIFO interrupt (TX_DS=1)
 291   1              if(test==0x20)                                  // TX_DS =1
 292   1              {
 293   2                  delay100();                                 // delay 100ms
 294   2              }
 295   1      }
 296          /**************************************************/
 297          
 298          
C51 COMPILER V8.05a   NRF24L01                                                             08/21/2009 10:54:35 PAGE 6   

 299          /**************************************************
 300          Function: CheckButtons();
 301          
 302          Description:
 303            check buttons ,if have press,read the key values,
 304            turn on led and transmit it;  after transmition,
 305            if received ACK, clear TX_DS interrupt and enter RX Mode;
 306            turn off the led
 307          /**************************************************/
 308          void CheckButtons()
 309          {
 310   1              if (k1==0)
 311   1              {       
 312   2                      delay_ms(10);
 313   2                      if (k1==0)
 314   2                      {
 315   3                                      //xx=0xaa;
 316   3                                      //tx_buf[0]=xx; // Save to tx_buf[0]
 317   3                                      TX_Mode();      // set TX Mode and transmitting
 318   3                                      check_ACK();            // if have acknowledgment from RX device,turn on all led
 319   3                                      SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
 320   3                                      delay_ms(200);                  
 321   3                                      RX_Mode();                      // set receive mode
 322   3      
 323   3                                      while(!k1);
 324   3                      }
 325   2              }
 326   1      }
 327          /**************************************************/
 328          
 329          /**************************************************
 330          Function: main();
 331          
 332          Description:
 333            control all subprogrammes;
 334          /**************************************************/
 335          void main(void)
 336          {       uchar i;
 337   1              init_io();              // Initialize IO port
 338   1              Inituart();             // initialize 232 uart
 339   1              init_int0();    // enable int0 interrupt
 340   1              RX_Mode();              // set RX mode
 341   1              
 342   1              while(1)
 343   1              {
 344   2                      CheckButtons(); // scan key value and transmit 
 345   2                      if(flag)                // finish received
 346   2                      {
 347   3                              flag=0;         //      set flag=0
 348   3                              for(i=0;i<5;i++)
 349   3                              {
 350   4                              P0=rx_buf[i];   // turn on led
 351   4                              delay_ms(2000);
 352   4                              }
 353   3                      }
 354   2              }
 355   1      }
 356          /**************************************************/
 357          
 358          /**************************************************
 359          Function: ISR_int0() interrupt 0;
 360          
C51 COMPILER V8.05a   NRF24L01                                                             08/21/2009 10:54:35 PAGE 7   

 361          Description:
 362            if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
 363            if RX_DR=1,read the payload from RX_FIFO and set flag;
 364          /**************************************************/
 365          void ISR_int0(void) interrupt 0
 366          {
 367   1              sta=SPI_Read(STATUS);   // read register STATUS's value
 368   1              if(RX_DR)                               // if receive data ready (RX_DR) interrupt
 369   1              {
 370   2                      SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
 371   2                      flag=1;
 372   2              }
 373   1              if(MAX_RT)
 374   1              {
 375   2                      SPI_RW_Reg(FLUSH_TX,0);
 376   2              }
 377   1                      SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
 378   1      }
 379          /**************************************************/
 380          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    569    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     17      11
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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