STM8 nRF24L01程序
//函数:uint SPI_RW(uint uchar)
//功能:NRF24L01的SPI写时序
uchar SPI_RW(uchar byte)
{
uchar i;
for(i=0;i<8;i++) // output 8-bit
{
if((byte & 0x80)==0)
{MOSI=0;}
else
{MOSI=1;}
byte = (byte < 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
if(MISO == 0)
{byte |= 0;} // capture current MISO bit
else
{byte |= 1;}
SCK = 0; // ..then set SCK low again
}
return(byte); // return read uchar
}
/*uchar SPI_RW(uchar byte)
{
uchar i;
for(i=0;i<8;i++) // output 8-bit
{
MOSI = (byte & 0x80); // output uchar, MSB to MOSI
byte = (byte < 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
byte |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(byte); // return read uchar
}*/
//函数:uchar SPI_Read(uchar reg)
//功能: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)
{
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:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)
{
uchar status,i;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status uchar
for(i=0;i
CSN = 1;
return(status); // return nRF24L01 status uchar
}
//函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)
{
uchar status,i;
CSN = 0; //SPI使能
status = SPI_RW(reg);
for(i=0; i
CSN = 1; //关闭SPI
return(status); //
}
//函数:void SetRX_Mode(void)
//功能:数据接收配置
void SetRX_Mode(void)
{
CE=0;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC,主接收
CE = 1;
delayms(1);
}
/******************************************************************************************************/
//函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//功能:数据读取后放如rx_buf接收缓冲区中
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
sta=SPI_Read(STATUS);// 读取状态寄存器来判断数据接收状况
if(RX_DR)// 判断是否接收到数据
{
CE = 0; //SPI使能
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
revale =1;//读取数据完成标志
}
SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清除中断标志
return revale;
}
//函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
//功能:发送 tx_buf中数据
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
CE=0;//StandBy I模式
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
CE=1; //置高CE,激发数据发送
delayms(1);
}
//***********端口配置******************/
void IO_config(void)
{
PC_DDR_DDR5=0;
PC_DDR_DDR6=1;
PC_DDR_DDR7=1;
PD_DDR_DDR2=1;
PD_DDR_DDR3=1;
PD_DDR_DDR4=0;
PC_CR1_C15=1;//输入上拉
PC_CR1_C16=1;
PC_CR1_C17=1;
PD_CR1_C12=1;
PD_CR1_C13=1;
PD_CR1_C14=0;//输入上拉
PC_CR2 = 0x00;
PD_CR2 = 0x00;
}
/***********************************/
/*****************主程序********************/
void main(void)
{
unsigned char TxBuf[20]={0}; //
unsigned char RxBuf[20]={0};
unsigned char
STM8nRF24L0 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)