⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 radar.c

📁 倒车雷达的原理图与源码
💻 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 + -