24L01發(fā)送程序1138IO模擬移植
STM32寄存器多多,所以決定用回AVR結(jié)果發(fā)現(xiàn)AVR的穩(wěn)定性非常值得懷疑。首先就是內(nèi)部參考電壓用不到,2片MEGA16結(jié)果有一片用不了內(nèi)部參考電壓(PS電路程序完全一樣,而且外部REF是懸空不會(huì)導(dǎo)致內(nèi)部短路),不過(guò)這個(gè)就原諒它了。編程CTC模式,結(jié)果發(fā)現(xiàn)1S中斷的程序竟然有時(shí)進(jìn)可以運(yùn)行有時(shí)不可運(yùn)行(這中情況在重插電源時(shí)交替發(fā)生,后來(lái)就干脆一直進(jìn)不了中斷),還有一芯片一個(gè)不小心燒錯(cuò)時(shí)鐘溶絲,連續(xù)都讀不出東西更別說(shuō)下載了,不過(guò)突然間有下得了,之后又下載不了。。。所以覺(jué)得還是用51好,要是當(dāng)初用匯編學(xué)51再用增強(qiáng)型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//**************發(fā)送緩沖區(qū)**********************************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 // 寫(xiě)寄存器指令#define RD_RX_PLOAD 0x61 // 讀取接收數(shù)據(jù)指令#define WR_TX_PLOAD 0xA0 // 寫(xiě)待發(fā)數(shù)據(jù)指令#define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令#define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令#define REUSE_TX_PL 0xE3 // 定義重復(fù)裝載數(shù)據(jù)指令#define NOP 0xFF // 保留//*******************SPI(nRF24L01)寄存器地址**************************#define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗(yàn)?zāi)J揭约笆瞻l(fā)狀態(tài)響應(yīng)方式#define EN_AA 0x01 // 自動(dòng)應(yīng)答功能設(shè)置#define EN_RXADDR 0x02 // 可用信道設(shè)置#define SETUP_AW 0x03 // 收發(fā)地址寬度設(shè)置#define SETUP_RETR 0x04 // 自動(dòng)重發(fā)功能設(shè)置#define RF_CH 0x05 // 工作頻率設(shè)置#define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設(shè)置#define STATUS 0x07 // 狀態(tài)寄存器#define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測(cè)功能#define CD 0x09 // 地址檢測(cè) #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址#define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址#define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址#define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址#define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址#define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址#define TX_ADDR 0x10 // 發(fā)送地址寄存器#define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長(zhǎng)度#define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設(shè)置//***************************************unsigned int sta;//狀態(tài)標(biāo)志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);//********************長(zhǎng)延時(shí)**********************void Delay(unsigned int s){unsigned int i;for(i=0; is; i++);for(i=0; is; i++);}//******************************************************//延時(shí)函數(shù)/****************************************************************/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);// 設(shè)置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); // 寫(xiě)本地地址 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 寫(xiě)接收端地址SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 頻道0自動(dòng) ACK應(yīng)答允許 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允許接收地址只有頻道0,如果需要多頻道可以參考Page21 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 設(shè)置信道工作為2.4GHZ,收發(fā)必須一致SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //設(shè)置接收數(shù)據(jù)長(zhǎng)度,本次設(shè)置為32字節(jié)SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設(shè)置發(fā)射速率為1MHZ,發(fā)射功率為最大值0dB SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主發(fā)送}
/*******************************************************************/*函數(shù):uint SPI_RW(uint val)/*功能:NRF24L01的SPI寫(xiě)時(shí)序/*************************************************************/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}/***********************************************************/*函數(shù):uchar SPI_Read(uchar reg)/*功能:NRF24L01的SPI時(shí)序/**************************************************************/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讀寫(xiě)寄存器函數(shù)/**************************************************************/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}/****************************************************************//*函數(shù):uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)/*功能: 用于讀數(shù)據(jù),reg:為寄存器地址,pBuf:為待讀出數(shù)據(jù)地址,num:讀出數(shù)據(jù)的個(gè)數(shù)/*********************************************************/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}/******************************************************************/*函數(shù):uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)/*功能: 用于寫(xiě)數(shù)據(jù):為寄存器地址,pBuf:為待寫(xiě)入數(shù)據(jù)地址,num:寫(xiě)入數(shù)據(jù)的個(gè)數(shù)/**************************************************************/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); //關(guān)閉SPIreturn(status); // }/********************************************************//*函數(shù):void SetRX_Mode(void)/*功能:數(shù)據(jù)接收配置 /*******************************************************************/void SetRX_Mode(void){GPIOPinWrite(NRF_PORT, CE_PIN,0x00);SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應(yīng),16位CRC ,主接收GPIOPinWrite(NRF_PORT, CE_PIN,0xff);inerDelay_us(130); //延時(shí)不能太短}/*函數(shù):unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)/*功能:數(shù)據(jù)讀取后放如rx_buf接收緩沖區(qū)中/*****************************************************************/unsigned char nRF24L01_RxPacket(unsigned char* rx_buf){unsigned char revale=0;sta=SPI_Read(STATUS); // 讀取狀態(tài)寄存其來(lái)判斷數(shù)據(jù)接收狀況if(RX_DR) // 判斷是否接收到數(shù)據(jù){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; //讀取數(shù)據(jù)完成標(biāo)志}SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到數(shù)據(jù)后RX_DR,TX_DS,MAX_PT都置高為1,通過(guò)寫(xiě)1來(lái)清楚中斷標(biāo)志return revale;}/******************************************************************************/*函數(shù):void nRF24L01_TxPacket(unsigned char * tx_buf)/*功能:發(fā)送 tx_buf中數(shù)據(jù)/************************************************************************/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); // 裝載數(shù)據(jù) // SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應(yīng),16位CRC,主發(fā)送GPIOPinWrite(NRF_PORT, CE_PIN,0xff); //置高CE,激發(fā)數(shù)據(jù)發(fā)送inerDelay_us(10);}#endif主程序extern unsigned int sta;//狀態(tài)標(biāo)志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);
評(píng)論