蓝桥杯单片机第七届国赛代码

//main.c   其他的子函数自己添加,功能基本实现,逻辑上懒得考虑

#include "tim.h"
#include "seg.h"
#include "stdio.h"
#include "key.h"
#include "STC15F2K60S2.h"
#include "ds1302.h"
#include "i2c.h"
#include "intrins.h"
unsigned char ucSec,ucLed;
unsigned char ucKey_Dly, ucKey_Old;
unsigned char pucSeg_Buf[9], pucSeg_Code[8], ucSeg_Pos;//pucseg_buf储存阿拉伯数字,code储存16进制
unsigned long ulms,ulKey_Time;
unsigned int ulms1=0;
unsigned int ulms2=0;//控制闪烁
unsigned char mode=0,mode1=0,mode2=0,mode3=0,mode4=0;//mode2控制是否为压控,mode3控制频率和周期,mode4储存异常
unsigned int uiFre_Val;
unsigned char pucRtc[3] = {23, 59, 50};
unsigned char pucRtcnew[3] = {23, 59, 50};//储存电压异常事件时间
unsigned char leixing=0;//储存事件类型
unsigned int ya=0,yafaup=5000,yafadown=500;//电压
bit shan=0;
// 注意: sprintf()会在字符串后面添加”\0”,所以 pucSeg_Buf[]的长度应为 9。
// 如果字符串中包含小数点, pucSeg_Buf[]的长度应为 10。
void keydo();
void Key_Proc(void);
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}

