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

📄 89lv51.lst

📁 基于nRF24L01的通讯程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 209          Description:
 210            This function initializes one nRF24L01 device to
 211            RX Mode, set RX address, writes RX payload width,
 212            select RF channel, datarate & LNA HCURR.
 213            After init, CE is toggled high, which means that
 214            this device is now ready to receive a datapacket.
 215          /**************************************************/
 216          void RX_Mode(void)
 217          {
 218   1              CE=0;
 219   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
 220   1      
 221   1              SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 222   1              SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 223   1              SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 224   1              SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
 225   1              SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 226   1              SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
             -ed..
 227   1      
 228   1              CE = 1; // Set CE pin high to enable RX device
 229   1      
 230   1        //  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
             -ss
 231   1        //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
 232   1      
 233   1      }
 234          /**************************************************/
 235          
 236          /**************************************************
 237          Function: TX_Mode();
C51 COMPILER V8.05a   89LV51                                                               06/18/2008 21:31:17 PAGE 5   

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

 298   1              while(TI==0);
 299   1                      TI=0;
 300   1      }
 301          /**************************************************/
 302          
 303          /**************************************************
 304          Function: CheckButtons();
 305          
 306          Description:
 307            check buttons ,if have press,read the key values,
 308            turn on led and transmit it;  after transmition,
 309            if received ACK, clear TX_DS interrupt and enter RX Mode;
 310            turn off the led
 311          /**************************************************/
 312          void CheckButtons()
 313          {
 314   1              uchar Temp,Tempi;
 315   1      
 316   1              P0=0xff;
 317   1              Temp=P0&KEY;                             //read key value from port P0
 318   1              if (Temp!=KEY)
 319   1              {       
 320   2                      delay_ms(10);
 321   2                      Temp=P0&KEY;                            // read key value from port P0
 322   2                      if (Temp!=KEY)
 323   2                      {
 324   3                                      Tempi=Temp>>1;          // Left shift 4 bits
 325   3                                      P0=Tempi;                   // Turn On the led
 326   3                                      tx_buf[0]=Temp;         // Save to tx_buf[0]
 327   3                                      TX_Mode();                      // set TX Mode and transmitting
 328   3                                      TxData(Tempi);                  // send data to uart
 329   3                                      check_ACK();            // if have acknowledgment from RX device,turn on all led
 330   3                                      SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
 331   3                                      delay_ms(200);
 332   3                                      P0=0xff;                        // Turn off the led                             
 333   3                                      RX_Mode();                      // set receive mode
 334   3      
 335   3                                      while((P0&KEY)!=KEY);
 336   3                      }
 337   2              }
 338   1      }
 339          /**************************************************/
 340          
 341          /**************************************************
 342          Function: main();
 343          
 344          Description:
 345            control all subprogrammes;
 346          /**************************************************/
 347          void main(void)
 348          {
 349   1              uchar xx;
 350   1              init_io();              // Initialize IO port
 351   1              Inituart();             // initialize 232 uart
 352   1              //init_int0();  // enable int0 interrupt
 353   1              RX_Mode();              // set RX mode
 354   1              while(1)
 355   1              {
 356   2                      CheckButtons(); // scan key value and transmit 
 357   2      
 358   2                      sta=SPI_Read(STATUS);   // read register STATUS's value
 359   2                  if(RX_DR)                           // if receive data ready (RX_DR) interrupt
C51 COMPILER V8.05a   89LV51                                                               06/18/2008 21:31:17 PAGE 7   

 360   2                    {
 361   3                         SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
 362   3                         flag=1;
 363   3                }
 364   2                  if(MAX_RT)
 365   2                    {
 366   3                         SPI_RW_Reg(FLUSH_TX,0);
 367   3                }
 368   2                      SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
 369   2      
 370   2                      
 371   2                      if(flag)                // finish received
 372   2                      {
 373   3                              flag=0;         //      set flag=0
 374   3                              P0=rx_buf[0]>>1;        // turn on led
 375   3                              delay_ms(200);
 376   3                              P0=0xff;                // turn off led
 377   3                              xx=rx_buf[0]>>1;
 378   3                              TxData(xx);             // send data to uart
 379   3                      }
 380   2              }
 381   1      }
 382          /**************************************************/
 383          
 384          /**************************************************
 385          Function: ISR_int0() interrupt 0;
 386          
 387          Description:
 388            if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
 389            if RX_DR=1,read the payload from RX_FIFO and set flag;
 390          /**************************************************/
 391          void ISR_int0(void) interrupt 0
 392          {
 393   1              sta=SPI_Read(STATUS);   // read register STATUS's value
 394   1          if(RX_DR)                           // if receive data ready (RX_DR) interrupt
 395   1          {
 396   2                      SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
 397   2                      flag=1;
 398   2          }
 399   1          if(MAX_RT)
 400   1          {
 401   2                      SPI_RW_Reg(FLUSH_TX,0);
 402   2              }
 403   1              SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
 404   1      }
 405          /**************************************************/
 406          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    632    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     47      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 + -