51_LanQiaoBei/题目/真题/12 第十二届国赛_左岚/第十二届国赛/User/main.c

320 lines
6.6 KiB
C
Raw Normal View History

2025-04-13 01:02:19 +08:00
#include "main.h"
#define LIGHT_NUM 50
#define DARK_NUM 10
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uchar Key_Slow_Down; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ר<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
uchar Seg_Buf[8] = {10, 10, 10, 10, 10, 10, 10, 10}; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uchar Seg_Point[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uchar Seg_Pos; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>ר<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
uint Seg_Slow_Down; // <20><><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC><EFBFBD>ר<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
uchar ucLed[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // Led<65><64>ʾ<EFBFBD><CABE><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uchar ucRtc[3] = {0x11, 0x11, 0x11};
/*<2A><><EFBFBD><EFBFBD>*/
bit Seg_show_mode; // 0<><30><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD>
uchar data_show_mode; // 0ʱ<30><CAB1> 1<><31><EFBFBD><EFBFBD> 2<><32>¼
bit celi_show_mode; // 0ʱ<30><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bit burst_mode; // 0<><30><EFBFBD><EFBFBD>ģʽ 1<><31>ʱģʽ
uchar data_show_memory; // 0<><30><EFBFBD><EFBFBD>ֵ 1<><31>Сֵ 2ƽ<32><C6BD>ֵ
/*<2A><><EFBFBD><EFBFBD>*/
uchar collection_time = 2;
uchar collection_time_arr[5] = {2, 3, 5, 7, 9};
uchar collection_time_index;
uchar dis_demo = 10;
uint dis_value;
uint dis_max;
uint dis_min;
uint collection_count;
uchar wring_count;
bit wring_flag;
float dis_aver;
uchar light_value, light_value_old;
void init_Seg()
{
uchar i;
for (i = 0; i < 8; i++)
{
Seg_Buf[i] = 10;
Seg_Point[i] = 0;
}
}
void hide_high_Seg(uchar start_num, uchar end_num)
{
uchar i;
for (i = start_num; i < end_num; i++)
{
if (Seg_Buf[i] != 0)
break;
else
Seg_Buf[i] = 10;
}
}
/* <20><><EFBFBD>̴<EFBFBD><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void Key_Proc()
{
static uchar Key_Val, Key_Down, Key_Old, Key_Up; // <20><><EFBFBD><EFBFBD>ר<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>
if (Key_Slow_Down)
return;
Key_Slow_Down = 1; // <20><><EFBFBD>̼<EFBFBD><CCBC>ٳ<EFBFBD><D9B3><EFBFBD>
Key_Val = Key_Read(); // ʵʱ<CAB5><CAB1>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ֵ
Key_Down = Key_Val & (Key_Old ^ Key_Val); // <20><>׽<EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>
Key_Up = ~Key_Val & (Key_Old ^ Key_Val); // <20><>׽<EFBFBD><D7BD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD>
Key_Old = Key_Val; // <20><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (Key_Down == 4)
{
Seg_show_mode ^= 1;
celi_show_mode = data_show_mode = data_show_memory = 0;
init_Seg();
}
if (Key_Down == 5)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (Seg_show_mode)
{
celi_show_mode ^= 1;
init_Seg();
}
// <20><><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
else
{
data_show_mode = (++data_show_mode) % 3;
init_Seg();
}
}
if (Key_Down == 8 && Seg_show_mode == 0)
{
if (data_show_mode == 1)
burst_mode ^= 1;
else if (data_show_mode == 2)
{
data_show_memory = (++data_show_memory) % 3;
init_Seg();
}
}
if (Key_Down == 9 && Seg_show_mode == 1)
{
if (celi_show_mode)
dis_demo = (++dis_demo > 80) ? 10 : dis_demo;
else
{
collection_time_index = (++collection_time_index) % 5;
collection_time = collection_time_arr[collection_time_index];
}
}
}
/* <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void Seg_Proc()
{
uchar temp_dis;
if (Seg_Slow_Down)
return;
Seg_Slow_Down = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC>ٳ<EFBFBD><D9B3><EFBFBD>
Read_Rtc(ucRtc);
light_value = Ad_Read(0x01);
// <20><>ʱģʽ
if (burst_mode)
{
if ((ucRtc[2] / 16 * 10 + ucRtc[2] % 16) % collection_time == 0)
{
dis_value = Ut_Wave_Data();
collection_count = (++collection_count) % 65535;
dis_aver = (dis_aver * (collection_count - 1) + dis_value) / collection_count;
if (dis_value > dis_demo - 5 || dis_value < dis_demo + 5)
{
if (++wring_count > 3)
{
wring_count = 4;
}
}
else
wring_count = 0;
}
}
// <20><><EFBFBD><EFBFBD>ģʽ
else
{
// <20><>-><3E><>
if (light_value_old > LIGHT_NUM && light_value < DARK_NUM)
{
dis_value = Ut_Wave_Data();
collection_count = (++collection_count) % 65535;
dis_aver = (dis_aver * (collection_count - 1) + dis_value) / collection_count;
if (dis_value > dis_demo - 5 || dis_value < dis_demo + 5)
wring_count = (++wring_count) % 4;
else
wring_count = 0;
}
}
if (dis_max < dis_value)
dis_max = dis_value;
if (dis_min > dis_value || dis_min == 0)
dis_min = dis_value;
if (dis_value > 80)
Da_Write(255);
else if (dis_value < 10)
Da_Write(51);
else
Da_Write(dis_value / 70.0 * 4 * 51);
light_value_old = light_value;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (Seg_show_mode)
{
Seg_Buf[0] = 18; // P
Seg_Buf[1] = (uchar)celi_show_mode + 1;
if (celi_show_mode)
{
Seg_Buf[6] = collection_time / 10 % 10;
Seg_Buf[7] = collection_time % 10;
}
else
{
Seg_Buf[6] = dis_demo / 10 % 10;
Seg_Buf[7] = dis_demo % 10;
}
}
// <20><><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
else
{
switch (data_show_mode)
{
case 0:
/* ʱ<><CAB1><EFBFBD><EFBFBD>ʾ */
Seg_Buf[0] = ucRtc[0] / 16;
Seg_Buf[1] = ucRtc[0] % 16;
Seg_Buf[2] = 16;
Seg_Buf[3] = ucRtc[1] / 16;
Seg_Buf[4] = ucRtc[1] % 16;
Seg_Buf[5] = 16;
Seg_Buf[6] = ucRtc[2] / 16;
Seg_Buf[7] = ucRtc[2] % 16;
break;
case 1:
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ*/
Seg_Buf[0] = 11; // L
Seg_Buf[1] = (burst_mode) ? 12 : 13;
Seg_Buf[5] = dis_value / 100 % 10;
Seg_Buf[6] = dis_value / 10 % 10;
Seg_Buf[7] = dis_value % 10;
hide_high_Seg(5, 7);
break;
case 2:
/*<2A><><EFBFBD>ݼ<EFBFBD>¼*/
Seg_Buf[0] = 14; // H
Seg_Buf[1] = data_show_memory + 15;
switch (data_show_memory)
{
case 0:
/* <20><><EFBFBD><EFBFBD>ֵ */
Seg_Buf[4] = dis_max / 1000 % 10;
Seg_Buf[5] = dis_max / 100 % 10;
Seg_Buf[6] = dis_max / 10 % 10;
Seg_Buf[7] = dis_max % 10;
hide_high_Seg(4, 7);
break;
case 1:
/*ƽ<><C6BD>ֵ*/
temp_dis = dis_aver * 10;
Seg_Buf[4] = temp_dis / 1000 % 10;
Seg_Buf[5] = temp_dis / 100 % 10;
Seg_Buf[6] = temp_dis / 10 % 10;
Seg_Buf[7] = temp_dis % 10;
hide_high_Seg(4, 6);
Seg_Point[6] = 1;
break;
case 2:
/*<2A><>Сֵ*/
Seg_Buf[4] = dis_min / 1000 % 10;
Seg_Buf[5] = dis_min / 100 % 10;
Seg_Buf[6] = dis_min / 10 % 10;
Seg_Buf[7] = dis_min % 10;
hide_high_Seg(4, 7);
break;
}
break;
}
}
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD> */
void Led_Proc()
{
ucLed[0] = (Seg_show_mode == 0 && data_show_mode == 0);
ucLed[1] = (Seg_show_mode == 0 && data_show_mode == 1);
ucLed[2] = (Seg_show_mode == 0 && data_show_mode == 2);
ucLed[3] = !burst_mode;
if (burst_mode)
{
Beep(wring_count >= 3);
ucLed[4] = (wring_count >= 3);
}
else
{
Beep(0);
ucLed[4] = 0;
}
ucLed[5] = (light_value > LIGHT_NUM);
}
/* <20><>ʱ<EFBFBD><CAB1>0<EFBFBD>жϳ<D0B6>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void Timer0Init(void) // 1<><31><EFBFBD><EFBFBD>@12.000MHz
{
AUXR &= 0x7F; // <20><>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1>12Tģʽ
TMOD &= 0xF0; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ģʽ
TL0 = 0x18; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ʼֵ
TH0 = 0xFC; // <20><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>ʼֵ
TF0 = 0; // <20><><EFBFBD><EFBFBD>TF0<46><30>־
TR0 = 1; // <20><>ʱ<EFBFBD><CAB1>0<EFBFBD><30>ʼ<EFBFBD><CABC>ʱ
ET0 = 1; // <20><>ʱ<EFBFBD><CAB1><EFBFBD>ж<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>
EA = 1; // <20><><EFBFBD>жϴ<D0B6><CFB4><EFBFBD>
}
/* <20><>ʱ<EFBFBD><CAB1>0<EFBFBD>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void Timer0Server() interrupt 1
{
if (++Key_Slow_Down == 10)
Key_Slow_Down = 0; // <20><><EFBFBD>̼<EFBFBD><CCBC><EFBFBD>ר<EFBFBD><D7A8>
if (++Seg_Slow_Down == 500)
Seg_Slow_Down = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC><EFBFBD>ר<EFBFBD><D7A8>
if (++Seg_Pos == 8)
Seg_Pos = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾר<CABE><D7A8>
Seg_Disp(Seg_Pos, Seg_Buf[Seg_Pos], Seg_Point[Seg_Pos]);
Led_Disp(Seg_Pos, ucLed[Seg_Pos]);
}
void Delay750ms() //@12MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 35;
j = 51;
k = 182;
do
{
do
{
while (--k)
;
} while (--j);
} while (--i);
}
/* Main */
void main()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶ȶ<C2B6>ȡ<EFBFBD>Ļ<EFBFBD>
rd_temperature();
Delay750ms();
System_Init();
Timer0Init();
Set_Rtc(ucRtc);
while (1)
{
Key_Proc();
Seg_Proc();
Led_Proc();
}
}