单片机课程设计 - 图文 下载本文

南华大学电气工程学院课程设计(论文)

//nrf24l01.c

#include \#include \#include \#include \

//*********************************************************************************

uint8_t TX_ADDRESS[TX_ADR_WIDTH]= {0x13,0x31,0x63,0x66,0x85}; //±?μ?μ??· uint8_t RX_ADDRESS[RX_ADR_WIDTH]= {0x13,0x31,0x63,0x66,0x85}; //?óê?μ??·

uint8_t NRF24L01_RXDATA[32]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /*

***************************************************************** ***************************************************************** */

uint8_t NRF_Write_Reg(uint8_t reg, uint8_t value) { uint8_t status; SPI_CSN_L(); status = SPI_RW(reg); SPI_RW(value); /* D′êy?Y */ SPI_CSN_H(); /* ???1???÷?t */ return status; } /*

***************************************************************** ***************************************************************** */

uint8_t NRF_Read_Reg(uint8_t reg) { uint8_t reg_val; SPI_CSN_L(); SPI_RW(reg); reg_val = SPI_RW(0); /* ?áè?????′??÷·μ??êy?Y */ SPI_CSN_H(); /* ???1???÷?t */ return reg_val; } /*

***************************************************************** *****************************************************************

第34页,共69页

南华大学电气工程学院课程设计(论文)

*/

uint8_t NRF_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t uchars) { uint8_t i; uint8_t status; SPI_CSN_L(); /* ??í¨?÷?t */ status = SPI_RW(reg); /* D′??′??÷μ??· */ for(i=0; i

*****************************************************************

***************************************************************** */

uint8_t NRF_Read_Buf(uint8_t reg, uint8_t *pBuf, uint8_t uchars) { uint8_t i; uint8_t status; SPI_CSN_L(); /* ??í¨?÷?t */ status = SPI_RW(reg); /* D′??′??÷μ??· */ for(i=0; i

/****************************************************************** * D′êy?Y°ü

******************************************************************/ void NRF_TxPacket(uint8_t * tx_buf, uint8_t len) { SPI_CE_L(); //StandBy I?£ê? NRF_Write_Buf(WR_TX_PLOAD, tx_buf, len); // ×°??êy?Y SPI_CE_H(); //????CE£??¤·¢êy?Y·¢?í } /*

*****************************************************************

第35页,共69页

南华大学电气工程学院课程设计(论文)

***************************************************************** */

u8 NRF_CHECK(void) { u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5}; u8 i; NRF_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5); //?ònRFμ???′??÷D′è?????êy×? buf[1] = 0X11; NRF_Read_Buf(TX_ADDR,buf,5); //?ònRFμ???′??÷D′è?????êy×? for(i=0;i<5;i++) if(buf[i]!=0XA5) break; if(i!=5)return 0;//?ònRFμ???′??÷D′è?????êy×? return 1; }

void NRF24L01_INIT(void) {

SPI_CE_L(); //StandBy I?£ê? SPI_CE_L(); NRF_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);//D′RX?úμ?μ??· NRF_Write_Buf(NRF_WRITE_REG+TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH); //D′TX?úμ?μ??· NRF_Write_Reg(NRF_WRITE_REG+EN_AA,0x01); NRF_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01) NRF_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a); NRF_Write_Reg(NRF_WRITE_REG+RF_CH,40); //éè??RFí¨μà?aCHANAL NRF_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//?? NRF_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x26); NRF_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0f); // IRQê?·¢íê3é?D???ìó|£?16??CRC£??÷·¢?í SPI_CE_H(); } /*

***************************************************************** * ?óê??£ê?

***************************************************************** */

void SetRX_Mode(void)

第36页,共69页

南华大学电气工程学院课程设计(论文)

{ SPI_CE_L(); NRF_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0f); // IRQê?·¢íê3é?D???ìó|£?16??CRC £??÷?óê? SPI_CE_H(); } /*

***************************************************************** * ·¢?í?£ê?

***************************************************************** */

void SetTX_Mode(void) { SPI_CE_L(); NRF_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0e); // IRQê?·¢íê3é?D???ìó|£?16??CRC£??÷·¢?í SPI_CE_H(); }

/*======================================================================*/

void NRF_IRQ(void) { uint8_t sta = NRF_Read_Reg(NRF_READ_REG + NRFRegSTATUS); if(sta & (1<

NRF_Read_Buf(0x61,NRF24L01_RXDATA,32);// read receive payload from RX_FIFO buffer NRF_Write_Reg(0x27, 0xff);//??3ynrfμ??D??±ê???? } if(sta & (1<

void NRF_GPIO_Interrupt_Init(void) //nRFê1ó?μ?ía2??D??μ?òy??3?ê??ˉ { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

GPIOPinTypeGPIOInput(GPIO_PORTE_BASE,GPIO_PIN_2); IntEnable(INT_GPIOE);

第37页,共69页