最近调试一对nRF905,昨天晚上调试成功了,一直很稳定,今天再试就不行了,接收方DR变高后读数据有时候正确有时候错误(为0x00),后来发现,昨天发送端进入发送模式后DR变高但是接着会变低(这里是不是因为数据发送完了就变低?),今天是变高后一直未高,是不是说没有发送完?可是程序我又没变,很是郁闷...请教大家帮我看看,我实在是不知道哪儿有问题,我用的是M16,下面是程序,有不对的地方请指教,谢谢了!
#include <mega16.h>
#include <delay.h>
#define uint unsigned int
#define uchar unsigned char
#define DATA7 (DATA_BUF&(1<<7))!= 0
#define DATA0 (DATA_BUF&(1<<0))!= 0
//----------------配置口定义---------------------------------------
#define TXEN_1 PORTB.0=1// (PORTB|=(1<<0))
#define TXEN_0 PORTB.0=0// (PORTB&=~(1<<0))
#define TRX_CE_1 PORTB.1=1// (PORTB|=(1<<1))
#define TRX_CE_0 PORTB.1=0// (PORTB&=~(1<<1))
#define PWR_1 PORTB.2=1// (PORTB|=(1<<2))
#define PWR_0 PORTB.2=0// (PORTB&=~(1<<2))
#define MISO PINB.3// (PINB&(1<<3))
#define MOSI_1 PORTB.4=1// (PORTB|=(1<<4))
#define MOSI_0 PORTB.4=0// (PORTB&=~(1<<4))
#define SCK_1 PORTB.5=1// (PORTB|=(1<<5))
#define SCK_0 PORTB.5=0// (PORTB&=~(1<<5))
#define CSN_1 PORTB.6=1// (PORTB|=(1<<6))
#define CSN_0 PORTB.6=0// (PORTB&=~(1<<6))
//---------------------NRF905状态IO-------------------------------------
#define AM PINA.0
#define DR PINA.1
#define CD PINA.2
//--------------------------------nrf905控制指令------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
uchar TxAddress[4]={0xcc,0xcc,0xcc,0xcc}; //发送端发送的地址(接收端的本机地址)
unsigned char DATA_BUF;
unsigned char TxBuf[4]=
{
0x01,0x02,0x03,0x04
};
unsigned char RxBuf[4];
unsigned char RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xCC,0xCC,0xCC,0xCC, //本机地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
// SPI读操作
unsigned char SpiRead(void)
{
unsigned char j;
for(j=0;j<8;j++)
{
DATA_BUF=DATA_BUF<<1;
SCK_1; //初始化时SCK为低电平
if(MISO) //读取最高位先保存到末尾,通过左移到最高位
{
DATA_BUF|=(1<<0);
}
else
{
DATA_BUF&=~(1<<0);
}
SCK_0;
}
return DATA_BUF;
}
//spi写函数
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;
DATA_BUF=DATA_BUF<<1;
SCK_0;
}
}
void nRF905Init(void)
{
CSN_1; // Spi disable
SCK_0; // Spi clock line init low
PORTA=0XFF; //AM DR CD输入拉高
DDRA=0X00;
PWR_1; // nRF905 power on
TRX_CE_0; // Set nRF905 in standby mode
TXEN_0; // set radio in Rx mode
}
//--------------------初始化寄存器-----------------------------------
void Config905(void)
{
unsigned char i;
CSN_0; // Spi enable for write a spi command
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf[i]);
// delay_us(2);
}
CSN_1; // Disable Spi
}
//---------------------------发送数据打包-----------------------------------
void TxPacket(uchar *TxRxBuf)
{
unsigned char i;
CSN_0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxBuf[i]); // Write 32 bytes Tx data
// delay_us(2);
}// Spi enable for write a spi command
CSN_1;
// delay_ms(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[i]);
// delay_us(2);
}
CSN_1; // Spi disable
// delay_us(2);
TRX_CE_1; // Set TRX_CE high,start Tx data transmission
delay_ms(1); // // while (DR!=1);
delay_ms(1);
TRX_CE_0; // Set TRX_CE low 发送完成后进入空闲模式
}
//------------------------设置发送状态------------------------------
void SetTxMode(void)
{
TRX_CE_0;
TXEN_1;
delay_ms(2); // delay for mode change(>=650us)
}
//------------设置接受状态-----------------------------------------
void SetRxMode(void)
{
TXEN_0;
TRX_CE_1;
delay_ms(2); // delay for mode change(>=650us)
}
//----------读NRF905接收数据--------------------------------
void RxPacket(void)
{
uchar i;
TRX_CE_0;
CSN_0; // Spi enable for write a spi command
delay_ms(1);
SpiWrite(RRP);
for (i = 0 ;i < 4 ;i++)
{
RxBuf[i]=SpiRead(); // Read data and save to buffer
delay_us(2);
}
CSN_1;
delay_ms(1);
TRX_CE_1;
}
void main(void)
{
// Declare your local variables here
DDRB=0xF7; //MISO(PB3)为输入 其他输出
DDRA=0x00;
nRF905Init();
Config905();
while (1)
{
SetTxMode();
TxPacket(TxBuf);
}
} |