nRF24l01无线模块 发送端程序for 51单片机

#include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include //标准输入输出
#include
#include
sbit RS = P2^4; //Pin4
sbit RW = P2^5; //Pin5
sbit E = P2^6; //Pin6

#define Data P0//数据端口
//=============================================================================================
#define MODE 0//MODE=1时为发送代码 为0则是接受代码
#define uchar unsigned char

#define RX_ADR_WIDTH 5
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 1 // 20 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 ch,Num,read_flag;
uchar Tx_Buf[TX_PLOAD_WIDTH],Rx_Buf[RX_PLOAD_WIDTH];
/**************************************************/
sbit CE = P1^6;
sbit CSN= P1^0;
sbit SCK= P1^1;
sbit MOSI= P1^2;
sbit MISO= P1^3;
sbit IRQ = P3^2;
/**************************************************/
uchar bdata sta;
sbit RX_DR =sta^6;
sbit TX_DS =sta^5;
sbit MAX_RT =sta^4;
/**************************************************/
/*1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK*/

/*-----------------------------------------------
名称:LCD1602
公司:上海浩豚电子科技有限公司
网站:https://www.360docs.net/doc/9d7904226.html,
编写:师访
日期:2009.5
修改:无
内容:通过标准程序静态显示2行字符
------------------------------------------------*/


char data TimeNum[]=" ";
/******************************************************************/
/* 函数声明 */
/******************************************************************/
/******************************************************************/
/* 微秒延时函数 */
/******************************************************************/
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);
}
}
/******************************************************************/
/* 写入命令函数 */
/******************************************************************/
void WriteCommand(unsigned char c)
{
DelayMs(5);//操作前短暂延时,保证信号稳定
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/***********

*******************************************************/
/* 写入数据函数 */
/******************************************************************/
void WriteData(unsigned char c)
{
DelayMs(5); //操作前短暂延时,保证信号稳定
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/******************************************************************/
/* 写入字节函数 */
/******************************************************************/
void ShowChar(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0; //是第1行则命令代码高4位为0xc
else
p=pos+0x80; //是第2行则命令代码高4位为0x8
WriteCommand (p);//写命令
WriteData (c); //写数据
}
/******************************************************************/
/* 写入字符串函数 */
/******************************************************************/
void ShowString (unsigned char line,char *ptr)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<16;i++)
ShowChar (l++,*(ptr+i));//循环显示16个字符
}

void ShowNum(uchar num)
{
uchar o,p,q;
o=num/100;
p=num/10%10;
q=num%10;
ShowChar(1,o);
ShowChar(1,p);
ShowChar(1,q);

}
/******************************************************************/
/* 初始化函数 */
/******************************************************************/
void InitLcd()
{
DelayMs(15);
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x06); //显示光标移动位置
WriteCommand(0x0c); //显示开及光标设置
WriteCommand(0x01); //显示清屏
}

//================================以上为LCD显示程序============================================

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;
IE=0x90;
TI=1;
//start timer1
}
/******************************************************************/
/* 微秒延时函数 */
/******************************************************************/
void delay_us(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1; /*12MHz频率*/
while(--uscnt);
}
/******************************************************************/

/**************************************************/
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 register 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
}
/**************************************************/
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接收函数***************************/
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;
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);
return revale;
}
/*******************************************/
void nRF24L01_TX_Config(void)
{
DelayUs(100);
CE=0;
CSN=1;
SCK=0;

SPI_RW_Reg(WRITE_REG+CONFI

G,0x0e);
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,RX_ADDRESS,RX_ADR_WIDTH);
CE=1;
}
/********************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+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 send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0);
TI = 0 ;
}

/***********串口接收中断函数***************************/

void serial () interrupt 4

{
if (RI)
{
RI = 0 ;
ch=SBUF;
read_flag= 1 ; //就置位取数标志
P1=ch;
}
}
/******************************************/
void main(void)
{
InitLcd();
nRF24L01_TX_Config();
Inituart();
while(1)
{


if(read_flag==1)
{
read_flag=0;
Tx_Buf[0]=ch;
DelayUs(10);

//接收到COM的数据后 启动nRF模块
nRF24L01_TxPacket(Tx_Buf);
DelayMs(10);
send_char_com(ch);
sta=SPI_Read(STATUS);


SPI_RW_Reg(WRITE_REG+STATUS,sta);

/*
if(ch==0x01||ch==0x02||ch==0x03||ch==0x04)
{
nRF24L01_RX_Config();
while(1)
{
if(nRF24L01_RxPacket(Rx_Buf))
{
ch=Rx_Buf[0];
send_char_com(ch);
ShowNum(ch);
}
}
}
*/
}
}// end of while



}

相关文档
最新文档