#include <STC12C5A.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned char uint;
/
void inerDelay_us(unsigned char n)
{
for(;n>0;n--)
_nop_();
}
/
void init_NRF24L01(void)
{
inerDelay_us(100);
CE=0;
// chip enable
CSN=1;
// Spi disable
SCK=0;
// Spi clock line init high
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 + EN_AA, 0x01);
//
频道0自动
ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
//
允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0);
//
设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为20字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
//设置发射速率为1MHZ,发射功率为最大值0dB
}
uint SPI_RW(uint uchar)
{
uint bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI=(uchar&0x80);
// output 'uchar', MSB to MOSI
uchar=(uchar << 1);
// shift next bit into MSB..
SCK=1;
// Set SCK high..
uchar |=MISO ;
// capture current MISO bit
SCK=0;
// ..then set SCK low again
}
return(uchar);
// return read uchar
}
uchar SPI_Read(uchar reg)
//读取被放置在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
//比至于SPI_RW我的感觉是其实现了读入数据与输出数据的分离
}
uint SPI_RW_Reg(uchar reg, uchar value)
//读入reg的值输出val的值
{
uint 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 uchar
}
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//函数用于把reg中值读入,将MISO的值读进缓存数组中
{
uint status,uchar_ctr;
CSN = 0;
// Set CSN low,init SPI tranaction
status = SPI_RW(reg);
// Select register to write to and read status uchar
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
pBuf[uchar_ctr] = SPI_RW(0);
CSN = 1;
return(status);
// return nRF24L01 status uchar
}
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//读入reg内的值,再将缓存数组中的值读出
{
uint status,uchar_ctr;
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned char uint;
/
void inerDelay_us(unsigned char n)
{
}
/
void init_NRF24L01(void)
{
}
uint SPI_RW(uint uchar)
{
}
uchar SPI_Read(uchar reg)
{
}
uint SPI_RW_Reg(uchar reg, uchar value)
{
}
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
}
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
}
void SetRX_Mode(void)
{
}
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
}
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
}
/
}