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

📄 rxmain.lst

📁 RF24L01用源程序,非常好用
💻 LST
📖 第 1 页 / 共 2 页
字号:
 207   1              status = SPI_RW(reg);    // Select register to write to and read status byte
 208   1              for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
 209   1              SPI_RW(*pBuf++);
 210   1              CSN = 1;                 // Set CSN high again
 211   1              return(status);          // return nRF24L01 status byte
 212   1      }
 213          /**************************************************/
 214          
 215          /**************************************************
 216          Function: RX_Mode();
 217          
 218          Description:
 219            This function initializes one nRF24L01 device to
 220            RX Mode, set RX address, writes RX payload width,
 221            select RF channel, datarate & LNA HCURR.
 222            After init, CE is toggled high, which means that
 223            this device is now ready to receive a datapacket.
 224          /**************************************************/
 225          void RX_Mode(void)
 226          {
 227   1              CE=0;
 228   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
 229   1      
 230   1              SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 231   1              SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 232   1              SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 233   1              SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
 234   1              SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 235   1              SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
             -ed..
 236   1      
 237   1              CE = 1; // Set CE pin high to enable RX device
 238   1      
 239   1        //  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
C51 COMPILER V8.08   RXMAIN                                                                02/02/2009 14:31:25 PAGE 5   

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

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

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


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