找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

求助 NRF905发送和接收问题

[复制链接]
admin 发表于 2012-9-2 13:19:47 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
我用两种51单片机做NRF905的实验,一个发送一个接收 却不好使,哪个大侠看看

接收程序
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.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
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 TxRxBuf_Len 4
unsigned char TxRxBuffer[TxRxBuf_Len];
//------------------------------------------------NRF905端口配置--------------------------------------------------
sbit TXEN=P2^7;
sbit PWR=P2^6;
sbit CD=P2^5;
sbit DR=P2^4;
sbit MOSI=P2^1;
sbit CSN=P2^0;
sbit MISO=P1^3;
sbit SCK=P1^4;
sbit AM=P1^2;
sbit TRX_CE=P1^0;
//-----------------------------------------------NRF905寄存器配置------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c,
0x0c,
0x44,0x04,0x04,
0xcc,0xcc,0xcc,0xcc,
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//-----------------------------------------------NRF905控制命令--------------------------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//-----------------------------------------------延时------------------------------------------------------------
void Delay(uchar n)
{
uint k;
while(n--)
for(k=0;k<200;k++);
}
///-----------------------------------------------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;
}
//------------------------------------------------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;
}
}
//--------------------------------------------------初始化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
}
//--------------------------------------------------NRF905初始化寄存器-------------------------------------------
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[i]);
}
CSN=1; // Disable Spi
}
//--------------------------------------------------NRF905接收模式-----------------------------------------------
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;
}
}
//--------------------------------------------------数据接收----------------------------------------------------------
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++)
{
TxRxBuffer[i]=SpiRead(); // Read data and save to buffer
}
CSN=1;
Delay(10);
TRX_CE=1;
}
//--------------------------------------------------NRF905数据接收------------------------------------------------
void RX(void)
{
SetRxMode(); // Set nRF905 in Rx mode
while (CheckDR()==0);
Delay(10);
RxPacket();
Delay(10);
}
//-------------------------------------------------串口出事化----------------------------------------------------
void StartUART( void )
{ //波特率4800
SCON = 0x50;
TMOD = 0x20;
TH1 = 0xFA;
TL1 = 0xFA;
PCON = 0x00;
TR1 = 1;
}
//------------------------------------------------串口发送--------------------------------------------------------
void R_S_Byte(uchar R_Byte)
{
SBUF = R_Byte;
while( TI == 0 ); //查询法
TI = 0;
}
//----------------------------------------------------------------------------------------------------------------
void main(void)
{
StartUART();
nRF905Init();
Config905();
while(1)
{
char i;
RX();
Delay(10);
CSN=0;
for(i=0;i<4;i++)
{
R_S_Byte(TxRxBuffer[i]);
}
CSN=1;
Delay(1);
}
}

发送程序
#include "STC12C5A60S2.h"
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.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
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 TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len]=
{
0x29,0x30,0x31,0x32,
};
//--------------------------------------------------NRF905端口配置------------------------------------------------
sbit TXEN=P1^5;
sbit TRX_CE=P1^4;
sbit PWR=P1^1;
sbit MISO=P0^1;
sbit MOSI=P0^0;
sbit SCK=P3^2;
sbit CSN=P3^3;
//---------------------------------------------------NRF905状态IO----------------------------------------------------
sbit AM=P4^2;
sbit DR=P1^0;
sbit CD=P3^4;
//---------------------------------------------------NRF905寄存器配置---------------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在423MHZ
0x0C, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xee,0xee,0xee,0xee, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
//----------------------------------------------------NRF905控制指令----------------------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//----------------------------------------------------延时------------------------------------------------------
static void Delay(uchar n)
{
uint i;
while(n--)
for(i=0;i<200;i++);
}
//---------------------------------------------------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;
}
}
//---------------------------------------------------初始化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
}
 楼主| admin 发表于 2012-9-2 13:19:51 | 显示全部楼层
//---------------------------------------------------初始化NRF905寄存器--------------------------------------------
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[i]);
}
CSN=1; // Disable Spi
}
//----------------------------------------------------数据打包----------------------------------------------------------------
void TxPacket(void)
{
uchar i;
CSN=0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxRxBuf[i]); // 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[i]);
}
CSN=1; // Spi disable
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
Delay(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//----------------------------------------------------设置发送状态-------------------------------------------------
void SetTxMode(void)
{
TRX_CE=0;
TXEN=1;
Delay(1); // delay for mode change(>=650us)
}
//-----------------------------------------------------数据发送---------------------------------------------------
void TX(void)
{
SetTxMode();// Set nRF905 in Tx mode
TxPacket();// Send data by nRF905
// SetRF_PA_PWR(unsigned char i); //设置发射功率
}
void main(void)
{
nRF905Init();
Config905();
while(1)
{
TX();
}
}
都是11.0592的晶振
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-25 00:25 , Processed in 0.055343 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表