网上下载的程序,自己也进行修改了很多地方,但双方就是不能通信。#define TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len]=
{
0x00,0x00,0x00,0x00,
};
//---------------------------------------------------配置口定义---------------------------------------------------
sbit TXEN=P1^4;
sbit TRX_CE=P1^6;
sbit PWR=P1^5;//出
sbit MISO=P0^1;
sbit MOSI=P0^2;
sbit SCK=P0^0;
sbit CSN=P0^3;
sbit AM=P0^6;
sbit DR=P0^4;
sbit CD=P0^5;
sbit ZB=P2^0;//振动传感器接口
//----------------------------------------------------按键--------------------------------------------------
sbit KEY0=P1^1;
sbit KEY1=P1^7;
//----------------------------------------------------蜂鸣器与LED-------------------------------------------------------
sbit led0=P1^0;
sbit beep=P1^2;
sbit led1=P1^3;
//-------------------------------------------------------nrf905控制指令-------------------------------------------
#define WC 0x00 //写配置寄存器
#define RC 0x10 //读配置寄存器
#define WTP 0x20 //写TX有效数据
#define RTP 0x21 //读TX有效数据
#define WTA 0x22 //写TX地址
#define RTA 0x23 //读TX地址
#define RRP 0x24 //读RX有效数据
//------------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00,
0x4c, // 配置频道
0x0c, //自动重发关,发送节电模式关,输出功率10dB,915MHz
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<100;i++);
}
//==================================================
void PCA0_Init (void)
{
PCA0MD &= ~0x40; // Disable the Watchdog Timer
}
//==================================================
void Oscillator_Init (void)
{
OSCICN = 0x83;
// OLKSEL = 0x00; // Set the internal oscillator to // 12 MHz 八分频
}
//==================================================
void Port_Init (void)
{
P0MDOUT = 0x0d; // Make SCK, MOSI, and NSS push-pull
P0SKIP=0xFF;
MISO=1;
P1MDOUT=0xFF;
// P1MDIN=0xFF;
P1SKIP=0xFF;
P2MDIN=0x01;
P2MDOUT=0x00; // Make the LED push-pull
P2SKIP=0XFF;
// XBR0= 0x02; // XBR0=0x02; // Enable the SPI on the XBAR
XBR1 = 0x40; // Enable the XBAR and weak pull-ups
}
//----------------------------------------------
/*void SPI0_Init()
{
SPI0CFG = 0x40; // Enable the SPI as a Master
// CKPHA = '0', CKPOL = '0'
SPI0CN = 0x0D; // 4-wire Single Master, SPI enabled
// SPI clock frequency equation from the datasheet
SPI0CKR = (SYSCLK/(2*SPI_CLOCK))-1;
ESPI0 = 1; // Enable SPI interrupts
} */
//==================================================
void Init_Device (void)
{
PCA0_Init (); // Disable the Watchdog Timer first
Oscillator_Init ();
Port_Init ();
// SPI0_Init();
Delay(100);
}
//----------------------------------------------
//---------------------------------------------------SPI读函数-----------------------------------------------------
unsigned char SpiRead(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;
}
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;
}
}
//------------------------------------------------------初始化nRF905---------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // 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[i]);
}
CSN=1; // Disable Spi
}
//void SwitchChannel(uchar TxAddress)
//{
// CSN = 0;
// SpiWrite(TxAddress); //跳频。
// CSN = 1;
//}
//-----------------------------------------------设置接收状态---------------------------------------------------
void SetRxMode(void)
{
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)
{
// Delay(50) ;
return 1;
}
else
{
return 0;
}
}
//----------------------------------------------------读NRF905接收数据------------------------------------------------------------
void RxPacket(void)
{
uchar i;
Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
Delay(1);
SpiWrite(RRP);
// for (i = 0 ;i < 4 ;i++)
// {
TxRxBuf[0]=SpiRead(); // Read data and save to buffer
// }
led0=1;
CSN=1;
while(DR);
Delay(10);
TRX_CE=1;
}
void RX(void)
{
SetRxMode(); // Set nRF905 in Rx mode
// while (DR==0);
Delay(10);
RxPacket();
if(TxRxBuf[0]==0x29)
{
led1=!led1;
beep=!beep;
Delay(10);
led1=1;
beep=0;
/* while(1)
{
if(KEY0==0||ZB ==1)
{
led0=!led0;
beep=!beep;
Delay(100);
led0=0;
beep=0;
}
} */
}
if(TxRxBuf[1]==0x30)
{
led1=!led1;
beep=!beep;
Delay(10);
led1=0;
beep=0;
}
if(TxRxBuf[2]==0x31)
{
led1=1;
Delay(10);
led1=0;
/* while(1)
{
if(KEY0==0||ZB ==1)
{
led0=!led0;
Delay(100);
led0=0;
}
}*/
}
if(TxRxBuf[3]==0x32)
{
led1=1;
if(KEY1==0)
{
led1=0;
}
}
}
/*void Out_interrupt1_init(void)
{
EA=0;
EX1=0;
//IT01CF&=~0X08; //INT0改为低电平有效
IT01CF|=0XF0; //IT01CF的高四位设为1111,即INT1高电平有效,p0^7输入
//IT01CF&=0X9f;
TCON |=0X04; //INT1 边沿触发
//INT1设为上升沿触发
EX1=1;
}*/
void main(void)
{
int Input=16;
Init_Device ();
nRF905Init();
Config905();
// Out_interrupt1_init();
led0=0;
led1=0;
beep=0;
SetRxMode();
while(1)
{
if(CheckDR()==1)
RX();
if(KEY0==0)// (ZB==1)
{
led1=1;
}
if(KEY0==1)
{
led1=0;
}
}
} |