|
楼主 |
发表于 2009-7-11 20:17:16
|
显示全部楼层
NRF24L01.c
//
// nRF24L01.c
#include "nRF24L01.h"
unsigned char const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
unsigned char const RX_ADDRESS[RX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static RX address
//unsigned char const RX_ADDRESS[RX_ADR_WIDTH] = {0x1,0x1,0x1,0x1,0x1};
//union STATUS_24L01 sta_2401;
void nRF24L01_Config(void)
{
//initial io
nRF24L01_MISO_DDR =0;
nRF24L01_MOSI_DDR =1;
nRF24L01_SCK_DDR =1;
nRF24L01_CE_DDR =1;
nRF24L01_CSN_DDR =1;
nRF24L01_IRQ_DDR =0;
nRF24L01_CE =0;
nRF24L01_CSN =1;
nRF24L01_SCK =1;
nRF24L01_MISO =0;
nRF24L01_MOSI =0;
nRF24L01_IRQ =0;
_NOP_ms(10);
nRF24L01_CE =0;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);
SPI_RW_Reg(WRITE_REG + EN_AA, 0x00);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03);
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00);
SPI_RW_Reg(WRITE_REG + RF_CH, 0);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f);
// STATUS
// OBSERVE_TX
// CD
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);
SPI_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 + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
nRF24L01_CE=1; //
_NOP_us(130);
nRF24L01_CE =0;
}
/**************************************************
Function: SPI_RW();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char byte)
{
unsigned char i;
nRF24L01_SCK = 0;
for(i=0;i<8;i++) // output 8-bit
{
nRF24L01_MOSI = (byte & 0x80)>>7; // output 'byte', MSB to MOSI
byte = byte<<1; // shift next bit into MSB..
nRF24L01_SCK = 1; // Set SCK high..
byte |= (nRF24L01_MISO_PIN&0X01); // capture current MISO bit
nRF24L01_SCK = 0; // ..then set SCK low again
}
return(byte); // return read byte
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status=0;
nRF24L01_CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
nRF24L01_CSN = 1; // CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
nRF24L01_CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
nRF24L01_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
**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status=0;
unsigned char i=0;
nRF24L01_CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(i=0;i<bytes;i++)
{
pBuf = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
}
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
**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char const *pBuf, unsigned char bytes)
{
unsigned char status=0;
unsigned char i=0;
nRF24L01_CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(i=0; i<bytes; i++) // then write all byte in buffer(*pBuf)
{
SPI_RW(*pBuf++);
}
nRF24L01_CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
unsigned char tmp=0;
if(nRF24L01_IRQ_PIN==1)
return 0;
tmp=SPI_Read(STATUS); // read register STATUS's value
if(tmp&RX_DR)
{
nRF24L01_CE = 0; // stand by mode
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
revale =1;//we have receive data
}
SPI_RW_Reg(WRITE_REG+STATUS,tmp);
nRF24L01_CE =0;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
nRF24L01_CE = 1;
_NOP_us(130);
return revale;
}
/**************************************************
Function: nRF24L01_TxPacket();
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 nRF24L01_TxPacket(unsigned char * tx_buf)
{
nRF24L01_CE =0;
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);
nRF24L01_CE=1;
_NOP_us(160);
nRF24L01_CE=0;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
nRF24L01_CE = 1;
_NOP_us(130);
}
/**************************************************/ |
|