嵌入式单片机开发 STM32HAL库 NRF24L01模块收发数据
立即下载
资源介绍:
嵌入式单片机开发 STM32HAL库 NRF24L01模块收发数据
#include "wifi.h"
__weak void Wait_us(uint16_t time)
{
UNUSED(time);
HAL_Delay(1);
}
__weak uint8_t NRF_ErrorDeal() //检测到发送失败时调用该函数
{
}
/**
* @brief 将NRF24L01模块句柄参数设置为缺省默认值.
* @param hRF NRF24L01模块句柄
*/
void NRF_DeInit(RF_HandlerTypeDef *hRF)
{
#ifndef NRF_SPI
hRF->myspi=NULL;
#else
hRF->myspi=NRF_SPI;
#endif
#ifndef NRF_CE_PORT
hRF->Gpio_CE=NULL;
#else
hRF->Gpio_CE=NRF_CE_PORT;
#endif
#ifndef NRF_CE_PIN
hRF->Gpio_CE_Pin=NULL;
#else
hRF->Gpio_CE_pin=NRF_CE_PIN;
#endif
#ifndef NRF_NSS_PORT
hRF->Gpio_SS=NULL;
#else
hRF->Gpio_SS=NRF_CE_PORT;
#endif
#ifndef NRF_NSS_PIN
hRF->Gpio_SS_Pin=NULL;
#else
hRF->Gpio_SS_Pin=NRF_NSS_PIN;
#endif
#ifndef NRF_INT_PORT
hRF->Gpio_INT=NULL;
#else
hRF->Gpio_INT=NRF_INT_PORT;
#endif
#ifndef NRF_INT_PIN
hRF->Gpio_INT_Pin=NULL;
#else
hRF->Gpio_INT_Pin=NRF_INT_PIN;
#endif
hRF->AddressSize=3; //传输使用的地�???宽度(hRF->Address_Pipe0)
hRF->EnablePipe=NRF24L01_INIT_ENABLE_CHANNEL0; //使能接收通道0
hRF->EnablePipeAck=NRF24L01_INIT_ENABLE_ACK_IN_PIP0; //使能通道0的自动确认
hRF->Max_Repet=10; //最大重发次数(距离过远或受到干扰发送失败时,自动重新发送的次数
hRF->Channel=0; //频道(多个模块互相通信应该使用同一频道)
hRF->DeviceMode=NRF24L01_INIT_CONFIG_EN_CRC|NRF24L01_INIT_CONFIG_CRC_ONE_BYTE|NRF24L01_INIT_CONFIG_POWER_ON; //设备工作模式(使用CRC,CRC占用1位,电源开启
hRF->RetryTime=2000; //自动重发间隔时间(us)详细设置要求见技术手册P
hRF->Address_Pipe0=0x123456UL; //通道0地址(当接收到数据后,目标会发送ACK确认包到0x123456UL,所以必须设置通道0地址为0x123456UL以实现自动确认)
hRF->PackageSize_Pipex[0]=4; //通道0要接收的数据包长为x
}
//写寄存器
void NRF_WriteReg(RF_HandlerTypeDef *hRF,uint8_t cmd,uint8_t *p_value,uint8_t data_length)
{
HAL_GPIO_WritePin(hRF->Gpio_SS,hRF->Gpio_SS_Pin,GPIO_PIN_RESET);
Wait_us(10);
HAL_SPI_Transmit(hRF->myspi,&cmd,1,HAL_MAX_DELAY);
HAL_SPI_Transmit(hRF->myspi,p_value,data_length,HAL_MAX_DELAY);
Wait_us(10);
HAL_GPIO_WritePin(hRF->Gpio_SS,hRF->Gpio_SS_Pin,GPIO_PIN_SET);
Wait_us(10);
}
//读寄存器
void NRF_ReadReg(RF_HandlerTypeDef *hRF,uint8_t cmd,uint8_t *p_value,uint8_t data_length)
{
HAL_GPIO_WritePin(hRF->Gpio_SS,hRF->Gpio_SS_Pin,GPIO_PIN_RESET);
Wait_us(10);
HAL_SPI_Transmit(hRF->myspi,&cmd,1,HAL_MAX_DELAY);
HAL_SPI_Receive(hRF->myspi,p_value,data_length,HAL_MAX_DELAY);
Wait_us(10);
HAL_GPIO_WritePin(hRF->Gpio_SS,hRF->Gpio_SS_Pin,GPIO_PIN_SET);
Wait_us(10);
}
/**
* @brief 初始化NRF24L01模块
* @param hRF NRF24L01模块句柄
*/
void NRF_Init(RF_HandlerTypeDef *hRF)
{
uint8_t buf;
NRF_WriteReg(hRF,NRF24L01_REG_CONFIG|NRF24L01_CMD_REGISTER_W,&(hRF->DeviceMode),1); //配置CONFIG寄存器(使能电源,CRC等)
NRF_WriteReg(hRF,NRF24L01_REG_RF_CH|NRF24L01_CMD_REGISTER_W,&(hRF->Channel),1); //设置信道
NRF_WriteReg(hRF,NRF24L01_REG_EN_AA|NRF24L01_CMD_REGISTER_W,&(hRF->EnablePipeAck),1); //配置使能的管道的自动应答
NRF_WriteReg(hRF,NRF24L01_REG_EN_RXADDR|NRF24L01_CMD_REGISTER_W,&(hRF->EnablePipe),1); //配置使能的管道
buf=hRF->AddressSize>>1;
NRF_WriteReg(hRF,NRF24L01_REG_SETUP_AW|NRF24L01_CMD_REGISTER_W,&buf,1); //配置地址宽度
buf=(((hRF->RetryTime/250)-1)<<4)|((hRF->Max_Repet)&0x0F);
NRF_WriteReg(hRF,NRF24L01_REG_SETUP_RETR|NRF24L01_CMD_REGISTER_W,&buf,1); //配置自动重传间隔和最大计数
buf=hRF->Speed|hRF->Power;
NRF_WriteReg(hRF,NRF24L01_REG_RF_SETUP|NRF24L01_CMD_REGISTER_W,&buf,1); //配置传输速率和功率选项
NRF_WriteReg(hRF,NRF24L01_REG_RX_ADDR_P0|NRF24L01_CMD_REGISTER_W,(uint8_t*)&(hRF->Address_Pipe0),hRF->AddressSize); //设置接收地址0
NRF_WriteReg(hRF,NRF24L01_REG_RX_ADDR_P1|NRF24L01_CMD_REGISTER_W,(uint8_t*)&(hRF->Address_Pipe1),hRF->AddressSize); //设置接收地址1
for(uint8_t i=0;i<4;i++) //配置接收地址2~5
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W+NRF24L01_REG_RX_ADDR_P1+i,&hRF->Address_Pipex[i],1);
for(uint8_t i=0;i<6;i++) //配置0~5接收通道的数据长度
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W+NRF24L01_REG_RX_PW_P0+i,&hRF->PackageSize_Pipex[i],1);
HAL_GPIO_WritePin(hRF->Gpio_CE,hRF->Gpio_CE_Pin,GPIO_PIN_RESET); //默认进入接收模式下的待机模式
}
/**
* @brief 开启NRF24L01模块接收监听
* @param hRF NRF24L01模块句柄
*/
void NRF_StartReceive(RF_HandlerTypeDef *hRF)
{
uint8_t buf=(hRF->DeviceMode&0xFE)|RF_MODULE_WORKMODE_RECEIVE;
NRF_WriteReg(hRF,NRF24L01_REG_CONFIG|NRF24L01_CMD_REGISTER_W,&buf,1);
HAL_GPIO_WritePin(hRF->Gpio_CE,hRF->Gpio_CE_Pin,GPIO_PIN_SET); //接收数据
}
/**
* @brief 通过NRF24L01模块发送数据
* @param hRF NRF24L01模块句柄
* @param source 要发送的数据的首地址
* @param length 要发送的数据长度(最大32字节)
* @param DeviceAddress 数据要发往的地址
* @retval 如果发送成功,返回本次重新发送的次数,其它详见REMAND.md
*/
uint8_t NRF_SendData(RF_HandlerTypeDef *hRF,uint8_t *source,uint8_t length,uint64_t DeviceAddress)
{
uint8_t buf;
HAL_GPIO_WritePin(hRF->Gpio_CE,hRF->Gpio_CE_Pin,GPIO_PIN_RESET); //重置CE电平
Wait_us(10);
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W|NRF24L01_REG_TX_ADDR,(uint8_t*)&DeviceAddress,5);
NRF_WriteReg(hRF,NRF24L01_CMD_TX_PLOAD_W,source,length);
buf=((hRF->DeviceMode)&0xFE)|RF_MODULE_WORKMODE_SEND; //配置为发送模式
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W|NRF24L01_REG_CONFIG,&buf,1);
HAL_GPIO_WritePin(hRF->Gpio_CE,hRF->Gpio_CE_Pin,GPIO_PIN_SET); //拉高一次CE启动发送
Wait_us(20);
HAL_GPIO_WritePin(hRF->Gpio_CE,hRF->Gpio_CE_Pin,GPIO_PIN_RESET);
while(HAL_GPIO_ReadPin(hRF->Gpio_INT,hRF->Gpio_INT_Pin)); //等待发送完成
NRF_ReadReg(hRF,NRF24L01_CMD_REGISTER_R|NRF24L01_REG_STATUS,&buf,1); //读取到STATUS寄存器的值
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W|NRF24L01_REG_STATUS,&buf,1); //写1清除中断标志位
NRF_ReadReg(hRF,NRF24L01_CMD_REGISTER_R|NRF24L01_REG_OBSERVE_TX,&buf,1); //检查发送状态
if(buf&0xF0) //存在丢失的数据包(重发次数大于15),请求错误处理函数
{
NRF_ErrorDeal();
NRF_WriteReg(hRF,NRF24L01_REG_RF_CH|NRF24L01_CMD_REGISTER_W,&(hRF->Channel),1); //重置错误计数器(技术手册手册要求P59)
}
return buf&0x0F; //返回本次发送重传次数
}
/**
* @brief 读取寄存器,接收数据
* @param hRF NRF24L01模块句柄
* @param buffer 接收数据的缓冲区
* @param buffer 接收到的数据的长度
* @param channel 接收到的数据来自的通道号
* @retval 如果接收成功,返回NRF24L01_MSG_RX_SUCCESS,详见REMAND.md
*/
int8_t NRF_ReceiveData(RF_HandlerTypeDef *hRF,uint8_t *buffer,uint8_t *length,uint8_t *channel)
{
uint8_t rec=0,reg=0;
NRF_ReadReg(hRF,NRF24L01_CMD_REGISTER_R|NRF24L01_REG_STATUS,&rec,1); //读取到STATUS寄存器的值
NRF_WriteReg(hRF,NRF24L01_CMD_REGISTER_W|NRF24L01_REG_STATUS,&rec,1); //写1清除中断标志位
if(!(rec&0x40)) //数据准备好中断
return NRF24L01_MSG_RX