本文写了数码管的动态显示,比如说显示一个年份2024 0126
详见代码。
#include<STC15F2K60S2.H>
#include<intrins.h>
unsigned char code a[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void Delay2ms() //@12.000MHz
{
unsigned char data i, j;
i = 24;
j = 85;
do
{
while (--j);
} while (--i);
}
void HC138(unsigned char channel)
{
switch(channel)
{
case 4: P2=P2&0x1f|0x80;break;
case 5: P2=P2&0x1f|0xa0;break;
case 6: P2=P2&0x1f|0xc0;break;
case 7: P2=P2&0x1f|0xe0;break;
case 8: P2=P2&0x1f;break;
}
}
void SMG(unsigned char dat, unsigned char pos)
{
HC138(6); //下一个函数紧接着就可以用到上一个自定义的函数。
P0=0x01<<pos;
HC138(8);
HC138(7);
P0=dat;
HC138(8);
}
void smg()
{
SMG(a[2],0);
Delay2ms();
SMG(a[0],1);
Delay2ms();
SMG(a[2],2);
Delay2ms();
SMG(a[4],3);
Delay2ms();
SMG(a[0],4);
Delay2ms();
SMG(a[1],5);
Delay2ms();
SMG(a[2],6);
Delay2ms();
SMG(a[6],7);
Delay2ms();
}
void main()
{
P2=P2&0x1f|0x80;
P0=0xff;
P2=P2&0x1f;
P2=P2&0x1f|0xa0;
P0=0x00;
P2=P2&0x1f;
while(1)
{
smg();
}
}