24L01发送程序1138IO模拟移植
STM32寄存器多多,所以决定用回AVR结果发现AVR的稳定性非常值得怀疑。首先就是内部参考电压用不到,2片MEGA16结果有一片用不了内部参考电压(PS电路程序完全一样,而且外部REF是悬空不会导致内部短路),不过这个就原谅它了。编程CTC模式,结果发现1S中断的程序竟然有时进可以运行有时不可运行(这中情况在重插电源时交替发生,后来就干脆一直进不了中断),还有一芯片一个不小心烧错时钟溶丝,连续都读不出东西更别说下载了,不过突然间有下得了,之后又下载不了。。。所以觉得还是用51好,要是当初用汇编学51再用增强型51那可能爽很多,,。
#ifndef __NRF_H#define __NRF_H#include"main.h"/*NRF24L01端口定义CE-PF6 IRQ-PF5 CSN-PF0 SCK-PF1 MOSI-PF2 MISO-PF3*/#define NRF_PERIPH SYSCTL_PERIPH_GPIOF#define NRF_PORT GPIO_PORTF_BASE#define CSN_PIN GPIO_PIN_0#define SCK_PIN GPIO_PIN_1#define MOSI_PIN GPIO_PIN_2#define MISO_PIN GPIO_PIN_3#define IRQ_PIN GPIO_PIN_5#define CE_PIN GPIO_PIN_6//**************发送缓冲区**********************************uchar TxBuf[32]={0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,}; // //***********************NRF24L01*************************************#define TX_ADR_WIDTH 5 // 5 uints TX address width#define RX_ADR_WIDTH 5 // 5 uints RX address width#define TX_PLOAD_WIDTH 32 // 32 uints TX payload#define RX_PLOAD_WIDTH 32 // 32 uints TX payloaduchar TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址//******************NRF24L01寄存器指令***********************************#define READ_REG 0x00 // 读寄存器指令#define WRITE_REG 0x20 // 写寄存器指令#define RD_RX_PLOAD 0x61 // 读取接收数据指令#define WR_TX_PLOAD 0xA0 // 写待发数据指令#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令#define NOP 0xFF // 保留//*******************SPI(nRF24L01)寄存器地址**************************#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式#define EN_AA 0x01 // 自动应答功能设置#define EN_RXADDR 0x02 // 可用信道设置#define SETUP_AW 0x03 // 收发地址宽度设置#define SETUP_RETR 0x04 // 自动重发功能设置#define RF_CH 0x05 // 工作频率设置#define RF_SETUP 0x06 // 发射速率、功耗功能设置#define STATUS 0x07 // 状态寄存器#define OBSERVE_TX 0x08 // 发送监测功能#define CD 0x09 // 地址检测 #define RX_ADDR_P0 0x0A // 频道0接收数据地址#define RX_ADDR_P1 0x0B // 频道1接收数据地址#define RX_ADDR_P2 0x0C // 频道2接收数据地址#define RX_ADDR_P3 0x0D // 频道3接收数据地址#define RX_ADDR_P4 0x0E // 频道4接收数据地址#define RX_ADDR_P5 0x0F // 频道5接收数据地址#define TX_ADDR 0x10 // 发送地址寄存器#define RX_PW_P0 0x11 // 接收频道0接收数据长度#define RX_PW_P1 0x12 // 接收频道0接收数据长度#define RX_PW_P2 0x13 // 接收频道0接收数据长度#define RX_PW_P3 0x14 // 接收频道0接收数据长度#define RX_PW_P4 0x15 // 接收频道0接收数据长度#define RX_PW_P5 0x16 // 接收频道0接收数据长度#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置//***************************************unsigned int sta;//状态标志unsigned int RX_DR;//=sta(0x00000020);//sta第6位unsigned int TX_DS;//=sta(0x00000010);//sta第5位unsigned int MAX_RT;//=sta(0x00000008);//sta第4位//******************************************************void Delay(uint s);void inerDelay_us(uchar n);void init_NRF24L01(void);uint SPI_RW(uint val);uchar SPI_Read(uchar reg);void SetRX_Mode(void);uint SPI_RW_Reg(uchar reg, uchar value);uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num);uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num);uchar nRF24L01_RxPacket(uchar* rx_buf);void nRF24L01_TxPacket(uchar * tx_buf);//********************长延时**********************void Delay(unsigned int s){unsigned int i;for(i=0; is; i++);for(i=0; is; i++);}//******************************************************//延时函数/****************************************************************/void inerDelay_us(unsigned char n){unsigned int i;for(;n>0;n--)for(i=8;i>0;i--);}//************************************************************/*NRF24L01初始化//*********************************************************/void init_NRF24L01(void){inerDelay_us(100);SysCtlPeripheralEnable(NRF_PERIPH); // 使能NRF所在的GPIO端口GPIOPinTypeGPIOOutput(NRF_PORT, CSN_PIN|SCK_PIN|MOSI_PIN|CE_PIN);// 设置NRF所在的管脚为输出GPIOPinTypeGPIOInput(NRF_PORT,MISO_PIN|IRQ_PIN);GPIOPinWrite(NRF_PORT, CE_PIN,0x00); // chip enableGPIOPinWrite(NRF_PORT, CSN_PIN,0xff); // Spi disable GPIOPinWrite(NRF_PORT, SCK_PIN,0x00); // Spi clock line init highSPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送}
/*******************************************************************/*函数:uint SPI_RW(uint val)/*功能:NRF24L01的SPI写时序/*************************************************************/uint SPI_RW(uint val){uint bit_ctr;for(bit_ctr=0;bit_ctr8;bit_ctr++) // output 8-bit{if(val 0x80)GPIOPinWrite(NRF_PORT, MOSI_PIN,0xff);elseGPIOPinWrite(NRF_PORT, MOSI_PIN,0x00); // output 'uchar', MSB to MOSIval = (val 1); // shift next bit into MSB..GPIOPinWrite(NRF_PORT, SCK_PIN,0xff); // Set SCK high..if (GPIOPinRead(NRF_PORT, MISO_PIN) == 0xff)val |= 1; // capture current MISO bitGPIOPinWrite(NRF_PORT, SCK_PIN,0x00); // ..then set SCK low again}return(val); // return read val}/***********************************************************/*函数:uchar SPI_Read(uchar reg)/*功能:NRF24L01的SPI时序/**************************************************************/uchar SPI_Read(uchar reg){uchar reg_val;GPIOPinWrite(NRF_PORT, CSN_PIN,0x00); // CSN low, initialize SPI communication...reg_val=SPI_RW(reg); // Select register to read from..reg_val = SPI_RW(0); // ..then read registervalueGPIOPinWrite(NRF_PORT, CSN_PIN,0xff); // CSN high, terminate SPI communicationreturn(reg_val); // return register value}/**********************************************************//*功能:NRF24L01读写寄存器函数/**************************************************************/uint SPI_RW_Reg(uchar reg, uchar value){uint status;GPIOPinWrite(NRF_PORT, CSN_PIN,0x00); // CSN low, init SPI transactionstatus = SPI_RW(reg); // select registerSPI_RW(value); // ..and write value to it..GPIOPinWrite(NRF_PORT, CSN_PIN,0xff); // CSN high againreturn(status); // return nRF24L01 status uchar}/****************************************************************//*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,num:读出数据的个数/*********************************************************/uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num){uint status,uchar_ctr;GPIOPinWrite(NRF_PORT, CSN_PIN,0x00); // Set CSN low, init SPI tranactionstatus = SPI_RW(reg); // Select register to write to and read status ucharfor(uchar_ctr=0;uchar_ctrnum;uchar_ctr++)pBuf[uchar_ctr] = SPI_RW(0); // GPIOPinWrite(NRF_PORT, CSN_PIN,0xff); return(status); // return nRF24L01 status uchar}/******************************************************************/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,num:写入数据的个数/**************************************************************/uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num){uint status,uchar_ctr;GPIOPinWrite(NRF_PORT, CSN_PIN,0x00); //SPI使能 status = SPI_RW(reg); for(uchar_ctr=0; uchar_ctrnum; uchar_ctr++) //SPI_RW(*pBuf++);GPIOPinWrite(NRF_PORT, CSN_PIN,0xff); //关闭SPIreturn(status); // }/********************************************************//*函数:void SetRX_Mode(void)/*功能:数据接收配置 /*******************************************************************/void SetRX_Mode(void){GPIOPinWrite(NRF_PORT, CE_PIN,0x00);SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收GPIOPinWrite(NRF_PORT, CE_PIN,0xff);inerDelay_us(130); //延时不能太短}/*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)/*功能:数据读取后放如rx_buf接收缓冲区中/*****************************************************************/unsigned char nRF24L01_RxPacket(unsigned char* rx_buf){unsigned char revale=0;sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况if(RX_DR) // 判断是否接收到数据{GPIOPinWrite(NRF_PORT, CE_PIN,0x00); //SPI使能SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO bufferrevale =1; //读取数据完成标志}SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志return revale;}/******************************************************************************/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)/*功能:发送 tx_buf中数据/************************************************************************/void nRF24L01_TxPacket(unsigned char * tx_buf){GPIOPinWrite(NRF_PORT, CE_PIN,0x00); //StandBy I模式 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据 // SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送GPIOPinWrite(NRF_PORT, CE_PIN,0xff); //置高CE,激发数据发送inerDelay_us(10);}#endif主程序extern unsigned int sta;//状态标志extern unsigned int RX_DR;//sta第6位extern unsigned int TX_DS;//sta第5位extern unsigned int MAX_RT;//sta第4位extern uchar TxBuf[32]; nRF24L01_TxPacket(TxBuf);
评论