|
//#include
#include
//#include
//#define MULTITX_PROGRAM in Target->Options->C51
#include "def.h"
#include "nRf905\nRF905.h"
#include "elseFiles\func.h"
//#include "print\print.h"
#define ID_H 0x4e
#define ID_L 0x05
unsigned char TxFlag=0;
#define TIME_DELAY_DEF 0
#define TIME_LIMIT_DEF 15
unsigned char time_delay = TIME_DELAY_DEF;
unsigned char time_limit = TIME_LIMIT_DEF;
unsigned char TxBuf[4];
unsigned char RxBuf[32];
//**********************************
//定时器中断函数
//*********************************
#pragma vector=TIMERA0_VECTOR
__interrupt void timer_A(void)
{
//WDTCTL=WDT_ARST_1000; //0.5s中断一次,喂狗一次
if (time_delay
}
void ChangeModeAndTx()
{
unsigned char i = 0;
unsigned int randt;
//P0=P0>>1; //调试用
//if (P0==0x0f) P0=0xff;
if (nRF905State != RXSTATE)
{
SetRxMode();
}
while(CheckCD())
{
i++;
randt = ID_H*0x100 + ID_L;
// randt = i*rand()/0x100;
time_delay_for(randt);
}
SetTxMode();
TxPacket(TxBuf); //发送数据包
SetRxMode();
}
//具体核对代码可根据需要修改,这里只是作一个示范
unsigned char CheckACKData()
{
unsigned char i,j;
j=RxBuf[0];
for (i=0;i
{
if (RxBuf[j*2]==ID_H && RxBuf[j*2+1]==ID_L)
{
return 1;
}
}
// for (i=0;i<0x20;i++) print_Hex(RxBuf,0);
return 0;
}
//准备发送下一组数据,具体代码可根据需要修改,这里同样是示范
void DataContinue()
{
TxFlag++;
TxBuf[2] = TxFlag;
}
void DataAgain()
{
TxBuf[2] = TxFlag;
}
//P0=P0>>1; //调试用
//if (P0==0x0f) P0=0xff;
void main()
{
// unsigned int i,j;
WDTCTL=WDTPW+WDTHOLD; //先关闭看门狗
//P2DIR = 0X08 ;
TACTL=TASSEL0+TACLR; //选择时钟为ACLK,并且清除计数器
CCTL0=CCIE; //CCR0允许中断
CCR0=3276; //0.1s中断一次
TACTL |= MC0; //设置工作模式为增计数模式
_EINT(); //开总中断
//_BIS_SR(LPM3_bits); //进入低功耗模式3
TxBuf[0] = ID_H;
TxBuf[1] = ID_L;
TxBuf[2] = TxFlag;
//----------------------------
//<初始化>
//print_init();
nRF905Init();
Config905();
// SetTxMode();
// srand( ID_H*0x100 + ID_L ); //通过2字节的ID生成随机数种子
//P0=0xff;
//print_Str("MUltiTx_v0.1\n\r");
time_delay = TIME_DELAY_DEF;
time_limit = TIME_LIMIT_DEF;
ChangeModeAndTx();
//-----------
while(1)
{
// while(P1&0x01 == 1); //等待按键
while (CheckDR()==0) //等待接收端的ACK响应
{
if (time_delay
{
}
else //<超时,数据重发>
{
DataAgain();
ChangeModeAndTx();
time_delay = TIME_DELAY_DEF;
time_limit = TIME_LIMIT_DEF;
}
}
//P0=P0>>1; //调试用
//if (P0==0x0f) P0=0xff;
//print_Str("\n\r#");
//print_Hex(time_delay,0);
RxPacket(RxBuf);
if ( CheckACKData() ) //接收端返回ACK响应
{
//print_Str("\n\rNext");
DataContinue(); //准备发送下一组数据
ChangeModeAndTx();
time_delay = TIME_DELAY_DEF;
time_limit = TIME_LIMIT_DEF;
}
else //返回的响应不符合要求继续等待
{
//print_Str("\n\r#AG");
DataAgain();
}
}
}
|
|