有关的硬件原理图:
c程序:
//温馨提示:
/*程序还没有调试完结,实践电路板调理时刻不时-分-秒都会有闪耀现象,其实我是不知道的,why,也求解说
#include //包括头文件,一般状况不需要改动,头文件包括特别功用寄存器的界说
#include //与_nop_有关
sbit duan=P2^2; //界说锁存使能端口 段锁存
sbit wei=P2^3; // 位锁存
sbit key1 = P1^1;
sbit key2 = P1^2;
sbit key3 = P1^3;
sbit key4 = P1^4;
unsigned char code duanma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // 显现段码值0~9
unsigned char code weima[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //别离对应相应的数码管点亮,即位码
unsigned char temp[8]; //存储显现值的全局变量
char hour; minute; second, num, t, i;
//函数声明
void delay(unsigned int t);
void iniTIal();
void delay20ms();
void keyscan();
void caifen();
void display();
/*————————————————
主函数
————————————————*/
void main()
{
iniTIal(); //调用初始化程序
while(1)
{
keyscan(); //调用键盘扫描
caifen(); //调用拆分以时-分-秒的方法
display(); //调用时刻的显现程序
}
}
//初始化程序
void iniTIal()
{
TMOD = 0x01; //设置T1作业于守时方法1(有16位计数)
TH0 = (65536 – 2000)/256; //守时器1赋初值,是TH1而不是TH0噢(若要设置是守时器0的话,把1改为0,下同);计数了2000次,即为2ms
TL0 = (65536 – 2000)%256;
EA = 1; //开中止
TR0 = 1; //开守时器0
ET0 = 1; //敞开守时器中止0
hour = 23; //赋初值
minute = 59; //赋初值
second = 57; //赋初值
}
//中止服务程序
void TIme0(void) interrupt 1
{
TH0 = (65536 – 2000)/256; //从头赋值
TL0 = (65536 – 2000)%256;
num ++; //中止500次后 即2ms X 500 = 1000MS = 1S,(一般可计数久点如50000ms,中止20次,这样差错就不会太大)
if (num == 500)
{
num = 0;
second ++;
if (second == 60) //秒为60则miao置0,minute加一
{
second = 0;
minute ++;
if (minute == 60) //分为60则minute置0,hour加一
{
minute = 0;
hour ++;
if (hour == 24) //小时为24则hour置0
{
hour = 0;
}
}
}
}
}
//键盘扫描程序,可履行时分的加减
void keyscan()
{
if(key1 == 0)
{
delay20ms(); //延时20ms,去颤动
while(key1 == 0); //等候(上升沿则履行下一行,TRO = 0)
TR0 = 0; //关守时器0
t ++; //t++与调理时分秒有关
}
switch(t) //挑选调理时分秒有关,与上面的t相关联
{
case 1:
if(key2 == 0)
{
delay20ms();
if(key2 == 0)
{
while(key2 == 0);
hour ++;
if(hour == 24)
{
hour = 0;
}
}
}
if(key3 == 0)
{
delay20ms();
if(key3 == 0)
{
while(key3 == 0);
hour –;
if(hour == -1) //由于hour是char型,所以能够有负数,若是unsigned char则不答应,下面的minute同理
{
hour =23;
}
}
}
break;
case 2:
if(key2 == 0)
{
delay20ms();
if(key2 == 0)
{
while(key2 == 0);
minute ++;
if(minute == 60)
{
minute = 0;
}
}
}
if(key3 == 0)
{
delay20ms();