905程序(发送)Nrf905程序(接收)
2008年05月23日 星期五 06:59 P.M.
#include
#include
#include
#include
#define uint unsigned int //0 ~ 255
#define uchar unsigned char
/////////////////
#define BYTE_BIT0 0x01
#define BYTE_BIT1 0x02
#define BYTE_BIT2 0x04
#define BYTE...
Nrf905程序(接收)
2008年05月23日 星期五 06:59 P.M.
#include
#include
#include
#include
#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)
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
#define TxRxBuf_Len 4
unsigned char TxRxBuffer[TxRxBuf_Len];
char temp[4];
uint count=1;
sbit TXEN=P2^7;
sbit TRX_CE=P3^2;
sbit PWR=P2^6;
sbit MISO=P2^1;
sbit MOSI=P2^2;
sbit SCK=P2^0;
sbit CSN=P2^4;
/////////////////////////////////////////////////////
sbit AM=P2^3;
sbit DR=P3^3;
sbit CD=P2^5;
///////////////////////////////////////////////////////
/////////////////////////////////////////////
sbit led0=P1^0;
sbit led1=P1^2;
sbit led2=P1^4;
/////////////////////////////////////////////////////
uchar seg[10]={0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0~~9段码
//uchar seg1[10]={0x02,0xf2,0x24,0x0c,0x98,0x48,0x40,0x1e,0x00,0x08}; //0~~9段码
//RF寄存器配置//
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c,0x0c,0x44,0x04,0x04,0xcc,0xcc,0xcc,0xcc,0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
bit lcdbit;
///////////80us延时/////////////////
void Delay(uchar n)
{
uint k;
while(n--)
for(k=0;k<8;k++);
}
void delay1(uint i)
{
for(;i>0;i--);
}
///////////////
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=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[i]);
}
CSN=1; // Disable Spi
}
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);
// TRX_CE=0; // Set nRF905 in standby mode
Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
Delay(1);
SpiWrite(RRP);
// Delay(10000);
// Delay(10000); // Read payload command
for (i = 0 ;i < 4 ;i++)
{ // Delay(10000);
// Delay(10000);
// Delay(10000);
// Delay(10000);
TxRxBuffer[i]=SpiRead(); // Read data and save to buffer
// Delay(10000);
// Delay(10000);
// Delay(10000);
}
CSN=1;
Delay(10);
// Delay(10);
TRX_CE=1;
}
////////////////
void RX(void)
{
SetRxMode(); // Set nRF905 in Rx mode
//Delay(10000);
while (CheckDR()==0);
//Delay(10000);
Delay(10);
RxPacket();
//Delay(10000); // Recive data by nRF905
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 time0sym()
{
TH0=0xE2 ; //重载计数初始值
TL0=0x00 ;
TR0=1;
ET0=1;
EA=1;
TF0=0 ; //清中断
}
void time0_int() interrupt 1
{
if(TF0==1) //10ms中断
{
TF0=0 ; //清中断
TH0=0xE2 ; //重载计数初始值
TL0=0x00 ;
count=count+1 ;
if(count==100) //1s
{
count=0; //每1秒清count ,重新开始等待1秒
}
}
}
void main(void)
{
//char i;
StartUART();
time0sym();
nRF905Init();
Config905();
while(1)
{
RX();
Delay(10);
CSN=0;
if(TxRxBuffer[0])
{
if(TxRxBuffer[0]==0x29)
{
// R_S_Byte(TxRxBuffer[0]);
led0=0;led1=1;led2=0;
delay1(50);
//led0=1;led1=1;led2=1;led3=1;
}
if(TxRxBuffer[0]==0x30)
{
// P0=0x00;
// R_S_Byte(TxRxBuffer[0]);
led0=1;led1=0;led2=1;
delay1(50);
// led0=1;led1=1;led2=1;led3=1;
}
}
}
CSN=1;
Delay(1);
}
本文档为【905程序(发送)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。