今天在学习数码管的被一个小细节困扰了一下,在此分享
如上面的题目,要求最后两位显示月份,并且这个月份要隔一段时间就要变化,一般而言,隔一段时间需要用执行空语句延时,像这样
void Delay(unsigned int t)
{
while(t--);
}
然而动态数码管显示正是利用视觉残留,空语句延时的话动态数码管效果就不能如意了,为了实现这种延时可以这样做
void DELAY1(unsigned int t)
{
while(t--)
{
xianshi();//数码管动态显示的程序
}
}
即在对 月份++ 的延时里嵌入动态显示的过程,使数码管正常显示。
我对这段代码有几点体会,一是这里的t每次减减 都要把动态显示的过程执行一遍,所以这个t不必取得太大即可完成对月份延时;二是延时函数的t注意选取合适类型,普通的空语句延时往往会把t设的很大,所以t不能设为char型;三是函数取名区分大小写。
而我的错误是惯性的在这个while(t--)后面加了分号,致使显示异常。
完整代码如下
#include"reg52.h"
unsigned char code SMG_duanma[18]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,
0xbf,0x7f};
unsigned char yu = 1;
void InitHC138(unsigned char n)
{
switch(n)
{
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;
}
}
void SMG_xuanze(unsigned char dat,unsigned pos)
{
InitHC138(6);
P0 = 0X01 << pos;
InitHC138(7);
P0 = dat;
}
void Delay(unsigned int t)
{
while(t--);
}
void xianshi()
{
SMG_xuanze(SMG_duanma[2],0);
Delay(500);
SMG_xuanze(SMG_duanma[0],1);
Delay(500);
SMG_xuanze(SMG_duanma[1],2);
Delay(500);
SMG_xuanze(SMG_duanma[8],3);
Delay(500);
SMG_xuanze(SMG_duanma[16],4);
Delay(500);
SMG_xuanze(SMG_duanma[16],5);
Delay(500);
SMG_xuanze(SMG_duanma[yu/10],6);
Delay(500);
SMG_xuanze(SMG_duanma[yu%10],7);
Delay(500);
}
void DELAY1(unsigned int t)
{
while(t--)
{
xianshi();
}
}
void InitSystem()
{
InitHC138(5);
P0 = 0x00;
}
void main()
{
InitSystem();
while(1)
{
xianshi();
yu++;
if(yu > 12)
{
yu = 1;
}
DELAY1(500);
}
}
版权声明
本文仅代表作者观点,不代表xx立场。
本文系作者授权xx发表,未经许可,不得转载。
评论列表
发表评论