收發一體,硬件SPI
單片機源程序如下:
- /******************** (C) COPYRIGHT 2014青風電子********************************
- * 文件名 :main.c
- * 描述 :I2C 讀寫(AT24C02)測試。
- *
- * 實驗平臺:QF-STM8開發板
- * 庫版本 :ST3.0.0
- *
- * 作者 :青風
- *
- **********************************************************************************/
- /***頭文件調用****/
- #include <stdio.h>
- #include "stm8s.h"
- #include "stm8s_clk.h"
- #include "string.h"
- #include "stm8s_spi.h"
- #include "stm8s_gpio.h"
- #include "stm8s_clk.h"
- #include "rf24l01.h"
- #include "uart.h"
- #include "led.h"
- extern volatile unsigned char touch_flag;
- uchar rece_buf[32];
- //接收串口數據
- int recive(void)
- {
- uint i=0;uchar j=0;
- if(((UART1->SR)&(1<<5)))//串口狀態寄存器讀數據寄存器非空判斷是否接收數據
- {
- rece_buf[++j]=UART1->DR;//把接收的數據放入DR寄存器中
- UART1->SR=(0<<5);
- while(i<500) //設置接收長度
- {
- if(((UART1->SR)&(1<<5)))//再判斷一次
- {
- rece_buf[++j]=UART1->DR;
- i=0;
- }
- i++;//循環接收
- }
- rece_buf[0] =j;
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int main(void)
- {
-
- uchar i=0;uint t=0;
- SPI_RF_Init();//初始化無線nrf24l01底層驅動
- USART_Configuration();//初始化串口底層驅動
- while(NRF24L01_Check()); //自發送自接收檢查是否無線設備完好
- NRF24L01_RT_Mode();//設置模式
-
- while(1)//循環判斷
- {
- if(SPI_RF_IRQ()==0) //通過nrf的中斷管腳判斷是否是接收無線數據
- {
- if(NRF24L01_RxPacket(rece_buf)==0)
- {
- if((rece_buf[0])<32)
- {
- for(i=0;i<rece_buf[0];i++)
- UART_send_byte(rece_buf[i+1]);
- }
- }
- }
- if(recive()==0) //發送無線數據
- {
- SPI_RF_CE_LOW();
- NRF24L01_Write_Reg(WRITE_REG+CONFIG,0x0e);
- SPI_RF_CE_HIGH();
- NRF24L01_TxPacket(rece_buf);
- SPI_RF_CE_LOW();
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
實驗四十一:無線串口透傳.zip
(1.15 MB, 下載次數: 10)
2018-9-15 20:33 上傳
點擊文件名下載附件
源碼 24L01 下載積分: 黑幣 -5
|