找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3895|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

2020年藍(lán)橋杯單片機(jī)第二場(chǎng)題目及代碼(省一等獎(jiǎng))

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

main.c程序如下(內(nèi)含注釋?zhuān)创a在附件,請(qǐng)自行下載):
#include "user.h"

bit dac_flag = 0;//
bit set_mode = 0, set_index = 1, set_error = 0;
bit temp_flag = 0;
bit set_in = 0;
bit key_flag = 0;

uchar state = 0;
uchar dsp[8] = {10, 10, 10, 10, 10, 10, 10, 10};
uchar temp_max, temp_min;
uchar t_max = 30, t_min = 20;
uchar temperature = 0;

extern uchar Cont, Trg;

void task1(void);
void Show_Temperature(void);
void task2(void);
void Show_ParamSet(void);

void main(void) {
    rd_temperature();// 系統(tǒng)初始化前讀取一次溫度
    System_init();// 系統(tǒng)初始化
    while(1) {
        switch(state) {
            case 0 : task1(); break;// 數(shù)據(jù)顯示模式
            case 1 : task2(); break;// 參數(shù)設(shè)置模式
        }
        Show_SMG(dsp);
    }
}

void task1(void) {
    if(!set_mode) {// 非設(shè)置模式
        if(temp_flag) {
            temp_flag = 0;
            temperature = rd_temperature();
        }
        Show_Temperature();// 顯示溫度
    }
    if(dac_flag) {// 判斷溫度范圍,進(jìn)行相應(yīng)的DA轉(zhuǎn)換
        dac_flag = 0;
        if(temperature > t_max) {
            IIC_Write_DAC(0x40, 209);
        } else if(temperature >= t_min && temperature <= t_max) {
            IIC_Write_DAC(0x40, 157);
        } else if(temperature < t_min) {
            IIC_Write_DAC(0x40, 106);
        }
    }
    if(key_flag) {
        key_flag = 0;
        Read_Keyboard();
        // 按下參數(shù)設(shè)置按鈕之后,將正常的范圍值賦值到臨時(shí)變量中
        // 設(shè)置錯(cuò)誤時(shí)將臨時(shí)變量中的值重新賦值給正常的范圍值
        if(Trg & Key_S4) {
            if(!set_in) {
                set_in = 1;
                temp_min = t_min;
                temp_max = t_max;
            }
            set_mode = ~set_mode;
            state = 1;// 切換到參數(shù)設(shè)置模式
        }
    }
}

void task2(void) {
    if(set_mode) {// 顯示參數(shù)設(shè)置界面
        Show_ParamSet();
    }
    if(set_in) {// 將標(biāo)志位清零
        set_in = 0;
    }
    if(key_flag) {
        key_flag = 0;
        Read_Keyboard();
        // 按下切換模式按鈕時(shí),判斷參數(shù)設(shè)置是否符合規(guī)范
        // 不符合則將原來(lái)的范圍值重新賦值,并將設(shè)置錯(cuò)誤標(biāo)志位置1
        if(Trg & Key_S4) {
            if(!(t_max >= t_min)) {
                t_max = temp_max;
                t_min = temp_min;
                set_error = 1;
            } else {
                set_error = 0;
            }
            set_mode = ~set_mode;
            state = 0;
            set_index = 1;
        }
        if(Trg & Key_S5) {
            set_index = ~set_index;
        }
        if(Trg & Key_S6) {
            if(!set_index) {// 設(shè)置上限值
                t_max +=1;
                if(t_max >= 100) {
                    t_max = 0;
                }
            } else {// 設(shè)置下限值
                t_min +=1;
                if(t_min >= 100) {
                    t_min = 0;
                }
            }
        }
        if(Trg & Key_S7) {
            if(!set_index) {// 設(shè)置上限值
                t_max -=1;
                if(t_max == 255) {
                    t_max = 99;
                }
            } else {// 設(shè)置下限值
                t_min -=1;
                if(t_min == 255) {
                    t_min = 99;
                }
            }
        }
    }
}

// 數(shù)據(jù)顯示界面
void Show_Temperature(void) {
    dsp[0] = 11;
    dsp[1] = dsp[2] = dsp[3] = dsp[4] = dsp[5] = 10;
    dsp[6] = (temperature >= 10) ? (temperature / 10 % 10) : 10;
    dsp[7] = temperature % 10;
}

// 參數(shù)設(shè)置界面
void Show_ParamSet(void) {
    dsp[0] = 12;
    dsp[1] = dsp[2] = dsp[5] = 10;
    dsp[3] = (t_max >= 10) ? (t_max / 10 % 10) : 10;
    dsp[4] = t_max % 10;
    dsp[6] = (t_min >= 10) ? (t_min / 10 % 10) : 10;
    dsp[7] = t_min % 10;
}

// 中斷服務(wù)函數(shù)
void timer_isr(void) interrupt 1 {
    static uint key_cnt, temp_cnt, adc_cnt;
    TL0 = 0x18;        //設(shè)置定時(shí)初值
    TH0 = 0xFC;        //設(shè)置定時(shí)初值
    Display_ON();
    if(set_error && (temperature > t_max)) {// 如果參數(shù)設(shè)置錯(cuò)誤,且溫度大于最大值(左邊),01101111  //0xf6
        Switch_HC138(2, 0xf6);
    } else if(!set_error && (temperature > t_max)) {// 如果參數(shù)設(shè)置正確,且溫度大于最大值(左邊),01111111  //0xfe
        Switch_HC138(2, 0xfe);
    } else if(set_error && (temperature <= t_max && temperature >= t_min)){// 如果參數(shù)設(shè)置錯(cuò)誤,且溫度在最大值和最小值的范圍之間,10101111  //0xf5
        Switch_HC138(2, 0xf5);
    } else if(!set_error && (temperature <= t_max && temperature >= t_min)){// 如果參數(shù)設(shè)置正確,且溫度在最大值和最小值的范圍之間,10111111  //0xfd
        Switch_HC138(2, 0xfd);
    } else if(set_error && (temperature < t_min)) {// 如果參數(shù)設(shè)置錯(cuò)誤,且溫度小于最小值(右邊),11001111  //0xf3
        Switch_HC138(2, 0xf3);
    } else if(!set_error && (temperature < t_min)) {// 如果參數(shù)設(shè)置正確,且溫度小于最小值(左邊),11011111  //0xf
        Switch_HC138(2, 0xfb);
    }
    if(++key_cnt >= 30) {// 獨(dú)立按鍵消抖
        key_cnt = 0;
        key_flag = 1;
    }
    if(++temp_cnt >= 250) {// 250ms檢測(cè)一次溫度
        temp_cnt = 0;
        temp_flag = 1;
    }
    if(++adc_cnt >= 250) {// 300ms進(jìn)行一次dac轉(zhuǎn)換
        adc_cnt = 0;
        dac_flag = 1;
    }
}


全部資料51hei下載地址:
2020年10月第二場(chǎng)省賽(省一).zip (1.78 MB, 下載次數(shù): 39)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表