基于51单片机的电子钟C语言程序.doc
基于51单片机的电子钟C语言程序includereg51.hincludeabsacc.hdefine uchar unsigned chardefine uint unsigned int/*七段共阴管显示定义*/ uchar code dispcode 0 x3F,0 x06,0 x5B,0 x4F,0 x66,0 x6D,0 x7D,0 x07,0 x7F,0 x6F, 0 xBF,0 x86,0 xCB,0 xCF,0 xEF,0 xED,0 xFD,0 x87,0 xFF,0 xDF;/*定义并初始化变量*/uchar seconde0;uchar minite0;uchar hour12;uchar mstcnt0;sbit P1_0P10; second 调整定义sbit P1_1P11; minite调整定义sbit P1_2P12; hour调整定义/*函数声明*/void delayuint k ; 延时子程序void delay1uchar h ;void time_pro ; 时间处理子程序void display ; 显示子程序void keyscan ; 键盘扫描子程序/**延时子程序*/void delay1 uchar huchar j;whileh0 forj0;j125;j ; void delay uint kuint a,b,c;forck;c0;c forb38;b0;b fora130;a0;a; /*时间处理子程序*/void time_pro void ifseconde60 秒钟设为60进制 seconde0; minite; if minite60 分钟设为60进制 minite0; hour; ifhour24 时钟设为24进制 hour0; /** 显示子程序**/void displayvoid P20 xfe; P0dispcodehour/10; 显示小时的十位 delay14; P20 xfd; P0dispcodehour10|0X80; 显示小时的个位 delay14; P20 xfb; P0dispcodeminite/10; 显示分的十位 delay14; P20 xf7; P0dispcodeminite10|0X80; 显示分的个位 delay14; P20 xef; P0dispcodeseconde/10; 显示秒的十位 delay14; P20 xdf; P0dispcodeseconde10; 显示秒的个位 delay14; /**键盘扫描子程序**/void keyscan void ifP1_00 按键1秒的调整 delay2; ifP1_00 delay300; seconde; ifseconde60 seconde0; ifP1_10 按键2分的调整 delay2; ifP1_10 delay300; minite; ifminite60 minite0; ifP1_20 按键3小时的调整 delay2; ifP1_20 delay300; hour; ifhour24 hour0; void timer0void interrupt 1 using 0 定时器0方式1,50ms中断一次 TH00 x3c;TMOD 0 x11; mstcnt; ifmstcnt20 seconde; mstcnt0; 注意点。对计数单元的清零十分的重要,本次调试中就是 因为忽略了这一点,给我早成了很大的被动。 /*主函数*/void mainvoid P10 xff; 初始化p1口,全设为1 TMOD 0 x11; time0为定时器,方式1TH00 x3c; 预置计数初值TL00 xb0;EA1; ET01; TR01; while 1 keyscan ; 按键扫描 time_pro ; 时间处理 display ; 显示时间