51单片机最完美数码管驱动,仿真无缺显,实物无鬼影,最高亮度无黯淡
驱动程序https://blog.csdn.net/xianfajushi/article/details/89448933
#include "reg52.h" unsigned char code ShuMaGuan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0X80};//0~9隐.小数点
unsigned char code WeiXuanBiao[] = { 0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f };
void ZhongDuanSheZhi()
{
TH0+=0X4c;/*定时器赋初值,11.0592Mz定时50ms触发中断,自动补偿方式*/
TL0+=0X00;
TMOD=0X01;
TR0=1;
ET0=1; //开启定时器,溢出不用
EA=1; //全局中断开关,溢出不用
}
void ShuMaXianShi(unsigned char s,unsigned char w,unsigned char d)
{//我写的最完整完美的51单片机数码管驱动
P0=WeiXuanBiao[w];
wei=1;
wei=0;
P0=ShuMaGuan[s]|ShuMaGuan[d];//d显示小数点11,10关闭
duan=1;
YanShi=255;while(YanShi--);
P0=ShuMaGuan[10];//清除本次段值免下次产生影响
duan=0;
}
void JiShi()
{
if(!(Js%2))
if(!k&&++Hm>=10) Hm=0;
if(Js>=20)
{
if(++Mo>=60) Mo=0;
Js=0;
}
}
void XianShi()
{
ShuMaXianShi(Hm,8,10);
ShuMaXianShi(Mo%10,7,11);
ShuMaXianShi(Mo/10,6,10);
ShuMaXianShi(10,4,10);
ShuMaXianShi(10,5,10);
ShuMaXianShi(10,1,10);
ShuMaXianShi(10,2,10);
ShuMaXianShi(10,3,10);
JiShi();
}
void main()
{
ZhongDuanSheZhi();
while(1)
{
// if(TF0){TF0=0;LED=!LED;ZhongDuanSheZhi();}//中断溢出
XianShi();
if(!QiTin)
{
//XianShi(/*巧借8个数码显示消抖*/);
YanShi=Js;
if(!TR0) TR0=k=1;//重启中断
while(Js==YanShi);//中断消抖
if(!QiTin) if(!k) TR0=k; else k=0;//关闭中断,消抖后清除标记
while(!QiTin) XianShi(/*按下期间显示*/);
}
}
}
void ZhongDuan() interrupt 1
{
++Js;
TH0+=0X4c;
TL0+=0X00;
}
1.原仿真显示缺字个位5和6,十位1和4.http://www.51hei.com/bbs/dpj-164030-1.html
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DE=P2^6;
sbit WE=P2^7;
uint m,m0,m1,m2,gew,shiw;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uint);
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
while(1)
{
WE=1;
P0=0xfe;
WE=0;
DE=1;
P0=table[shiw];
DE=0;
delay(20);
WE=1;
P0=0xfd;
WE=0;
DE=1;
P0=table[gew];
DE=0;
delay(20);
}
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
m0++;
if(m0==20)
{
m0=0;
m++;
if(m==60)
m=0;
shiw=m/10;
gew=m%10;
}
}
void delay(uint ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=110;j>ms;j--);
}
我给修改为如下代码解决了显示缺字问题:
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
while(1)
{
P0=0xfe;
WE=1;//调整代码顺序后缺显示现象解决了
WE=0;
DE=1;
P0=table[shiw];
DE=0;
delay(20);
P0=0xfd;
WE=1;
WE=0;
DE=1;
P0=table[gew];
DE=0;
delay(20);
}
}
2.实物开发板淡红鬼影,形成原因前次锁存的段数据在打开位锁存时就送给数码管了,虽然短暂却能让人眼看到,因此看到的是淡红;因为执行送新段数据必须是打开段锁存才能再次送入段数据,这是一个顺序的执行过程,因此锁存前次的段数据会显示出来虽然短暂却能让人眼看到,所以采取的措施是本次送的段数据延时后清除锁存,即下次再打开位时就不会有前次的段值显示出来了。
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
while(1)
{
P0=0xfe;
WE=1;
WE=0;
P0=table[shiw];
DE=1;
delay(20);
P0=0X00;//调整代码顺序后显示鬼影现象解决了
DE=0;
P0=0xfd;
WE=1;
WE=0;
P0=table[gew];
DE=1;
delay(20);
P0=0X00;
DE=0;
}
}
3.实物开发板数字显示闪烁
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
while(1)
{
P0=0xfe;
WE=1;
WE=0;
P0=table[shiw];
DE=1;
delay(255);
P0=0X00;//调整代码顺序后显示鬼影现象解决了
DE=0;
P0=0xfd;
WE=1;
WE=0;
P0=table[gew];
DE=1;
delay(255);
P0=0X00;
DE=0;
}
}
void delay(uchar ms)
{
while(ms--);
}