nRF24l01无线模块 接收端程序for 51单片机

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*****************头文件和宏定义*********************************/
#include
#include
#include
#include "api.h"

#define uchar unsigned char
#define uint unsigned int

#define RX_ADR_WIDTH 5
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 1 // 1 bytes TX payload
#define RX_PLOAD_WIDTH 1

uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uchar const RX_ADDRESS[RX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static RX address

uchar Rx_Buf[RX_PLOAD_WIDTH];
uchar Tx_Buf[RX_PLOAD_WIDTH]={0xff};

uchar i=0,Num=0,zhuanflag=0,OK_flag=0,pos_flag=0,stop_flag=0,MODE=0;
/*****************I/O管脚定义*********************************/
sbit CSN = P1^0;
sbit SCK = P1^1;
sbit MOSI = P1^2;
sbit MISO = P1^3;
sbit IRQ = P1^5;
sbit CE = P1^6;
sbit enyou = P2^1;
sbit enzuo = P2^2;
sbit buzzer= P2^3;
sbit you1 = P2^4;
sbit you2 = P2^5;
sbit zuo1 = P2^6;
sbit zuo2 = P2^7;
sbit zuoqian=P3^0;
sbit youhou =P3^1;
sbit zuohou =P3^2;
sbit youqian=P3^3;
/**************************************************/
uchar bdata sta;
sbit RX_DR =sta^6;
sbit TX_DS =sta^5;
sbit MAX_RT =sta^4;
/**************************************************/
/******************************************************************/
/* 微秒延时函数 */
/******************************************************************/
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1; /*12MHz频率*/
while(--uscnt);
}
/******************************************************************/
/* 毫秒函数声明 */
/******************************************************************/
void DelayMs(unsigned char ms)
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
/**************************SPI时序模拟函数**************************/
uchar SPI_RW(uchar byte)
{
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (byte & 0x80); // output 'byte', 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 byte
}
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
BYTE reg_val;

CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read register value
CSN = 1; // CSN high, terminate SPI communication

return(reg_val); // return regist

er value
}
/**************************/
uchar SPI_RW_Reg(BYTE reg,BYTE 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
}
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE 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_ctrSPI_RW(*pBuf++);
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE 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_ctrpBuf[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
}
/***********************RX_MODE接收函数***************************/
uchar nRF24L01_RxPacket(uchar *rx_buf)
{
uchar revale=0;

CE=1;
DelayUs(130);

sta=SPI_Read(STATUS);
if(RX_DR)
{
CE=0;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
revale=1;
}
CE=0;
SPI_RW_Reg(WRITE_REG+STATUS,sta);
return revale;
}
/**************************************************/
void nRF24L01_TxPacket(uchar *tx_buf)
{
CE=0;
SPI_RW_Reg(FLUSH_TX,0x00);
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_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG+CONFIG,0x0e);
CE=1;
DelayUs(15);
CE=0;
}
/********************RX_MODE配置函数***********************/
void nRF24L01_RX_Config(void)
{
CE=0;
CSN=1;
SCK=0;
SPI_RW_Reg(WRITE_REG+CONFIG,0x0f);
SPI_RW_Reg(WRITE_REG+EN_AA,0x01);
SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x01);
SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x1a);
SPI_RW_Reg(WRITE_REG+RF_CH,40);
SPI_RW_Reg(WRITE_REG+RF_SETUP,0x07);
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,TX_ADDRESS,TX_ADR_WIDTH);
}
/*******************************************/
void nRF24L01_TX_Config(void)
{
DelayUs(100);
CE=0;
CSN=1;
SCK=0;

SPI_RW_Reg(WRITE_REG+CONFIG,0x0e);
SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x01);
SPI_RW_Reg(WRITE_REG+S

ETUP_RETR,0x1a);
SPI_RW_Reg(WRITE_REG+RF_CH,40);
SPI_RW_Reg(WRITE_REG+RF_SETUP,0x07);
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);
CE=1;
}
/****************************波特率设置*****************************/
void Inituart(void)
{
TMOD = 0x20; //timer1 working mode 2
TH1 = 0xfd;
TL1 = 0xfd; //...fd=9600 for 11.0592mhz Fosc
SCON = 0x50; //uart mode 1,ren==1
PCON = 0x00; //smod=0
TR1 = 1; //start timer1
}
/********************向串口发送一个字节******************************/
void R_S_Byte(uchar R_Byte)
{
SBUF=R_Byte; // write data R_Byte to SBUF
while(TI==0);
TI=0;
}
/******************************以下为小车巡线的程序***************************************/



void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}


void delay_us(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=11;y>0;y--);
}

void youzhuan(void)
{
enzuo=0;
enyou=0;
zuo1=1;
zuo2=0;
you1=0;
you2=0;
}

