|
麻烦大家帮我看看以下程序怎么发送不了数据
发送部分
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#include <io_def.h>
#define uint unsigned int
#define uchar unsigned char
#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)
unsigned char bflag;//指示是否需要发送无线
#define TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len];
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晶振
};
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
///////////延时/////////////////
static void Delay(uchar n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
void Delaynop(void)
{
unsigned char i;
for(i=0;i<10;i++);
}
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
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf);
}
CSN=1; // Disable Spi
}
/////////////发送数据
void TxPacket(unsigned char *Txbuf)
{
uchar i;
//Config905();
CSN=0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(Txbuf); // Write 32 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)
{
PWR=1;
TRX_CE=1;
TXEN=1;
Delay(2); // delay for mode change(>=650us)
}
unsigned char CheckCD(void) //Pin->检查是否已存在 同频率载波
{
if (CD==1)
{
return 1;
}
else
{
return 0;
}
}
void TX(unsigned char *TxRxBuf)
{
SetTxMode();// Set nRF905 in Tx mode
TxPacket(TxRxBuf);// Send data by nRF905
CheckCD(); // 返回CD的当前电平
}
void main()
{
nRF905Init();
Config905();
while (1)
{
if(KEY1==0)
{
Delay(1);
LED1 =0;
bflag =1;
TxRxBuf[0]=0x29;
}
if(KEY2==0)
{
Delay(1);
LED2 =0;
bflag =1;
TxRxBuf[0]=0x30;
}
Delay(1000);
LED2 =1;
LED1 =1;
if(bflag==1)
{
bflag =0;
TX(TxRxBuf);
Delay(1000);
TX(TxRxBuf);
}
}
}
接收部分
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#include <io_def.h>
#define uint unsigned int //0 ~ 255
#define uchar unsigned char
/////////////////
#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
////////////////
#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)
#define TxRxBuf_Len 4
unsigned char TxRxBuffer[TxRxBuf_Len];
//RF寄存器配置//
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c,0x0c,0x44,0x04,0x04,0xcc,0xcc,0xcc,0xcc,0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
///////////80us延时/////////////////
void Delay(uchar n)
{
uint k;
while(n--)
for(k=0;k<80;k++);
}
void Delaynop(void)
{
unsigned char i;
for(i=0;i<10;i++);
}
///////////////
unsigned char SpiRead(void)
{
unsigned char j;
for (j=0;j<8;j++)
{
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
//SpiWrite(WC); // Write config command写放配置命令
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf);
}
CSN=1; // Disable Spi
}
void SetRxMode(void)
{
PWR=1;
TXEN=0;
TRX_CE=1;
Delay(1); // 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(unsigned char *TxRxBuffer)
{
uchar i;
Delay(1);
Delay(100);
TRX_CE=0; // Set nRF905 in standby mode
CSN=0; // Spi enable for write a spi command
Delay(1);
SpiWrite(RRP);
// Read payload command
for (i = 0 ;i < 4 ;i++)
{
TxRxBuffer=SpiRead(); // Read data and save to buffer
}
CSN=1;
Delay(10);
TRX_CE=1;
}
////////////////
void RX(void)
{
SetRxMode(); // Set nRF905 in Rx mode
if( CheckDR()==1)
{
Delay(10);
RxPacket(TxRxBuffer);
}
if(TxRxBuffer[0]==0x29)
{
LED1=0;
LED2=1;
Delay(10);
}
if(TxRxBuffer[0]==0x30)
{
LED1=1;
LED2=0;
Delay(10);
}
Delay(10); // Recive data by nRF905
}
void main()
{
nRF905Init();
Config905();
while(1)
{
RX();
}
}
麻烦大家帮忙看看了,搞了很久还是没进展,有没高手可以讨论下,还有有没类似调试成功的程序,万分感谢。。。 |
|