51hei.png (90.37 KB, 下載次數: 69)
下載附件
2020-11-7 16:21 上傳
main.c程序如下(內含注釋,源代碼在附件,請自行下載):
#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();// 系統初始化前讀取一次溫度
System_init();// 系統初始化
while(1) {
switch(state) {
case 0 : task1(); break;// 數據顯示模式
case 1 : task2(); break;// 參數設置模式
}
Show_SMG(dsp);
}
}
void task1(void) {
if(!set_mode) {// 非設置模式
if(temp_flag) {
temp_flag = 0;
temperature = rd_temperature();
}
Show_Temperature();// 顯示溫度
}
if(dac_flag) {// 判斷溫度范圍,進行相應的DA轉換
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();
// 按下參數設置按鈕之后,將正常的范圍值賦值到臨時變量中
// 設置錯誤時將臨時變量中的值重新賦值給正常的范圍值
if(Trg & Key_S4) {
if(!set_in) {
set_in = 1;
temp_min = t_min;
temp_max = t_max;
}
set_mode = ~set_mode;
state = 1;// 切換到參數設置模式
}
}
}
void task2(void) {
if(set_mode) {// 顯示參數設置界面
Show_ParamSet();
}
if(set_in) {// 將標志位清零
set_in = 0;
}
if(key_flag) {
key_flag = 0;
Read_Keyboard();
// 按下切換模式按鈕時,判斷參數設置是否符合規范
// 不符合則將原來的范圍值重新賦值,并將設置錯誤標志位置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) {// 設置上限值
t_max +=1;
if(t_max >= 100) {
t_max = 0;
}
} else {// 設置下限值
t_min +=1;
if(t_min >= 100) {
t_min = 0;
}
}
}
if(Trg & Key_S7) {
if(!set_index) {// 設置上限值
t_max -=1;
if(t_max == 255) {
t_max = 99;
}
} else {// 設置下限值
t_min -=1;
if(t_min == 255) {
t_min = 99;
}
}
}
}
}
// 數據顯示界面
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;
}
// 參數設置界面
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;
}
// 中斷服務函數
void timer_isr(void) interrupt 1 {
static uint key_cnt, temp_cnt, adc_cnt;
TL0 = 0x18; //設置定時初值
TH0 = 0xFC; //設置定時初值
Display_ON();
if(set_error && (temperature > t_max)) {// 如果參數設置錯誤,且溫度大于最大值(左邊),01101111 //0xf6
Switch_HC138(2, 0xf6);
} else if(!set_error && (temperature > t_max)) {// 如果參數設置正確,且溫度大于最大值(左邊),01111111 //0xfe
Switch_HC138(2, 0xfe);
} else if(set_error && (temperature <= t_max && temperature >= t_min)){// 如果參數設置錯誤,且溫度在最大值和最小值的范圍之間,10101111 //0xf5
Switch_HC138(2, 0xf5);
} else if(!set_error && (temperature <= t_max && temperature >= t_min)){// 如果參數設置正確,且溫度在最大值和最小值的范圍之間,10111111 //0xfd
Switch_HC138(2, 0xfd);
} else if(set_error && (temperature < t_min)) {// 如果參數設置錯誤,且溫度小于最小值(右邊),11001111 //0xf3
Switch_HC138(2, 0xf3);
} else if(!set_error && (temperature < t_min)) {// 如果參數設置正確,且溫度小于最小值(左邊),11011111 //0xf
Switch_HC138(2, 0xfb);
}
if(++key_cnt >= 30) {// 獨立按鍵消抖
key_cnt = 0;
key_flag = 1;
}
if(++temp_cnt >= 250) {// 250ms檢測一次溫度
temp_cnt = 0;
temp_flag = 1;
}
if(++adc_cnt >= 250) {// 300ms進行一次dac轉換
adc_cnt = 0;
dac_flag = 1;
}
}
51hei.png (13.44 KB, 下載次數: 73)
下載附件
2020-11-7 16:21 上傳
全部資料51hei下載地址:
2020年10月第二場省賽(省一).zip
(1.78 MB, 下載次數: 39)
2020-11-7 13:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|