当前位置:网站首页>蓝桥杯单片机第七届决赛
蓝桥杯单片机第七届决赛
2022-06-13 00:42:00 【昊月光华】
总觉得题目读的含糊不清,比如按下原key后完成装载,那就是修改后若是按别的key就不装载到epprom,总要考虑到一些错误的操纵方式。各种标记之类的清理和判断真磨人心态。总有一些精细化的控制需要调试,也许这就是高手的水平吧。
main
#include "sys.h"
u8 s=0;
u8 smgtime=0;
u8 keytime=0;
u16 ft=0;//闪烁时间
bit ff=0;//闪烁标志
u16 pl=0;//频率
u16 zq=0;//周期
u16 plt=0;//测量频率的时间
bit event=0;//0 低于 1超出
u16 adc=0;//测量的电压值
u16 adct=0;//读取ADC的时间间隔
u8 uic=0; //0 时钟正常显示 1 闪时 2 闪分 3 闪秒
//4电压测量 5 闪电压上 6 闪电压下
//7 频率测量显示 8周期显示 9 查询显示 10 显示事件时间
u16 rt=0;//读取时间间隔
bit modify=0;//修改时间标志
bit modify_v=0;//修改电压标志
u8 wi=0;//写入epprom计数
u8 wi2=0;
bit wt=0;//存事件时间标志
void ClearFlag(){
if(modify){
if(rkey!=7)modify=0;
}
if(modify_v==1){
if(rkey!=6)modify_v=0;
}
}
void SaveTimeTe(){//保存事件发生的时间
if(wt){
switch(wi2){
case 0:
Write_epprom(2,BTIME[0]);
break;
case 1:
Write_epprom(3,BTIME[1]);
break;
case 2:
Write_epprom(4,BTIME[2]);
case 3:
wi2=0;
wt=0;
break;
}
wi2++;
}
}
//保存电压上限和电压下限
void SaveToep(){
if(modify_v==2){
switch(wi){
case 0:
Write_epprom(0,ADLIM[0]/100);
wi++;
break;
case 1:
Write_epprom(1,ADLIM[1]/100);
wi++;
break;
case 2:
wi=0;
modify_v=0;
break;
}
}
}
void UISetting(){
if(smgtime>45){
smgtime=0;
SaveTimeTe();
if(!uic){
DT[0]=TIME[0]/10;
DT[1]=TIME[0]%10;
DT[2]=11;
DT[3]=TIME[1]/10;
DT[4]=TIME[1]%10;
DT[5]=11;
DT[6]=TIME[2]/10;
DT[7]=TIME[2]%10;
}
else if(uic==1){
DT[0]=ff?TIME[0]/10:10;
DT[1]=ff?TIME[0]%10:10;
DT[2]=11;
DT[3]=TIME[1]/10;
DT[4]=TIME[1]%10;
DT[5]=11;
DT[6]=TIME[2]/10;
DT[7]=TIME[2]%10;
}
else if(uic==2){
DT[0]=TIME[0]/10;
DT[1]=TIME[0]%10;
DT[2]=11;
DT[3]=ff?TIME[1]/10:10;
DT[4]=ff?TIME[1]%10:10;
DT[5]=11;
DT[6]=TIME[2]/10;
DT[7]=TIME[2]%10;
}
else if(uic==3){
DT[0]=TIME[0]/10;
DT[1]=TIME[0]%10;
DT[2]=11;
DT[3]=TIME[1]/10;
DT[4]=TIME[1]%10;
DT[5]=11;
DT[6]=ff?TIME[2]/10:10;
DT[7]=ff?TIME[2]%10:10;
}
else if(uic==4){
DT[0]=11;
DT[1]=1;
DT[2]=11;
DT[3]=10;
DT[4]=adc/1000;
DT[5]=adc/100%10;
DT[6]=adc/10%10;
DT[7]=adc%10;
}
else if(uic==5){
DT[0]=ff?ADLIM[0]/1000:10;
DT[1]=ff?ADLIM[0]/100%10:10;
DT[2]=ff?ADLIM[0]/10%10:10;
DT[3]=ff?ADLIM[0]%10:10;
DT[4]=ADLIM[1]/1000;
DT[5]=ADLIM[1]/100%10;
DT[6]=ADLIM[1]/10%10;
DT[7]=ADLIM[1]%10;
}
else if(uic==6){
DT[0]=ADLIM[0]/1000;
DT[1]=ADLIM[0]/100%10;
DT[2]=ADLIM[0]/10%10;
DT[3]=ADLIM[0]%10;
DT[4]=ff?ADLIM[1]/1000:10;
DT[5]=ff?ADLIM[1]/100%10:10;
DT[6]=ff?ADLIM[1]/10%10:10;
DT[7]=ff?ADLIM[1]%10:10;
}
else if(uic==7||uic==8){
DT[0]=11;
DT[1]=2;
DT[2]=11;
DT[3]=(uic==7?pl/10000%10:zq/10000%10);
DT[4]=(uic==7?pl/1000%10:zq/1000%10);
DT[5]=(uic==7?pl/100%10:zq/100%10);
DT[6]=(uic==7?pl/10%10:zq/10%10);
DT[7]=(uic==7?pl%10:zq%10);
}
else if(uic==9){
DT[0]=10;
DT[1]=10;
DT[2]=10;
DT[3]=10;
DT[4]=10;
DT[5]=10;
DT[6]=0;
DT[7]=event?1:0;
}
else if(uic==10){
DT[0]=BTIME[0]/10;
DT[1]=BTIME[0]%10;
DT[2]=11;
DT[3]=BTIME[1]/10;
DT[4]=BTIME[1]%10;
DT[5]=11;
DT[6]=BTIME[2]/10;
DT[7]=BTIME[2]%10;
}
}
}
void KeyConsole(){
if(keytime>12){
keytime=0;
key_scan();
if(rkey){
ClearFlag();
switch(rkey){
case 7://时钟
uic=0;
if(modify){
modify=0;
WriteTime();
}
break;
case 6://电压
uic=4;
if(modify_v==1)modify_v=2;
break;
case 5://频率
uic=7;
break;
case 4://选择
if(uic<4){
if(uic==3)uic=1;
else{
uic=++uic%4;
}
}
else if(4<=uic&&uic<7){
if(uic==6)uic=5;
else {
uic=++uic%7;
}
}
else if(uic==7||uic==8){
uic=(uic==7?8:7);
}
else if(uic==9||uic==10){
uic=(uic==9?10:9);
}
break;
case 9://查询
uic=9;
break;
case 11://加
if(uic==1||uic==2||uic==3){
modify=1;
if(uic==1) TIME[uic-1]=++TIME[uic-1]%24;
else if(uic==2||uic==3)TIME[uic-1]=++TIME[uic-1]%60;
}
else if(uic==5||uic==6){
ADLIM[uic==5?0:1]+=500;
modify_v=1;
}
break;
case 10://减
if(uic==1||uic==2||uic==3){
if(!TIME[uic-1])break;
TIME[uic-1]--;
modify=1;
}
else if(uic==5||uic==6){
if(!ADLIM[uic==5?0:1])break;
ADLIM[uic==5?0:1]-=500;
modify_v=1;
}
break;
}
}
SaveToep();
}
}
void main(){
sys_init();
Timer1Init();
WriteTime();
Timer0Init();
while(1){
KeyConsole();
UISetting();
if(rt>1000){
rt=0;
if(uic!=1&&uic!=2&&uic!=3)
ReadTime();
}
if(adct>450){
adct=0;
adc= 5000/255* Read_adc(0x03);
if(adc>ADLIM[0]||adc<ADLIM[1]){
event=adc>ADLIM[0]?1:0;
BTIME[0]=TIME[0];
BTIME[1]=TIME[1];
BTIME[2]=TIME[2];
wt=1;
wi2=0;
}
}
}
}
void TIME1() interrupt 3{
smg_play(DT[s],s++);
if(s>7)s=0;
smgtime++;keytime++;
rt++;
adct++;
if(uic==1||uic==2||uic==3||uic==5||uic==6){
if(ft++==1000){
ff=~ff;
ft=0;
}
}
else
ft=0;
if(plt==0){
TR0=1;
plt++;
}
else if(plt++==500){
plt=0;
TR0=0;
pl=TH0<<8|TL0;
pl<<=1;
TH0=TL0=0;
zq=1000000/pl;
}
}
sys.c
#include "sys.h"
u8 code SMGINDEX[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff
,0xbf // -
};
u8 DT[]={10,10,10,10,10,10,10,10};
u8 key_state=0;
u8 rkey=0;
u8 key_v=0;
u8 xdata TIME[]={23,59,50};
u8 xdata BTIME[]={0,0,0};
u16 xdata ADLIM[]={0,0};
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
//系统初始化并epprom初始化
void sys_init(){
u8 temp1;
u8 temp2;
P0=0xff;
lock(7);
P0=0;
lock(6);
P0=0;
lock(5);
P0=0xff;
lock(4);
temp1= Read_epprom(0x10);
Delay5ms();
temp2= Read_epprom(0x11);
Delay5ms();
if( temp1!=9&&temp2!=9){//255*255
Write_epprom(0x10,9);
Delay5ms();
Write_epprom(0x11,9);
Delay5ms();
Write_epprom(0,20);
Delay5ms();
Write_epprom(1,10);
Delay5ms();
ADLIM[0]= Read_epprom(0)*100;
Delay5ms();
ADLIM[1]= Read_epprom(1)*100;
Delay5ms();
}
else {
ADLIM[0]= Read_epprom(0)*100;
Delay5ms();
ADLIM[1]= Read_epprom(1)*100;
Delay5ms();
BTIME[0]= Read_epprom(2);
Delay5ms();
BTIME[1]= Read_epprom(3);
Delay5ms();
BTIME[2]= Read_epprom(4);
Delay5ms();
}
}
void smg_play(u8 du,u8 we){
P0=0xff;
lock(7);
P0=0;
P0=1<<we;
lock(6);
P0=0xff;
P0=SMGINDEX[du];
lock(7);
}
void key_scan(){
switch(key_state){
case 0:
key_v=0;
P3=P3|0x0f;
P42=0;
P44=0;
if((P3&0x0f)!=0x0f)key_state=1;
break;
case 1:
P3=P3|0x0f;
P42=0;
P44=0;
if((P3&0x0f)!=0x0f){
key_state=2;
if(!P30)key_v=7;
else if(!P31)key_v=6;
else if(!P32)key_v=5;
else if(!P33)key_v=4;
P3&=0xf0;
P42=1;
P44=1;
if(!P42)key_v+=4;
rkey=key_v;
}
else
key_state=0;
break;
case 2:
rkey=0;
P3=P3|0x0f;
P42=0;
P44=0;
if((P3&0x0f)==0x0f)key_state=0;
break;
}
}
void Timer1Init(void) //1000微秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xFC; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA=1;
ET1=1;
}
void Timer0Init(void) //100微秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD|=0x05;
TL0 = 0x00; //设置定时初始值
TH0 = 0x00; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 0; //定时器0开始计时
ET0=0;
}
sys.h
#ifndef __SYS_H_
#define __SYS_H_
typedef unsigned char u8;
typedef unsigned int u16;
#define lock(x) P2=P2&0x1f|(x<<5); P2=P2&0x1f;
#include "mystc.h"
void sys_init();
void smg_play(u8 du,u8 we);
void key_scan();
void Timer1Init(void);
void ReadTime();
void WriteTime();
u8 Read_adc(u8 word);
u8 Read_epprom(u8 word);
void Write_epprom(u8 word,u8 dat);
void Timer0Init(void);
u8 DT[];
extern u8 rkey;
u8 xdata TIME[];
u16 xdata ADLIM[];
u8 xdata BTIME[];
#endif
边栏推荐
猜你喜欢
Kali system -- dnsmap for DNS collection and analysis
In / out / inout details of MySQL stored procedures
Buuctf babyupload[gxyctf2019]
Maya modeling VI
Kali system -- dnsrecon for DNS collection and analysis
市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?
antdPro - ProTable 实现两个选择框联动效果
Canvas game lower level 100
Buuctf's babysql[geek challenge 2019]
Win10 home vs pro vs enterprise vs enterprise LTSC
随机推荐
硬(磁)盘(一)
[LeetCode]14. Longest common prefix thirty-eight
睡前小故事之MySQL起源
1. Google grpc framework source code analysis Hello World
Transaction creation of btcd transaction process (I)
Converting Chinese numbers to Arabic numbers in Delphi
[LeetCode]13. Roman numerals to integers thirty
MySQL lpad() and rpad() concatenate string functions with specified length
Lambda expression
Arduino control tm1637 common positive four digit nixie tube
高阶极点对于波形的影响
Stm32f4 development of DMA transmission to GPIO port
Set sail
[GYCTF2020]Ezsqli --BUUCTF
[ciscn2019 North China Day2 web1]hack world --buuctf
After so long use, CSDN has finally opened a blog
Penetration test summary
Win10 home vs pro vs enterprise vs enterprise LTSC
MAYA建模六
Kali system -- dnsrecon for DNS collection and analysis