?? radar.c
字號:
/*
***************************************************************************************************
* Copyright (C),2007
* Author : YanZhongsan
* Email : yanzhongsan@gmail.com
* Date : 2007-10-17
* File name : radar.c
* Description : Radar communication file
* Version : V 1.0
* Others : System use extral crystal oscillator 11.0592MHz
***************************************************************************************************
*/
/****head files****/
#include "includes.h"
#include "HT1621B.h"
#include "radar.h"
#include "key.h"
/***golbal data define*****/
volatile UCHAR_8 Radar_counter;
UCHAR_8 Radar[4];
UCHAR_8 RadarDisBuf[2];
UCHAR_8 Alarm;
UCHAR_8 SendBack=0xF0;
UCHAR_8 Radar_time;
/*
***************************************************************************************************
* Function name : Radar_communication
* Description : Conmunication with the host,use single wire
* Note : This function is used to receive the data from the radar host,and then send the
* : back message
* Parameters : None
* Returns : None,the routine will use the golbal data Radar[4] for store the data received
* Attribute : Public function
* Others : None
***************************************************************************************************
*/
void Radar_communication(void)
{
UCHAR_8 temp[4]={0xFF,0xFF,0xFF,0xFF};
UCHAR_8 i,j;
WDR();
if (RADAR_LINE_IS_HIGH)
{
return;
}
SET_RADAR_IN;
TIMER2_BEGIN;
SET_T2_290us;
Radar_counter=1;
while (RADAR_LINE_IS_LOW&&Radar_counter) WDR();//等待響應條件
if (RADAR_LINE_IS_LOW)
{
Back:
TIMER2_STOP;
TIMER1_BEGIN;
return ;
}
SET_RADAR_OUT;//轉I/O口為輸出
SET_RADAR_LOW;//拉低總線,響應同步
DELAY_1us(64);
SET_RADAR_HIGH;//拉高總線等待開始位
SET_RADAR_IN;//轉I/O口為輸入
SET_T2_70us;
Radar_counter=1;
while (RADAR_LINE_IS_HIGH&&Radar_counter);//等待起始位
if (0==Radar_counter)
{
goto Back;
}
DELAY_1us(40);
TIMER1_STOP;
for (i=0;i<4;i++)
{
WDR();
for (j=0;j<8;j++)
{
temp[i]<<=1;
SET_T2_70us;
Radar_counter=1;
if (RADAR_LINE_IS_HIGH)
{
while (RADAR_LINE_IS_HIGH&&Radar_counter);
if (0==Radar_counter)
{
goto Back;
}
temp[i] &= 0xFE;
}
else
{
while (RADAR_LINE_IS_LOW&&Radar_counter);
if (0==Radar_counter)
{
goto Back;
}
temp[i] |= 0x01;
}
DELAY_1us(35);//如果無法通信或通信不良請調節此處
}
}
i=temp[0]+temp[1]+temp[2];
if (i!=temp[3])
{
goto Back;
}
SET_RADAR_OUT;
SET_RADAR_HIGH;
DELAY_1us(10);
WDR();
SET_RADAR_LOW;
SET_T2_32us;
Radar_counter=1;
while (Radar_counter);
j=SendBack;
for (i=0;i<8;i++)
{
if (j&0x80)
{
SET_RADAR_LOW;
SET_T2_32us;
Radar_counter=1;
while (Radar_counter);
SET_RADAR_HIGH;
SET_T2_32us;
Radar_counter=1;
while (Radar_counter);
}
else
{
SET_RADAR_HIGH;
SET_T2_32us;
Radar_counter=1;
while (Radar_counter);
SET_RADAR_LOW;
SET_T2_32us;
Radar_counter=1;
while (Radar_counter);
}
j<<=1;
}
SET_RADAR_HIGH;
SET_RADAR_IN;
TIMER2_STOP;
TIMER1_BEGIN;
for (j=0;j<4;j++)
{
Radar[j]=temp[j];
}
Radar_data_trade();
return ;
}
/*
***************************************************************************************************
* Function name : Radar_data_trade
* Description : 雷達數據處理函數,根據接收到的數據更新報警級別以及顯示
* Note : None
* Parameters : None
* Returns : None
* Attribute : Public function
* Others : None
***************************************************************************************************
*/
void Radar_data_trade(void)
{
UCHAR_8 i,j,k;
//dis[0]: bit0:T1 bit1:T2 bit2:T3 bit3:T4 bit4:T5 bit5:T6 bit6:T7 bit7:T8
//dis[1]: bit0:T9 bit1:T10 bit2:T11 bit3:T12
UCHAR_8 dis[2];
WDR();
RadarKey();
Radar_time = 25;
if (TESTBIT(SysFlag,Fir_Bit))
{
CLEARBIT(SysFlag,Fir_Bit);
HT1621BClearAll();
}
if ((26<(Radar[1]&0x1F))&&(0x0F==(Radar[2]&0x0F)))
{
switch (Radar[0]&0x30)
{
case 0x00:
SendBack = 0xCC;
break;
case 0x10:
SendBack = 0xDD;
break;
case 0x20:
SendBack = 0xEE;
break;
case 0x30:
SendBack = 0xFF;
break;
default:
SendBack = 0xCC;
break;
}
if (TESTBIT(Radar[2]&0xF0,4))
{
HT1621BWritedata(0x02,LED_CODE[10]);//左中探頭錯誤
}
else
{
HT1621BWritedata(0x02,0x02);//左中探頭正常
}
if (TESTBIT(Radar[2]&0xF0,5))
{
HT1621BWritedata(0x04,LED_CODE[10]);//右中探頭錯誤
}
else
{
HT1621BWritedata(0x04,0x02);//右中探頭正常
}
if (TESTBIT(Radar[2]&0xF0,6))
{
HT1621BWritedata(0x00,LED_CODE[10]);//左一探頭錯誤
}
else
{
HT1621BWritedata(0x00,0x02);//左一探頭正常
}
if (TESTBIT(Radar[2]&0xF0,7))
{
HT1621BWritedata(0x06,LED_CODE[10]);//右一探頭錯誤
}
else
{
HT1621BWritedata(0x06,0x02);//右一探頭正常
}
return ;
}
i=Radar[1]&0x1F;//左邊距離
j=Radar[2]&0x1F;//右邊距離
(i>j)?(k=j):(k=i);
if (k>=16)
{
Alarm=0;
}
else if (k>=10)
{
Alarm=3;
}
else if (k>=6)
{
Alarm=2;
}
else
{
Alarm=1;
}
if (TESTBIT(Radar[0],0))
{
//倒車數據
if (i>=16)//左邊距離
{
dis[0] = 0x00;
dis[1] = 0x00;
}
else if (i>=10)
{
dis[0] = 0x00;
dis[1] = 0x01;
}
else if (i>=6)
{
dis[0] = 0x80;
dis[1] = 0x01;
}
else
{
dis[0] = 0xC0;
dis[1] = 0x01;
}
if (j>=16)//右邊距離
{
dis[0] |= 0x00;
}
else if (j>=10)
{
dis[1] |= 0x08;
}
else if (j>=6)
{
dis[1] |= 0x0C;
}
else
{
dis[1] |= 0x0E;
}
}
else
{
//前行數據
dis[1] = 0x00;
if (i>=16)//左邊距離
{
dis[0] = 0x00;
}
else if (i>=10)
{
dis[0] = 0x04;
}
else if (i>=6)
{
dis[0] = 0x06;
}
else
{
dis[0] = 0x07;
}
if (j>=16)//右邊距離
{
dis[0] |= 0x00;
}
else if (j>=10)
{
dis[0] |= 0x20;
}
else if (j>=6)
{
dis[0] |= 0x30;
}
else
{
dis[0] |= 0x38;
}
}
if (k>=26)
{
HT1621BWritedata(0x00,0x82);
HT1621BWritedata(0x02,0x82);
HT1621BWritedata(0x04,0x02);
HT1621BWritedata(0x06,0x82);
}
else
{
HT1621BWritedata(0x00,0x80);
HT1621BWritedata(0x02,LED_CODE[k/10]);
HT1621BWritedata(0x04,LED_CODE[k%10]&0x7F);
HT1621BWritedata(0x06,0x80);
}
HT1621BWritedata(0x18,dis[0]);
HT1621BWritedata(0x1A,dis[1]);
if (TESTBIT(SysFlag,Mute_Bit))
{
HT1621BWritedata(20,0x00);//顯示十位數,不顯示喇叭
}
else
{
HT1621BWritedata(20,0x80);//顯示十位數,顯示喇叭
}
}
/*
***************************************************************************************************
* Function name : RadarAlarm
* Description : 無源蜂鳴器報警
* Note : 僅由定時器0的20ms中斷函數調用此函數
* Parameters : const UCHAR_8 * const addr:中斷計時函數中的20ms計數器指針
* Returns : None
* Attribute : Spcial function just for timer0 interrupt
* Others : None
***************************************************************************************************
*/
void RadarAlarm(const UCHAR_8 * const time_p)
{
if (TESTBIT(Radar[0],7)&&(!TESTBIT(SysFlag,Mute_Bit)))//主機沒有語音芯片,由顯示屏蜂鳴器完成報警音提示
{
switch (Alarm)
{
case 0:
TIMER0_STOP;
break;
case 1:
TIMER0_BEGIN;
break;
case 2:
if (0x00==(*time_p)%5)
{
if (TIMER0_IS_RUNNING)
{
TIMER0_STOP;
}
else
{
TIMER0_BEGIN;
}
}
break;
case 3:
if (0x00==(*time_p)%10)
{
if (TIMER0_IS_RUNNING)
{
TIMER0_STOP;
}
else
{
TIMER0_BEGIN;
}
}
break;
case 4:
if (0x00==(*time_p)%15)
{
if (TIMER0_IS_RUNNING)
{
TIMER0_STOP;
}
else
{
TIMER0_BEGIN;
}
}
break;
default:
break;
}
}
else
{
TIMER0_STOP;
}
}
/*
***************************************************************************************************
* Function name : RadarKey
* Description : Radar key trade
* Note : 當主機有語音芯片時,按鍵調節主機語音,當主機沒有語音芯片時,按鍵調節屏幕報警音
* Parameters : None
* Returns : None
* Attribute : Public function
* Others : None
***************************************************************************************************
*/
void RadarKey(void)
{
if (TESTBIT(ButtonFlag,KEY3_BIT))
{
ButtonFlag = 0;
if (TESTBIT(Radar[0],7))//主機沒有語音芯片,由顯示屏蜂鳴器完成報警音提示
{
if (TESTBIT(SysFlag,Mute_Bit))
{
CLEARBIT(SysFlag,Mute_Bit);
}
else
{
SETBIT(SysFlag,Mute_Bit);
}
}
else
{
SendBack += 0x11;
if (SendBack<0xCC)
{
SendBack = 0xCC;
SETBIT(SysFlag,Mute_Bit);//設置系統靜音標志
}
else
{
CLEARBIT(SysFlag,Mute_Bit);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -