通過燈的閃爍來判斷數(shù)據(jù)的接受和發(fā)射
有接收端和發(fā)射段的程序
如果24L01用reg51那么兩個設備都要用reg51,如果用reg52就都得用reg52!
PC通過串口發(fā)送給單片機命令[相當于協(xié)調(diào)器],單片機把命令通過24L01無線發(fā)送給另一個單片機,另一個單片機控制燈LED1,LED2,LED3,LED4閃爍。
無線發(fā)射程序:
無線接收程序:
- #include"2401.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit KEY8=P3^7; //發(fā)送按鍵
- sbit beep=P2^3; //喇叭
- sbit LED6=P1^6; //發(fā)送數(shù)據(jù)時顯示燈
- sbit LED1=P0^0; //接收到數(shù)據(jù)后的功能實現(xiàn)燈
- sbit LED2=P0^1;
- sbit LED3=P0^2;
- sbit LED4=P0^3;
- void delay_ms(uint z) //延時函數(shù)
- {
- uint y;
- while(z--)
- for(y=110;y>0;y--);
- }
- void main()
- {
- uchar Tx_Buf1[]={1}; //發(fā)送的信息1
- uchar Rx_Buf[32]; //接收到的數(shù)據(jù)暫存器,最多32字節(jié)數(shù)據(jù)
- init_NRF24L01();
- LED6=1; //初始燈6熄滅
- while(NRF24L01_Check()) //檢查不到24l01則報警
- {
- beep=0;
- delay_ms(200);
- beep=1;
- delay_ms(200);
- }
- while(1)
- {
- RX_Mode(); //接收模式
- while(!nRF24L01_RxPacket(Rx_Buf)) //等待接收數(shù)據(jù),返回1則接收到數(shù)據(jù),在等待接收數(shù)據(jù)期間,可以隨時變成發(fā)送模式
- {
- if(KEY8==0) //按了按鍵8,則變成發(fā)送模式,發(fā)送對應數(shù)據(jù),發(fā)送完后變成接收模式
- {
- delay_ms(5);//消抖動
- if(KEY8==0)
- {
- while(!KEY8);
- TX_Mode(); //發(fā)送模式
- nRF24L01_TxPacket(Tx_Buf1); //發(fā)送命令數(shù)據(jù)
- LED6=0;
- delay_ms(300);
- LED6=1;
- delay_ms(300); //發(fā)送后LED6閃一下
- break; //退出最近的循環(huán),從而變回接收模式,這句關鍵
- }
- }
- }
- //1//if(Rx_Buf[0]==1) //若接收到對應的數(shù)據(jù)則實現(xiàn)對應功能
- //1//{
- //1// Rx_Buf[0]=0; //清空數(shù)據(jù)
- //1// LED6=0;
- //1// delay_ms(300);
- //1// LED6=1;
- //1// delay_ms(300); //接收到數(shù)據(jù) 后閃爍
- //1//}
- switch(Rx_Buf[0]){//對數(shù)據(jù)進行分析來控制燈亮
- case 0:
- break;
- case 1:
- Rx_Buf[0]=0; //清空數(shù)據(jù)
- LED1=0;
- delay_ms(300);
- LED1=1;
- delay_ms(300);
- break;
- case 2:
- Rx_Buf[0]=0; //清空數(shù)據(jù)
- LED2=0;
- delay_ms(300);
- LED2=1;
- delay_ms(300);
- break;
- case 3:
- Rx_Buf[0]=0; //清空數(shù)據(jù)
- LED3=0;
- delay_ms(300);
- LED3=1;
- delay_ms(300);
- break;
- default:
- Rx_Buf[0]=0; //清空數(shù)據(jù)
- LED4=0;
- delay_ms(300);
- LED4=1;
- delay_ms(300);
- break;
- }
- }
- }
復制代碼
全部資料下載地址:
【小成果】2、PC-單片機-單片機無線串口控制.zip
(90.8 KB, 下載次數(shù): 1341)
2017-8-8 14:50 上傳
點擊文件名下載附件
nrf24l01
|