void zuozhuan(void)
{
enzuo=0;
enyou=0;
zuo1=0;
zuo2=0;
you1=1;
you2=0;
}
void zhixing(void)
{
enzuo=0;
enyou=0;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
}
void houtui(void)
{
enzuo=0;
enyou=0;
zuo1=0;
zuo2=1;
you1=0;
you2=1;
}
void stop(void)
{
zuo1=0;
zuo2=0;
you1=0;
you2=0;
enzuo=0;
enyou=0;
}
void qiandao(void)
{
enzuo=0;
enyou=0;
buzzer=1;
delay(500);
buzzer=0;
delay(500);
buzzer=1;
delay(500);
buzzer=0;
}

void zuowan(void)
{
while(OK_flag==0)
{
while(zuoqian==1)//检测转弯的角度是否已经合适,已经合适--跳出循环
{

zuozhuan();
enyou=1;

delay(8);
enyou=0;

delay(15);
OK_flag=1;

}
while(zuoqian==0)
{
zuozhuan();

enyou=1;

delay(8);
enyou=0;

delay(15);
}
}
}
void youwan(void)
{
while(OK_flag==0)
{
while(youqian==1)//检测转弯的角度是否已经合适,已经合适--跳出循环
{

youzhuan();
enzuo=1;

delay(9);
enzuo=0;

delay(15);
OK_flag=1;

}
while(youqian==0)
{
EX0=1;
youzhuan();

enzuo=1;

delay(8);
enzuo=0;

delay(15);
}
}
}

void xunxian(void)
{

if(zuoqian==1&&youqian==0)
{
youzhuan();
enzuo=1;
delay(4);
enzuo=0;
delay(20);
}
else if(zuoqian==0&&youqian==1)
{
zuozhuan();
enyou=1;
delay(4);
enyou=0;
delay(20);
}
else
{
if(zuoqian==0&&youqian==0)
{
if(MODE==0x00)
{
EX0=1;
}
}
zhixing();
enzuo=1;
delay_us(7);
enyou=1;
delay(7);
enzuo=0;
enyou=0;
delay(15

);
}

if(zuohou==0&&youhou==0)
{
stop_flag=1;
}

}
void AUTO_MODE(void)
{
Num=Rx_Buf[0];
while(1)
{
xunxian(); //转弯之前的巡线
if(zhuanflag==1&&youhou==0)//停车
{
qiandao();
stop();
DelayMs(50);
nRF24L01_TX_Config();
while(1)
{
nRF24L01_TxPacket(Tx_Buf);
sta=SPI_Read(STATUS);
if(TX_DS)//发送成功
{
qiandao();
SPI_RW_Reg(WRITE_REG+STATUS,sta);
DelayMs(50);
}
else if(MAX_RT)
{
SPI_RW_Reg(WRITE_REG+STATUS,sta);
DelayMs(50);
}
}

}
}
}
void CONTROL_MODE(void)
{

Num=Rx_Buf[0];
if(Num==0x21)
{

while(1)
{
xunxian();
if(stop_flag==1)
{
stop_flag=0;
break;
}
} //Num=Rx_Buf[0];
}
else if(Num==0x22)
{
}
else if(Num==0x23)
{
zuowan();
}
else if(Num==0x24)
{
youwan();
}

}


main(void)
{
qiandao();
buzzer=0;
enzuo=0;
enyou=0;

TCON=0x01;
EA=1;
EX0=0;

zhuanflag=0;
pos_flag=0;
OK_flag=0;

Inituart();
nRF24L01_RX_Config();
while(1)
{
i=nRF24L01_RxPacket(Rx_Buf);
DelayMs(100);
if(i==1)
{
i=0;
MODE=Rx_Buf[0];
if(MODE==0x00)
{
while(1)
{
i=nRF24L01_RxPacket(Rx_Buf);
if(i==1)
{
AUTO_MODE();
}
}
}
else if(MODE==0x10)
{
while(1)
{
i=nRF24L01_RxPacket(Rx_Buf);
if(i==1)
{
CONTROL_MODE();
i=0;
}

}
}
i=0;
}//接收到数据
}//接收nrf数据
}

void intersvr(void) interrupt 0 using 1
{

//1仓库
if(zhuanflag==0&&Num==1)
{
EX0=0;
zuowan();
zhuanflag=1;
}
//2仓库
if(zhuanflag==0&&Num==2)
{
EX0=0;
youwan();
zhuanflag=1;
}
//3仓库
if(zhuanflag==0&&Num==3)
{
if(pos_flag==1)
{
EX0=0;
zuowan();
zhuanflag=1;
}
else
{
EX0=0;
while(youhou==0||zuohou==0)
{
xunxian();//过十字的巡线
pos_flag=1;
}
}
}

//4仓库
if(zhuanflag==0&&Num==4)
{
if(pos_flag==1)
{
EX0=0;
youwan();
zhuanflag=1;
}
else
{
EX0=0;
while(youhou==0||zuohou==0)
{
xunxian();//过十字的巡线
}
pos_flag=1;
}
}
}

相关文档
最新文档