|
- #include "NRF24L01.h"
- //P9.0 -> CE
- //P9.4 -> CSN
- //P9.5 -> IRQ
- uint8_t TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //Send Addr
- uint8_t RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //Receive Addr
- void NRF_GPIO_Config()
- {
- P9SEL &= ~(BIT0 + BIT4);
- P9DIR |= BIT0 + BIT4;
- P9DIR &= ~BIT5;
- P9DS |= BIT0 + BIT4;
- }
- uint8_t NRF_WriteReg(uint8_t reg,uint8_t val)
- {
- uint8_t temp = 0;
- NRF_CSN_0;
- temp = SPI2_SendReceive_SingleByte(reg);
- SPI2_SendReceive_SingleByte(val);
- NRF_CSN_1;
- return temp;
- }
- uint8_t NRF_ReadReg(uint8_t reg)
- {
- uint8_t val = 0;
- NRF_CSN_0;
- SPI2_SendReceive_SingleByte(reg);
- val=SPI2_SendReceive_SingleByte(0xFF);
- NRF_CSN_1;
- return val;
- }
- uint8_t NRF_ReadBuf(uint8_t reg,uint8_t *buff,uint8_t num)
- {
- uint8_t i = 0,val = 0;
- NRF_CSN_0;
- val = SPI2_SendReceive_SingleByte(reg);
- for(i = 0;i < num;i++)
- buff[i] = SPI2_SendReceive_SingleByte(0xFF);
- NRF_CSN_1;
- return val;
- }
- uint8_t NRF_WriteBuf(uint8_t reg,uint8_t *buff,uint8_t num)
- {
- uint8_t val = 0,i = 0;
- NRF_CSN_0;
- val = SPI2_SendReceive_SingleByte(reg);
- for(i = 0;i < num;i++)
- SPI2_SendReceive_SingleByte(buff[i]);
- NRF_CSN_1;
- return val;
- }
- void Set_Rx_Mode()
- {
- NRF_CE_0;
- NRF_WriteReg(WRITE_REG+CONFIG, 0x0F);
- NRF_CE_1;
- delay_ms(1);
- }
- void Set_Tx_Mode()
- {
- NRF_CE_0;
- NRF_WriteReg(WRITE_REG+CONFIG, 0x0E);
- NRF_CE_1;
- delay_ms(1);
- }
- uint8_t NRF_SendPacket(uint8_t *TxBuf)
- {
- uint8_t val = 0;
- NRF_CE_0;
- NRF_WriteBuf(WR_TX_PLOAD,TxBuf,TX_PLOAD_WIDTH);
- Set_Tx_Mode();
- NRF_CE_1;
- delay_ms(1);
- // while(NRF_IRQ != 0);
- val=NRF_ReadReg(STATUS);
- NRF_WriteReg(WRITE_REG+STATUS,0xFF);
- if(val&TX_OK)
- return TX_OK;
- if(val&MAX_TX)
- {
- NRF_WriteReg(WRITE_REG+STATUS,0xff);
- NRF_WriteReg(FLUSH_TX,0xFF);
- return MAX_TX;
- }
- return 0xFF;
- }
- uint8_t NRF_ReceivePacket(uint8_t *RxBuf)
- {
- uint8_t val = 0;
- NRF_CE_0;
- Set_Rx_Mode();
- NRF_CE_1;
- delay_us(500);
- while(NRF_IRQ != 0);
- val=NRF_ReadReg(STATUS);
- if(val&RX_OK)
- {
- NRF_ReadBuf(RD_RX_PLOAD,RxBuf,RX_PLOAD_WIDTH);
- NRF_WriteReg(FLUSH_RX,0xff);
- return 0;
- }
- return 0xff;
- }
- void NRF_Config()
- {
- delay_us(1000);
- NRF_GPIO_Config();
- SPI2_Init();
- delay_us(250);
- NRF_CE_0;
- NRF_CSN_1;
- P9OUT &= ~BIT3;
- NRF_WriteBuf(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
- NRF_WriteBuf(WRITE_REG + RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);
- NRF_WriteReg(WRITE_REG + EN_AA,0x01);
- NRF_WriteReg(WRITE_REG + EN_RXADDR,0x01);
- NRF_WriteReg(WRITE_REG + RF_CH,0);
- NRF_WriteReg(WRITE_REG + RX_PW_P0,RX_PLOAD_WIDTH);
- NRF_WriteReg(WRITE_REG + RF_SETUP,0x07);
- }
复制代码 |
赞赏
-
1
查看全部赞赏
-
|