void show()
{
if(mode==0)//时钟显示界面
{
Read_RTC(pucRtc);
sprintf(pucSeg_Buf, "%2u-%2u-%2u", (unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);//打入字符串,以字符形式储存
if(mode1!=0)
{
if(shan)
{
switch(mode1)
{
case 1:sprintf(pucSeg_Buf, " -%2u-%2u",(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);//打入字符串,以字符形式储存
break;
case 2:sprintf(pucSeg_Buf, "%2u- -%2u",(unsigned int)pucRtc[0],(unsigned int)pucRtc[2]);//打入字符串,以字符形式储存
break;
case 3:sprintf(pucSeg_Buf, "%2u-%2u- ",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1]);//打入字符串,以字符形式储存
break;
default:break;

}
}
}
}
else if(mode==1&&mode2==0)//电压显示界面
{
ya=Pcf8591_adc(0x03);

if(ya>yafaup)
{
leixing=1;
pucRtcnew[0]=pucRtc[0];
pucRtcnew[1]=pucRtc[1];
pucRtcnew[2]=pucRtc[2];
write_eeprom(0x00,leixing);
Delay10ms();
write_eeprom(0x01,pucRtcnew[0]);
Delay10ms();
write_eeprom(0x02,pucRtcnew[1]);
Delay10ms();
write_eeprom(0x03,pucRtcnew[2]);

}
if(ya<yafadown)
{
leixing=2;
pucRtcnew[0]=pucRtc[0];
pucRtcnew[1]=pucRtc[1];
pucRtcnew[2]=pucRtc[2];
write_eeprom(0x00,leixing);
Delay10ms();
write_eeprom(0x01,pucRtcnew[0]);
Delay10ms();
write_eeprom(0x02,pucRtcnew[1]);
Delay10ms();Delay10ms();Delay10ms();
write_eeprom(0x03,pucRtcnew[2]);

}
sprintf(pucSeg_Buf, "-%u- %4u", (unsigned int)mode,(unsigned int)(ya*1000.0/51));//打入字符串,以字符形式储存
}
else if(mode==1&&mode2!=0)//电压阈值界面
{
sprintf(pucSeg_Buf, "%4u%4u", (unsigned int)yafaup,(unsigned int)yafadown);//打入字符串,以字符形式储存
if(shan)
{
switch(mode2)
{
case 1:sprintf(pucSeg_Buf, " %4u",(unsigned int)yafadown);//打入字符串,以字符形式储存
break;
case 2:sprintf(pucSeg_Buf, "%4u ",(unsigned int)yafaup);//打入字符串,以字符形式储存
break;
default:break;

}
}
}
else if(mode==2)//频率显示
{
if(mode3==1)
{
sprintf(pucSeg_Buf, "-%u-%5u", (unsigned int)mode,(unsigned int)uiFre_Val);//打入字符串,以字符形式储存
}
else if(mode3==2)
{
sprintf(pucSeg_Buf, "-%u-%5u", (unsigned int)mode,(unsigned int)(1000.0/uiFre_Val*1000));//打入字符串,以字符形式储存
}
}
else if(mode==3&&mode4==0)
{
sprintf(pucSeg_Buf, " %2u", (unsigned int)leixing);//打入字符串,以字符形式储存
}
else if(mode==3&&mode4==1)
{
sprintf(pucSeg_Buf, "%2u-%2u-%2u", (unsigned int)pucRtcnew[0],(unsigned int)pucRtcnew[1],(unsigned int)pucRtcnew[2]);
}
Seg_Tran(pucSeg_Buf, pucSeg_Code);//将阿拉伯数字转换成16进制
}
void Timer0Init(void) // 16 位计数器模式
{
AUXR &= 0x7F; // 定时器时钟 12T 模式
TMOD |= 5; // 设置 16 位计数器模式
TL0 = 0; // 设置计数初值
TH0 = 0; // 设置计数初值
TF0 = 0; // 清除 TF0 标志
TR0 = 1; // 开始计数
}
void main(void)
{

Cls_Peripheral();
Timer1Init();
Timer0Init();
Set_RTC(pucRtc);
ya=Pcf8591_adc(0x03);
leixing=read_eeprom(0x00);
Delay10ms();
pucRtcnew[0]=read_eeprom(0x01);
Delay10ms();
pucRtcnew[1]=read_eeprom(0x02);
Delay10ms();Delay10ms();
pucRtcnew[2]=read_eeprom(0x03);
Delay10ms();
yafaup=read_eeprom(0x05);
Delay10ms();
yafaup+=read_eeprom(0x04)*256;
Delay10ms();
yafadown=read_eeprom(0x07);
Delay10ms();
yafadown+=read_eeprom(0x06)*256;
Delay10ms();
while(1)
{

Key_Proc();
if(ulms==150)
{
show();
}
}
}
void keydo(unsigned char key_value)//按键操作
{
if(key_value==7&&mode1==0)//显示时钟界面
{

mode=0;
mode1=0;
}
else if(key_value==7&&mode1!=1)
{
mode1=0;
}
else if(mode==0&&key_value==4)
{
mode1+=1;
if(mode1==4)
{
mode1=1;
}
}
else if(mode==0&&mode1!=0&&key_value==11)//加
{
pucRtc[mode1-1]+=1;
if(pucRtc[2]==60)
{
pucRtc[2]=0;
pucRtc[1]+=1;
}
if(pucRtc[1]==60)
{
pucRtc[1]=0;
pucRtc[0]+=1;
}
if(pucRtc[1]==24)
{
pucRtc[0]=0;
}
Set_RTC(pucRtc);
}
else if(mode==0&&mode1!=0&&key_value==10)//减
{
if(mode1==3&&pucRtc[2]==0&&pucRtc[1]!=0)
{
pucRtc[2]=59;
pucRtc[1]-=1;
}
else if(mode1==3&&pucRtc[2]==0&&pucRtc[1]==0&&pucRtc[0]!=0)
{
pucRtc[2]=59;
pucRtc[1]=59;
pucRtc[0]-=1;
}
else if(mode1==3&&pucRtc[2]==0&&pucRtc[1]==0&&pucRtc[0]==0)
{
pucRtc[2]=59;
pucRtc[1]=59;
pucRtc[0]=23;
}
else if(mode1==2&&pucRtc[1]==0&&pucRtc[0]!=0)
{
pucRtc[1]=59;
pucRtc[0]-=1;
}
else if(mode1==2&&pucRtc[1]==0&&pucRtc[0]==0)
{
pucRtc[1]=59;
pucRtc[0]=23;
}
else if(mode1==1&&pucRtc[0]==0)
{
pucRtc[0]=23;
}
else
{
pucRtc[mode1-1]-=1;
}
Set_RTC(pucRtc);
}
else if(key_value==6&&mode!=1)//压测
{
mode=1;
}
else if(mode==1&&key_value==4)//压显示阈值
{
mode2+=1;
if(mode2==3)
{
mode2=1;
}
}
else if(mode==1&&mode2!=0&&key_value==6)
{
mode2=0;
}
else if(mode==1&&mode2==1&&key_value==11)//上限加
{
if(yafaup<10000)
{
yafaup+=500;
}
else
{
yafaup=99999;
}
write_eeprom(0x04,yafaup/256);
Delay10ms();
write_eeprom(0x05,yafaup%256);
Delay10ms();
}
else if(mode==1&&mode2==1&&key_value==10)//上限减
{
if(yafaup>500)
{
yafaup-=500;
}
else
{
yafaup=0;
}
write_eeprom(0x04,yafaup/256);
Delay10ms();
write_eeprom(0x05,yafaup%256);
Delay10ms();
}
else if(mode==1&&mode2==2&&key_value==11)//下限加
{
if(yafadown<10000)
{
yafadown+=500;
}
else
{
yafadown=9999;
}
write_eeprom(0x06,yafadown/256);
Delay10ms();
write_eeprom(0x07,yafadown%256);
Delay10ms();
}
else if(mode==1&&mode2==2&&key_value==10)//下限减
{
if(yafadown>500)
{
yafadown-=500;
}
else
{
yafadown=0;
}
write_eeprom(0x06,yafadown/256);
Delay10ms();
write_eeprom(0x07,yafadown%256);
Delay10ms();
}
else if(mode!=2&&key_value==5)
{
mode=2;mode3=1;
}

else if(mode==2&&key_value==4&&mode3!=0)
{
mode3+=1;
if(mode3>2)
{
mode3=1;
}
}
else if(mode!=3&&key_value==9)
{
mode=3;
}
else if(mode==3&&key_value==4)
{
mode4+=1;
if(mode4>1)
{
mode4=0;
}
}
}
void Time_1(void) interrupt 3
{
ulms1++;
ulms++;
if(ulms1==1000)
{
uiFre_Val=(TH0<<8)+TL0;
TH0=0;
TL0=0;
ulms1=0;

}
if(++ucKey_Dly == 10)
ucKey_Dly = 0;
if(mode1!=0||mode2!=0)//闪烁开始
{
ulms2++;
if(ulms2==1000)
{
ulms2=0;
shan=~shan;
}
}
else
{
ulms2=0;
}
if(ulms>300)
{
ulms=0;
}
Seg_Disp(pucSeg_Code, ucSeg_Pos);
if(++ucSeg_Pos == 8) ucSeg_Pos = 0;
}
void Key_Proc(void)
{
unsigned char ucKey_Val, ucKey_Down, ucKey_Up;
if(ucKey_Dly) return;
ucKey_Dly = 1;
ucKey_Val = Key_Read();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);//^按位异或
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
if(ucKey_Down)//判断old是否和新的一样,一样说明是同一个,不会进入
{Led_Disp(0x01);
ulKey_Time = ulms;
keydo(ucKey_Down);
}
}

posted @ 2020-11-13 12:03  通信小九  阅读(464)  评论(0编辑  收藏  举报