二刷第十四届省赛,本来以为要修修改改反复提交几次,没想到一次就过了。十四届省赛的细节特别多,难度可以跟国赛媲美了。
细节:
- 数码管小数点什么时候使能
- 各按键功能在什么时候有效
- 防止数据采集重复触发
- 数据采集时所有按键无效
- 长按S9的功能只清除所有回显数据,不是回到初始条件
- 数据采集触发时要考虑湿度数据发生变化不能对显示产生影响(这边的湿度数据发生变化是指湿度数据从无效到有效/从有效到无效)
附件:第十四届蓝桥杯单片机省赛
一、页面流转
由图,可以定义MainMode
控制主页面流转(0-时间页面 1-温湿度页面)
定义SegMode
控制S4
按键有关的三个页面(0-时间页面 1-温度回显页面 2-参数页面)
定义SetMode
控制S5
按键有关的三个回显子页面(0-温度回显子页面 1-湿度回显子页面 2-时间回显子页面)
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long int u32;
idata u8 MainMode; //主页面 0-数据主页面 1-温湿度页面
idata u8 SegMode; //0-时间 1-回显 2-参数
idata u8 SetMode; //0-温度回显 1-湿度回显 2-时间回显
触发采集时,在按键处理函数直接退出就可以使按键失灵,页面流转代码如下:
void KeyProc()
{
KeyVal = KeyDisp();
KeyDown = KeyVal & ~KeyOld;
KeyUp = ~KeyVal & KeyOld;
KeyOld = KeyVal;
if(MainMode)//触发采集时退出按键函数
return;
switch(KeyDown)
{
case 4:
if(++SegMode == 3)
SegMode = 0;
if(SegMode == 2)
SetMode = 0;
break;
case 5:
if(SegMode == 1)
if(++SetMode == 3)
SetMode = 0;
break;
}
}
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间显示页面
break;
case 1://回显页面
switch(SetMode)
{
case 0://温度回显子页面
break;
case 1://湿度回显子页面
break;
case 2://时间回显子页面
break;
}
break;
case 2://参数设置页面
break;
}
}
else//温湿度页面
{
}
}
二、触发采集
1.触发采集条件
暗状态时,光敏电阻电压小于1V,在电压处理函数中进行触发采集的判断以及数据采集。
/*频率*/
unsigned int Freq, Time_1s;
/*电压*/
unsigned int RD1_100x; //光敏电压值放大100倍
unsigned int RD1_100x_old; //上一次光敏电压值
unsigned int Time_3s; //触发采集时计时3s
bit CaptureFlag; //触发采集标志位
bit DateCaptureFlag; //触发采集时,防止数据重复采集 0-有效 1-无效
void ADProc()
{
RD1_100x = AdRead() / 51.0 * 100;
if(RD1_100x < 100 && RD1_100x_old >= 100 && !CaptureFlag)
{
CaptureFlag = 1;//采集触发
MainMode = 1;//切换到温湿度页面
DateCaptureFlag = 0;//防止数据重复采集
}
RD1_100x_old = RD1_100x;
}
void Timer1_Isr(void) interrupt 3
{
if(++Time_1s == 1000)
{
Time_1s = 0;
Freq = (TH0 << 8) | TL0;
TH0 = TL0 = 0;
}
/*数据捕获处理*/
if(CaptureFlag)//采集触发
{
if(++Time_3s >= 3000)
{
Time_3s = 0;//清空计时变量
CaptureFlag = 0;//采集未触发
MainMode = 0;//3s后回到原页面
}
}
else
{
Time_3s = 0;
}
}
2.参数回显
触发采集时需要采集的数据有:触发采集时的湿度、温度、触发次数、最大温度、最大湿度、平均温度、平均湿度、触发采集时间。
可以定义以下变量:
/*数据捕获*/
idata bit CaptureFlag; //触发采集标志位
idata bit DateCaptureFlag; //触发采集时,防止数据重复采集 0-有效 1-无效
idata u16 freq_capture; //本次数据采集时读取的频率
idata u8 tem_capture; //本次数据采集时读取的温度
//idata u8 tem_capture_old=99; //上次数据采集时读取的温度
idata u8 humidity_capture; //本次数据采集时读取的湿度
//idata u8 humidity_capture_old=99; //上次数据采集时读取的湿度
idata u8 CaptureCount; //数据采集次数
idata u8 tem_capture_max; //数据采集中读取的最大温度
idata u8 tem_capture_sum; //数据采集中读取的温度总和
idata float tem_capture_ave; //数据采集中读取的平均温度
idata u8 humidity_capture_max; //数据采集中读取的最大湿度
idata u8 humidity_capture_sum; //数据采集中读取的湿度总和
idata float humidity_capture_ave; //数据采集中读取的平均湿度
pdata u8 Rtc_capture[2]; //数据采集时间
//idata bit tem_upper, hum_upper; //温度、湿度升高标志位
void ADProc()
{
RD1_100x = AdRead() / 51.0 * 100;
if(RD1_100x < 100 && RD1_100x_old >= 100 && !CaptureFlag)
{
CaptureFlag = 1;
MainMode = 1;
DateCaptureFlag = 0;//防止数据重复采集
}
RD1_100x_old = RD1_100x;//保存上一次的RD1值
if(CaptureFlag && !DateCaptureFlag)//触发采集并且本次触发采集第一次读取数据
{
/*频率处理*/
freq_capture = Freq;//读取当前的频率
HumidityFlag = (freq_capture < 200 || freq_capture > 2000);//无效数据
if(!HumidityFlag)//如果是有效数据进行湿度转换
{
Humidity = 2.0/45*freq_capture+10-400.0/45;
}
DateCaptureFlag = 1;//本次数据已经采集,这行代码也可以放在最后
tem_capture = tem;//读取温度
humidity_capture = Humidity;//读取湿度
if(!HumidityFlag)//湿度数据有效,正常采集数据
{
GetRtc(Rtc_capture);//读取当前时间
CaptureCount++;//采集次数加1
if(tem_capture > tem_capture_max)
tem_capture_max = tem_capture;//逐个比较,温度最大值
tem_capture_sum += tem_capture;//温度总和
tem_capture_ave = tem_capture_sum*1.0/CaptureCount;//温度平均值
if(humidity_capture > humidity_capture_max)
humidity_capture_max = humidity_capture;//逐个比较,湿度最大值
humidity_capture_sum += humidity_capture;//湿度总和
humidity_capture_ave = humidity_capture_sum*1.0/CaptureCount;//湿度平均值
}
}
}
三、数码管
有了上面的回显数据,数码管处理起来就很简单了。
1.数码管底层
#include <STC15F2K60S2.H>
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xff, //空
0xbf, //-
0xc6, //C
0x89, //H
0x8e, //F
0x8c, //P
0x86, //E
0x88 //A
};
void SegDisp(unsigned char wela, unsigned char dula, unsigned char point)
{
P0 = 0xff;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;
P0 = (0x01 << wela);
P2 = P2 & 0x1f | 0xc0;
P2 &= 0x1f;
P0 = Seg_Table[dula];
if(point)
P0 &= 0x7f;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;
}
2.时间页面
/*时间*/
pdata u8 Rtc[3] = {0x23,0x59,0x50};
void RtcProc()//读取当前时间
{
GetRtc(Rtc);
}
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间页面
SegBuf[2] = SegBuf[5] = 11;
for(i = 0; i < 3; i++)
{
SegBuf[3*i] = Rtc[i] / 16;
SegBuf[3*i+1] = Rtc[i] % 16;
}
break;
}
}
}
void main()
{
SetRtc(Rtc);//设置DS1302时间
while(1);
}
3.参数回显页面
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间页面
//...
break;
case 1:
switch(SetMode)
{
case 0:
SegBuf[0] = 12;//C
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? tem_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? tem_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)tem_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)tem_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(tem_capture_ave * 10) % 10 : 10;
break;
case 1:
SegBuf[0] = 13;//H
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? humidity_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? humidity_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)humidity_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)humidity_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(humidity_capture_ave * 10) % 10 : 10;
break;
case 2:
SegBuf[0] = 14;//F
SegBuf[1] = CaptureCount / 10;
SegBuf[2] = CaptureCount % 10;
SegBuf[3] = CaptureCount ? Rtc_capture[0] / 16 : 10;
SegBuf[4] = CaptureCount ? Rtc_capture[0] % 16 : 10;
SegBuf[5] = CaptureCount ? 11 : 10;
SegBuf[6] = CaptureCount ? Rtc_capture[1] / 16 : 10;
SegPoint[6] = 0;
SegBuf[7] = CaptureCount ? Rtc_capture[1] % 16 : 10;
break;
}
break;
}
}
}
4.参数设置页面
idata u8 tem_set = 30; //温度参数,默认30,范围0~99
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间页面
//...
break;
case 1:
//...
break;
case 2:
SegBuf[0] = 15;//P
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = 10;
SegBuf[4] = 10;
SegBuf[5] = 10;
SegBuf[6] = tem_set / 10;
SegPoint[6] = 0;
SegBuf[7] = tem_set % 10;
break;
}
}
}
5.温湿度页面
void SegProc()
{
if(!MainMode)//数据主页面
{
//...
}
else//温湿度页面
{
SegBuf[0] = 16;//E
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = tem_capture / 10 % 10;
SegBuf[4] = tem_capture % 10;
SegBuf[5] = 11;//-
SegBuf[6] = HumidityFlag ? 17 : humidity_capture / 10;//湿度数据有效正常显示湿度,无效显示A
SegBuf[7] = HumidityFlag ? 17 : humidity_capture % 10;//湿度数据有效正常显示湿度,无效显示A
SegPoint[6] = 0;
}
}
6.完整代码
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间页面
SegBuf[2] = SegBuf[5] = 11;
for(i = 0; i < 3; i++)
{
SegBuf[3*i] = Rtc[i] / 16;
SegBuf[3*i+1] = Rtc[i] % 16;
}
break;
case 1:
switch(SetMode)
{
case 0:
SegBuf[0] = 12;//C
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? tem_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? tem_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)tem_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)tem_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(tem_capture_ave * 10) % 10 : 10;
break;
case 1:
SegBuf[0] = 13;//H
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? humidity_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? humidity_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)humidity_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)humidity_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(humidity_capture_ave * 10) % 10 : 10;
break;
case 2:
SegBuf[0] = 14;//F
SegBuf[1] = CaptureCount / 10;
SegBuf[2] = CaptureCount % 10;
SegBuf[3] = CaptureCount ? Rtc_capture[0] / 16 : 10;
SegBuf[4] = CaptureCount ? Rtc_capture[0] % 16 : 10;
SegBuf[5] = CaptureCount ? 11 : 10;
SegBuf[6] = CaptureCount ? Rtc_capture[1] / 16 : 10;
SegPoint[6] = 0;
SegBuf[7] = CaptureCount ? Rtc_capture[1] % 16 : 10;
break;
}
break;
case 2:
SegBuf[0] = 15;//P
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = 10;
SegBuf[4] = 10;
SegBuf[5] = 10;
SegBuf[6] = tem_set / 10;
SegPoint[6] = 0;
SegBuf[7] = tem_set % 10;
break;
}
}
else
{
SegBuf[0] = 16;//E
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = tem_capture / 10 % 10;
SegBuf[4] = tem_capture % 10;
SegBuf[5] = 11;//-
SegBuf[6] = HumidityFlag ? 17 : humidity_capture / 10;
SegBuf[7] = HumidityFlag ? 17 : humidity_capture % 10;
SegPoint[6] = 0;
}
}
四、按键
/*按键*/
idata u8 KeyVal,KeyDown,KeyUp,KeyOld;
idata bit KeyPressFlag; //按键长按标志位
idata u16 Time_2s; //计时2s
void KeyProc()
{
KeyVal = KeyDisp();
KeyDown = KeyVal & ~KeyOld;
KeyUp = ~KeyVal & KeyOld;
KeyOld = KeyVal;
if(MainMode)
return;
if(KeyDown == 9 && SetMode == 2)//处于时间回显子页面并且按键S9按下
KeyPressFlag = 1;//开始计时
if(KeyUp == 9)//松手
{
KeyPressFlag = 0;//停止计时
if(Time_2s == 2000)//长按
{
//变量清零
CaptureFlag = DateCaptureFlag = tem_capture = humidity_capture = CaptureCount = 0;
tem_capture_max = tem_capture_sum = tem_capture_ave = 0;
humidity_capture_max = humidity_capture_sum = humidity_capture_ave = 0;
Rtc_capture[0] = Rtc_capture[1] = 0;
freq_capture = HumidityFlag = 0;
}
}
switch(KeyDown)
{
case 4:
if(++SegMode == 3)
SegMode = 0;
if(SegMode == 2)
SetMode = 0;
break;
case 5:
if(SegMode == 1)
if(++SetMode == 3)
SetMode = 0;
break;
case 8:
if(SegMode == 2)
if(++tem_set == 100)
tem_set = 99;
break;
case 9:
if(SegMode == 2)
if(--tem_set == 255)
tem_set = 0;
break;
}
}
void Timer1_Isr(void) interrupt 3
{
/*按键长按*/
if(KeyPressFlag)
{
if(++Time_2s >= 2000)
Time_2s = 2000;
}
else
Time_2s = 0;
}
五、指示灯
1.页面指示灯
void LedProc()
{
if(!MainMode)
{
ucLed[0] = !SegMode;
ucLed[1] = (SegMode == 1);
}
ucLed[2] = MainMode;
}
2.报警指示灯
- L4、L5
/*LED*/
pdata u8 ucLed[8] = {0,0,0,0,0,0,0,0};
idata bit L4Flag; //L4使能
idata bit LedFlash; //闪烁标志位
idata u8 Time_100ms; //100ms闪烁
void TemProc()//温度处理函数
{
tem = TemRead();
L4Flag = (tem > tem_set);
}
void LedProc()
{
ucLed[3] = LedFlash;
ucLed[4] = HumidityFlag;
LedDisp(ucLed);
}
void Timer1_Isr(void) interrupt 3
{
/*指示灯闪烁*/
if(L4Flag)
{
if(++Time_100ms == 100)
{
Time_100ms = 0;
LedFlash = !LedFlash;
}
}
else
LedFlash = 0;
}
- L6
L6点亮前提条件是触发次数至少2次,所以可以通过给上一次采集到的数据初始化为99就可以保证第一次温度、湿度均升高时L6不点亮。
idata bit L6Flag; //L6使能
idata u8 tem_capture_old=99; //上次数据采集时读取的温度
idata u8 humidity_capture_old=99; //上次数据采集时读取的湿度
idata bit tem_upper, hum_upper; //温度、湿度升高标志位
void ADProc()
{
//...
if(!HumidityFlag)//湿度数据有效,正常采集数据
{
tem_upper = tem_capture > tem_capture_old;
hum_upper = humidity_capture > humidity_cpture_old;
L6Flag = tem_upper & hum_upper;
//...
humidity_capture_old = humidity_capture;
}
}
void LedProc()
{
ucLed[5] = L6Flag;
LedDisp(ucLed);
}
六、伪代码
#include <STC15F2K60S2.H>
#include <Init.h>
#include <Led.h>
#include <Key.h>
#include <Seg.h>
#include <ds18b20.h>
#include <ds1302.h>
#include <iic.h>
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long int u32;
/*按键*/
idata u8 KeyVal,KeyDown,KeyUp,KeyOld;
idata bit KeyPressFlag; //按键长按标志位
idata u16 Time_2s; //计时2s
/*数码管*/
idata u8 SegPos;
pdata u8 SegBuf[8] = {10,10,10,10,10,10,10,10};
pdata u8 SegPoint[8] = {0,0,0,0,0,0,0,0};
idata u8 MainMode; //主页面 0-数据主页面 1-温湿度页面
idata u8 SegMode; //0-时间 1-回显 2-参数
idata u8 SetMode; //0-温度回显 1-湿度回显 2-时间回显
/*LED*/
pdata u8 ucLed[8] = {0,0,0,0,0,0,0,0};
idata bit L4Flag; //L4使能
idata bit LedFlash; //闪烁标志位
idata u8 Time_100ms; //100ms闪烁
idata bit L6Flag; //L6使能
/*频率*/
idata u16 Freq, Time_1s;
idata u8 Humidity; //湿度
idata u8 HumidityFlag; //湿度是否有效 0-有效 1-无效
/*时间*/
pdata u8 Rtc[3] = {0x23,0x59,0x50};
/*电压*/
idata u16 RD1_100x; //光敏电压值放大100倍
idata u16 RD1_100x_old; //上一次光敏电压值
idata u16 Time_3s; //触发采集时计时3s
/*温度*/
idata u8 tem; //实时温度 保留整数
idata u8 tem_set = 30; //温度参数,默认30,范围0~99
/*数据捕获*/
idata bit CaptureFlag; //触发采集标志位
idata bit DateCaptureFlag; //触发采集时,防止数据重复采集 0-有效 1-无效
idata u16 freq_capture;
idata u8 tem_capture; //本次数据采集时读取的温度
idata u8 tem_capture_old=99; //上次数据采集时读取的温度
idata u8 humidity_capture; //本次数据采集时读取的湿度
idata u8 humidity_capture_old=99; //上次数据采集时读取的湿度
idata u8 CaptureCount; //数据采集次数
idata u8 tem_capture_max; //数据采集中读取的最大温度
idata u8 tem_capture_sum; //数据采集中读取的温度总和
idata float tem_capture_ave; //数据采集中读取的平均温度
idata u8 humidity_capture_max; //数据采集中读取的最大湿度
idata u8 humidity_capture_sum; //数据采集中读取的湿度总和
idata float humidity_capture_ave; //数据采集中读取的平均湿度
pdata u8 Rtc_capture[2]; //数据采集时间
idata bit tem_upper, hum_upper; //温度、湿度升高标志位
void KeyProc()
{
KeyVal = KeyDisp();
KeyDown = KeyVal & ~KeyOld;
KeyUp = ~KeyVal & KeyOld;
KeyOld = KeyVal;
if(MainMode)
return;
if(KeyDown == 9 && SetMode == 2)
KeyPressFlag = 1;
if(KeyUp == 9)
{
KeyPressFlag = 0;
if(Time_2s == 2000)//长按
{
CaptureFlag = DateCaptureFlag = tem_capture = humidity_capture = CaptureCount = 0;
tem_capture_max = tem_capture_sum = tem_capture_ave = 0;
humidity_capture_max = humidity_capture_sum = humidity_capture_ave = 0;
Rtc_capture[0] = Rtc_capture[1] = 0;
freq_capture = HumidityFlag = 0;
}
}
switch(KeyDown)
{
case 4:
if(++SegMode == 3)
SegMode = 0;
if(SegMode == 2)
SetMode = 0;
break;
case 5:
if(SegMode == 1)
if(++SetMode == 3)
SetMode = 0;
break;
case 8:
if(SegMode == 2)
if(++tem_set == 100)
tem_set = 99;
break;
case 9:
if(SegMode == 2)
if(--tem_set == 255)
tem_set = 0;
break;
}
}
void SegProc()
{
u8 i;
if(!MainMode)//数据主页面
{
switch(SegMode)
{
case 0://时间页面
SegBuf[2] = SegBuf[5] = 11;
for(i = 0; i < 3; i++)
{
SegBuf[3*i] = Rtc[i] / 16;
SegBuf[3*i+1] = Rtc[i] % 16;
}
break;
case 1:
switch(SetMode)
{
case 0:
SegBuf[0] = 12;//C
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? tem_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? tem_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)tem_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)tem_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(tem_capture_ave * 10) % 10 : 10;
break;
case 1:
SegBuf[0] = 13;//H
SegBuf[1] = 10;
SegBuf[2] = CaptureCount ? humidity_capture_max / 10 : 10;
SegBuf[3] = CaptureCount ? humidity_capture_max % 10 : 10;
SegBuf[4] = CaptureCount ? 11 : 10;
SegBuf[5] = CaptureCount ? (u8)humidity_capture_ave / 10 : 10;
SegBuf[6] = CaptureCount ? (u8)humidity_capture_ave % 10 : 10;
SegPoint[6] = CaptureCount;
SegBuf[7] = CaptureCount ? (u16)(humidity_capture_ave * 10) % 10 : 10;
break;
case 2:
SegBuf[0] = 14;//F
SegBuf[1] = CaptureCount / 10;
SegBuf[2] = CaptureCount % 10;
SegBuf[3] = CaptureCount ? Rtc_capture[0] / 16 : 10;
SegBuf[4] = CaptureCount ? Rtc_capture[0] % 16 : 10;
SegBuf[5] = CaptureCount ? 11 : 10;
SegBuf[6] = CaptureCount ? Rtc_capture[1] / 16 : 10;
SegPoint[6] = 0;
SegBuf[7] = CaptureCount ? Rtc_capture[1] % 16 : 10;
break;
}
break;
case 2:
SegBuf[0] = 15;//P
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = 10;
SegBuf[4] = 10;
SegBuf[5] = 10;
SegBuf[6] = tem_set / 10;
SegPoint[6] = 0;
SegBuf[7] = tem_set % 10;
break;
}
}
else
{
SegBuf[0] = 16;//E
SegBuf[1] = 10;
SegBuf[2] = 10;
SegBuf[3] = tem_capture / 10 % 10;
SegBuf[4] = tem_capture % 10;
SegBuf[5] = 11;//-
SegBuf[6] = HumidityFlag ? 17 : humidity_capture / 10;
SegBuf[7] = HumidityFlag ? 17 : humidity_capture % 10;
SegPoint[6] = 0;
}
}
void LedProc()
{
if(!MainMode)
{
ucLed[0] = !SegMode;
ucLed[1] = (SegMode == 1);
}
ucLed[2] = MainMode;
ucLed[3] = LedFlash;
ucLed[4] = HumidityFlag;
ucLed[5] = L6Flag;
LedDisp(ucLed);
}
void RtcProc()
{
GetRtc(Rtc);
}
void TemProc()
{
tem = TemRead();
L4Flag = (tem > tem_set);
}
void ADProc()
{
RD1_100x = AdRead() / 51.0 * 100;
if(RD1_100x < 100 && RD1_100x_old >= 100 && !CaptureFlag)
{
CaptureFlag = 1;
MainMode = 1;
DateCaptureFlag = 0;
}
RD1_100x_old = RD1_100x;
if(CaptureFlag && !DateCaptureFlag)
{
freq_capture = Freq;
HumidityFlag = (freq_capture < 200 || freq_capture > 2000);//无效数据
if(!HumidityFlag)//有效数据
{
Humidity = 2.0/45*freq_capture+10-400.0/45;
}
DateCaptureFlag = 1;
tem_capture = tem;
humidity_capture = Humidity;
if(!HumidityFlag)//湿度数据有效,正常采集数据
{
tem_upper = tem_capture > tem_capture_old;
hum_upper = humidity_capture > humidity_capture_old;
L6Flag = tem_upper & hum_upper;
GetRtc(Rtc_capture);
CaptureCount++;
if(tem_capture > tem_capture_max)
tem_capture_max = tem_capture;
tem_capture_sum += tem_capture;
tem_capture_ave = tem_capture_sum*1.0/CaptureCount;
if(humidity_capture > humidity_capture_max)
humidity_capture_max = humidity_capture;
humidity_capture_sum += humidity_capture;
humidity_capture_ave = humidity_capture_sum*1.0/CaptureCount;
tem_capture_old = tem_capture;
humidity_capture_old = humidity_capture;
}
}
}
void Timer0_Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x05;
TL0 = 0; //设置定时初始值
TH0 = 0; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer1_Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xFC; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; //使能定时器1中断
EA = 1;
}
void Timer1_Isr(void) interrupt 3
{
if(++SegPos == 8) SegPos = 0;
SegDisp(SegPos,SegBuf[SegPos],SegPoint[SegPos]);
if(++Time_1s == 1000)
{
Time_1s = 0;
Freq = (TH0 << 8) | TL0;
TH0 = TL0 = 0;
}
/*数据捕获处理*/
if(CaptureFlag)
{
if(++Time_3s >= 3000)
{
Time_3s = 0;
CaptureFlag = 0;
MainMode = 0;
}
}
else
{
Time_3s = 0;
}
/*按键长按*/
if(KeyPressFlag)
{
if(++Time_2s >= 2000)
Time_2s = 2000;
}
else
Time_2s = 0;
/*指示灯闪烁*/
if(L4Flag)
{
if(++Time_100ms == 100)
{
Time_100ms = 0;
LedFlash = !LedFlash;
}
}
else
LedFlash = 0;
}
void main()
{
SystemInit();
Timer0_Init();
Timer1_Init();
TemRead();
SetRtc(Rtc);
while(1)
{
}
}