菜鸟写的程序。。。 没什么高级。。。至少没有BUG 高手勿喷。。。
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
code uint8 x[] = {0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xf0,0x00,0x20,0xff};
code uint8 y[] = {0xf2,0x48,0x60,0x32,0x24,0x04,0x00}; //星期专用。。。
uint8 ss1,ss2,ss3,ss4,ss5,ss6,ss7,ss8,
k,k1,k2,k3,k4,k5,k6,k7,k8 = 1,k9 = 10,k10 = 1,k11 = 10,k12 = 1,k13 = 1,k14,k15 = 2,
t,t1 = 1,q;
void shizhongjinwei() //时钟进位函数 中断调过去刷一下。。。。。~
{
if(t == 4) //秒 十分位
{
t = 0;k++;
}
if(k == 10) //秒 个位
{
k = 0;k1++;
}
if(k1 == 10) //秒 十位
{
k1 = 0;k2++;
}
if(k2 == 6) //分 个位
{
k2 = 0;k3++;
}
if(k3 == 10) //分 十位
{
k3 = 0;k4++;
}
if(k4 == 6) //时 个位
{
k4 = 0;k5++;
}
if(k5 == 10) //时 十位
{
k5 = 0;
if(k6 == 10)
{
k6 = 0;
}
k6++;
}
if(k6 == 2)
{
if(k5 == 4)
{
k6 = 0;k5 = 0;k7++;k8++; //k8++日期进位
}
}
if(k6 == 0) //零点跳到一点时灭掉第7位数码管 亮着那个零 浪费电啊???- -
{
if(k5 == 1)
{
k6 = 10;
}
}
if(k7 == 7) //星期。。。
{
k7 = 0;
}
}
void rilijinwei() //日历进位函数 中断调过去刷一下~~。。。。。
{
static uint8 n; //这里定uint8 不会影响程序。。。 不用uint16有原因的。。。会影响时钟的精确
n = k15 * 1000 + k14 * 100 + k13 * 10 + k12; //不要把这个代到下面if里面的n 。。。原因同上
if(k8 == 10) //日
{
k8 = 0;
if(k9 == 10)
{
k9 = 0;
}
k9++;
}
if(t1 == 1 || t1 == 3 || t1 == 5 || t1 == 7 || t1 == 8 || t1 == 10 || t1 == 12) //大月31天++进位
{
if(k9 == 3)
{
if(k8 == 2)
{
k9 = 10;k8 = 1;k10++;t1++;
}
}
}
if(t1 == 4 || t1 == 6 || t1 == 9 || t1 == 11) //小月30天++进位
{
if(k9 == 3)
{
if(k8 == 1)
{
k9 = 10;k8 = 1;k10++;t1++;
}
}
}
if(n % (n % 100 ? 4 : 400) == 0) //判断闰年 是就2月29天++进位~~~
{
if(t1 == 2)
{
if(k9 == 3)
{
if(k8 >= 0)
{
k9 = 10;k8 = 1;k10++;t1++;
}
}
}
}
else //不是闰年就28天++进位 ~~~
{
if(t1 == 2)
{
if(k9 == 2)
{
if(k8 == 9)
{
k9 = 10;k8 = 1;k10++;t1++;
}
}
if(k9 == 3)
{
if(k8 >= 0)
{
k9 = 10;k8 = 1;k10++;t1++;
}
}
}
}
if(k10 == 10) //月
{
k10 = 0;
if(k11 == 10)
{
k11 = 0;
}
k11++;
}
if(k11 == 1)
{
if(k10 == 3)
{
k11 = 10;k10 = 1;t1 = 1;k12++;
}
}
if(k12 == 10) //年
{
k12 = 0;k13++;
}
if(k13 == 10)
{
k13 = 0;k14++;
}
if(k14 == 10)
{
k14 = 0;k15++;
}
if(k15 == 10)
{
k15 = 0;
}
}
main()
{
uint8 ss9 = 1;
uint16 j;
TMOD = 0x11;
TH0 = 0x3c; //定时器0(0.025秒的发生器 专用的) 做了调校。。。不然会有误差 = =~!
TL0 = 0xbc;
TR0 = 1;
TH1 = 0xf2; //定时器1(数码管动态扫描和进位函数刷新)。。。。。。
TL1 = 0x0c;
TR1 = 1;
ET0 = 1;
ET1 = 1;
EA = 1;
while(1) //按键控制部份。。。~~~~
{
if(ss9 == 1)
{
if(P2 == 0xf7) //按键1 显示切换 P2.3 IO口。。。
{
for(j = 0;j < 7000;j++);
if(P2 == 0xf7)
{
q++;
if(q == 2)
{
q = 0;
}
}
while(P2 == 0xf7);
}
}
if(ss9 == 0)
{
if(P2 == 0xf7) //按键1 当数码管为位选择时 切换为 被选位减减 P2.3 IO口。。。
{
for(j = 0;j < 7000;j++);
if(P2 == 0xf7)
{
if(q == 0)
{
if(ss1 == 1)
{
if(k7 >= 1)
{
k7--;
}
}
if(ss2 == 1)
{
if(k6 == 1 || k6 == 2)
{
if(k5 == 0)
{
k6--;k5 = 10;
if(k6 == 0)
{
k6 = 10;
}
}
}
if(k5 >= 1)
{
k5--;
if(k5 == 0)
{
if(k6 == 10)
{
k6 = 0;
}
}
}
}
if(ss4 == 1)
{
if(k4 >= 1)
{
k4--;
}
}
if(ss5 == 1)
{
if(k3 >= 1)
{
k3--;
}
}
if(ss6 == 1)
{
if(k2 >= 1)
{
k2--;
}
}
if(ss7 == 1)
{
if(k1 >= 1)
{
k1--;
}
}
if(ss8 == 1)
{
if(k >= 1)
{
k--;
}
}
}
if(q == 1)
{
if(ss1 == 1)
{
if(k15 >= 1)
{
k15--;
}
}
if(ss2 == 1)
{
if(k14 >= 1)
{
k14--;
}
}
if(ss3 == 1)
{
if(k13 >= 1)
{
k13--;
}
}
if(ss4 == 1)
{
if(k12 >= 1)
{
k12--;
}
}
if(ss5 == 1)
{
if(k11 == 1)
{
if(k10 == 0)
{
k11--;k10 = 10;
if(k11 == 0)
{
k11 = 10;
}
}
}
if(k11 == 1)
{
if(k10 >= 1)
{
k10--;
}
}
if(k11 == 10)
{
if(k10 >= 2)
{
k10--;
}
}
if(t1 >= 2)
{
t1--;
}
}
if(ss7 == 1)
{
if(k9 == 1 || k9 == 2 || k9 == 3)
{
if(k8 == 0)
{
k9--;k8 = 10;
if(k9 == 0)
{
k9 = 10;
}
}
}
if(k9 == 1 || k9 == 2 || k9 == 3)
{
if(k8 >= 1)
{
k8--;
}
}
if(k9 == 10)
{
if(k8 >= 2)
{
k8--;
}
}
}
}
}
while(P2 == 0xf7);
}
}
if(P2 == 0xfb) //按键2 数码管位的选择(调时间用) P2.2 IO口。。。
{
for(j = 0;j < 7000;j++);
if(P2 == 0xfb)
{
ss1++;ss2++;ss3++;ss4++;ss5++;ss6++;ss7++;ss8++;ss9++;
if(ss1 == 1)
{
ss2 = 0;ss3 = 0;ss4 = 0;ss5 = 0;ss6 = 0;ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss2 == 1)
{
if(q == 1)
{
ss3 = 0;
}
ss4 = 0;ss5 = 0;ss6 = 0;ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss3 == 1)
{
ss4 = 0;ss5 = 0;ss6 = 0;ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss4 == 1)
{
ss5 = 0;ss6 = 0;ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss5 == 1)
{
if(q == 0)
{
ss6 = 0;
}
ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss6 == 1)
{
ss7 = 0;ss8 = 0;ss9 = 0;
}
if(ss7 == 1)
{
if(q == 0)
{
ss8 = 0;
}
ss9 = 0;
}
if(ss8 == 1)
{
ss9 = 0;
}
if(ss9 == 1)
{
ss1 = 0;
}
}
while(P2 == 0xfb);
}
if(P2 == 0xfd) //每按一次数码管的被选位加加 P2.1 IO口。。。
{
for(j = 0;j < 7000;j++);
if(P2 == 0xfd)
{
if(q == 0)
{
if(ss1 == 1)
{
k7++;
}
if(ss2 == 1)
{
k5++;
}
if(ss4 == 1)
{
k4++;
}
if(ss5 == 1)
{
k3++;
}
if(ss6 == 1)
{
k2++;
}
if(ss7 == 1)
{
k1++;
}
if(ss8 == 1)
{
k++;
}
}
if(q == 1)
{
if(ss1 == 1)
{
k15++;
}
if(ss2 == 1)
{
k14++;
}
if(ss3 == 1)
{
k13++;
}
if(ss4 == 1)
{
k12++;
}
if(ss5 == 1)
{
k10++;t1++;
}
if(ss7 == 1)
{
k8++;
}
}
}
while(P2 == 0xfd);
}
if(P2 == 0xfe) //时间计时 开始/暂停 P2.0 IO口。。。
{
for(j = 0;j < 7000;j++);
if(P2 == 0xfe)
{
TR0 = ~TR0;
}
while(P2 == 0xfe);
}
}
}
void zhongduan1() interrupt 1 //定时器0中断函数 0.025秒时钟计时~~~~
{
TH0 = 0x3c;
TL0 = 0xbc;
t++;
}
void zhongduan2() interrupt 3 //定时器1中断函数 动态扫描和显示切换~~~~~
{
static uint8 s1,tt,r;
TH1 = 0xf2; //定时刷新560hz 每个数玛管刷70hz就OK了。。不要太高。。否则。。。
TL1 = 0x0c;
tt++;
s1++;
if(tt == 50)
{
tt = 0;r++;
if(r == 2)
{
r = 0;
}
}
shizhongjinwei(); //刷新时钟进位函数 刷560hz是够的。。。
rilijinwei(); //刷新日历进位函数
if(s1 == 1) //刷第1位数码管
{
if(ss8 == 0,2)
{
if(q == 0)
{
P0 = 0xfe[s:9]1 = x[k];
}
if(q == 1)
{
P0 = 0xfe[s:9]1 = x[k8];
}
}
if(ss8 == 1)
{
if(r == 1)
{
P0 = 0xfe[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xfe[s:9]1 = x[k];
}
if(q == 1)
{
P0 = 0xfe[s:9]1 = x[k8];
}
}
}
}
if(s1 == 2) //刷第2位数码管
{
if(ss7 == 0,2)
{
if(q == 0)
{
P0 = 0xfd[s:9]1 = x[k1];
}
if(q == 1)
{
P0 = 0xfd[s:9]1 = x[k9];
}
}
if(ss7 == 1)
{
if(r == 1)
{
P0 = 0xfd[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xfd[s:9]1 = x[k1];
}
if(q == 1)
{
P0 = 0xfd[s:9]1 = x[k9];
}
}
}
}
if(s1 == 3) //刷第3位数码管
{
if(ss6 == 0,2)
{
if(q == 0)
{
P0 = 0xfb[s:9]1 = x[k2];
}
if(q == 1)
{
P0 = 0xfb[s:9]1 = x[k10];
}
}
if(ss6 == 1)
{
if(r == 1)
{
P0 = 0xfb[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xfb[s:9]1 = x[k2];
}
if(q == 1)
{
P0 = 0xfb[s:9]1 = x[k10];
}
}
}
}
if(s1 == 4) //刷第4位数码管
{
if(ss5 == 0,2)
{
if(q == 0)
{
P0 = 0xf7[s:9]1 = x[k3];
}
if(q == 1)
{
P0 = 0xf7[s:9]1 = x[k11];
}
}
if(ss5 == 1)
{
if(r == 1)
{
P0 = 0xf7[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xf7[s:9]1 = x[k3];
}
if(q == 1)
{
P0 = 0xf7[s:9]1 = x[k11];
}
}
}
}
if(s1 == 5) //刷第5位数码管
{
if(ss4 == 0,2)
{
if(q == 0)
{
P0 = 0xef[s:9]1 = x[k4];
}
if(q == 1)
{
P0 = 0xef[s:9]1 = x[k12];
}
}
if(ss4 == 1)
{
if(r == 1)
{
P0 = 0xef[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xef[s:9]1 = x[k4];
}
if(q == 1)
{
P0 = 0xef[s:9]1 = x[k12];
}
}
}
}
if(s1 == 6) //刷第6位数码管
{
if(ss3 == 0,2)
{
if(q == 0)
{
P0 = 0xdf[s:9]1 = x[k5];
}
if(q == 1)
{
P0 = 0xdf[s:9]1 = x[k13];
}
}
if(ss3 == 1)
{
if(r == 1)
{
P0 = 0xdf[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xdf[s:9]1 = x[k5];
}
if(q == 1)
{
P0 = 0xdf[s:9]1 = x[k13];
}
}
}
}
if(s1 == 7) //刷第7位数码管
{
if(ss2 == 0,2)
{
if(q == 0)
{
P0 = 0xbf[s:9]1 = x[k6];
}
if(q == 1)
{
P0 = 0xbf[s:9]1 = x[k14];
}
}
if(ss2 == 1)
{
if(r == 1)
{
P0 = 0xbf[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0xbf[s:9]1 = x[k6];
}
if(q == 1)
{
P0 = 0xbf[s:9]1 = x[k14];
}
}
}
}
if(s1 == 8) //刷第8位数码管
{
if(ss1 == 0,2)
{
if(q == 0)
{
P0 = 0x7f[s:9]1 = y[k7];
}
if(q == 1)
{
P0 = 0x7f[s:9]1 = x[k15];
}
}
if(ss1 == 1)
{
if(r == 1)
{
P0 = 0x7f[s:9]1 = x[10];
}
if(r == 0)
{
if(q == 0)
{
P0 = 0x7f[s:9]1 = y[k7];
}
if(q == 1)
{
P0 = 0x7f[s:9]1 = x[k15];
}
}
}
s1 = 0;
}
}
200字以内,仅用于支线交流,主线讨论请采用回复功能。