单片机蓝桥杯
代码可以运行,但是我用二个按键控制一个变量,这一个变量又控制了两个只是情况不同,效果确一样的功能。按按键的时候,在一个功能下按另一个,也造成了变量的变化,从而引起混乱,有什么其他的解决办法么,不想设好多变量~
//这是main.c
#include <STC15F2K60S2.H>
#include"seg.h"
#include"stdio.h"
unsigned char k_mark=0,s_mark=0,ring_mark=0;
unsigned int num=0,num1=0;
unsigned char seg_buf[8],seg_str[9];
unsigned int hour=23,hour1=0;
unsigned int min=59,min1=0;
unsigned int sec=50,sec1=0;
unsigned char wei,i=0;
unsigned char deng=0;
unsigned char ring=0;
void seg_proc();
void key_proc();
void ring_proc();
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;ET0=1;P36=0;
}
void main(){
Timer0Init();
P0=0xff;P2=(P0&0x1f)|0x80;P2=(P0&0x1f);//关灯
while(1){
seg_proc();
key_proc();
ring_proc();
}
}
void int_0() interrupt 1{
TL0 = 0x18;
TH0 = 0xFC;
num++;
if(++num1==1000)num1=0;
if(++k_mark==100)k_mark=0;
if(++s_mark==500) s_mark=0;
seg_dis(seg_buf,wei,deng,num1);
if(++wei==8) wei=0;
if(num==1000){
sec=sec+1;
i++;
num=0;
}
if(sec==60){ //正常
sec=0;
min=min+1;
}
if(min==60){
min=0;
hour=hour+1;
}
if(hour==24){
hour=0;
}
}
void key_proc(){
if(k_mark) return;
if(P30==0){ //按下s7键,进行时分秒的选择
if(++deng==4)
deng=0;
while(!P30)
;
}
if(ring==1||ring==2||ring==3){
switch(deng){
case 1 : if(P32==0){hour1=hour1+1;while(!P32);}if(P33==0){hour1=hour1-1;while(!P33);}break;
case 2 : if(P32==0){min1=min1+1;while(!P32);}if(P33==0){min1=min1-1;while(!P33);}break;
case 3 : if(P32==0){sec1=sec1+1;while(!P32);}if(P33==0){sec1=sec1-1;while(!P33);}break;
}
}else{
switch(deng){
case 1 : if(P32==0){hour=hour+1;while(!P32);}if(P33==0){hour=hour-1;while(!P33);}break;
case 2 : if(P32==0){min=min+1;while(!P32);}if(P33==0){min=min-1;while(!P33);}break;
case 3 : if(P32==0){sec=sec+1;while(!P32);}if(P33==0){sec=sec-1;while(!P33);}break;
}
}
}
void seg_proc(){
if(s_mark) return;
if(P31==0){
if(++ring==4) ring=0;
if(++deng==4){
deng=0;
}
while(!P31)
;
}
if(ring==1||ring==2||ring==3) sprintf(seg_str,"%02d-%02d-%02d",hour1,min1,sec1);
else
sprintf(seg_str,"%02d-%02d-%02d",hour,min,sec);
seg_tran(seg_str,seg_buf);
}
void ring_proc(){
if(!(hour1==hour&&min1==min1&&sec1==sec))return;
while(i<10){
if((num1>0&&num1<200)||(num1>400&&num1<800))
{ P0=0;
P2=(P2&0X1F)|0X80;
P2=(P2&0X1F);
}
if((num1>200&&num1<400)||(num1>800&&num1<1000))
{ P0=0xff;
P2=(P2&0X1F)|0X80;
P2=(P2&0X1F);
}
P0=0xff;P2=(P0&0x1f)|0x80;P2=(P0&0x1f);//关灯
if(P30==0||P31==0||P32==0||P33==0)return;
}
i=0;
}
//这是seg.c
#include"seg.h"
void seg_tran(unsigned char *seg_str,unsigned char *seg_buf){
unsigned char i,j=0;
for(i=0;i<8;i++,j++){
switch(seg_str[j]){
case'0' :seg_buf[i]=0xc0;break;
case'1' :seg_buf[i]=0xf9;break;
case'2' :seg_buf[i]=0xa4;break;
case'3' :seg_buf[i]=0xb0;break;
case'4' :seg_buf[i]=0x99;break;
case'5' :seg_buf[i]=0x92;break;
case'6' :seg_buf[i]=0x82;break;
case'7' :seg_buf[i]=0xf8;break;
case'8' :seg_buf[i]=0x80;break;
case'9' :seg_buf[i]=0x90;break;
case'-' :seg_buf[i]=0xbf;break;
case' ' :seg_buf[i]=0xff;break;
case'C' :seg_buf[i]=0xc6;break;
default :seg_buf[i]=0xff;
}
if(seg_str[j+1]=='.'){
seg_buf[i]= seg_buf[i]&0x7F;
j=j+1;
}
}
}
void seg_dis(unsigned char *seg_buf,unsigned char wei,unsigned char deng,unsigned int num1){
P0=0XFF;
P2=(P2&0X1F)|0XE0;
P2=(P2&0X1F);
P0=1<<wei;
if(num1>0&&num1<500){
switch(deng){
case 1 : P00=0;P01=0;break;
case 2 : P03=0;P04=0;break;
case 3 : P06=0;P07=0;break;
}
}
if(num1>500&&num1<1000)P0=1<<wei;
P2=(P2&0X1F)|0Xc0;P2=(P2&0X1F);
P0=seg_buf[wei];
P2=(P2&0X1F)|0Xe0;
P2=(P2&0X1F);
}
//这是seg.h
#ifndef _seg_h
#define _seg_h
#include <STC15F2K60S2.H>
void seg_tran(unsigned char *seg_str,unsigned char *seg_buf);
void seg_dis(unsigned char *seg_buf,unsigned char wei,unsigned char deng,unsigned int num1);
#endif
没有报错
不想再设变量,不过如果这样写不了的话,还请同志们支招
如果可以的话,看一下闹铃时间到了,如何一边闪灯,数码管一边正常显示。我现在的情况是,只有小灯闪烁结束后,数码管才可以正常显示