#include "nrf24L01.h" #include "printf.h" #include "delay.h" const u8 TX_ADDRESS[5]={0x34,0x43,0x10,0x10,0x01}; //发送地址 const u8 RX_ADDRESS[5]={0x34,0x43,0x10,0x10,0x01}; const u8 addr[5]={0x33,0x20,0x0D,0x54,0x79}; const u8 Jiang_Xi[5]={0x33,0x20,0x0D,0x54,0x79}; const u8 TEL_PO[5]={0x34,0x43,0x10,0x10,0x01}; const u8 YUN_NAN[5]={0x01,0x10,0x10,0x43,0x34}; const u8 GUI_ZHOU[5]={0x32,0x23,0xAB,0xBA,0x01}; const u8 SI_CHUAN[5]={0x34,0x12,0xBB,0xAA,0x01}; const u8 NEI_MENG_GU[5]={0x34,0x12,0x86,0x75,0x01}; const u8 AN_HUI[5]={0x46,0x56,0x43,0x10,0x00}; const u8 SHAN_XI_XIN_NUO[5]={0x34,0x6E,0x46,0x10,0x01}; const u8 VCM[5]={0x34,0x43,0x10,0x10,0x01}; const u8 LIAO_NING[5]={0x86,0x2c,0x95,0x01,0x01}; const u8 NEW_LIAO_NING[5] = {0x36,0xE4,0xA7,0xBC,0x01}; const u8 TP[5] = {0x42,0x18,0xba,0xe8,0x01}; const u8 NEI_MENG_GU_YI_DONG[5]={0x34,0x12,0x86,0x75,0x01}; const u8 GUANG_XI_YI_DONG[5]={0x33,0x20,0x0D,0x54,0x00}; const u8 LIAO_NING_DIAN_XI[5]={0x86,0x2C,0x95,0x00,0x00}; const u8 FU_JIAN_ZHEN_ZHUO[5]={0x5A,0x45,0x4E,0x4B,0x00}; const u8 SU_ZHOU_MU_LANG[5]={0xAA,0xBB,0x12,0x34,0x00}; const u8 CHONG_QIN_YI_DONG[5]={0x34,0x12,0xBB,0xAA,0x00}; const u8 SHAN_XI_YI_DONG[5]={0x25,0x43,0x10,0x12,0x00}; const u8 QUAN_TONG[5]={0xe7,0x7e,0xe3,0x00,0x00}; const u8 GAN_SU_YI_DONG[5]={0x42,0x22,0xB6,0xE9,0x00}; const u8 XDF[5]={0xC0,0x13,0xE3,0x00,0x00}; const u8 DH[5] = {0x83,0x21,0x95,0x15,0x00}; const u8 ANHUI_DIANXIN[5] = {0x41,0x15,0x43,0x12,0x00}; //初始化24L01的IO口 void NRF24L01_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //使能PA,B,C端口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = SPI1_CE_Pin; // SPI1--CE GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_CE_Port, &GPIO_InitStructure); //初始化指定IO GPIO_SetBits(SPI1_CE_Port,SPI1_CE_Pin);//上拉 GPIO_InitStructure.GPIO_Pin = SPI1_CS_Pin; // SPI1--CS GPIO_Init(SPI1_CS_Port, &GPIO_InitStructure); //初始化指定IO GPIO_SetBits(SPI1_CS_Port,SPI1_CS_Pin);//上拉 GPIO_InitStructure.GPIO_Pin = SPI1_IRQ_Pin; // SPI1--IRQ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_IRQ_Port, &GPIO_InitStructure); //初始化指定IO GPIO_InitStructure.GPIO_Pin = SPI2_CE_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI2_CE_Port, &GPIO_InitStructure); //初始化指定IO GPIO_SetBits(SPI2_CE_Port,SPI2_CE_Pin);//上拉 GPIO_InitStructure.GPIO_Pin = SPI2_CS_Pin; GPIO_Init(SPI2_CS_Port, &GPIO_InitStructure); //初始化指定IO GPIO_SetBits(SPI2_CS_Port,SPI2_CS_Pin);//上拉 GPIO_InitStructure.GPIO_Pin = SPI2_IRQ_Pin; //SPI2--IQR GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI2_IRQ_Port, &GPIO_InitStructure); //初始化指定IO SPI2_Init(); //初始化SPI SPI1_Init(); SPI1_CE =0; //使能24L01 SPI1_CSN=1; //SPI片选取消 SPI2_CE =0; //使能24L01 SPI2_CSN=1; //SPI片选取消 SPI_SetSpeed(SPI_REV,SPI_BaudRatePrescaler_4); } void NRF24L01_Config(SPIType spiNum) { if(spiNum == SPI_PORT1) { SPI1_CE = 0; //chip enable SPI1_CSN = 1; //SPI disable } else { SPI2_CE = 0; //chip enable SPI2_CSN = 1; //SPI disable } } //SPI写寄存器 //reg:指定寄存器地址 //value:写入的值 u8 NRF24L01_Write_Reg(SPIType spiNum,u8 reg,u8 value) { u8 status; if(spiNum == SPI_PORT1){ SPI1_CSN=0; //使能SPI传输 status =SPI_ReadWriteByte(spiNum,reg);//发送寄存器号 SPI_ReadWriteByte(spiNum,value); //写入寄存器的值 SPI1_CSN=1; //禁止SPI传输 }else{ SPI2_CSN=0; //使能SPI传输 status =SPI_ReadWriteByte(spiNum,reg);//发送寄存器号 SPI_ReadWriteByte(spiNum,value); //写入寄存器的值 SPI2_CSN=1; //禁止SPI传输 } return(status); //返回状态值 } //读取SPI寄存器值 //reg:要读的寄存器 u8 NRF24L01_Read_Reg(SPIType spiNum,u8 reg) { u8 reg_val; if(spiNum == SPI_PORT1){ SPI1_CSN = 0; //使能SPI传输 SPI_ReadWriteByte(spiNum,reg); //发送寄存器号 reg_val=SPI_ReadWriteByte(spiNum,0XFF);//读取寄存器内容 SPI1_CSN = 1; //禁止SPI传输 }else{ SPI2_CSN = 0; //使能SPI传输 SPI_ReadWriteByte(spiNum,reg); //发送寄存器号 reg_val=SPI_ReadWriteByte(spiNum,0XFF);//读取寄存器内容 SPI2_CSN = 1; //禁止SPI传输 } return(reg_val); //返回状态值 } //在指定位置读出指定长度的数据 //reg:寄存器(位置) //*pBuf:数据指针 //len:数据长度 //返回值,此次读到的状态寄存器值 u8 NRF24L01_Read_Buf(SPIType spiNum,u8 reg,u8 *pBuf,u8 len) { u8 status,u8_ctr; if(spiNum == SPI_PORT1){ SPI1_CSN = 0; //使能SPI传输 status=SPI_ReadWriteByte(spiNum,reg);//发送寄存器值(位置),并读取状态值 for(u8_ctr=0;u8_ctr