📄 nrf24l01.c
字号:
#include<pic.h>
#include "def.h"
__CONFIG (UNPROTECT & CPROTECT & BORDIS & MCLRDIS & PWRTDIS & WDTDIS & INTOSCCLKO);
#define uchar unsigned char
#define uint unsigned int
#define LED_RETRANSMIT RA0
/*************************************************/
static volatile uchar sta=0x0;
static volatile uchar stat @ 0x20; //tmpData定位在地址0x20
static volatile bit RX_DR @ (unsigned)&stat*8+6;
static volatile bit TX_DS @ (unsigned)&stat*8+5;
static volatile bit MAX_RT @ (unsigned)&stat*8+4;
/**************************************************/
/*
unsigned char FLAG @ 0XEF;
#define FLAGIT(adr,bit) ((unsigned)(&adr)*8+(bit)) //绝对寻址位操作指令
static bit FLAG1 @ FLAGIT(FLAG,0);
static bit FLAG2 @ FLAGIT(FLAG,1);
static bit FLAG3 @ FLAGIT(FLAG,2);
*/
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 2 // 20 bytes TX payload
uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x63}; // Define a static TX address
uchar rx_buf[TX_PLOAD_WIDTH];
uchar tx_buf[TX_PLOAD_WIDTH];
/**************************************************
Function: Port_init();
Description:
flash led one time,chip enable(ready to TX or RX Mode),
Spi disable,Spi clock line init high
/**************************************************/
void Port_init(void){
ANSEL=0x00; //0 = Digital I/O. Pin is assigned to port or special function.
CMCON=0x07; //CM2:CM0 = 111 Comparator Off (Lowest power)
TRISA=0x00; //RA2(P32) INPUT
PORTA=0xff;
TRISC=0x03; //MISO & IRQ as input
CE=0; // chip enable
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
LED_RETRANSMIT=1; //Turn led off
}
/**************************************************/
/**************************************************
Function: Dlms(uint z);
Description:
delay zms
/**************************************************/
void Dlms(uint z){
uint i,a;
for(a=z;a>0;a--)
for(i=110;i>0;i--);
}
/**************************************************/
/**************************************************
Function: SPI_RW();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 during write, according to SPI protocol
/**************************************************/
uchar SPI_RW(uchar byte){
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++){ // output 8-bit
byte = (byte << 1); // shift next bit into MSB..
MOSI = CARRY; // output 'byte', MSB to MOSI ??? byte to bit.
SCK = 1; // Set SCK high..
byte |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(byte); // return read byte
}
/**************************************************/
/**************************************************
Function: SPI_Write();
Description:
Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_Write(uchar reg, uchar value){
uchar status;
CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN = 1; // CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
/**************************************************/
uchar SPI_Read(uchar reg){
uchar reg_val;
CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN = 1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
Description:
Reads 'bytes' #of bytes from register 'reg'
Typically used to read RX payload, Rx/Tx address
/**************************************************/
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes){
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
Description:
Writes contents of buffer '*pBuf' to nRF24L01
Typically used to write TX payload, Rx/Tx address
/**************************************************/
uchar SPI_Write_Buf(uchar reg, const uchar *pBuf, uchar bytes){
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: RX_Mode();
Description:
This function initializes one nRF24L01 device to
RX Mode, set RX address, writes RX payload width,
select RF channel, datarate & LNA HCURR.
After init, CE is toggled high, which means that
this device is now ready to receive a datapacket.
/**************************************************/
void RX_Mode(void){
CE=0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_Write(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_Write(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_Write(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_Write(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_Write(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_Write(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
CE = 1; // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}
/**************************************************/
/**************************************************
Function: TX_Mode();
Description:
This function initializes one nRF24L01 device to
TX mode, set TX address, set RX address for auto.ack,
fill TX payload, select RF channel, datarate & TX pwr.
PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
ToDo: One high pulse(>10us) on CE will now send this
packet and expext an acknowledgment from the RX device.
/**************************************************/
void TX_Mode(void)
{
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
// SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
SPI_Write(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_Write(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_Write(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_Write(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_Write(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_Write(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
// CE=1;
}
/**************************************************/
/**************************************************
Function: Transmit();
Description:
Transmit the data in tx_buf[].
/**************************************************/
void Transmit(void){
CE=1;
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
CE=0;
}
/**************************************************
Function: Nrf_power_down();
Description:
For the since of energy conservation,turn nRF24L01 in Power Down mode.
/**************************************************/
void Nrf_power_down(void){
SPI_Write(WRITE_REG + CONFIG, SPI_Read(READ_REG + CONFIG)&0xFD);
}
void Nrf_power_up(void){
SPI_Write(WRITE_REG + CONFIG, SPI_Read(READ_REG + CONFIG)|0x02);
//delay for 1.5 ms for Start up
}
/**************************************************
Function: check_ACK();
Description:
check if have "Data sent TX FIFO interrupt",if TX_DS=1,
all led light and after delay 100ms all led close
/**************************************************/
void Check_ACK(void)
{
sta=SPI_Read(READ_REG+STATUS); // read register STATUS's
while(!TX_DS){
if(MAX_RT){
LED_RETRANSMIT=0; //Indicate that no ACK have received and retransmit now
SPI_Write(WRITE_REG+STATUS,0x10); // clear interrupt flag(MAX_RT)
CE=1;
}
sta=SPI_Read(READ_REG+STATUS); // read register STATUS's
}
LED_RETRANSMIT=1;
CE=0;
SPI_Write(WRITE_REG+STATUS,sta); // clear all interrupt flag
}
/**************************************************/
/**************************************************
Function: main();
Description:
control all subprogrammes;
/**************************************************/
void main(void){
OSCCAL = _READ_OSCCAL_DATA();
Port_init();
Dlms(15); //delay for 15ms let nrf24L01 POR reliably
TX_Mode(); // set TX Mode and transmitting
while(1) {
Nrf_power_up();
tx_buf[0]='h'; // Save to tx_buf[0]
Transmit();
Check_ACK();
Nrf_power_down();
// SPI_Write(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
Dlms(10);
}
}
/**************************************************/
/**************************************************
Function: ISR_int0() interrupt 0;
Description:
if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
if RX_DR=1,read the payload from RX_FIFO and set flag;
/**************************************************
void ISR_int0(void) interrupt 0
{
sta=SPI_Read(STATUS); // read register STATUS's value
if(RX_DR) // if receive data ready (RX_DR) interrupt
{
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
flag=1;
}
if(MAX_RT)
{
SPI_Write(FLUSH_TX,0);
}
SPI_Write(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
}
/**************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -