Single-Chip Microcomputer: the stopwatch count is implemented in C Language (Keys start, end, reset)

Source: Internet
Author: User

Single-Chip Microcomputer: the stopwatch count is implemented in C Language (Keys start, end, reset)

Implement the stopwatch counting function, using the timer and external interrupt. The 8 button indicates the start/end of the timer, and the C button indicates that the value is reset to 0. For convenience of demonstration, the upper limit is set to 15 seconds.

The Code contains detailed notes:

 

# Include
 
  
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}; // The field code value is 0 ~ 9 unsigned char code dofly_WeiMa [] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; // the corresponding Digital tubes are lit, that is, the location code void delay (int t) {while (t --);} // The first parameter I represents the number of digital tubes to be controlled, and n represents the number to be displayed 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; // The timer initializes TMO D | = 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.1 mssec = 0; if (I = 101) // 1 s {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 ;}}
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.