The stopwatch counting function is implemented using timers and external interrupts. The number 8th button represents the start/end of the timer, the C key represents a reset of 0, and is limited to 15 seconds for demonstration purposes.
There is a detailed comment in the code:
#include <reg52.h>sbit wei=p2^7;sbit duan=p2^6;sbit l1=p3^4;unsigned char code dofly_duanma[11]={0x3f,0x06,0x5b , 0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//displays the segment code value 0~9unsigned char code DOFLY_WEIMA[]={0XFE,0XFD,0XFB,0XF7,0XEF, 0xdf,0xbf,0x7f};//corresponding to the corresponding digital tube lit, the accession code void delay (int t) {while (t--);} The first parameter I represents the control of the first few digital tubes, n represents the number to be displayed in void screen (int i,int n) {p0=0; Duan=1; duan=0; P0=dofly_weima[i]; Wei=1; wei=0; P0=dofly_duanma[n]; Duan=1; duan=0;} int Sec;int num;int i;int flag;int Main () {flag=1; i=0; num=0;sec=0; wei=0; duan=0; Open interrupt ex0=1; It0=1; Ex1=1; It1=1; Timer initialization tmod |= 0x01; Ea=1; Et0=1; Tr0=1; l1=0; while (1) {if (num>=10) screen (3,NUM/10); Screen (4,NUM%10); Delay (10); Screen (5,10); Screen (6,SEC/10); Screen (7,sec%10);} return 0;} void Timer_0 () Interrupt 1 using 1{th0= (65536-10000)/256; TL0= (65535-10000)%256; if (flag==1) {i++; Sec++;if (sec==100)//0.1mssec=0; if (i==101)//1s {i=0; num++; if (num==15) num=0; }}}void ACTION1 () interrupt 0 {Flag=!flag;} void ACTION2 () Interrupt 2 {if (flag==0) {screen (7,0); num=0;sec=0; }}
Single-chip microcomputer: C language Implementation stopwatch count (start, end, reset button)