原本是数码管动态扫描并用按键调加减数字变化,刚开始是OK的,在写EEPROM保存数据设置,后来更改数码管扫描方式按键没作用了,如把显示取消后,按键就有作用,现用LED作测试,发现系统只能执行一道任务类似,MCU用的是STC15W408AS,按键改成最老方式和状态按键都是这样,硬件也更换MCU了,这几天都搞晕了,请各位大神帮忙看看指点下。谢谢!
请看下面的程序 :
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();
}
|