本实验无仿真,自制实验板;相关连接见附件。
c:
NRF905收发程序 温度传送.zip
pcb图:
NRF终极版.zip
涉及芯片:NRF 905 1023BG
三极管:S9011 C 331
电容:100UF
二极管:IN5819
引脚:1,VCC 2,TXE
3,CE, 4,PWR
5,NC 6,CD
7,AM 8,DR
9,MISO 10,MOSI
11,SCK 12,CSN
13,GND 14,GND
/*发送模块*/
#include
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar code ICE52[3] _at_ 0x003b;//char code reserve [3] _at_ 0x3b; //仿真器避开0x3b地址
#define BIT0 0x01
#define BIT7 0x80
#define TxRxBuf_Len 4
uchar TxRxBuf[TxRxBuf_Len];//-------发送数据缓冲区-----------//
uchar code TxAddress[4]={0xe8,0xe7,0xe7,0xe7};
uchar zm2[]=" 00.0 ";
//-------------------------------18b20------------------------------------------
sbit dq=P2^0;
//---------------------------NRF905工作模式控制端口---------------------------------//
sbit TXEN=P1^0;
sbit PWR=P1^1;
sbit TRX_CE=P1^2;
//---------------------------------NRF905 SPI数据交换端口-------------------------------------//
sbit MOSI=P1^4;
sbit MISO=P1^7;
sbit CSN=P1^5;
sbit SCK=P1^6;
//----------------------------------------NRF905状态端口--------------------------------------//
// sbit AM=P1^1;
sbit DR=P1^3;
// sbit CD=P1^2;
//-------------------------------NRF905 SPI控制指令------------------------------------------//
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//-----------------------------------NRF905配置寄存器的配置--------------------------------//
uchar idata RFConf[11]=
{
0x00, //配置命令:配置寄存器的首地址//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为4字节
0xcc,0xcc,0xcc,0xcc, //接收地址
0xd8 //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
uchar readRF[11];
//---------------------NRF905延时函数-------------//
void delay(int n)
{
while(n--);
}
//===============================SPI读函数===================//
uchar SpiRead(void)
{
unsigned char j,DATA_BUF;
for (j=0;j<8;j++)
{
SCK=1;
DATA_BUF=DATA_BUF<<1;
DATA_BUF=DATA_BUF|MISO;
_nop_();
_nop_();
SCK=0;
_nop_();
_nop_();
}
return DATA_BUF;
}
//================================SPI写函数=================//
void SpiWrite(uchar send)
{
unsigned char i,DATA_BUF;
DATA_BUF=send;
for (i=0;i<8;i++)
{
SCK=0;
_nop_();
_nop_();
if (DATA_BUF&0x80) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
_nop_();
_nop_();
}
SCK=0;
}
//--------------------------------------------初始化nRF905---------------------------------//
void nRF905Init(void)
{
CSN=1; // Spi 功能失效
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
//AM=1; // Init AM for input
//CD=1; // Init CD for input
PWR=1; // nRF905通电
TXEN=0; // 设置无线在Rx方式下
TRX_CE=0; // 设置nRF905在准备方式下
}
//------------------------------------------初始化寄存器--------------------------------------//
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 TxPacket(uchar *TxRxBuf)
{
uchar i;
CSN=0;
SpiWrite(WTP); // Write payload command
for (i=0;i<4;i++)
{
SpiWrite(TxRxBuf[i]); // Write 32 bytes Tx data
}
CSN=1;
//read_disp(0x21,4);
delay(1); // Spi disable
CSN=0; // Spi enable for write a spi command
SpiWrite(WTA); // Write address command
for (i=0;i<4;i++) // 写入与对方地址一样的地址
{
SpiWrite(TxAddress[i]);
}
CSN=1; // Spi disable
//read_disp(0x23,4);
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
//delay(1); //
while (DR!=1);
TRX_CE=0;
}
//--------------------------------------设置发送初始状态-------------------------------//
void SetTxMode(void)
{
TXEN=1;
TRX_CE=0;
delay(1); // nrf905_Delay for mode change(>=650us)
}
/*void read_disp(uchar com,uchar len)
{
uchar i,temp;
CSN=0; // Spi enable for write a spi command
SpiWrite(com); // Write config command写放配置命令
for (i=0;i
{
readRF[i]=SpiRead();
}
CSN=1; // Disable Spi
for(i=0;i
{
temp=readRF[i]>>4;
P2=mazhi[temp];
delay(60000);
P2=mazhi[readRF[i]&0x0f];
delay(60000);
}
} */
/*----------------------------------------------------------
DS18B20温度采集模块
-----------------------------------------------------------*/
void DS18_delay(int useconds)//延时函数
{
int s;
for (s=0; s
}
uint reset()
{ uchar flag;
dq=0; //复位程序
DS18_delay(29);
dq=1;
DS18_delay(3);
flag=dq;
DS18_delay(25);
return(flag);
}
uchar read() //读操作低位在前
{ uchar dat=0,i;
for(i=0;i<8;i++)
{ dq=1;
DS18_delay(1);
dq=0;
dat>>=1;
dq=1;
if(dq)
dat|=0x80;
DS18_delay(4);
}
return(dat);
}
void write(uchar dat) //写低位在前
{ uchar i;
for(i=0;i<8;i++)
{ dq=0;
dq=dat&0x01; // 充当1us_nop_();
DS18_delay(5);
dq=1;
dat>>=1;
}
DS18_delay(4);
}
void disp(uchar lsb,uchar msb)
{ uchar result,sign,point; //p小数 s正负号,flag器件存在标志,i循环变量
sign=(msb>>3)&0x01; //取符号
lsb>>=4;
msb<<=4;
result=lsb|msb;
point=(int)((float)(((~(lsb-=1))&0x0f)*0.625));
if(sign==1)
{ result-=1;
result=~result;
result=result&0x7f; }
if(result>=0x64)
{zm2[5]=result/100+0x30;} else zm2[5]=' ';
if(result>=0x0a)
{zm2[6]=(result/10)%10+0x30;} else zm2[6]=' ';
zm2[7]=result%10+0x30;
zm2[9]=point%10+0x30;
if(sign) zm2[4]='-'; else zm2[4]=' ';
}
void DS18B20()
{ uchar flag;
flag=reset();
if(!flag)
{ write(0xcc);
write(0x44);
DS18_delay(125);
reset();
write(0xcc);
write(0xbe);
disp(read(),read());
}
}
void main()
{
nRF905Init();
Config905();
//read_disp(0x10,10);
while(1)
{
DS18B20();
TxRxBuf[0]=zm2[6];
TxRxBuf[1]=zm2[7];
TxRxBuf[2]='.';
TxRxBuf[3]=zm2[9];
SetTxMode();
TxPacket(TxRxBuf); // 发送命令数据
}
}
//---------------接收模块-----------------//
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define TxRxBuf_Len 4
//---------发送数据缓冲区--------------//
unsigned char Buf[TxRxBuf_Len];
uchar code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};
//---------------------------NRF905工作模式控制端口---------------------------------//
sbit TXEN=P1^0;
sbit PWR=P1^1;
sbit TRX_CE=P1^2;
//---------------------------------NRF905 SPI数据交换端口-------------------------------------//
sbit MOSI=P1^4;
sbit MISO=P1^5;
sbit CSN=P1^6;
sbit SCK=P1^7;
//----------------------------------------NRF905状态端口--------------------------------------//
sbit AM=P2^1;
sbit DR=P1^3;
sbit CD=P2^0;
//----------------------------------------NRF905控制指令---------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//------------------------------------------------NRF905寄存器配置----------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令:配置寄存器的首地址//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为4字节
0xe8,0xe7,0xe7,0xe7, //接收地址
0xd8 //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//---------------------NRF905延时函数-------------//
void delay(int n)
{
while(n--);
}
//===============================SPI读函数===================//
unsigned char SpiRead(void)
{
unsigned char j,DATA_BUF;
for (j=0;j<8;j++)
{
SCK=1;
DATA_BUF=DATA_BUF<<1;
DATA_BUF=DATA_BUF|MISO;
_nop_();
_nop_();
SCK=0;
_nop_();
_nop_();
}
return DATA_BUF;
}
//================================SPI写函数=================//
void SpiWrite(unsigned char send)
{
unsigned char i,DATA_BUF;
DATA_BUF=send;
for (i=0;i<8;i++)
{
SCK=0;
_nop_();
_nop_();
if (DATA_BUF&0x80) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
_nop_();
_nop_();
}
SCK=0;
}
//------------------------------------------------------初始化NRF905----------------------------
void nRF905Init()
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
PWR=1; // nRF905 power on
TXEN=0; // set radio in Rx mode
TRX_CE=0; // Set nRF905 in standby mode
}
//-----------------------------------------------------初始化配置寄存器-------------------------
void Config905()
{
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
}
//----------------------------------------------------读NRF905接收数据-----------------------
void RxPacket()
{
uchar i;
TRX_CE=0; //待机模式
CSN=0; // Spi enable for write a spi command
SpiWrite(RRP); //写读配置命令
for (i=0;i<4;i++)
{
Buf[i]=SpiRead(); // Read data and save to buffer
}
CSN=1;
delay(1);
}
//-----------------------------------------------设置接收初始化----------------------------
void SetRxMode()
{
TXEN=0;
TRX_CE=1;
delay(100); // nrf905_Delay for mode change(>=650us)
}
//--------------------------------------------------------数据接收---------------------------------
//1602液晶显示修改程序
sbit RS=P2^4;
sbit RW=P2^5;
sbit EN=P2^6;
uchar VOL[]="welcome to me!";
uchar table2[]="00.0v";
void delay_50us(uint t)
{
unsigned int j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void write_com(uchar com)
{
EN=0;
RS=0;
RW=0;
P0=com;
delay_50us(10);
EN=1;
delay_50us(20);
EN=0;
}
void write_data(uchar dat)
{
EN=0;
RS=1;
RW=0;
P0=dat;
delay_50us(10);
EN=1;
delay_50us(20);
EN=0;
}
void init_1602(void)
{
delay_50us(300);
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
/*void read_disp(uchar com,uchar len)
{
uchar i,temp;
uchar readRF[10];
CSN=0; // Spi enable for write a spi command
SpiWrite(com); // Write config command写放配置命令
for (i=0;i
{
readRF[i]=SpiRead();
}
CSN=1; // Disable Spi
write_com(0x80);
for(i=0;i
{
temp=readRF[i]>>4;
write_data(ascii[temp]);
write_data(ascii[readRF[i]&0x0f]);
}
} */
void main()
{
uchar m;
init_1602();
nRF905Init();
Config905();
//read_disp(0x10,10);
write_com(0x80);
for(m=0;m<14;m++)
{
write_data(VOL[m]);
}
while(1)
{
SetRxMode();
while(!CD);
while(!AM);
while(!DR);
RxPacket();
table2[0]=Buf[0];
table2[1]=Buf[1];
table2[2]=Buf[2];
table2[3]=Buf[3];
//-------------------------------1602液晶显示---------------------------------------------------
write_com(0x80+0x40);
for(m=0;m<5;m++)
{
write_data(table2[m]);
}
}
}
阅读(2342) | 评论(0) | 转发(1) |