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

📄 89lv51.lst

📁 基于51单片机的NRF24L01的计较好的程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 217   1              SPI_RW(*pBuf++);
 218   1              CSN = 1;                 // Set CSN high again
 219   1              return(status);          // return nRF24L01 status byte
 220   1      }
 221          /**************************************************/
 222          
 223          /**************************************************
 224          Function: RX_Mode();
 225          
 226          Description:
 227            This function initializes one nRF24L01 device to
 228            RX Mode, set RX address, writes RX payload width,
 229            select RF channel, datarate & LNA HCURR.
 230            After init, CE is toggled high, which means that
 231            this device is now ready to receive a datapacket.
 232          /**************************************************/
 233          void RX_Mode(void)
 234          {
 235   1              CE=0;
 236   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
 237   1      
 238   1              SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 239   1              SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
C51 COMPILER V7.06   89LV51                                                                03/21/2007 11:35:07 PAGE 5   

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

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

 360          
 361          Description:
 362            control all subprogrammes;
 363          /**************************************************/
 364          void main(void)
 365          {
 366   1              uchar xx;
 367   1              init_io();              // Initialize IO port
 368   1              Inituart();             // initialize 232 uart
 369   1              //init_int0();  // enable int0 interrupt
 370   1              RX_Mode();              // set RX mode
 371   1              while(1)
 372   1              {
 373   2                      CheckButtons(); // scan key value and transmit 
 374   2                      sta=SPI_Read(STATUS);   // read register STATUS's value
 375   2                  if(RX_DR)                           // if receive data ready (RX_DR) interrupt
 376   2                    {
 377   3                         SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
 378   3                         flag=1;
 379   3                }
 380   2                  if(MAX_RT)
 381   2                    {
 382   3                         SPI_RW_Reg(FLUSH_TX,0);
 383   3                }
 384   2                      SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
 385   2                      if(flag)                // finish received
 386   2                      {
 387   3                              flag=0;         //      set flag=0
 388   3                              P0=rx_buf[0];   // turn on led
 389   3                              delay_ms(500);
 390   3                              P0=0xff;                // turn off led
 391   3                              xx=rx_buf[0]>>1;// right shift 4 bits
 392   3                              TxData(xx);             // send data to uart
 393   3                      }
 394   2              }
 395   1      }
 396          /**************************************************/
 397          
 398          /**************************************************
 399          Function: ISR_int0() interrupt 0;
 400          
 401          Description:
 402            if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
 403            if RX_DR=1,read the payload from RX_FIFO and set flag;
 404          /**************************************************/
 405          void ISR_int0(void) interrupt 0
 406          {
 407   1              sta=SPI_Read(STATUS);   // read register STATUS's value
 408   1              if(RX_DR)                               // if receive data ready (RX_DR) interrupt
 409   1              {
 410   2                      SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
 411   2                      flag=1;
 412   2              }
 413   1              if(MAX_RT)
 414   1              {
 415   2                      SPI_RW_Reg(FLUSH_TX,0);
 416   2              }
 417   1                      SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
 418   1      }
 419          /**************************************************/
 420          

C51 COMPILER V7.06   89LV51                                                                03/21/2007 11:35:07 PAGE 8   


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    606    ----
   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 + -