以下的程序功能,当取消数码管显示后,按键就有作用了LED1会亮和灭,如一起执行按键就没作用,本来想用按键调数码管数字加减变化的。现在请各位高手指点下,看那里出问题了。thanks!
单片机源程序如下:
include"STC15W.H"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
void digplay(uint hig);
void key();
sbit PWM=P1^7;
sbit LED1=P5^5;
sbit up=P1^4;
sbit down=P1^5;
sbit ba=P1^1;
sbit sh=P1^0;
sbit ge=P1^2;
sbit dn=P1^3;
uint hig=250;
uchar code LED[]={0xCF,0x81,0xEA,0xE3,0xA5,0x67,0x6F,0xC1,0xEF,0xE7,0x5e};
/************延时子程序*************/
void delayxms(uint z)
{
uint y,x;
for(y=z;x>0;x--)
for(y=110;y>0;y--);
}
void KeyScan(void)//按键扫描
{
static uchar key1_lock=0;//自锁标志
static uint count1=0;//计数延时
static uchar key2_lock=0;//
static uint count2=0;//
if(up==0)
{
if((count1++>=5)&&(key1_lock==0))
{
key1_lock=1;
LED1=~LED1;
}
}
else
{
key1_lock=0;
count1=0;
}
if(down==0)
{
if((count2++>=5)&&(key2_lock==0))
{
key2_lock=1;
LED1=~LED1;
}
}
else
{
key2_lock=0;
count2=0;
}
}
void Init2()//定时器2初始化
{
AUXR&=0x04; //12T
T2L=0x78; //延时5MS
T2H=0xec; //
IE2|=0x04;
AUXR|=0x10; //开始计时
EA=1;
}
void digplay(uint hig)//数码管动态扫描
{
static uchar i=0;
P3=0x00;
switch(i)
{
case 0:
P3=LED[hig/100];
P1=0x02;
P1=0x00;
i++;
break;
case 1:
P3=LED[hig/10%10];
P1=0x01;
P1=0x00;
i++;
break;
case 2:
P3=LED[hig%10];
P1=0x04;
P1=0x00;
i++;
break;
case 3:
P3=0x5e;
P1=0x08;
P1=0x00;
i=0;
break;
}
delayxms(5);
}
void main()//主程序
{
Init2();
P5M0=0xff;
P5M1=0x00;
P3M0=0xff;
P3M1=0x00;
P1M0=0xff;
P1M1=0xf0;
while(1)
{
digplay(hig);
}
}
void t2int() interrupt 12
{
KeyScan();
}
|