標(biāo)題: 單片機(jī)原程序是發(fā)送一個(gè)數(shù)據(jù),我在發(fā)送的數(shù)組里放了倆個(gè)數(shù)據(jù),程序接收后就沒反應(yīng)了 [打印本頁(yè)]

作者: 李志崗    時(shí)間: 2017-5-26 00:16
標(biāo)題: 單片機(jī)原程序是發(fā)送一個(gè)數(shù)據(jù),我在發(fā)送的數(shù)組里放了倆個(gè)數(shù)據(jù),程序接收后就沒反應(yīng)了


【小成果】1、無線發(fā)射控制LED【不同命令亮不同的燈】.rar (111.73 KB, 下載次數(shù): 4)

發(fā)送程序
  1. #include<reg51.h>
  2. #include"2401.h"

  3. #define uint unsigned int
  4. #define uchar unsigned char

  5. sbit KEY1=P3^4;                                         //發(fā)送按鍵
  6. sbit KEY2=P3^5;
  7. sbit KEY3=P3^6;
  8. sbit KEY4=P3^7;
  9. sbit beep=P2^3;                                        //喇叭
  10. sbit LED6=P1^6;                                         //發(fā)送數(shù)據(jù)時(shí)顯示燈+接收到數(shù)據(jù)后的功能實(shí)現(xiàn)燈
  11.   
  12. void delay_ms(uint z)                        //延時(shí)函數(shù)
  13. {
  14.         uint y;
  15.         while(z--)
  16.                 for(y=110;y>0;y--);
  17. }
  18. void main()
  19. {
  20.          uchar Tx_Buf[2];        //發(fā)送的信息1
  21. //         uchar Rx_Buf[32];          //接收到的數(shù)據(jù)暫存器,最多32字節(jié)數(shù)據(jù)  
  22.          init_NRF24L01();
  23.          LED6=1;                                //初始燈6熄滅   

  24.         while(NRF24L01_Check())                                        //檢查不到24l01則報(bào)警
  25.         {
  26.                 beep=0;
  27.                 delay_ms(1000);
  28.                 beep=1;
  29.                 delay_ms(1000);
  30.         }
  31.         while(1)
  32.         {       
  33.             
  34.                         if(KEY1==0)                                                 //按了按鍵1,則變成發(fā)送模式,發(fā)送對(duì)應(yīng)數(shù)據(jù),發(fā)送完后變成接收模式
  35.                         {       
  36.                                 delay_ms(5);//消抖動(dòng)
  37.                                 if(KEY1==0)
  38.                                 {
  39.                                          while(!KEY1);
  40.                                         TX_Mode();                                                 //發(fā)送模式
  41.                                         Tx_Buf[0]=1;                                        //將1發(fā)送過去
  42.                                         Tx_Buf[1]=2;
  43.                               nRF24L01_TxPacket(Tx_Buf);                //發(fā)送命令數(shù)據(jù)
  44.                                         LED6=0;
  45.                                     delay_ms(300);
  46.                                         LED6=1;
  47.                                     delay_ms(300);                                        //發(fā)送后LED1閃一下
  48.                                         break;                                                        //退出最近的循環(huán),從而變回接收模式,這句關(guān)鍵
  49.                                  }       
  50.                         }
  51.          }
  52. }

復(fù)制代碼



接收源代碼
  1. #include<reg51.h>
  2. #include"2401.h"

  3. #define uint unsigned int
  4. #define uchar unsigned char

  5. sbit KEY8=P3^7;                                     //發(fā)送按鍵
  6. sbit beep=P2^3;                                        //喇叭
  7. sbit LED6=P1^6;                                         //發(fā)送數(shù)據(jù)時(shí)顯示燈
  8. sbit LED1=P1^0;                                        //接收到數(shù)據(jù)后的功能實(shí)現(xiàn)燈
  9. sbit LED2=P1^1;
  10. sbit LED3=P1^2;
  11. sbit LED4=P1^3;  
  12. sbit IN1=P1^4;
  13. sbit IN2=P1^5;
  14. sbit ENA=P1^6;

  15. void delay_us(unsigned int aa);

  16. void delay_ms(uint z)                        //延時(shí)函數(shù)
  17. {
  18.         uint y;
  19.         while(z--)
  20.                 for(y=110;y>0;y--);
  21. }
  22. void main()
  23. {
  24.      uint cycle=0,T=1024;
  25.          uchar Tx_Buf1[]={1};        //發(fā)送的信息1
  26.          uchar Rx_Buf[32];          //接收到的數(shù)據(jù)暫存器,最多32字節(jié)數(shù)據(jù)  
  27.          init_NRF24L01();
  28.          LED6=1;                                //初始燈6熄滅   

  29.         while(NRF24L01_Check())                                        //檢查不到24l01則報(bào)警
  30.         {
  31.                 beep=0;
  32.                 delay_ms(200);
  33.                 beep=1;
  34.                 delay_ms(200);
  35.         }
  36.         while(1)
  37.         {       
  38.                 RX_Mode();                                                        //接收模式  
  39.                  //1//if(Rx_Buf[0]==1)                                                           //若接收到對(duì)應(yīng)的數(shù)據(jù)則實(shí)現(xiàn)對(duì)應(yīng)功能
  40.                  //1//{
  41.                  //1//   Rx_Buf[0]=0;                                //清空數(shù)據(jù)
  42.                  //1//   LED6=0;
  43.                  //1//         delay_ms(300);
  44.                  //1//         LED6=1;
  45.                  //1//         delay_ms(300);                                //接收到數(shù)據(jù) 后閃爍          
  46.                  //1//}
  47.                 switch(Rx_Buf[0])
  48.            {    //對(duì)數(shù)據(jù)進(jìn)行分析來控制燈亮
  49.                 case 0:
  50.                         break;
  51.                 case 1:
  52.                         Rx_Buf[0]=0;                                //清空數(shù)據(jù)
  53.                     LED1=0;
  54.                         delay_us(300);
  55.                         LED1=1;
  56.                         delay_us(300);
  57.                     IN1=1;   
  58.                 IN2=0;
  59.             while(cycle!=T)   
  60.                 {
  61.                   ENA=1;
  62.               delay_us(cycle++);
  63.               ENA=0;
  64.               delay_us(T-cycle);      
  65.             }
  66.                         IN1=1;   
  67.                 IN2=0;                
  68.                         break;
  69.                 default:
  70.                         Rx_Buf[0]=0;                                //清空數(shù)據(jù)
  71.                     LED4=0;
  72.                         delay_ms(300);
  73.                         LED4=1;
  74.                         delay_ms(300);       
  75.                         break;
  76.                 }                
  77.         }
  78. }
  79. void delay_us(unsigned int aa)
  80. {
  81. while(aa--);
  82. }


復(fù)制代碼








歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1