本帖最后由 teejay 于 2013-3-29 10:44 编辑
用AT89S52来控制nrf905无线收发模块的数据发送与接收,再把接收端收到的数据通过串口传到PC机,但是一直出错,调了好几天了,还是这个问题,PC端收到的数据一直是00 00 00 00,若把接收程序main()函数中的数据接收函数注释掉,PC显示的就是接收程序中给接收数组事先赋的值,而只要一加上那个接收函数,PC收到的就都是0了,劳烦各位帮忙看看问题出在哪,谢谢了~程序如下:
下面是我的收发程序:
//////发送端程序///////////////
#include<reg52.h>
#include <ABSACC.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
/////////////////////////////
#define BYTE_BIT0 0x01
#define BYTE_BIT1 0x02
#define BYTE_BIT2 0x04
#define BYTE_BIT3 0x08
#define BYTE_BIT4 0x10
#define BYTE_BIT5 0x20
#define BYTE_BIT6 0x40
#define BYTE_BIT7 0x80
////////////SPI指令
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
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;
unsigned char TxRxBuf[4]=
{
0x31,0x32,0x33,0x34
};
//IO/////////////
sbit TXEN=P1^7;
sbit TRX_CE=P1^6;
sbit PWR=P1^5;
///SPI IO///////
sbit MISO=P1^1;
sbit MOSI=P1^0;
sbit SCK=P3^7;
sbit CSN=P3^3;
//static IO//
sbit CD=P1^4;
sbit AM=P1^3;
sbit DR=P3^2;
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^3;
/////////////////
void Delaynop(void);
//RF寄存器配置//
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在423MHZ
0x0C, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xCC,0xCC,0xCC,0xCC, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
bit lcdbit;
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
///////////延时/////////////////
static void Delay(uint n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
void Delaynop(void)
{
unsigned char i;
for(i=0;i<10;i++);
}
///////////////
unsigned char SpiRead(void)
{
unsigned char i;
for (i=0;i<8;i++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
Delaynop();
if (MISO)//读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
Delaynop();
}
return DATA_BUF;
}
void SpiWrite(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
for (i=0;i<8;i++)
{
if (DATA7)//总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
Delaynop();
DATA_BUF=DATA_BUF<<1;
SCK=0;
Delaynop();
}
}
////////////////初始化nRF905///////////////////
void nRF905Init(void)
{
CSN=1;// Spi disable
SCK=0;// Spi clock line init low
DR=0;// Init DR for input
AM=0;// Init AM for input
CD=0;// 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
for (i=0;i<11;i++)// Write configration words 写放配置字
{
SpiWrite(RFConf);
}
CSN=1;// Disable Spi
}
/////////////发送数据
void TxPacket(void)
{
uchar i;
CSN=0;
SpiWrite(WTP);// Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxRxBuf);// Write 4 bytes Tx data
}// Spi enable for write a spi command
CSN=1;
Delay(1);// Spi disable
CSN=0;// Spi enable for write a spi command
SpiWrite(WTA);// Write address command
for (i=0;i<4;i++)// Write 4 bytes address
{
SpiWrite(TxAddress);
}
CSN=1;// Spi disable
TRX_CE=1;// Set TRX_CE high,start Tx data transmission
Delay(5);// while (DR!=1);
TRX_CE=0;// Set TRX_CE low
}
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
Delay(2); // delay for mode change(>=650us)
}
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
Delay(2); // delay for mode change(>=650us)
}
void main()
{
nRF905Init();
Config905();
LED3=0;
Delay(2000);
LED3=1;
while(1)
{
LED2=0;
Delay(2000);
LED2=1;
Delay(2000);
SetRxMode();
Delay(2);
SetTxMode();
TxPacket();
LED3=0;
Delay(2000);
LED3=1;
Delay(2);
Delay(5000);
}
}
/////////////////////////////////////////////////////////
//接收端程序/////////////////////////////////////////////
#include<reg52.h>
#include <ABSACC.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
/////////////////////////////
#define BYTE_BIT0 0x01
#define BYTE_BIT1 0x02
#define BYTE_BIT2 0x04
#define BYTE_BIT3 0x08
#define BYTE_BIT4 0x10
#define BYTE_BIT5 0x20
#define BYTE_BIT6 0x40
#define BYTE_BIT7 0x80
////////////SPI指令
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
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;
unsigned char RxBuf[4]=
{
0x34,0x33,0x32,0x31
};
//IO/////////////
sbit TXEN=P1^7;
sbit TRX_CE=P1^6;
sbit PWR=P1^5;
///SPI IO///////
sbit MISO=P1^1;
sbit MOSI=P1^0;
sbit SCK=P3^7;
sbit CSN=P3^3;
//static IO//
sbit CD=P1^4;
sbit AM=P1^3;
sbit DR=P3^2;
sbit LED1=P0^0;
sbit LED2=P0^1;
sbit LED3=P0^3;
/////////////////
void Delaynop(void);
//RF寄存器配置//
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在423MHZ
0x0C, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xCC,0xCC,0xCC,0xCC, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
bit lcdbit;
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
///////////延时/////////////////
static void Delay(uint n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
void Delaynop(void)
{
unsigned char i;
for(i=0;i<10;i++);
}
///////////////
unsigned char SpiRead(void)
{
unsigned char i;
for (i=0;i<8;i++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
Delaynop();
if (MISO)//读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
Delaynop();
}
return DATA_BUF;
}
void SpiWrite(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
for (i=0;i<8;i++)
{
if (DATA7)//总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
Delaynop();
DATA_BUF=DATA_BUF<<1;
SCK=0;
Delaynop();
}
}
////////////////初始化nRF905///////////////////
void nRF905Init(void)
{
CSN=1;// Spi disable
SCK=0;// Spi clock line init low
DR=0;// Init DR for input
AM=0;// Init AM for input
CD=0;// 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
for (i=0;i<11;i++)// Write configration words 写放配置字
{
SpiWrite(RFConf);
}
CSN=1;// Disable Spi
}
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
Delay(2); // delay for mode change(>=650us)
}
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
Delay(2); // delay for mode change(>=650us)
}
unsigned char CheckDR(void)//检查是否有新数据传入 Data Ready
{
if (DR=1&&TRX_CE==1 && TXEN==0)
{
return 1;
}
else
{
return 0;
}
}
void RxPacket(void)
{
uchar i;
Delay(1);
Delay(100);
TRX_CE=0;
CSN=0;// Spi enable for write a spi command
Delay(1);
SpiWrite(RRP);
for (i = 0;i < 4 ;i++)
{
RxBuf=SpiRead();// Read data and save to buffer
}
CSN=1;
Delay(10);
TRX_CE=1;
}
////////////////
void RX(void)
{
SetRxMode();// Set nRF905 in Rx mode
// while (CheckDR()==0);
//Delay(10000);
if( CheckDR()==1)
{
Delay(10);
RxPacket();
}
Delay(10);
}
void Init_MCU(void)
{
SCON = 0x50; /* uart in mode 1 (8 bit), REN=1 */
TMOD = 0x20 ; /* Timer 1 in mode 2 */
TH1 = 0xFA; /* 9600 Bds at 11.059MHz */
TL1 = 0xFA; /* 9600 Bds at 11.059MHz */
PCON=0X80;
TR1 = 1;
}
void Sendpc(uchar mess)
{
SBUF=mess;
while(TI==0);
TI=0;
}
void main()
{
uchar i;
Init_MCU();
nRF905Init();
Config905();
Delay(5);
while(1)
{
for(i=0;i<4;i++)
{
Sendpc(RxBuf);
}
SetRxMode();
RxPacket();
/*!注如果我把这句RxPacket()注释掉,PC端收到的是接收函数里接收数组事先定义的值,但是加了这个接收函数之后,PC收到的就一直是00 00 00 00 */
SetTxMode();
Delay(500);
for(i=0;i<4;i++)
{
Sendpc(RxBuf);
}
SetTxMode();
Delay(2);
Delay(5000);
}
}
|