The timer of 51 single-chip microcomputer is 0 in the way of 1 timing. The result is displayed with four digital tubes, which are 10 seconds, 1 second, milliseconds, and 10 milliseconds. Therefore, the maximum time is 1 minute, accurate to 0.01 s. At the same time, add three independent buttons to control the reset, pause, and start functions of the stopwatch.
According to the modular idea, first write the functions and predefined values to a header file. The predefined values and global variables to be used are as follows:
# Include <reg52.h> # define uchar unsigned charsbit clear = P3 ^ 7; // press the key to clear sbit stop = P3 ^ 6; // press the key to pause sbit restart = P3 ^ 5; // press the key to start sbit Dula = P2 ^ 6; // select the P2 ^ 6 port sbit wela = P2 ^ 7; // path selection is connected to port P2 ^ 7 // The digital display is 0 ~ In the Table Array ~ Uchar code table [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71}; // The timing time. If the timer is interrupted once, 1 is added. The maximum value is 5999int time;
The following is the function declaration to be used:
// Void delay (), a short latency function for dynamic scanning of digital tubes; // void longdelay (), a long latency function for keyboard jitter elimination (); // initialize void in (); // display ten seconds, one second, one hundred seconds, and ten microseconds in the digital tube. // It is difficult to express it in pinyin, let's take a look at Void display (uchar shimiao, uchar gemiao, uchar baiwei, uchar Shiwei); // the zeroth function. Call void CLC () When pressing the zeroth key; // pause the function, call void stoptimer () when you press the sequence key; // start or start the function again, call void restarttimer () when you press the START key; // scan the function on the keyboard to see if a key is pressed, and execute the corresponding operation void keyscan ();
Next is the main function:
Void main () {uchar shimiao, gemiao, baiwei, Shiwei; Inner (); // initialize while (1) {If (time = 6000) // to the maximum stopwatch value, auto return to {time = 0;} // The number of digits to be displayed, shimiao = Time/1000; gemiao = time % 1000/100; Wei = time % 100/10; shiwei = time % 10; // display (shimiao, gemiao, baiwei, Shiwei); includelay (); // Keyboard Scan, keyscan ();}}
The following job is to write the corresponding processing function:
There are two delay functions:
void ShortDelay(){ uchar a=100; while(a--);}void LongDelay(){ int a=5000; while(a--);}
Initialization function:
Void inula () {Dula = 0; wela = 0; time = 0; EA = 1; et0 = 1; // enable the timer 0 interrupt tmod = 0x01; th0 = 0xdc; tl0 = 0x01; // initial loading value. The timing method is 1 clear = 1; stop = 1; restart = 1; // The output height of the key interface pin}
Function for displaying time on a digital tube:
Void display (uchar shimiao, uchar gemiao, uchar baiwei, uchar Shiwei) {// output 10 pieces of wela = 1; p0 = 0xfb; wela = 0; p0 = 0xff; dula = 1; p0 = table [shimiao]; Dula = 0; delayed delay (); // output second bit wela = 1; p0 = 0xf7; wela = 0; p0 = 0xff; dula = 1; p0 = table [gemiao]; Dula = 0; delayed delay (); wela = 1; p0 = 0xef; wela = 0; p0 = 0xff; Dula = 1; p0 = table [tailwei]; Dula = 0; includelay (); wela = 1; p0 = 0xdf; wela = 0; p0 = 0xff; Dula = 1; p0 = table [Shiwei]; Dula = 0; delayed delay ();}
Keyboard Scan function:
Void keyscan () {If (clear = 0) {longdelay (); // deshake if (clear = 0) // It is indeed pressed {// do somthing CLC () ;}// the restart and stop detection methods are the same if (stop = 0) {longdelay (); if (stop = 0) {// do somthing stoptimer () ;}} if (restart = 0) {longdelay (); If (restart = 0) {// do somthing restarttimer ();}}}
Finally, the processing function after the three buttons are pressed:
Void CLC () {tr0 = 0; // time temporarily stopped time = 0; // time returns to zero display (0000,); // display delayed delay ();} void stoptimer () {// timing is temporarily stopped, only tr0 = 0;} void restarttimer () {// timing starts again, only tr0 = 1 ;}
So far, the. HEX file generated after the link is compiled can be downloaded to the 51 microcontroller.