nRF905程序,为啥通讯不成功
时间:10-02
整理:3721RD
点击:
以下是我的nRF905的程序,为啥通讯不成功啊,这是接收程序,发送程序只不过是地址和这个不同,为啥我发了一下自己却能接收到呢,找不出原因啊
#ifndef SPI_H
#define SPI_H
#include "STC89C5AS02.h"
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
void spi_write(unsigned char send);
#endif
----------------------------------------------------------------------------------
#include"SPI.h"
//#include <ABSACC.h>
//#include <intrins.h>
//#include <stdio.h>
//----------------------------------------------------------------------------------------------------------------
#define uint unsigned int
#define uchar unsigned char
//----------------------------------------------------------------------------------------------------------------
#define BYTE_BIT0 0x01
#define BYTE_BIT7 0x80
//----------------------------------------------------------------------------------------------------------------
bdata unsigned char DATA_BUF;
#define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
//----------------------------------------------------------------------------------------------------------------
#define Fosc 22118400
#define BAUD 9600 //波特率
#define RELOAD_22118400 (256 - (Fosc/16*10/BAUD+5)/10 ) //1T模式, 波特率加倍 70
#define BRTx12_enable() AUXR |= 0x04 //BRT 独立波特率发生器的溢出率快 12 倍
#define BRT_start() AUXR |= 0x10 //启动独立波特率发生器 BRT 计数
//----------------------------------------------------------------------------------------------------------------
/*********************串口初始化***********************/
void UartInit(void) //9600bps@22.1184MHz
{
ES = 0; //禁止串口中断
SCON = 0x50; //可变波特率. 8位无奇偶校验
AUXR |= 0x01; //使用独立波特率发生器
PCON |= 0x80; //波特率加倍
BRTx12_enable(); //BRT 独立波特率发生器的溢出率快 12 倍
BRT = RELOAD_22118400; //设置独立波特率发生器 BRT 的自动重装数
BRT_start(); //启动独立波特率发生器 BRT 计数。
EA = 1; //打开单片机总中断
ES = 1;
}
//************************************************//
// 发送一个字节数据 //
//************************************************//
void Send_byte( uchar byte )
{
SBUF = byte;
while(!TI);
TI = 0;
}
//---------------------------------------------------发送数据缓冲区-------------------------------------------------
#define TxRxBuf_Len 4
//unsigned char TxRxBuf[TxRxBuf_Len]={0x29,0x30,0x31,0x32,};
uchar TxBuf,RxBuf;
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
char tf;
//----------------------------------------NRF905工作模式控制端口------------------------------------------------------
sbit TXEN=P1^4;
sbit TRX_CE=P3^2;
sbit PWR=P1^3;
//----------------------------------------LED显示端口---------------------------------------------------
//sbit LED=P3^5;
//----------------------------------------NRF905 数据交换端口---------------------------------------------------
sbit MISO=P1^6;
sbit MOSI=P1^1;
sbit SCK=P1^5;
sbit CSN=P1^0;
//----------------------------------------nrf905状态端口---------------------------------------------------------
sbit AM=P1^7;
sbit DR=P3^3;
sbit CD=P1^2;
//---------------------------------------------------------------------------------------------------------------
//----------------------------------------按键端口-------------------------------------------------------
//sbit KEY=P3^4;
//----------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00 //写配置寄存器
#define RC 0x10 //读配置寄存器
#define WTP 0x20 //向TX-Payload寄存器写入发送有效数据
#define RTP 0x21 //从TX-Payload寄存器读取发送有效数据
#define WTA 0x22 //向TX-Address寄存器写入发送地址
#define RTA 0x23 //从TX-Address寄存器读取发送地址
#define RRP 0x24 //从RX-Payload寄存器读取接收到的有效数据
//------------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x01,0x01, //接收发送有效数据长度为1字节
0x5A,0x5A,0x5A,0x5A, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//================================================延时===========================================================
void nrf905_Delay(int n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
//------------------------------------------------------初始化nRF905---------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
PWR=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TXEN=0; // set radio in Rx mode
}
//-----------------------------------------------------初始化寄存器-----------------------------------------------
void Config905(void)
{
uchar i;
CSN=0; // Spi enable for write a spi command
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
spi_write(RFConf);
}
CSN=1; // Disable Spi
}
//=================================================SPI读函数=======================================================
unsigned char spi_read(void)
{
unsigned char j;
for (j=0;j<8;j++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
}
return DATA_BUF;
}
//===========================================SPI写函数===============================================================
void spi_write(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
Send_byte(send);
for (i=0;i<8;i++)
{
if (DATA7) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
SCK=0;
}
}
//-----------------------------------------------------发送数据打包---------------------------------------------------
void TxPacket(uchar TxRxBuf)
{
uchar i;
//Config905();
CSN=0;
spi_write(WTP); // Write payload command
/*for (i=0;i<4;i++)
{
spi_write(TxRxBuf); // Write 32 bytes Tx data
}*/
spi_write(TxRxBuf);
CSN=1;
nrf905_Delay(1); // Spi disable
CSN=0; // Spi enable for write a spi command
spi_write(WTA); // Write address command
for (i=0;i<4;i++) // 写入与对方地址一样的地址
{
spi_write(TxAddress);
}
CSN=1; // Spi disable
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
nrf905_Delay(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//----------------------------------------------设置发送初始状态---------------------------------------------
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//-----------------------------------------------设置接收初始化---------------------------------------------------
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//----------------------------------------------------读NRF905接收数据------------------------------------------------------------
void RxPacket(void)
{
//uchar i;
nrf905_Delay(1);
// TRX_CE=0; // Set nRF905 in standby mode
nrf905_Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
nrf905_Delay(1);
spi_write(RRP);
/*for (i = 0 ;i < 4 ;i++)
{
TxRxBuf=spi_read(); // Read data and save to buffer
Send_byte(TxRxBuf);
}*/
RxBuf=spi_read();
CSN=1;
nrf905_Delay(10);
TRX_CE=1;
}
//----------------------------------------------------判断数据接收状态--------------------------------------------
unsigned char CheckDR(void)
{
//DR=1;
if(DR==1&&TRX_CE==1 && TXEN==0)
{
//DR=0;
return 1;
}
else
{
return 0;
}
}
//--------------------------------------------------------数据接收------------------------------------------------
void RX(void)
{
//uchar i;
SetRxMode();
while (CheckDR()==0);
nrf905_Delay(10);
RxPacket();
Send_byte(RxBuf);
}
//---------------------------------------------------检查是否已存在同频率载波--------------------------------------
unsigned char CheckCD(void)
{
if(CD==1)
{
return 1;
}
else
{
return 0;
}
}
//---------------------------------------------------------main函数------------------------------------------------
void main (void)
{
//void timer0_init();
UartInit();
nRF905Init(); //初始化nRF905
Config905(); //初始化nRF905寄存器
//uchar Rev;
while(1)
{
RX();
//SetTxMode();
//TxRxBuf[0]=Receive();
//nrf905_Delay(1000);
//TxPacket(TxRxBuf);
}
}
//---------------------------------------------------------串口中断--------------------------------------------------
void serial()interrupt 4
{
//uchar buf;
if(RI)
{
RI=0;
//buf=SBUF;
TxBuf=SBUF;
SetTxMode();
TxPacket(TxBuf);
}
}
#ifndef SPI_H
#define SPI_H
#include "STC89C5AS02.h"
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
void spi_write(unsigned char send);
#endif
----------------------------------------------------------------------------------
#include"SPI.h"
//#include <ABSACC.h>
//#include <intrins.h>
//#include <stdio.h>
//----------------------------------------------------------------------------------------------------------------
#define uint unsigned int
#define uchar unsigned char
//----------------------------------------------------------------------------------------------------------------
#define BYTE_BIT0 0x01
#define BYTE_BIT7 0x80
//----------------------------------------------------------------------------------------------------------------
bdata unsigned char DATA_BUF;
#define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
//----------------------------------------------------------------------------------------------------------------
#define Fosc 22118400
#define BAUD 9600 //波特率
#define RELOAD_22118400 (256 - (Fosc/16*10/BAUD+5)/10 ) //1T模式, 波特率加倍 70
#define BRTx12_enable() AUXR |= 0x04 //BRT 独立波特率发生器的溢出率快 12 倍
#define BRT_start() AUXR |= 0x10 //启动独立波特率发生器 BRT 计数
//----------------------------------------------------------------------------------------------------------------
/*********************串口初始化***********************/
void UartInit(void) //9600bps@22.1184MHz
{
ES = 0; //禁止串口中断
SCON = 0x50; //可变波特率. 8位无奇偶校验
AUXR |= 0x01; //使用独立波特率发生器
PCON |= 0x80; //波特率加倍
BRTx12_enable(); //BRT 独立波特率发生器的溢出率快 12 倍
BRT = RELOAD_22118400; //设置独立波特率发生器 BRT 的自动重装数
BRT_start(); //启动独立波特率发生器 BRT 计数。
EA = 1; //打开单片机总中断
ES = 1;
}
//************************************************//
// 发送一个字节数据 //
//************************************************//
void Send_byte( uchar byte )
{
SBUF = byte;
while(!TI);
TI = 0;
}
//---------------------------------------------------发送数据缓冲区-------------------------------------------------
#define TxRxBuf_Len 4
//unsigned char TxRxBuf[TxRxBuf_Len]={0x29,0x30,0x31,0x32,};
uchar TxBuf,RxBuf;
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
char tf;
//----------------------------------------NRF905工作模式控制端口------------------------------------------------------
sbit TXEN=P1^4;
sbit TRX_CE=P3^2;
sbit PWR=P1^3;
//----------------------------------------LED显示端口---------------------------------------------------
//sbit LED=P3^5;
//----------------------------------------NRF905 数据交换端口---------------------------------------------------
sbit MISO=P1^6;
sbit MOSI=P1^1;
sbit SCK=P1^5;
sbit CSN=P1^0;
//----------------------------------------nrf905状态端口---------------------------------------------------------
sbit AM=P1^7;
sbit DR=P3^3;
sbit CD=P1^2;
//---------------------------------------------------------------------------------------------------------------
//----------------------------------------按键端口-------------------------------------------------------
//sbit KEY=P3^4;
//----------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00 //写配置寄存器
#define RC 0x10 //读配置寄存器
#define WTP 0x20 //向TX-Payload寄存器写入发送有效数据
#define RTP 0x21 //从TX-Payload寄存器读取发送有效数据
#define WTA 0x22 //向TX-Address寄存器写入发送地址
#define RTA 0x23 //从TX-Address寄存器读取发送地址
#define RRP 0x24 //从RX-Payload寄存器读取接收到的有效数据
//------------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x01,0x01, //接收发送有效数据长度为1字节
0x5A,0x5A,0x5A,0x5A, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//================================================延时===========================================================
void nrf905_Delay(int n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
//------------------------------------------------------初始化nRF905---------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
PWR=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TXEN=0; // set radio in Rx mode
}
//-----------------------------------------------------初始化寄存器-----------------------------------------------
void Config905(void)
{
uchar i;
CSN=0; // Spi enable for write a spi command
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
spi_write(RFConf);
}
CSN=1; // Disable Spi
}
//=================================================SPI读函数=======================================================
unsigned char spi_read(void)
{
unsigned char j;
for (j=0;j<8;j++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
}
return DATA_BUF;
}
//===========================================SPI写函数===============================================================
void spi_write(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
Send_byte(send);
for (i=0;i<8;i++)
{
if (DATA7) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
SCK=0;
}
}
//-----------------------------------------------------发送数据打包---------------------------------------------------
void TxPacket(uchar TxRxBuf)
{
uchar i;
//Config905();
CSN=0;
spi_write(WTP); // Write payload command
/*for (i=0;i<4;i++)
{
spi_write(TxRxBuf); // Write 32 bytes Tx data
}*/
spi_write(TxRxBuf);
CSN=1;
nrf905_Delay(1); // Spi disable
CSN=0; // Spi enable for write a spi command
spi_write(WTA); // Write address command
for (i=0;i<4;i++) // 写入与对方地址一样的地址
{
spi_write(TxAddress);
}
CSN=1; // Spi disable
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
nrf905_Delay(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//----------------------------------------------设置发送初始状态---------------------------------------------
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//-----------------------------------------------设置接收初始化---------------------------------------------------
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
nrf905_Delay(1); // nrf905_Delay for mode change(>=650us)
}
//----------------------------------------------------读NRF905接收数据------------------------------------------------------------
void RxPacket(void)
{
//uchar i;
nrf905_Delay(1);
// TRX_CE=0; // Set nRF905 in standby mode
nrf905_Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
nrf905_Delay(1);
spi_write(RRP);
/*for (i = 0 ;i < 4 ;i++)
{
TxRxBuf=spi_read(); // Read data and save to buffer
Send_byte(TxRxBuf);
}*/
RxBuf=spi_read();
CSN=1;
nrf905_Delay(10);
TRX_CE=1;
}
//----------------------------------------------------判断数据接收状态--------------------------------------------
unsigned char CheckDR(void)
{
//DR=1;
if(DR==1&&TRX_CE==1 && TXEN==0)
{
//DR=0;
return 1;
}
else
{
return 0;
}
}
//--------------------------------------------------------数据接收------------------------------------------------
void RX(void)
{
//uchar i;
SetRxMode();
while (CheckDR()==0);
nrf905_Delay(10);
RxPacket();
Send_byte(RxBuf);
}
//---------------------------------------------------检查是否已存在同频率载波--------------------------------------
unsigned char CheckCD(void)
{
if(CD==1)
{
return 1;
}
else
{
return 0;
}
}
//---------------------------------------------------------main函数------------------------------------------------
void main (void)
{
//void timer0_init();
UartInit();
nRF905Init(); //初始化nRF905
Config905(); //初始化nRF905寄存器
//uchar Rev;
while(1)
{
RX();
//SetTxMode();
//TxRxBuf[0]=Receive();
//nrf905_Delay(1000);
//TxPacket(TxRxBuf);
}
}
//---------------------------------------------------------串口中断--------------------------------------------------
void serial()interrupt 4
{
//uchar buf;
if(RI)
{
RI=0;
//buf=SBUF;
TxBuf=SBUF;
SetTxMode();
TxPacket(TxBuf);
}
}
自己能收到什么意思,找一下硬件连接上的问题
现在能通讯了,但是还有问题,我发送端连接的是COM1,接收端是COM6,下面是结果
为什么发送端发送完数据会把数据连同写进905的命令字都反馈给自己,而且接收端显示的也不只是数据,也会显示配置字。
如图发送端20 29 22 5A 5A 5A 5A:29是数据,20和22是控制命令,5A是接收端地址
接收端的 24 29:24是控制命令,29是数据
//----------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00 //写配置寄存器
#define RC 0x10 //读配置寄存器
#define WTP 0x20 //向TX-Payload寄存器写入发送有效数据
#define RTP 0x21 //从TX-Payload寄存器读取发送有效数据
#define WTA 0x22 //向TX-Address寄存器写入发送地址
#define RTA 0x23 //从TX-Address寄存器读取发送地址
#define RRP 0x24 //从RX-Payload寄存器读取接收到的有效数据
发送端
接收端
你好,我最近也在弄这个模块,一直没有通讯成功,你能帮一忙吗?
我也是马马虎虎弄弄,课题跟这个有关,想弄的话+Q469773738,说明是谁,一起合作