📄 f211.#3
字号:
//----------------------------------------------------------------------------//
// 用于传输指定消息 //
//----------------------------------------------------------------------------//
void Transmit_Direct_Message(unsigned int msg_code,unsigned char msg_bits)
{
unsigned char chbits=0;
for(chbits=0;chbits<msg_bits;chbits++) //写指定位数
{
if ((msg_code&0x8000)==0x8000)
DATAMSG=1;
else DATAMSG=0;
//等待100us
wait_time_100us(); //等待定时器0溢出
msg_code=msg_code<<1; //左移一位
}
DATAMSG=0;
}
//----------------------------------------------------------------------------//
// --------------发送过程----------------- //
//----------------------------------------------------------------------------//
void Transmit_Information_Process()
{
unsigned char temp=0;
unsigned char count=0;
unsigned char inde=0;
for (temp=0;temp<3;temp++)
{
LAMP_RED=0; //指示灯亮
Make_Transmit_Mode(); //进入传输模式
//Transmit_Data_Dispose(Tx_Data,65); //组织数据过程。
//Transmit_Direct_Message(MESSAGE_0101_CODE,0x10);
count=0;
timer0_count=0;
while(1) //传输头信息的时间(0.1*10)ms*2*200=400ms (360*120%=432ms)
{
for (inde=0;inde<2;inde++)
{ //等待定时器0溢出
wait_time_100us();
Transmit_Direct_Message(MESSAGE_WAKEUP_CODE,0x0A); //发送1010100001
}
count++;
if (count>=100) break;
}
wait_time_millisecond();
Transmit_Information_Package(); //延迟1毫秒
//wait_time_millisecond(); //延迟1毫秒
//Transmit_Information_Package();
//wait_time_millisecond(); //延迟1毫秒
//Transmit_Information_Package();
//-------至此,信息包发送完毕,等待应答信息到来-----------------------------//
Make_Receive_Mode(); //进入接收模式
//wait_answer_message();
}
}
//------------------------------------------------------------------------------//
// 延迟100us //
//------------------------------------------------------------------------------//
void bit_delay()
{
unsigned int inde=200;
while(inde--);
}
//--------------------------------------------------------------------------//
// 延迟1ms //
//--------------------------------------------------------------------------//
void wait_time_millisecond()
{
timer0_count=0;
TH0 = 0XF8; //定时100us
TL0 = 0X43;
while(timer0_count<10); //延1ms=10*100us
}
//-------------------------------------------------------------------------//
// 延迟10ms //
//-------------------------------------------------------------------------//
void wait_time_10millisencond()
{
timer0_count=0;
TH0 = 0XF8; //定时100us
TL0 = 0X43;
while(timer0_count<100); //延10ms=100*100us
}
void wait_time_100us()
{
timer0_count=0;
TH0 = 0XF8; //定时100us
TL0 = 0X43;
while(timer0_count<1); //延100us
}
//-------------------------------------------------------------------------//
// 传输指定信息包 //
//-------------------------------------------------------------------------//
void Transmit_Information_Package()
{
unsigned int inde=0;
unsigned char nbits=0;
unsigned char nCh=0;
DATAMSG=1; //保持1ms
wait_time_millisecond(); //等待1ms
DATAMSG=0;
wait_time_100us(); //延迟100us
nLen_Tx_Data = 10;
//传输数据长度为nLen_Tx_Data
for(inde=0;inde<10;inde++) //发送包中的所有信息
{
nCh =0x55;//OUTPUT_BUF[inde];
for(nbits=0;nbits<8;nbits++) //发送一个字节
{
if ((nCh&0x80)==0x80)
DATAMSG=1;
else DATAMSG=0;
wait_time_100us();
nCh = nCh<<1;
}
}
wait_time_100us(); //等待100us
DATAMSG=0;
}
//---------------------------------------------------------------------------//
// 接收数据信息包 //
//---------------------------------------------------------------------------//
unsigned char Receive_Information_Package()
{
unsigned char r_count=0;
unsigned int count =0;
unsigned char start_flag=0;
unsigned char ch_numbers=0;
unsigned char b_bits=0;
unsigned char nCh=0;
unsigned int inde=0;
unsigned char L_nCount=0;
unsigned char H_nCount=0;
unsigned char sample_number=0;
timer0_count=0;
start_flag=0;
Read_Wakeup_Data();
Make_Receive_Mode();
RFLAG=0;
while(count<400)
{
if (timer0_count<=10)
{
if (DATAMSG==0)
{
level_count=0;
while(DATAMSG==0)
{
if (level_count>=20)break;
}
}
if (DATAMSG==1) //在1ms中检测datamsg有无高电平到来
{
level_count=0;
TEST_PIN =1;
//如果datamsg为高电平,则最多等待1.5ms,判断是否为数据的头标志
while(DATAMSG==1)
{
if (level_count>10) break;
}
//如果在还没有到1ms的时间内,就跳变为0,则表示不是有用的起始信号,也就是说起始信号必须至少要大于等于1ms的高电平
if ((level_count>9)&&(level_count<=15))
{
TEST_PIN=0;
//wait_time_100us(); //找到合适的信号。
//level_count=0;
//while(DATAMSG==1) //等待下降沿的到来
//{
// if (level_count>=) break;
//}
//if(level_count<=1)
start_flag=1;
//TEST_PIN=1;
break;
}
// break;
//}
} //end if datamsg==1
} //end if (timer0_count<=10)
else
{
count++;
timer0_count=0;
level_count=0;
}
}
//如果找到合适的起始信号
//即在合适的时间内找到了有效的下降沿
if (start_flag)
{
start_flag=0;
wait_time_100us(); //等待100us
ch_numbers=0; //接收10个字节
b_bits = 0;
nCh = 0;
inde = 0;
while(1) //开始接收10个字节
{
if(DATACLK==0)
{
timer0_count=0;
while(DATACLK==0)
{
if (timer0_count>=2) break; //最多等待100us,如果都没有电平跳变,则强行退出。
}; //等待上升沿的到来
L_nCount=0;
H_nCount=0;
while(DATACLK==1) //如果上升沿到来了,则开始采集DATAMSG线上的数据
{
if (DATAMSG==0) //如果为低电平
{
for(sample_number=0;sample_number<30;sample_number++)
{
if (DATAMSG==0) //如果为低电平
{
H_nCount=0; //清高电平采集计数器
L_nCount++; //低电平采集计数器加1
}
else if (DATAMSG==1) L_nCount=0;
}
}
else if (DATAMSG==1) //如果为高电平
{
for(sample_number=0;sample_number<30;sample_number++)
{
if (DATAMSG==1) //如果为低电平
{ L_nCount=0; //清低电平采集计数器
H_nCount++; //高电平采集计数器加1
}
else if (DATAMSG==0) H_nCount=0;
}
}
if ((L_nCount>=20)||(H_nCount>=20)) //如果采集完毕后,采集到有效的低电平或者高电平
{
if(L_nCount>=20)
{ nCh = nCh|0x00; //如果采集到有效低电平
TEST_PIN=0;
}
else if (H_nCount>=20)
{
nCh = nCh|0x01; //如果采集到有效高电平
TEST_PIN=1;
}
if (b_bits==8) //如果计数器计数到8位,即1个字节
{
INPUT_BUF[inde++]=nCh; //压一个字节进缓冲区
b_bits = 0; //bit控制计数器清0
nCh = 0; //清字节存储器
}
nCh = nCh<<1;
b_bits++; //bit控制计数器加1
}
break; //数据采集完毕,强行退出。
} //end while(DATACLK==1)
} //end if(DATACLK==0)
if (inde>=10)break;
} //end while(1)
_nop_();
_nop_();
for(inde=0;inde<100;inde++)
INPUT_BUF[inde]=0;
_nop_();
} //end if (start_flag)
count=400;
while(count--)
{
wait_time_100us(); //等待100us
}
Make_Wakeup_Mode();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -