1..怎么用按键k3关开数码管。我思路是关定时器中断。可效果不理想,必须加p3= 0x00,数码管才没有残影,应该是刷新太块,按键关不断。有没有好办法关开数码管。
2..k3关闭数码管显示时程序所有停止,开数码管显示时程序从头开始运行,程序该怎么写?
3.程序有些乱,我新手写程序不久,能看懂的老师帮忙看看应该怎么解决,谢谢
单片机源程序如下:
#include <STC15F2K60S2.H>
#define uint unsigned int
sbit ADDR0 = P1^0; //位码
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit k1 = P5^4; //时间切换键
sbit k2 = P1^6; //启动按键
sbit k3 = P5^5; //开关
sbit fm = P1^7; //蜂鸣器
sbit mos= P1^4; //mos管
unsigned char knum,kn;
bit k1f=0;
bit k2f=0;
unsigned char code LedChar[] = {0xeb,0x88,0xb3,0xba,0xd8,0x7a,0x7b,0xa8,0xfb,0xfa};
unsigned char LedBuff[4] = {0,0,0,0};
unsigned char i = 0; // 动态扫描的索引
unsigned char sec = 10,ms=0;
unsigned char sec1 = 30,ms1=0;
unsigned char sec2 = 0,ms2=03;
unsigned char sec3 = 0,ms3=05;
unsigned char sec4 = 0,ms4=10;
unsigned int cnt=0;
void Timer2_Init();
void Delay1ms(unsigned char c) //误差 0us
{
unsigned char a,b;
for(;c>0;c--)
{ for(b=199;b>0;b--)
for(a=1;a>0;a--);
}
}
void keyscan()
{
if(k1==0) {Delay1ms(10);if(k1f==0)
{ k1f=1; knum++;
if(knum==4) knum=0; } }
else k1f=0;
if(k2==0) {Delay1ms(10);if(k2==0)
{ TR0=!TR0;mos=!mos; while(k2==0);} }
if(k3==0) {Delay1ms(10);if(k2f==0)
{ k2f=1; kn++;
if(kn==2) kn=0; } }
else k2f=0;}
void main()
{
P3M0 = 0xff;
P3M1 = 0x00;
Timer2_Init();
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
while(1)
{
if(kn==0){IE2&=~0x04;EA=0;P3=0x00;}
if(kn==1){IE2|=0x04;EA=1;}
keyscan();
if(knum==0)
{ LedBuff[0] = LedChar[ms/10];
LedBuff[1] = LedChar[ms % 10];
LedBuff[2] = LedChar[sec % 10];
LedBuff[3] = LedChar[sec /10]; }
if(knum==1)
{ LedBuff[0] = LedChar[ms1/10];
LedBuff[1] = LedChar[ms1 % 10];
LedBuff[2] = LedChar[sec1 % 10];
LedBuff[3] = LedChar[sec1 /10]; }
if(knum==2)
{ LedBuff[0] = LedChar[ms2/10];
LedBuff[1] = LedChar[ms2 % 10];
LedBuff[2] = LedChar[sec2 % 10];
LedBuff[3] = LedChar[sec2 /10]; }
if(knum==3)
{ LedBuff[0] = LedChar[ms3/10];
LedBuff[1] = LedChar[ms3 % 10];
LedBuff[2] = LedChar[sec3 % 10];
LedBuff[3] = LedChar[sec3 /10];}
}
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
if(cnt>=1000)
{ cnt=0;
switch(knum)
{case 0: sec--; if(sec==0) {sec = 10, ms=0; TR0=0;mos=1;fm=0;} break;
case 1: sec1--;if(sec1==0){sec1 = 30,ms1=0;TR0=0;mos=1;fm=0;} break;
case 2: if(ms2>0||sec2>0) {if(ms2 > 0) {sec2--;if(sec2 >59){sec2 = 59;ms2--; }}else if(sec2 > 0) { sec2--; }}
if(sec2==0&&ms2==0){sec2=0;ms2=03;TR0=0;mos=1;fm=0;}break;
case 3: if(ms3>0||sec3>0) {if(ms3 > 0) {sec3--;if(sec3 >59){sec3 = 59;ms3--; }}else if(sec3 > 0) { sec3--; }}
if(sec3==0&&ms3==0){sec3=0;ms3=05;TR0=0;mos=1;fm=0;}break;
default: break;
}
} }
void Timer2_Isr(void) interrupt 12
{
T2L = 0xFB; //设置定时初始值
T2H = 0xFF;
P3 = 0x00;
switch (i)
{
case 0:ADDR3=1; ADDR2 = 1; ADDR1 = 1; ADDR0 = 0; P3= LedBuff[0]; i++; break;
case 1:ADDR3=1; ADDR2 = 1; ADDR1 = 0; ADDR0 = 1; P3= LedBuff[1]+0x04; i++; break;
case 2:ADDR3=1; ADDR2 = 0; ADDR1 = 1; ADDR0 = 1; P3= LedBuff[2]; i++; break;
case 3:ADDR3=0; ADDR2 = 1; ADDR1 = 1; ADDR0 = 1; P3= LedBuff[3]; i=0; break;
}
}
void Timer2_Init(void) //5微秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0xFB; //设置定时初始值
T2H = 0xFF; //设置定时初始值
AUXR|=0x10;
}
|