当前位置:网站首页>蓝桥杯单片机第七届决赛
蓝桥杯单片机第七届决赛
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边栏推荐
- [imx6ull] video monitoring project (USB camera +ffmepeg)
- OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
- Easyexcel read excel simple demo
- BUUCTF之BabyUpload[GXYCTF2019]
- Daily buckle exercise - conclusion
- Transaction creation of btcd transaction process (I)
- Mysql批量插入数据时如何解决重复问题?
- 硬(磁)盘(二)
- Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
- Using fastjson to solve the problem of returning an empty array from a null value of a field string object
猜你喜欢
随机推荐
Cve-2021-24078 vulnerability analysis
Five mock technologies of go
String类中split()方法的使用
Buuctf's babysql[geek challenge 2019]
Kotlin 协程的四种启动模式
Daily buckle exercise - conclusion
Kalix system - use of information collection gadgets
Composite key relationships using Sqlalchemy - relationships on composite keys using Sqlalchemy
牌好不好无法预料
What are the conditions of index invalidation?
Delphi Chinese digit to Arabic digit
MySQL query table field information
Triangle wave and triangle wave convolution
三角波与三角波卷积
Also on STM32 using dma+ serial port to obtain variable length data
Using com0com/com2tcp to realize TCP to serial port (win10)
Programming training 1
How to solve the duplication problem when MySQL inserts data in batches?
People and gods are angry. Details of Tangshan "mass beating of women incident"
天津银行周传凯:从 0 到 1,我的分布式数据库落地经验谈








![[ciscn2019 North China Day2 web1]hack world --buuctf](/img/1e/ebf8cc06ba620261f03f84ca2c68e6.png)
