本帖最后由 renwocai 于 2012-5-30 11:33 编辑
比如4位共阳数码管,我希望它显示0123,结果只显示012,段码接P0口,位码接P2.0到P2.3.
程序如下:
- #include <at89x52.h>
- #define uchar unsigned char
- uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar i=0;
- sbit h4=P2^3; //共阳,低电平有效
- sbit h3=P2^2;
- sbit h2=P2^1;
- sbit h1=P2^0;
- void timer0()interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- switch(i)
- {
- case 0:
- {
- h1=1;h2=0;h3=0;h4=0;P0=tab[0];
- };break;
- case 1:
- {
- h1=0;h2=1;h3=0;h4=0;P0=tab[1];
- };break;
- case 2:
- {
- h1=0;h2=0;h3=1;h4=0;P0=tab[2];
- };break;
- case 3:
- {
- h1=0;h2=0;h3=0;h4=1;P0=tab[3];
- };break;
- }
- i++;
- if(i==4)
- i=0;
- }
- main()
- {
- TMOD=0X01;
- EA=1;
- ET0=1;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TR0=1;
- while(1);
- }
复制代码结果如下:
这个问题有多种方法可以解决,除了上面说明的外,还可以直接把if(i==4) i=0;放到switch前面,switch里面不用修改
#define uchar unsigned char
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar i=0;
void timer0() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
P2 = 0x01 << i;
P0 = tab[i];
if(++i >= 4)
i = 0;
}
void main(void)
{
TMOD = 0x01;
EA = 1;
ET0 = 1;
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
TR0 = 1;
while(1) ;
}
As you can see, h4 did go high and the desired value (0xb0) is written on P0 when h4 went high.
what you observed is proteus' animation anomaly.
the lesson learned here is that simulation is only useful if you understand its limitations.
bad C51 leds.PNG (30.54 KB, 下载次数: 1)
下载附件
2012-5-30 07:16 上传
一周热门 更多>