#include "bsp_nrf0241.h"
#include "stm32f0xx_gpio.h"
#include "bsp_spi.h"
#include "delay.h"
void KEY0_LED0_Config()
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_AHBPeriphClockCmd(PORTA_LCK , ENABLE);
//用户测试的按键和LED灯
GPIO_InitStruct.GPIO_Pin = KEY0;//按键
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(NRF_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = LED0;//LED灯
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(NRF_PORT , &GPIO_InitStruct);
Pin_LED0(1);//低电平 熄灭LED灯
//添加按键中断
EXTI_InitStructure.EXTI_Line = KEY0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//EXTI_Trigger_Rising , EXTI_Trigger_Falling , EXTI_Trigger_Rising_Falling
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
u8 Get_KEY0(void)
{
return GPIO_ReadInputDataBit(NRF_PORT, KEY0);
}
void Pin_LED0(u8 u)
{
if(u==0)
{
NRF_PORT->BRR = LED0;
}
else
{
NRF_PORT->BSRR = LED0;
}
}
//地址
u8 TX_ADDRESS1[5]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
u8 RX_ADDRESS1[5]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
//配置
void NRF_Config()
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//端口初始化
RCC_AHBPeriphClockCmd(PORTA_LCK , ENABLE);
GPIO_InitStruct.GPIO_Pin = NRF_CE;//CE 输出
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(NRF_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = NRF_IRQ;//IRQ 中断输入
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(NRF_PORT, &GPIO_InitStruct);
//中断
EXTI_InitStructure.EXTI_Line = NRF_IRQ;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//EXTI_Trigger_Rising , EXTI_Trigger_Falling , EXTI_Trigger_Rising_Falling
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//配置RNF
Pin_CSN(1);
Pin_CE(0);
//0x02 发射, 0x03 接收- crc不使能 crc模式:8位效验
//0x0e 发射, 0x0f 接收- crc使能 crc模式:16位效验
SPI_RWReg(0x20+0x00,0x02);//0x02 发射, 0x03 接收
SPI_RWReg(0x20+0x01,0x00);//自动应答 若通道0应答:0x01 禁止自动应答:0x00
SPI_RWReg(0x20+0x02,0x01);//接收地址允许 通道0允许
SPI_RWReg(0x20+0x03,0x03);//设置地址宽度 5个地址宽度
SPI_RWReg(0x20+0x04,0x00);//建立自动重发 禁止自动重发
SPI_RWReg(0x20+0x05,40);//射频通道
SPI_RWReg(0x20+0x06,0x07);//射频寄存器
SPI_RWReg(0x20+0x07,0x70);//状态寄存器
SPI_WriteBuf(0x20+0x0A,RX_ADDRESS1,5);//通道0接收地址
SPI_WriteBuf(0x20+0x10,TX_ADDRESS1,5);//发送地址
SPI_RWReg(0x20+0x11,32);//接收数据通道0有效数据宽度 1-32 字节
}
//发送
void TX_MODE()
{
//配置RNF
Pin_CSN(1);
Pin_CE(0);
//0x02 发射, 0x03 接收- crc不使能 crc模式:8位效验
//0x0e 发射, 0x0f 接收- crc使能 crc模式:16位效验
SPI_RWReg(0x20+0x00,0x02);
SPI_RWReg(0x20+0x01,0x00);//自动应答 若通道0应答:0x01 禁止自动应答:0x00
SPI_RWReg(0x20+0x02,0x01);//接收地址允许 通道0允许
SPI_RWReg(0x20+0x03,0x03);//设置地址宽度 5个地址宽度
SPI_RWReg(0x20+0x04,0x00);//建立自动重发 禁止自动重发
SPI_RWReg(0x20+0x05,40);//射频通道
SPI_RWReg(0x20+0x06,0x07);//射频寄存器
SPI_RWReg(0x20+0x07,0x70);//状态寄存器
SPI_WriteBuf(0x20+0x0A,RX_ADDRESS1,5);//通道0接收地址
SPI_WriteBuf(0x20+0x10,TX_ADDRESS1,5);//发送地址
SPI_RWReg(0x20+0x11,32);//接收数据通道0有效数据宽度 1-32 字节
}
//接收
void RX_MODE()
{
//配置RNF
Pin_CSN(1);
Pin_CE(0);
//0x02 发射, 0x03 接收- crc不使能 crc模式:8位效验
//0x0e 发射, 0x0f 接收- crc使能 crc模式:16位效验
SPI_RWReg(0x20+0x00,0x03);//0x02 发射, 0x03 接收
SPI_RWReg(0x20+0x01,0x00);//自动应答, 若通道0应答:0x01 禁止自动应答:0x00
SPI_RWReg(0x20+0x02,0x01);//接收地址允许 通道0允许
SPI_RWReg(0x20+0x03,0x03);//设置地址宽度 5个地址宽度
SPI_RWReg(0x20+0x04,0x00);//建立自动重发 禁止自动重发
SPI_RWReg(0x20+0x05,40);//射频通道
SPI_RWReg(0x20+0x06,0x07);//射频寄存器
SPI_RWReg(0x20+0x07,0x70);//状态寄存器
SPI_WriteBuf(0x20+0x0A,RX_ADDRESS1,5);//通道0接收地址
SPI_WriteBuf(0x20+0x10,TX_ADDRESS1,5);//发送地址
SPI_RWReg(0x20+0x11,32);//接收数据通道0有效数据宽度 1-32 字节
Pin_CE(1);
delay_us(120);
}
//发送数据
void send_data(u8 *buff,u8 len)
{
TX_MODE();
SPI_RWReg(0xE1,0xFF);//清除发送寄存器
SPI_WriteBuf(0xA0,buff,len);//写入发送寄存器
Pin_CE(1);
delay_us(20);
Pin_CE(0);
}
//接收数据
void receive_data()
{
RX_MODE();
SPI_RWReg(0xE2,0xFF);//清除接收寄存器
Pin_CE(1);
delay_us(120);
}
//获取数据
void Get_Data(u8 *buf,u8 len)
{
SPI_ReadBuf(0x61,buf,len);
}
//清除中断
void ClearStatus()
{
Pin_CE(0);
SPI_RWReg(0x20+0x07,0x70);
Pin_CE(1);
delay_us(20);
}
void Pin_CE(u8 u)
{
if(u==0)
{
NRF_PORT->BRR = NRF_CE;
}
else
{
NRF_PORT->BSRR = NRF_CE;
}
}
u8 Get_IRQ(void)
{
return GPIO_ReadInputDataBit(NRF_PORT, NRF_IRQ);
}
//用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
u8 SPI_WriteBuf(u8 reg, u8 *pBuf, u8 len)
{
u8 s,ctr;
Pin_CSN(0);
delay_us(100);
s=SPI_SendByte(reg);
for(ctr=0; ctr<len; ctr++)
SPI_SendByte(*pBuf++);
Pin_CSN(1);
delay_us(100);
return s;
}
//功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
u8 SPI_ReadBuf(u8 reg, u8 *pBuf, u8 len)
{
u8 s,ctr;
Pin_CSN(0);
delay_us(100);
s=SPI_SendByte(reg);
for(ctr=0; ctr<len; ctr++)
pBuf[ctr]=SPI_SendByte(NOP);
Pin_CSN(1);
delay_us(100);
return s;
}
//功能:NRF24L01 读写寄存器函数
u8 SPI_RWReg(u8 reg, u8 value)
{
u8 status = 0x00;
Pin_CSN(0);
delay_us(100);
status = SPI_SendByte(reg); //发送寄存器地址,
SPI_SendByte(value);//发送寄存器值
delay_us(100);
Pin_CSN(1);
return (status);
}
//功能:NRF24L01 读写寄存器函数
u8 Get_Status()
{
return SPI_RWReg(0x07,0xFF);
}
//检测是否存在
u8 nrf24l0_check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;
SPI_WriteBuf(0x20+0x10,buf,5);
SPI_ReadBuf(0x10,buf,5);
for(i=0; i<5; i++)if(buf[i]!=0xA5)break;
if(i!=5)return 1;
return 0;
}