1.功能
2.程序
#include "reg52.h"
sbit C1 = P2^7;
sbit C2 = P2^6;
sbit C3 = P2^5;
sbit C4 = P2^4;
//定义共阳数码管无小数点的数字段码值
unsigned char SMGNoDot_CA[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
unsigned int count = 1000;
/*==================数码管动态显示专用延时函数==================*/
void DelaySMG(unsigned int t)
{
while(t--);
}
/*=====================单个数码管显示函数=======================*/
void DisPlaySMG_Bit(unsigned char pos, unsigned char dat)
{
P0 = 0xff; //消隐
switch(pos) //选择数码管位置
{
case 1:
C1 = 1; C2 = 0; C3 = 0; C4 = 0;
break;
case 2:
C1 = 0; C2 = 1; C3 = 0; C4 = 0;
break;
case 3:
C1 = 0; C2 = 0; C3 = 1; C4 = 0;
break;
case 4:
C1 = 0; C2 = 0; C3 = 0; C4 = 1;
break;
}
P0 = dat; //输出数码管显示的内容
}
/*====================4个数码管动态显示函数======================*/
void DisPlaySMG_4Bit(unsigned int dat)
{
DisPlaySMG_Bit(1, SMGNoDot_CA[dat / 1000]); //千位
DelaySMG(100);
DisPlaySMG_Bit(2, SMGNoDot_CA[(dat % 1000) / 100]); //百位
DelaySMG(100);
DisPlaySMG_Bit(3, SMGNoDot_CA[(dat % 100) / 10]); //十位
DelaySMG(100);
DisPlaySMG_Bit(4, SMGNoDot_CA[(dat % 10)]); //个位
DelaySMG(100);
}
/*===================4个数码管依次显示0~9函数====================*/
void DisPlaySMG_zero_nine()
{
char i;
C1 = 1;
C2 = 1;
C3 = 1;
C4 = 1;
for(i = 0; i < 10; i++)
{
P0 = SMGNoDot_CA[i];
DelaySMG(40000);
}
}
/*=================带数码管刷新的延时函数======================*/
void Delay(unsigned int t)
{
while(t--)
{
DisPlaySMG_4Bit(count);
}
}
/*==========================主函数============================*/
void main()
{
DisPlaySMG_zero_nine();
while(1)
{
DisPlaySMG_4Bit(count);
Delay(1000);
count++;
if(count == 3000)
{
count = 1000;
}
}
}