Making a stopwatch timer, the recording time of 0-15 seconds. It requires accurate and controllable.
Claim:
1. Start the stopwatch can be controlled with the buttons
2. The button can be controlled by stopping the stopwatch
3. The key can be used to control the clock to zero
4. Use a timer to control the time precision is required.
5. Using a plurality of digital control and implement the functions keys.
Step: add a circuit diagram give two buttons, as shown below.
Step two: write code on demand, and run.
. 1 #include <reg52.h> 2 #define the LED P0 . 3 the sbit key1 Pl ^ = 0 ; // start, pause button . 4 the sbit key2 = Pl ^ . 1 ; // reset button . 5 unsigned int I = 0 ; . 6 unsigned char n- = 0 ; . 7 unsigned char NUM [] {= 0xC0 , 0xF9 , 0xA4 , 0xB0 , 0x99 , 0x92 , 0x82 , 0xF8, \ . 8 0x80 , 0x90 , 0x88 , 0x83 , 0xA7 , 0xA1 , 0x86 , 0x8E }; . 9 void INT_T0 ( void ) 10 { . 11 the TMOD = 0X01 ; // open timer 0, timer 1 using pattern 12 is TH0 is = 0xFC ; 13 is = TL0 0X18 ; // initial value 14 EA = . 1 ; // Turn on the main switch 15 the Reference Crop Evapotranspiration = . 1 ;// Open the T0 interrupt switch 16 } . 17 void main ( void ) 18 is { . 19 INT_T0 (); 20 is TR0 = 0 ; // timer function is turned 21 is the LED NUM = [ 0 ]; 22 is the while ( . 1 ) 23 is { 24 IF (key1 == 0 ) 25 { 26 is TR0 = . 1 ; // determination button is pressed, the opening timing of the timing function 27 } 28 the else 29 { 30 TR0 = 0 ; // Analyzing key release, the disconnection timing function 31 is } 32 IF (key2 == 0 ) 33 is BREAK ; // out of the loop, time to zero 34 is } 35 } 36 void TIME0 ( void ) interrupt . 1 37 [ { 38 is TH0 is = 0xFC ; 39 TL0 = 0X18 ; // initial value 40 IF (I < 1000 ) 41 is { 42 i++; 43 } 44 else 45 { 46 P0 = ~NUM[n]; 47 if(n<16) 48 { 49 n++; 50 } 51 else 52 { 53 n=0; 54 } 55 i = 0; 56 } 57 }
The third step: burn-in circuit diagram
Note (1 pressing the button and hold until the timing, not accounting when released, click the button 2 is reset)