中断中的消抖问题
书上的源程序是这样的:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ziweima P2
#define zixingma P0
uchar code zixing[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code ziwei[]={0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
uchar zdcs;
sbit K1=P3^1;
void zd0int()
{
IT0=1;
EX0=1;
EA=1;
}
void delay(uint i)
{
uint j,k;
for(j=0;j<i;j++)
for(k=0;k<121;k++);
}
void display()
{
zixingma=zixing[8];
ziweima=ziwei[zdcs];
}
void zd0() interrupt 0 using 0
{
zdcs++;
if(zdcs==8)
zdcs=0;
}
void main()
{
zd0int();
while(1)
display();
}
书上后面说明,这个程序在硬件实现的时候存在抖动现象,如果采用软件消抖会影响中断的实时性
然后我想试着用软件消抖的方法试试,在终端函数中加了消抖语句,如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ziweima P2
#define zixingma P0
uchar code zixing[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code ziwei[]={0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
uchar zdcs;
sbit K1=P3^1;
void zd0int()
{
IT0=1;
EX0=1;
EA=1;
}
void display()
{
zixingma=zixing[8];
ziweima=ziwei[zdcs];
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void zd0() interrupt 0 using 0
{
delayms(100);
if(K1==0)
zdcs++;
if(zdcs==8)
zdcs=0;
}
void main()
{
zd0int();
while(1)
display();
}
然后数码管就死了,按键按也没有响应
最后还有一个问题,关于我位定义的按键K1,我明明定义的端口是P3^1,可是对应的却不是那个按键,无法我改成P3^0还是2,3都是同一个按键才有效,很奇怪,求解。
想问一下,zdcs是uchar类型,为什么要zdcs++,接着与整数变量8比较,之后赋予整数型变量0呢?