源程序如下
#include <REGX51.H>
#include<intrins.h>
#define uchar unsigned char //宏定义
#define uint unsigned int //宏定义
sbit jian=P1^6;//设置
sbit jia=P1^7; //加
sbit md=P3^0;//减
sbit ss=P3^7;// 闪烁位
sbit CLK=P2^4; //11脚
sbit DAT=P2^5; //14脚
sbit RCLK=P2^6; //12脚
sbit A1=P2^0;
sbit A2=P2^1;
sbit A3=P2^2;
sbit A4=P2^3;
uchar code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80}; //共阴数据码管段码 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
uchar SW; //全局变量
void ys(uint x) //延时函数
{
uchar i,j; //定义两个变量
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void write595(unsigned char word)//写数据
{
uchar c,x;
x=duan[word];
for(c=0;c<8;c++)
{
CLK=0;
DAT=x&0x80;
x<<=1;
CLK=1;
}
RCLK=0;
RCLK=1;
}
void weih(uint a,b,c,d)
{
A1=a;
A2=b;
A3=c;
A4=d;
}
void xs(uchar h, m) //显示函数
{
uchar h1,h0,m1,m0; //定义局部变量时分的个位与十位
h1=h%10; //分离出时的十位
h0=h/10; //分离出时的个位
m1=m%10; //分离出分的十位
m0=m/10; //分离出分得个位
weih(1,1,1,1);
ys(15);
weih(0,1,1,1);
write595(h0);
ys(15);
weih(1,0,1,1);
write595(h1); //在这一位数码管上相一致显示小数点怎么实现?
ys(15);
weih(1,1,0,1);
write595(m0);
ys(15);
weih(1,1,1,0);
write595(m1);
ys(15);
}
void ajjc()
{
xs(SW,14);
if(jia==0)
{
ys(5);
if(jia==0)
{
SW++;
if(SW==100)
SW=0;
while(!jia)
{xs(SW,14);}
}
}
if(jian==0)
{
ys(5);
if(jian==0)
{
SW--;
if(SW==-1)
SW=99;
while(!jian)
{xs(SW,14);}
}
}
}
void main()
{
while(1)
{
//xs(SW,14); //调用显示函数
ajjc();
}
}
|