程序是某一个宝买的4位共阳数码管模块(带两块595)资料改的,原程序只能显示4位整数(少一位都会出错),我改成了能动态显示0~9999任意带小数点的实数,随便几位,实测都完美显示(比如原数3.14159、显示3.141,原数3.02、显示3.020)!
有喜欢的拿走。。。
单片机代码附上
#include <reg52.h> //头文件
#include <intrins.h>
#define uchar unsigned char // 数据类型简化-起别名
#define uint unsigned int
uchar i, duan, wei,dw;
uint num;
float x= 24.15; //0~9999任意实数
static uchar WEI;
sbit DIO = P1^4;
sbit R_CLK = P1^3;
sbit S_CLK = P1^2;
uchar code smgduan[] = {0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xfd,0xff,0xfe}; //{0,1,2,3,4,5,6,7,8,9,-,全灭,.}
uchar code smgwei[] = {0x10,0x20,0x40,0x80};
void display(num)
{
if (x>=0 && x<10) {num=x*1000;dw=0;}
if (x>=10 && x<100) {num=x*100;dw=1;}
if (x>=100 && x<1000) {num=x*10;dw=2;}
if (x>=1000 && x<10000) {num=x*1;dw=3;}
switch(WEI)
{
case 0 :
if (dw==0)
{duan = smgduan[num / 1000 % 10]& smgduan[12] ; break; }
else duan = smgduan[num / 1000 % 10];break;
case 1 :
if (dw==1)
{ duan = smgduan[num / 100 % 10]& smgduan[12] ; break; }
else duan = smgduan[num / 100 % 10]; break;
case 2 :
if (dw==2)
{ duan = smgduan[num / 10 % 10]& smgduan[12] ; break; }
else duan = smgduan[num / 10 % 10]; break;
case 3 :
if (dw==3)
{ duan = smgduan[num % 10]& smgduan[12] ; break; }
else duan = smgduan[num % 10]; break;
}
for(i = 0; i < 8; i ++)
{
S_CLK = 0;
R_CLK = 0;
if(duan & 0x01) DIO = 1;
else DIO = 0;
S_CLK = 1;
duan >>= 1; //从最低位读入
}
wei = smgwei[WEI];
for(i = 0; i < 8; i ++)
{
S_CLK = 0;
R_CLK = 0;
if(wei & 0x01) DIO = 1;
else DIO = 0;
S_CLK = 1;
wei >>= 1;
}
R_CLK = 1;
WEI ++;
if(WEI == 4) WEI = 0;
}
void timer0Init() //定时器0初始化
{
EA = 1; //打开总中断
ET0 = 1;//打开定时器0中断
TR0 = 1; //启动定时器0
TMOD |= 0X01; //定时器工作模式1,16位定时模式
TH0 = 0xf5;
TL0 = 0x33; //定时3ms
}
void main()
{
timer0Init();
while(1);
}
void timer0() interrupt 1 //定时器中断服务函数
{
TH0 = 0xf5;
TL0 = 0x33; //定时3ms
display(num);
}
|