本帖最后由 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);
- }
复制代码结果如下:
#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) ;
}
一周热门 更多>