/*----------------------------------------------------------
具备跳号跳频和跳址的nrf905的C51演示程序
----------------------------------------------------------*/
//#pragma src
#include
#include
#include
#include
/*---------------------*/
#include
#include "hotins.h"
/*---------------------*/
#pragma NOAREGS
void MainInit(void);//系统初始化
void SystemInit(void);//系统初始化
void SystemSetup(void);//系统设置
void SystemIoInit(void);//系统接口初始化
void TimeInit(void);//定时器定时参数设置
void UserSetup(void);//用户运行环境设置
void ClrWdt(void);//喂狗
void BeepSetup(unsigned char mode);//蜂鸣器发声模式设置
void Beep(void);//蜂鸣器发声驱动
void KeyInit(void);//键盘初始化
void KeyCommandExec(unsigned char commgroup, unsigned char commnum);//执行键盘命令
void Key00(void);//压键事件
void Key01(void);
void Key02(void);
void Key03(void);
void Key10(void);//放键事件
void Key11(void);
void Key12(void);
void Key13(void);
void Key20(void);//长键事件
void Key21(void);
void Key22(void);
void Key23(void);
/*-----------------------------------------------
nrf905库函数定义
------------------------------------------------*/
unsigned char SpiReadWrite(unsigned char);
void WriteTXBuffer(const unsigned char[]);
unsigned char ReadStatus(void);
void nRFPowerUP(void);
void nRFInit(void);//无线模块初始化
unsigned char VerifynRFConfig(void);//校验配置nRF905
void WritenRFConfig(unsigned char[]);//写配置nRF905
void ReadnRFConfig(unsigned char[]);//读配置nRF905
void FastWritenRFConfig(unsigned char);//快速配置nRF905
void SetTxMode(void);//设置发送模式
void SetRxMode(void);//设置接收模式
void StartTx(void);//开始发送
void nRFReadWrite(void);//无线收发模块
data SystemData SystemBuffers;//申请系统数据结构
idata SioData SioBuffers;//申请系统数据结构
/*------------------------------------------------
SPI读写一体化函数SpiReadWrite()
入口参数:
val写入命令或数据
出口参数:
R7读出数据
------------------------------------------------*/
unsigned char SpiReadWrite(unsigned char val) using 0
{
unsigned char i;
ACC = val;
for (i = 8; i > 0; i --)
{
CY = MISO;//取数据SO
_rlca_();//存数据ACC.0读数据ACC.7同时进行,acc循环左移,acc.7移入cy(出),cy(入)移到acc.0
MOSI = CY;//送数据SI
SCK = 1;//上升沿打入数据
_nop_();
SCK= 0;//下降沿读入数据(首次为假动作)
}
return ACC;
}
/*------------------------------------------------
写入nRF905发送数据函数WriteTXBuffer()
入口参数:
buffer 发送数据包
出口参数:
无
------------------------------------------------*/
void WriteTXBuffer(const unsigned char buffer[]) using 0
{
unsigned char i;
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN=0;//打开SPI
SpiReadWrite(WTA);//写发送地址
for (i = 0; i < 4; i++)
{
SpiReadWrite(buffer);// Write 32 bytes Tx data
}
CSN=1;//关闭SPI
_nop_();
CSN=0;// Spi enable for write a spi command
SpiReadWrite(WTP);// Write payload command
for (i = 0; i < 32; i++)
{
SpiReadWrite(buffer);// Write 32 bytes Tx data
}
CSN=1;// Spi disable
TRX_CE = !TX_EN;//
}
void FastWritenRFConfig(unsigned char low_ch_no) using 0
{
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN=0;//打开SPI
SpiReadWrite(CC | PA_PWR_10dBm | HFREQ_PLL_433MHz | CH_NO_BIT8);
SpiReadWrite(low_ch_no);//CH_NO
CSN=1;// Spi disable
TRX_CE = !TX_EN;//
}
void StartTx(void) using 0
{
_nop_();
_nop_();
_nop_();
TRX_CE=1;// Set TRX_CE high,start Tx data transmission
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TRX_CE=0;// Set TRX_CE low
}
/*------------------------------------------------
读入nRF905状态函数ReadStatus()
入口参数:
无
出口参数:
nRF905状态字
------------------------------------------------*/
unsigned char ReadStatus(void) using 0
{
unsigned char val;
CSN = 0;//打开SPI
val = SpiReadWrite(RC);//读状态
CSN = 1;//关闭SPI
return val;//返回nRF905状态字
}
void nRFPowerUP(void) using 0
{
CSN= 1;//关闭SPI
SCK= 0;//拉低时钟
DR= 1;//设置DR为输入方式
AM= 1;//设置AM为输入方式
PWR_UP = 1;// nRF905上电
TRX_CE = 0;//设置nRF905为待机方式
TX_EN= 0;//设置接收方式
}
void main(void)
{
//ReadnRFConfig(SystemBuffers.nRFTxBuff);
MainInit();//系统初始化
while (1) {//主循环
IE|= 0xb2;//保证中断可靠EA,ET2,ES,ET0
TCON |= 0x55;//保证定时器开
PCON |= SMOD_ + GF0_ + IDL_;//进入空闲状态并喂软件狗
_nop_();
_nop_();
}
}
void MainInit(void) using 0//系统初始化
{
_start_();
ClrWdt();//清除看门狗计数器
SystemIoInit();//系统接口初始化
SystemInit();//系统上电初始化
SystemSetup();//系统运行环境设置
UserSetup();//用户运行环境设置
}
void SystemInit(void) using 0//系统初始化
{
unsigned char i;
for (i = 0; i < 32; i ++) {
SioBuffers.SioTxBuff = 0;
SioBuffers.SioRxBuff = 0;
}
if (SystemBuffers.RamTest != 0x55aa) {//内存初始化
for (i = 0; i < 32; i ++) {
SystemBuffers.nRFTxBuff = 0;
SystemBuffers.nRFRxBuff = 0;
}
SystemBuffers.nRFRxBuff[0] = RX_ADDRESS_0;//本机地址
SystemBuffers.nRFRxBuff[1] = RX_ADDRESS_1;
SystemBuffers.nRFRxBuff[2] = RX_ADDRESS_2;
SystemBuffers.nRFRxBuff[3] = RX_ADDRESS_3;
SystemBuffers.nRFRxBuff = CH_NO_BYTE;
SystemBuffers.nRFTxBuff = CH_NO_BYTE;
SystemBuffers.RamTest = 0x55aa;
}
else {
}
}
void SystemSetup(void) using 0//系统设置
{
TimeInit();
IP = 0x05;//中断优先级EX0>EX1>ET0>ET1
SystemBuffers.TestTime = 0;
SystemBuffers.WaitTime = 0;
}
void TimeInit() using 0
{
//TCON = 0x55;//启动定时器
/*----------------------------------
定时器0定时参数设置
----------------------------------*/
TL0 = 0;
TH0 = 0;
TR0 = 1;//启动定时器0
/*----------------------------------
定时器1定时参数设置
----------------------------------*/
TMOD = 0x20;//timer1 working mode 1
TL1 = 0xfd;//f7=9600 for 16mhz Fosc,and ...
TH1 = 0xfd;//...fd=19200 for 11.0592mhz Fosc
SCON = 0xd8;//uart mode 3,ren==1
PCON = 0x80;
SioBuffers.SioTXCount = 0;//无发送数据
SioBuffers.SioRXCount = 32;//允许接收串口数据
TR1 = 1;//启动定时器1
/*----------------------------------
定时器2定时参数设置
----------------------------------*/
TIMEER2 = T2_5mS;
RCAP= T2_5mS;
TR2= 1;//启动定时器2
}
void nRFInit(void) using 0//无线模块初始化
{
do{
WritenRFConfig(SystemBuffers.nRFRxBuff);//配置nRF905
}
while(VerifynRFConfig());
SetRxMode();//设置接收模式
SystemBuffers.nRFTxCount = 0;//清空无线发送数据缓冲区
SystemBuffers.nRFRxCount = 0;//清空无线接收数据缓冲区
}
void ReadnRFConfig(unsigned char rxaddr[]) using 0//读配置nRF905
{
unsigned char i;
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN = 0;// Spi enable for write a spi command
SpiReadWrite(RC);// Read config command
for (i = 10; i < 20; i ++) rxaddrIdea = SpiReadWrite(0xff);
CSN = 1;// Disable Spi
TRX_CE = !TX_EN;//
}
void WritenRFConfig(unsigned char rxaddr[]) using 0//配置nRF905
{
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN = 0;// Spi enable for write a spi command
SpiReadWrite(WC);// Write config command
SpiReadWrite(rxaddr);//中心频率低8位
SpiReadWrite(PA_PWR_10dBm | HFREQ_PLL_433MHz); //发射+10dBm,发射频率433MHz,中心频率第9位=0
SpiReadWrite(TX_AFW_4BYTE | RX_AFW_4BYTE);//接收地址宽度4字节,发送地址宽度4字节
SpiReadWrite(RX_PW_32BYTE);//接收数据宽度32字节
SpiReadWrite(TX_PW_32BYTE);//发送数据宽度32字节
SpiReadWrite(rxaddr[0]);//接收有效地址第1字节
SpiReadWrite(rxaddr[1]);//接收有效地址第2字节
SpiReadWrite(rxaddr[2]);//接收有效地址第3字节
SpiReadWrite(rxaddr[3]);//接收有效地址第4字节
SpiReadWrite(CRC16_EN | XOF_16MHz);//CRC16模式使能,晶体振荡器频率16MHz
CSN = 1;// Disable Spi
TRX_CE = !TX_EN;//
}
unsigned char VerifynRFConfig(void) using 0//校验配置nRF905
{
unsigned char error = 0;
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN = 0;// Spi enable for write a spi command
SpiReadWrite(RC);// Read config command
SpiReadWrite(0xff);//中心频率低8位
if (SpiReadWrite(0xff) != (PA_PWR_10dBm | HFREQ_PLL_433MHz)) error ++; //发射+10dBm,发射频率433MHz,中心频率第9位=0
if (SpiReadWrite(0xff) != (TX_AFW_4BYTE | RX_AFW_4BYTE)) error ++;//接收地址宽度4字节,发送地址宽度4字节
if (SpiReadWrite(0xff) != (RX_PW_32BYTE)) error ++;//接收数据宽度32字节
if (SpiReadWrite(0xff) != (TX_PW_32BYTE)) error ++;//发送数据宽度32字节
SpiReadWrite(0xff);//接收有效地址第1字节
SpiReadWrite(0xff);//接收有效地址第2字节
SpiReadWrite(0xff);//接收有效地址第3字节
SpiReadWrite(0xff);//接收有效地址第4字节
if (SpiReadWrite(0xff) != (CRC16_EN | XOF_16MHz)) error ++;//CRC16模式使能,晶体振荡器频率16MHz
CSN = 1;// Disable Spi
TRX_CE = !TX_EN;//
return error;
}
/*******************************************************************************************/
//function SetTxMode();
/*******************************************************************************************/
void SetTxMode(void) using 0
{
_nop_();
_nop_();
_nop_();
TX_EN=1;
_nop_();
TRX_CE=0;
_nop_();
_nop_();
_nop_();
}
/*******************************************************************************************/
//function SetRxMode();
/*******************************************************************************************/
void SetRxMode(void) using 0
{
_nop_();
_nop_();
_nop_();
TX_EN=0;
_nop_();
TRX_CE=1;
//650uS
_nop_();
_nop_();
_nop_();
}
void SystemIoInit(void) using 0
{
IE = 0x00;//关闭中断
P2 = 0x9d;//P2口初始化
P0 = 0xff;//P0口初始化
P1 = 0xff;//P1口初始化
P3 = 0xff;//P3口初始化
//nRFPowerUP();
}
void UserSetup(void) using 0//用户运行环境设置
{
KeyInit();//键盘初始化
nRFInit();//无线模块初始化
SystemBuffers.TimeCount = 0;
}
void ClrWdt(void)using 0//喂狗
{
WDTRST = 0x1e;//89s52内狗
WDTRST = 0xe1;//89s52内狗
}
/*------------------------------------
定时器T0中断服务程序
------------------------------------*/
void t0proc() interrupt TF0_VECTOR using 1
{
}
/*------------------------------------
定时器T1中断服务程序
------------------------------------*/
void t1proc() interrupt TF1_VECTOR using 1
{
}
/*------------------------------------
定时器T2中断服务程序(5mS)
------------------------------------*/
void t2proc() interrupt TF2_VECTOR using 0
{
unsigned char key;
code unsigned char KeyPosTab[] = {0x01, 0x02, 0x04, 0x08};
TF2 = 0;
if (PCON & GF0_) {//中断是从主循环内跳入的才能喂狗
ClrWdt();//清除看门狗计数器
PCON &= ~GF0_;//清除标志
}
key = ~P0;//接收键盘数据每次可取多键)
if (key & KeyPosTab[SystemBuffers.TimeCount & 0x03]) {//有键压下(每次只取1键以实现“零耗时消除键盘抖动”)
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] ++;//压键计数
if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] == 2) {//短压键
SystemBuffers.WaitTime = 0;
KeyCommandExec(0, SystemBuffers.TimeCount & 0x03);//单击键Key00()~Key03()
}
else if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= SysPushKeyTime) {//长压键150 * 20mS = 3。0S
SystemBuffers.WaitTime = 0;
KeyCommandExec(2, SystemBuffers.TimeCount & 0x03);//长压键Key20()~Key23()
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 3;//清除长压键计数器(躲过单击键)
}
}
else {//无键压下(包括放键)
if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= 2) {//放键事件
SystemBuffers.WaitTime = 0;
KeyCommandExec(1, SystemBuffers.TimeCount & 0x03);//键释放Key10()~Key13()
}
else {
nRFReadWrite();//无线收发模块
}
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 0;//清除压键计数器
}
SystemBuffers.TimeCount++;//继续下1个5mS延时
if ((SystemBuffers.TimeCount & ~0x80) >= 100){//5mS*100=500mS
SystemBuffers.WaitTime ++;
if (SystemBuffers.WaitTime >= SysWaitTime) {//0.5*20=10S
P0 = 0xff;//关显示
SystemBuffers.WaitTime = 0;
}
SystemBuffers.TimeCount &= 0x80;
SystemBuffers.TimeCount ^= 0x80;//取反闪烁位
}
}
/*------------------------------------*/
void sioproc() interrupt SIO_VECTOR using 1
{
unsigned char i;
if (RI) {//接收中断
RI = 0;
if (SioBuffers.SioRXCount && (SioBuffers.SioRXCount <= 32)) {//允许串口接收数据
SioBuffers.SioRxBuff[32 - SioBuffers.SioRXCount] = SBUF;
SioBuffers.SioRXCount --;
if (SioBuffers.SioRXCount == 0) {
for (i = 0; i < 32; i ++) {
SioBuffers.SioTxBuff = SioBuffers.SioRxBuff;//自收自发
SystemBuffers.nRFTxBuff = SioBuffers.SioTxBuff;//更新无线发送数据
}
/*
switch(SystemBuffers.nRFTxBuff[9]) {
case 0x81: LED0 = 0;break;
case 0x82: LED1 = 0;break;
case 0x83: LED2 = 0;break;
case 0x84: LED3 = 0;break;
case 0x03: LED2 = 0;break;
case 0x04: LED3 = 0;break;
}
*/
SioBuffers.SioRXCount = 32;//串口接收缓冲区满
SioBuffers.SioTXCount = 32;//串口发送缓冲区满
TI = 1;//立即发送
SystemBuffers.nRFTxCount = 32;//要求间接无线发送
}
}
else SioBuffers.SioRXCount = 0;//不允许串口接收数据
}
if (TI) {//发送中断
TI = 0;
if (SioBuffers.SioTXCount && (SioBuffers.SioTXCount <= 32)) {//允许串口发送数据
SBUF = SioBuffers.SioTxBuff[32 - SioBuffers.SioTXCount];//串口显示
SioBuffers.SioTXCount --;
}
else SioBuffers.SioTXCount = 0;//不允许串口发送数据
}
}
//执行键盘命令
void KeyCommandExec(unsigned char commgroup, unsigned char commnum) using 0
{
unsigned int i;
unsigned key;
code void *KeyArray[3][4] = {//二维函数数组指针阵列表(散转命令地址表)
{(void *)Key00 + 0x5b7d, (void *)Key01 + 0xa6ea, (void *)Key02 + 0xf157, (void *)Key03 + 0x4cc4},
{(void *)Key10 + 0x9731, (void *)Key11 + 0xe2ae, (void *)Key12 + 0x3d1b, (void *)Key13 + 0x8888},
{(void *)Key20 + 0xd3f5, (void *)Key21 + 0x2e62, (void *)Key22 + 0x79df, (void *)Key23 + 0xc44c}
};
if ((commgroup < 3) && (commnum < 4)) {
ClrWdt();//清除看门狗计数器
key = commgroup * 4 + commnum + 1;
i = ((key * 53 & 0xf) * 0x1000)+ ((key * 43 & 0xf) * 0x100) + ((key * 23 & 0xf) * 0x10) + (key * 13 & 0xf);
_icall_((void *)KeyArray[commgroup][commnum] - i);//键盘命令散转
}
}
void BeepSetup(unsigned char mode)
{
//BeepBuffers.BeepCount = 1;
//BeepBuffers.BeepMode= mode;
//BeepBuffers.BeepValue = mode;
}
/*----------------------------------
蜂鸣器100mS发声程序
----------------------------------*/
void Beep(void)//蜂鸣器发声
{
//BeepBuffers.BeepCount --;
//if (BeepBuffers.BeepCount == 0){
//BeepBuffers.BeepCount = 0;
//BeepBuffers.BeepValue = BeepBuffers.BeepMode;
//if (BeepBuffers.BeepValue >= 0x80)//单声
//BeepBuffers.BeepMode = 0;//单声清除
//}
//BeepBuffers.BeepValue <<= 1;
//CY = ~CY;//硬件反向
//BeepOut = CY;//发声
}
void KeyInit(void) using 0//键盘初始化
{
unsigned char i;
for (i = 0; i < 4; i++){
SystemBuffers.KeyCount = 0;//清除键盘压键次数缓冲区
}
}
//收发无线读写模块
void nRFReadWrite(void)
{
unsigned char i;
unsigned int t;
if (!TX_EN && DR && AM) {//收到数据
ClrWdt();
TRX_CE = 0;//设置nRF905为待机方式
_nop_();
CSN=0;// Spi enable for write a spi command
SpiReadWrite(RRP);// Read payload command
for (i = 0; i < 32; i++)
{
SystemBuffers.nRFRxBuff=SpiReadWrite(0xff);//读入无线接收缓冲区
}
CSN=1;// Disable spi
TRX_CE = !TX_EN;//再回到原来的无线收发模式
//TRX_CE = 1;//再回到接收模式
switch(SystemBuffers.nRFRxBuff[9]) {
case 0x81: LED0 = 0;break;
case 0x82: LED1 = 0;break;
case 0x83: LED2 = 0;break;
case 0x84: LED3 = 0;break;
case 0x01: LED0 = 0;break;
case 0x02: LED1 = 0;break;
case 0x03: LED2 = 0;break;
case 0x04: LED3 = 0;break;
}
SystemBuffers.WaitTime = 0;
while(DR || AM);
SystemBuffers.nRFRxCount = 32;//收到32个字节数据
if (SioBuffers.SioTXCount == 0){//串口数据已发送完
for (i = 0; i < 32; i ++)
SioBuffers.SioTxBuff = SystemBuffers.nRFRxBuff;//串口显示
SioBuffers.SioTXCount = 32;//发送32个字节的显示数据
TI = 1;//立即显示
}
}
else if(!CD && (SystemBuffers.nRFRxCount == 32)) {//接收命令处理
SystemBuffers.nRFRxCount = 0;
for (i = 0; i < 4; i ++)
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[i + 4];//写入串口接收缓冲区
for (i = 4; i < 8; i ++)
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[i - 4];//写入串口接收缓冲区
for (i = 8; i < 32; i ++)
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff;//写入串口接收缓冲区
if (SystemBuffers.nRFRxBuff[9] == 0x80) {//系统消息请求
SystemBuffers.nRFTxBuff[9]= 0x00;//主机系统消息应答
ReadnRFConfig(SystemBuffers.nRFTxBuff);
SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x81) {//子机请求
//LED0 = 0;//主机灯亮
SystemBuffers.nRFTxBuff[9]= 0x01;//主机应答
SystemBuffers.nRFTxBuff[10] = 0x20;
SystemBuffers.nRFTxBuff[11] = 0x04;
SystemBuffers.nRFTxBuff[12] = 0x10;
SystemBuffers.nRFTxBuff[13] = 0x18;
SystemBuffers.nRFTxBuff[14] = 0x11;
SystemBuffers.nRFTxBuff[15] = 0x28;
SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x82) {//子机请求
//LED1 = 0;//主机灯亮
SystemBuffers.nRFTxBuff[9] =0x02;//主机应答
SystemBuffers.nRFTxBuff[10] = 0x20;
SystemBuffers.nRFTxBuff[11] = 0x04;
SystemBuffers.nRFTxBuff[12] = 0x10;
SystemBuffers.nRFTxBuff[13] = 0x26;
SystemBuffers.nRFTxBuff[14] = 0x21;
SystemBuffers.nRFTxBuff[15] = 0x09;
SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x83) {//子机变址请求
//LED2 = 0;//主机变址灯亮
SystemBuffers.nRFTxBuff[9]= 0x03;//主机变址应答
SystemBuffers.nRFTxBuff[10] = TH0;//
SystemBuffers.nRFTxBuff[11] = TL0;
SystemBuffers.nRFTxBuff[12] = SystemBuffers.WaitTime;
SystemBuffers.nRFTxBuff[13] = SystemBuffers.TimeCount;
SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x84) {//子机跳频请求
//LED3 = 0;//主机跳频灯亮
SystemBuffers.nRFTxBuff[9]= 0x04;//主机跳频应答
SystemBuffers.nRFTxBuff[10] = SystemBuffers.nRFTxBuff + 1;//主机跳频
SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x03) {//主机变址应答
LED2 = 0;//子机变址灯亮
SystemBuffers.nRFRxBuff[0] = SystemBuffers.nRFRxBuff[10];//改写接收地址第1字节
SystemBuffers.nRFRxBuff[1] = SystemBuffers.nRFRxBuff[11];//改写接收地址第2字节
SystemBuffers.nRFRxBuff[2] = SystemBuffers.nRFRxBuff[12];//改写接收地址第3字节
SystemBuffers.nRFRxBuff[3] = SystemBuffers.nRFRxBuff[13];//改写接收地址第4字节
WritenRFConfig(SystemBuffers.nRFRxBuff);//子机重新配置nRF905接收地址
}
else if (SystemBuffers.nRFRxBuff[9] == 0x04) {//主机跳频应答
//LED3 = 0;//子机跳频灯亮
SystemBuffers.nRFRxBuff = SystemBuffers.nRFRxBuff[10];
FastWritenRFConfig(SystemBuffers.nRFRxBuff);//快速配置nRF905,子机跳频
}
}
else if (!CD && (SystemBuffers.nRFTxCount == 32)) {//要求间接无线发送(发送命令处理)
SetTxMode();//设置发送模式
WriteTXBuffer(SystemBuffers.nRFTxBuff);
ClrWdt();//清除看门狗计数器
StartTx();//开始发送
SystemBuffers.nRFTxCount = 0;//结束发送模式
if (SystemBuffers.nRFTxBuff[9] == 0x03) {//主机变址应答
SystemBuffers.nRFRxBuff[4] = SystemBuffers.nRFTxBuff[10];//改写呼叫地址第1字节
SystemBuffers.nRFRxBuff[5] = SystemBuffers.nRFTxBuff[11];//改写呼叫地址第2字节
SystemBuffers.nRFRxBuff = SystemBuffers.nRFTxBuff[12];//改写呼叫地址第3字节
SystemBuffers.nRFRxBuff[7] = SystemBuffers.nRFTxBuff[13];//改写呼叫地址第4字节
}
else if (SystemBuffers.nRFTxBuff[9] == 0x04) {//主机跳频应答
SystemBuffers.nRFRxBuff = SystemBuffers.nRFTxBuff[10];//工作频率低8位
for (t = 0; t < 2500; t ++);//延时,等待发送结束
FastWritenRFConfig(SystemBuffers.nRFRxBuff);//快速配置nRF905,主机跳频
}
SetRxMode();//设置接收模式
}
}
//键
void Key00(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff;//呼叫地址第3字节
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff;//工作频率低8位
SystemBuffers.nRFTxBuff[9] = 0x81;//请求命令
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff = 0;
SystemBuffers.nRFTxCount = 32;//要求间接无线发送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff = SystemBuffers.nRFTxBuff;
SioBuffers.SioTXCount = 32;//要求间接串口发送
TI = 1;//送串口显示
//LED0 = 0;//压键灯亮
}
}
//键
void Key01(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff;//呼叫地址第3字节
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff;//工作频率低8位
SystemBuffers.nRFTxBuff[9] = 0x82;//请求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff = 0;
SystemBuffers.nRFTxCount = 32;//要求间接无线发送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff = SystemBuffers.nRFTxBuff;
SioBuffers.SioTXCount = 32;//要求间接串口发送
TI = 1;//送串口显示
//LED1 = 0;//压键灯亮
}
}
void Key02(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff;//呼叫地址第3字节
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff;//工作频率低8位
SystemBuffers.nRFTxBuff[9] = 0x83;//变址请求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff = 0;
SystemBuffers.nRFTxCount = 32;//要求间接无线发送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff = SystemBuffers.nRFTxBuff;
SioBuffers.SioTXCount = 32;//要求间接串口发送
TI = 1;//送串口显示
//LED2 = 0;//压键灯亮
}
}
void Key03(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff;//呼叫地址第3字节
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
SystemBuffers.nRFTxBuff = SystemBuffers.nRFRxBuff;//工作频率低8位
SystemBuffers.nRFTxBuff[9] = 0x84;//跳频请求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff = 0;
SystemBuffers.nRFTxCount = 32;//要求间接无线发送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff = SystemBuffers.nRFTxBuff;
SioBuffers.SioTXCount = 32;//要求间接串口发送
TI = 1;//送串口显示
//LED3 = 0;//压键灯亮
}
}
void Key10(void) using 0
{
LED0 = 1;//放键灭灯
}
void Key11(void) using 0
{
LED1 = 1;//放键灭灯
}
void Key12(void) using 0
{
LED2 = 1;//放键灭灯
}
void Key13(void) using 0
{
LED3 = 1;//放键灭灯
}
void Key20(void) using 0
{
LED0 = 1;//长压键灭灯
Key00();//连发
}
void Key21(void) using 0
{
LED0 = 1;//长压键灭灯
Key01();//连发
}
void Key22(void) using 0
{
LED0 = 1;//长压键灭灯
Key02();//连发
}
void Key23(void) using 0
{
LED0 = 1;//长压键灭灯
Key03();//连发
}
=============================================
*----------------------------------------------------------
具备跳号跳频和跳址的nrf905的C51演示程序nrf905.h
HotPower@126.com2006.6.6于大雁塔村队部
----------------------------------------------------------*/
#define nRFMainDev //主设备
//#define SysWaitTime 20//0.5*20=10S不压键停止闪烁
#define SysWaitTime 6//0.5*20=3S不压键停止闪烁
#define SysPushKeyTime 150//长压键150 * 20mS = 3。0S
#define T2_5mS -921.6*5//5ms 12MHz
/*------------------------------------------------
AT89S5X
--------------------------------------------------*/
sfrAUXR= 0x8e;
sfrWDTRST= 0xa6;
sfr16 TIMEER2 = 0xcc;
sfr16 RCAP= 0xca;
//全局变量定义
typedef struct Systemstruct{//系统数据结构
//unsigned char SystemFlage;//系统标志
unsigned char TimeCount;
unsigned int RamTest;
unsigned char TestTime;
unsigned char WaitTime;
unsigned char KeyCount[4];
unsigned char nRFTxCount;
unsigned char nRFRxCount;
unsigned char nRFTxBuff[32];
unsigned char nRFRxBuff[32];
}SystemData;
//全局变量定义
typedef struct Siostruct{//系统数据结构
unsigned char SioTxBuff[32];
unsigned char SioRxBuff[32];
unsigned char SioTXCount;
unsigned char SioRXCount;
}SioData;
/*-----------------------------------------------
nrf905管脚定义
------------------------------------------------*/
sbitTX_EN= P2^6;//0
sbitTRX_CE= P2^5;//0
sbitPWR_UP= P2^4;//1
sbitMISO= P2^3;//1
sbitMOSI= P2^2;//1
sbitSCK= P2^1;//0
sbitCSN= P2^0;//1
sbitAM= P3^2;//1
sbitDR= P3^3;//1
sbitCD= P3^5;//1
sbitKEY0= P0^0;
sbitKEY1= P0^1;
sbitKEY2= P0^2;
sbitKEY3= P0^3;
sbitLED0= P0^4;
sbitLED1= P0^5;
sbitLED2= P0^6;
sbitLED3= P0^7;
#define Status_AM 0x80
#define Status_DR 0x20
/*-----------------------------------------------
nrf905命令控制字
------------------------------------------------*/
#define WC0x00// Write configuration register command
#define RC0x10// Readconfiguration register command
#define WTP0x20// Write TX Payloadcommand
#define RTP0x21// ReadTX Payloadcommand
#define WTA0x22// Write TX Addresscommand
#define RTA0x23// ReadTX Addresscommand
#define RRP0x24// ReadRX Payloadcommand
#define CC0x80// fast config
/*-------------------------------------------------------------------------------------------
nrf905配置寄存器(10Byte)
工作频率f=(422.4+CH_NO/10)*(1+HFREQ_PLL)MHz
-------------------------------------------------------------------------------------------*/
#ifdefnRFMainDev
#define RX_ADDRESS0x00000000