本帖最后由 wnwnwn 于 2013-1-3 13:07 编辑
大家好,本人是纯新手,用单片机实验板运行下面这段程序,数码管相邻位总是出现前一位的阴影。大家帮忙看下这段程序问题出在哪里,功能很简单独立按键控制三个功能加1(P^0)、减1(P1^1)、清0(P1^2)。P0口连接数码管,P2^7是段选,P2^6是位选。
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar code DSY_CODE[] = {0xc0, 0xf9, 0xa4, 0xb0,
0x99, 0x92, 0x82, 0xf8,
0x80, 0x90, 0xff};
uchar Num_Buffer[] = {0, 0, 0};
uchar Key_Code, Key_Counts = 0;
sbit dula = P2^7;
sbit wela = P2^6;
void Show_Counts_ON_DSY(void);
void DelayMS(uint x);
void main(void)
{
uchar i;
P0 = 0xff;
P1 = 0xff;
// P2 = 0x00;
Key_Code = 0xff;
while (1){
Show_Counts_ON_DSY();
P1 = 0xff;
Key_Code = P1;
if (Key_Code != 0xff){
// DelayMS(10); // 去抖动会产生数码管闪烁。
// if (Key_Code != 0xff){
while (P1 != 0xff)
for (i = 0; i < 30; i++)
Show_Counts_ON_DSY();
}
switch (Key_Code){
case 0xfe: if (Key_Counts < 255) Key_Counts++; break;
case 0xfd: if (Key_Counts > 0) Key_Counts--; break;
case 0xfb: Key_Counts = 0; break;
}
Key_Code = 0xff;
}
}
void Show_Counts_ON_DSY(void)
{
uchar i, j = 0xfe;
Num_Buffer[2] = Key_Counts / 100;
Num_Buffer[1] = Key_Counts / 10 % 10;
Num_Buffer[0] = Key_Counts % 10;
for (i = 0; i < 3; i++){
P0 = 0x00; //消隐
j = _cror_(j, 1);
P0 = j;
wela = 1;
wela = 0;
P0 = ~DSY_CODE[Num_Buffer
];
dula = 1;
dula = 0;
DelayMS(8); //扫描间隙延迟,太长会造成闪烁,太短会造成重影。就是这个延迟设成1ms拖影很明显,设成 10ms就会出现数码管闪烁,取8ms拖影小但还是存在。
}
}
void DelayMS(uint x)
{
uchar i;
while (x--)
for (i = 0; i < 110; i++);
}
想再请教一下,按键去抖动的10ms产生的数码管闪烁问题怎么解决呢?
一周热门 更多>