一對二數據中轉
- /****************************************Copyright (c)****************************************************
- ** 遼寧科技大學
- **
- ** 電子協會
- **
- **--------------File Info---------------------------------------------------------------------------------
- ** File Name: main.c
- ** Last modified Date:
- ** Last Version:
- ** Description: 主函數
- **
- **--------------------------------------------------------------------------------------------------------
- ** Created By: 王愷
- ** Created date: 2013/03/28
- ** Version: V1.0
- ** Descriptions: NRF24L01無線模塊測試程序
- ** 波特率:4800
- **--------------------------------------------------------------------------------------------------------
- ** Modified by:
- ** Modified date:
- ** Version:
- ** Description:
- **
- *********************************************************************************************************/
- /*********************************************************************************************************
- 包含頭文件
- *********************************************************************************************************/
- #include "reg51.h"
- #include "intrins.h"
- #include "nrf24l01.h"
- #include "main.h"
- bit no1 = 0,no2 = 0;
- sbit key1 = P3^2;
- unsigned char rx_buf1[4]={0x99,0x99,0x99,0x99}; //必須是4位數組
- unsigned char rx_buf2[4]={0x99,0x99,0x99,0x99}; //必須是4位數組
- unsigned char rx_buf[4]={0x00,0x00,0x00,0x00}; //必須是4位數組
- void UART_Send_Str(unsigned char *s);
- void SendByte(unsigned char dat);
- /*------------------------------------------------
- 串口初始化
- ------------------------------------------------*/
- void UART_Init(void)
- {
- SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
- TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重裝
- TH1 = 0xFD; // TH1: 重裝值 9600 波特率 晶振 11.0592MHz
- TR1 = 1; // TR1: timer 1 打開
- EA = 1; //打開總中斷
- // ES = 1; //打開串口中斷
- }
- void Delay10ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 117;
- j = 184;
- do
- {
- while (--j);
- } while (--i);
- }
- /*********************************************************************************************************
- ** Function name: main
- ** Descriptions: 主函數(接收)
- ** input parameters: 無
- ** output parameters: 無
- ** Returned value: 無
- //*********************************************************************************************************/
- void main()
- {
-
- uchar number = 0,i = 1,j=0,k = 0;
- uchar number1 = 0;
- uchar data1[20] = {0};
- NRF24L01Int();
- UART_Init();
- /*
- * 接收
- */
- while (1)
- {
- if(no1 == 0)
- {
- s_dizhi(0);//設置地址
- NRF24L01Int();
- NRFSetRXMode();//設置為接收模式
- if (NRFRevDate(rx_buf))//開始接受數
- {
- SendByte(65); //A
- SendByte(70); //F
- SendByte(rx_buf[0]); //此為數據
- SendByte(rx_buf[1]); //此為數據
- SendByte(rx_buf[2]); //從機編碼
- SendByte(rx_buf[3]); //從機發送的次數
- SendByte(70); //F
- SendByte(65); //A
- NRF24L01Int();//進行初始化并且切換地址掃描
- // if(dizhi == 1)no1 = 1;
- // if(dizhi == 2)no2 = 1;
- }
- }
- if(no2 == 0)
- {
- s_dizhi(1);//設置地址
- NRF24L01Int();
- NRFSetRXMode();//設置為接收模式
- if (NRFRevDate(rx_buf))//開始接受數
- {
- SendByte(65); //A
- SendByte(70); //F
- SendByte(rx_buf[0]); //此為數據
- SendByte(rx_buf[1]); //此為數據
- SendByte(rx_buf[2]); //從機編碼
- SendByte(rx_buf[3]); //從機發送的次數
- SendByte(70); //F
- SendByte(65); //A
- NRF24L01Int();
- // if(dizhi == 1)no1 = 1;
- // if(dizhi == 2)no2 = 1;
- }
- }
-
- }
- }
- void SendByte(unsigned char dat)
- {
- SBUF = dat;
- while(!TI);
- TI = 0;
- }
- /*------------------------------------------------
- 發送一個字符串
- ------------------------------------------------*/
- void UART_Send_Str(unsigned char *s)
- {
- while(*s!='\0')// \0 表示字符串結束標志,通過檢測是否字符串末尾
- {
- SendByte(*s);
- s++;
- }
- }
- /*------------------------------------------------
- 串口中斷程序
- ------------------------------------------------*/
- void UART_SER (void) interrupt 4 //串行中斷服務程序
- {
- unsigned char Temp; //定義臨時變量
-
- if(RI) //判斷是接收中斷產生
- {
- RI=0; //標志位清零
- Temp=SBUF; //讀入緩沖區的值
- P1=Temp; //把值輸出到P1口,用于觀察
- SBUF=Temp; //把接收到的值再發回電腦端
- }
- if(TI) //如果是發送標志位,清零
- TI=0;
- }
復制代碼 |