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

📄 init.c

📁 倒车雷达的原理图与源码
💻 C
字号:
/*
***************************************************************************************************
* Copyright (C),2007
* Author        : YanZhongsan
* Email         : yanzhongsan@gmail.com
* Date          : 2007-10-17
* File name     : init.c
* Description   : System init file
* Version       : V 1.0
* Others        : System use extral crystal oscillator 11.0592MHz
***************************************************************************************************
*/

#include "includes.h"

#include "DS1302.h"
#include "DS18B20.h"
#include "HT1621B.h"
#include "radar.h"
#include "key.h"

/*
***************************************************************************************************
* Function Name : Init_devices
* Description   : Init the MCU's special function register
* Input         : None
* Output        : None
* Others        : 必须在此函数内开中断允许(SEI),系统加电启动默认为关中断
***************************************************************************************************
*/
void Init_devices(void)
{
    CLI();

    Init_port();

    Init_timer0();

    Init_timer1();

    Init_timer2();

    Init_HT1621B();

    Init_DS1302();

    Init_WDT();

    MCUCR = 0x00;
    EICRA = 0x00; //extended ext ints
    EIMSK = 0x00;

    TIMSK0 = 0x01; //timer 0 interrupt sources
    TIMSK1 = 0x01; //timer 1 interrupt sources
    TIMSK2 = 0x01; //timer 2 interrupt sources

    PCMSK0 = 0x00; //pin change mask 0
    PCMSK1 = 0x00; //pin change mask 1
    PCMSK2 = 0x00; //pin change mask 2
    PCICR = 0x00; //pin change enable
    PRR = 0x00; //power controller
    SEI(); //re-enable interrupts
    //all peripherals are now initialized
}

/*
***************************************************************************************************
* Function Name : Init_port
* Description   : Init the MCU's Input&Output port
* Input         : None
* Output        : None
* Others        : None
***************************************************************************************************
*/
void Init_port(void)
{
    DDRB  = BUZZER;
    PORTB = RADAR_LINE|TEMP_LINE|BUZZER;

    DDRC  = HT_CS|HT_DATA|HT_WR|DS_RST|DS_SCLK;
    PORTC = 0x00;

    DDRD  = BACK_LIGHT|HT_RD;
    PORTD = UP_BUTTON|DOWN_BUTTON|SET_BUTTON;
}

/*
***************************************************************************************************
* Function Name : Init_timer2
* Description   : Init the MCU's timer2
* Input         : None
* Output        : None
* Others        : None
* Prescale      : 32
* WGM           : Normal
* Value         : set
* Extral crystal oscillator: 11.0592MHz
***************************************************************************************************
*/
void Init_timer2(void)
{
    TCCR2B = 0x00; //stop
    ASSR  = 0x00; //set async mode
    TCNT2 = 0x9C; //setup
    OCR2A = 0x64;
    OCR2B = 0x00;
    TCCR2A = 0x00;
    //TCCR2B = 0x03; //start
}

#pragma vector=TIMER2_OVF_vect
__interrupt void timer2_ovf_isr(void)
{
    Radar_counter=0;
}

/*
***************************************************************************************************
* Function Name : Init_timer1
* Description   : Init the MCU's timer1
* Input         : None
* Output        : None
* Others        : None
* Prescale      : 64
* WGM           : Normal, TOP=0xFFFF
* Value         : desired value:20ms, actual value:19.996ms
* Extral crystal oscillator: 11.0592MHz
***************************************************************************************************
*/
void Init_timer1(void)
{
    TCCR1B = 0x00; //stop
    TCNT1H = 0xF2; //setup
    TCNT1L = 0x81;
    OCR1AH = 0x0D;
    OCR1AL = 0x7F;
    OCR1BH = 0x0D;
    OCR1BL = 0x7F;
    ICR1H  = 0x0D;
    ICR1L  = 0x7F;
    TCCR1A = 0x00;
    TCCR1B = 0x03; //start Timer
}

void Timer1_counter(void)
{
    static UCHAR_8 time_counter=250;

    time_counter--;

    if (0x00==time_counter)
    {
        time_counter = 250;//最大计时为250*20ms
    }

    if (0x00!=Radar_time)
    {
        Radar_time--;
    }

    if (0x00==time_counter%25)//500ms
    {
        SETBIT(SysFlag,Time_500ms_Bit);
    }
    if (0x00==time_counter%50)//1s
    {
        if (0x00!=SetTimeOver)
        {
            SetTimeOver--;
        }
    }

    RadarAlarm(&time_counter);
}
#pragma vector=TIMER1_OVF_vect
__interrupt void timer1_ovf_isr(void)
{
    TCNT1 = 0xF281;//reload the value

    SEI();

    Timer1_counter();

    KeyBoardScanf();
}

/*
***************************************************************************************************
* Function Name : Init_timer0
* Description   : Init the MCU's timer0
* Input         : None
* Output        : None
* Others        : None
* Prescale      : 1024
* WGM           : Normal
* Actual value  : 280us
* Extral crystal oscillator: 11.0592MHz
***************************************************************************************************
*/
void Init_timer0(void)
{
     TCCR0B = 0x00; //stop
     TCNT0  = 0xFD; //set count
     TCCR0A = 0x00;
     //TCCR0B = 0x05; //start timer
}

#pragma vector=TIMER0_OVF_vect
__interrupt void Timer0Over(void)
{
    TCNT0 = 0xFC;

    if (TESTBIT(BUZZER_PORT,BUZZER_BIT))
    {
        CLEARBIT(BUZZER_PORT,BUZZER_BIT);
    }
    else
    {
        SETBIT(BUZZER_PORT,BUZZER_BIT);
    }
}

/*
***************************************************************************************************
* Function Name : Init_WDT
* Description   : Init the watchdog
* Input         : None
* Output        : None
* Others        : None
***************************************************************************************************
*/
void Init_WDT(void)
{
    //write 1 to WDCE and WDE
    WDTCSR = (1u<<WDCE)|(1u<<WDE);
    //set over time 4s
    WDTCSR = (0u<<WDIF)|(0u<<WDIE)|(0u<<WDP3)|(0u<<WDCE)|(1u<<WDE)|(1u<<WDP2)|(1u<<WDP1)|(1u<<WDP0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -