#include <STC15F2K60S2.h> //单片机头文件
/***SPI引脚定义*****/
sbit CSN = P2^4; //从设备片选信号,由主设备控制。
sbit SCK = P0^0; //SCLK时钟信号,由主设备产生。
sbit MOSI = P2^5; //主设备数据输出,从设备数据输入。
sbit MISO = P2^7; //主设备数据输入,从设备数据输出。
sbit CE = P0^1; //NRF24L01模式控制线。在 CSN为低的
情况下,CE 协同CONFIG 寄存器
共同决定NRF24L01 的状态
sbit IRQ = P6^1; //NRF24L01中断线;
uchar bdata sta; //状态标志
sbit RX_DR =sta^6;
sbit TX_DS =sta^5;
sbit MAX_RT =sta^4;
//*****************************************长延时*****************************************
void delays(unsigned char s)
{
unsigned char i;
for(i=0; i<s; i++)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
// _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
// _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
// _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
/****************************************************************************************************
/*功能:NRF24L01的 SPI同步读写时序
/****************************************************************************************************/
uchar SPI_RW(uchar reg)
{
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (reg & 0x80); // output 'uchar', MSB to MOSI
reg = (reg << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
reg |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(reg); // return read uchar
}
/****************************************************************************************************
/*功能:NRF24L01的SPI读寄存器时序
/****************************************************************************************************/
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
}
/****************************************************************************************************/
/*功能:NRF24L01读写寄存器函数
/****************************************************************************************************/
uchar SPI_RW_Reg(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 uchar
}
/****************************************************************************************************/
/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
/****************************************************************************************************/
void SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uchar i;
CSN = 0; // Set CSN low, init SPI tranaction
SPI_RW(reg); // Select register to write to and read status uchar
for(i=0;i<uchars;i++)
pBuf[i] = SPI_RW(0); //
CSN = 1;
}
/*********************************************************************************************************
/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
/*********************************************************************************************************/
void SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
{
uchar status,i;
CSN = 0; //SPI使能
status = SPI_RW(reg);
for(i=0; i<uchars;i++) //
SPI_RW(*pBuf++);
CSN = 1; //关闭SPI
}