2015年蓝桥杯第6届单片机省赛真题——“采集、存储和读取温度”
作者:互联网
题目
代码
1、底层代码是直接运用官方提供的指导书配套的代码。
2、为了便于修改,此次编程运用了模块化编写。
3、因为蓝桥杯比赛的板子没有12分频,所以是比C51板子的运行速度快6-12倍,比赛时提供的底层代码ds1302.c中的_nop_()一定要记得修改,不然时间会跑的飞快!!!
4、main函数中一定要读取温度,不然存到数组中的第一个温度是不准确的!!!
主函数
unsigned char RTC_Init[]={23,59,50};
unsigned char Seg_Buf[12],Seg_Code[8],smgwei;
unsigned int Delay_Seg,Delay_Key,Delay_Led,Delay_1s,Delay_Temp;
bit Flag_1s=0;
unsigned char Time_Temp=0,jiange=1; //温度间隔时间
unsigned char Seg_Mode=0; //显示界面模式
unsigned int Temp; //温度
unsigned int Cap_Temp[10]; //存储温度
static unsigned char t=0,z=0;
void main(void)
{
banzi_Init();
Timer1Init();
rd_temperature();
RTC_Set(RTC_Init);
while(1)
{
Seg_Work();
Key_Work();
Function();
}
}
按键处理(独立按键BTN)
void Key_Work(void)
{
if(Delay_Key) return;
Delay_Key=1;
if(P33==0) //S4
{
delay(10);
if(P33==0)
{
Time_Temp++;
if(Time_Temp>=4) Time_Temp=0;
if(Time_Temp==0) jiange=1;
else if(Time_Temp==1) jiange=5;
else if(Time_Temp==2) jiange=30;
else if(Time_Temp==3) jiange=60;
}
while(!P33);
}
if(P32==0) //S5
{
delay(10);
if(P32==0)
{
if(Seg_Mode==0) Seg_Mode=1;
}
while(!P32);
}
if(P31==0) //S6
{
delay(10);
if(P31==0)
{
if(Seg_Mode==2 && t==0)
{
z++;
if(z==10) z=0;
}
if(Seg_Mode==1)
{
Seg_Mode=2;
t=0;
}
}
while(!P31);
}
if(P30==0) //S7
{
delay(10);
if(P30==0)
{
if(Seg_Mode==2)
{
Seg_Mode=0;
Time_Temp=0;
}
}
while(!P30);
}
}
显示处理
void Seg_Work(void)
{
if(Delay_Seg) return;
Delay_Seg=1;
RTC_Read(RTC_Init);
if(Seg_Mode==0)
sprintf(Seg_Buf," -%02d",(int)jiange);
else if(Seg_Mode==1)
{
if(Flag_1s)
{
Delay_1s=0;
Flag_1s = ~Flag_1s;
sprintf(Seg_Buf,"%02d-%02d-%02d",(unsigned int)RTC_Init[0],(unsigned int)RTC_Init[1],(unsigned int)RTC_Init[2]);
}
else
sprintf(Seg_Buf,"%02d %02d %02d",(unsigned int)RTC_Init[0],(unsigned int)RTC_Init[1],(unsigned int)RTC_Init[2]);
}
else if(Seg_Mode==2)
sprintf(Seg_Buf,"-%02d -%02d",(int)z,(unsigned int)Cap_Temp[z]/16);
Seg_Tran(Seg_Buf,Seg_Code);
}
中断函数
void Time1() interrupt 3
{
TL1 = 0x18; //1ms
TH1 = 0xFC;
Delay_Temp++;
if(++Delay_1s==1000) Flag_1s=~Flag_1s;
if(++Delay_Seg==500) Delay_Seg=0;
if(++Delay_Key==10) Delay_Key=0;
if(++Delay_Led==100) Delay_Led=0;
if(++smgwei==8) smgwei=0;
Seg_Dis(Seg_Code,smgwei);
}
外设
void Led_Dis(unsigned char led)
{
if(Delay_Led) return;
Delay_Led=1;
P0 = ~led;
P2 = (P2&0x1F) | 0x80; //LED
P2 &= 0x1F;
}
//存储10个温度的函数
void Function(void)
{
if(Seg_Mode==1)
{
if(Delay_Temp >= jiange*1000 && t<=10)
{
Cap_Temp[t]=rd_temperature();
Delay_Temp=0;
t++;
}
if(t>10)
{
if(Flag_1s)
Led_Dis(1);
else
Led_Dis(0);
}
}
}
标签:Delay,Temp,真题,int,unsigned,Seg,蓝桥,单片机,Mode 来源: https://blog.csdn.net/Ymaple0_o/article/details/122630897