51单片机实现时钟

2019-04-15 15:54发布

四年前写的程序了,纪念一下曾经玩过的单片机 #include "IAP15F2K61S2.h" #include "delay.h" sbit seg1 = P2^7; sbit seg2 = P2^6; sbit seg3 = P2^5; sbit seg4 = P2^4; sbit seg5 = P2^3; sbit seg6 = P2^2; sbit seg7 = P2^1; sbit seg8 = P2^0; unsigned int n = 0; //定时器计数 unsigned char hour = 0; unsigned char minute = 0; unsigned char second = 0; unsigned char code Num[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff}; unsigned char Display[8] = {11,11,10,11,11,10,11,11}; void Init_timer() { TMOD = 0x02; //方式二 TH0 = 0x06; //赋初值(255-6=250) TL0 = 0x06; EA = 1; //打开总中断 ET0 = 1; //打开定时器0中断 TR0 = 1; //启动定时器0 } void SEG_Display() { seg1 = 0; P0 = Num[Display[0]]; Delay1ms(); seg1 = 1; P0=0xff; seg2 = 0; P0 = Num[Display[1]]; Delay1ms(); seg2 = 1; P0=0xff; seg3 = 0; P0 = Num[Display[2]]; Delay1ms(); seg3 = 1; P0=0xff; seg4 = 0; P0 = Num[Display[3]]; Delay1ms(); seg4 = 1; P0=0xff; seg5 = 0; P0 = Num[Display[4]]; Delay1ms(); seg5 = 1; P0=0xff; seg6 = 0; P0 = Num[Display[5]]; Delay1ms(); seg6 = 1; P0=0xff; seg7 = 0; P0 = Num[Display[6]]; Delay1ms(); seg7 = 1; P0=0xff; seg8 = 0; P0 = Num[Display[7]]; Delay1ms(); seg8 = 1; P0=0xff; } void Clock_Run() { Display[0] = hour / 10; Display[1] = hour % 10; Display[3] = minute / 10; Display[4] = minute % 10; Display[6] = second / 10; Display[7] = second % 10; } int main() { Init_timer(); while(1) { if(n>4000) { n = 0; second++; if(second == 60) { second = 0; minute++; if(minute ==60) { minute = 0; hour++; if(hour == 24) { hour = 0; } } } } Clock_Run(); SEG_Display(); } return 0; } void Tomer0() interrupt 1 { n++; }