Design a stopwatch with digital tube display using timer interrupt

Publisher:数字思维Latest update time:2015-08-18 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//This stopwatch has time, minutes, seconds and milliseconds, and can record up to hours. It has pause and resume timing functions. Key1 on the independent keyboard is the pause and resume key, and key3 is the reset and start timing key.

//Since the ms interrupt time is very short, if the interrupt and display delay are not handled properly, the stopwatch will not run accurately.

 

 

#include

#define uchar unsigned char

#define uint unsigned int

uchar code table[]={0x 3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};        // Display the hours, minutes, and seconds with a decimal point

uchar ms,s,m,h,count,count1;

sbit k1=P3^0;

sbit k3=P3^2;

 

void delay(uint z)

{

     uint x,y;

     for(x=z;x>0;x--)

       for(y=110;y>0;y--);

}

 

 

 

void displays(uchar temp)       // Function to dynamically display seconds on digital tube

{

     uchar shi,ge,i;

     i=0;

     shi=temp/10;

     ge=temp;

 

    

        

         P0=0xef;

         P1=table[shi];

         delay(1);               // There must be a delay, dynamic scanning, in order not to affect the entire stopwatch eight-digit scanning rate to improve the display effect, the delay should not be too high, ms is more appropriate       

    

         P0=0xdf;

         P1=table1[ge];

         i=0;

         delay(1);

    

}

 

void displayms(uchar temp)       //Function for dynamically displaying milliseconds on digital tube

{

     uchar shi,ge,i;

     i=0;

     shi=temp/10;

     ge=temp;

 

    

        

         P0=0xbf;

         P1=table[shi];

         delay(1);

    

         P0=0x7f;

         P1=table[ge];

         i=0;

         delay(1);

    

}

 

void displaym(uchar temp)       //digital tube dynamic display function

{

     uchar shi,ge,i;

     i=0;

     shi=temp/10;

     ge=temp;

 

    

        

         P0=0xfb;

         P1=table[shi];

         delay(1);

    

         P0=0xf7;

         P1=table1[ge];

         i=0;

         delay(1);

    

}

 

void displayh(uchar temp)       // Function for dynamically displaying the hour position of the digital tube

{

     uchar shi,ge,i;

     i=0;

     shi=temp/10;

     ge=temp;

 

    

        

         P0=0xfe;

         P1=table[shi];

         delay(1);

    

         P0=0xfd;

         P1=table1[ge];

         i=0;

         delay(1);

    

}

 

 

 

void keyscan()            //Keyboard scanning function

{

     if(k1==0)

     {

     delay(5);

     if(k1==0)            //Check if k1 is pressed to prevent jitter

     {

         count++;

         while(!k1);         //Detect release

         delay(1);            // Check if the hand is released

         while(!k1);

 

         if(count==1)

         TR0=0;                //Pause timer

         if(count==2)

         {

              TR0=1;            //Timer continues timing

              count=0;

         }

     }

     }

     if(k3==0)

     {

     delay(5);

     if(k3==0)

     {

         count1++;

         while(!k3);

         delay(1);

         while(!k3);

         if(count1==1)     // reset the stopwatch

         {

         TR0=0;              

         ms=0;

         s=0;

         m=0;

         h=0;

         }

         if(count1==2)      //Restart timing        

         {

              TR0=1;          

              count1=0;

         }

     }

     }

}

 

void main()

{

 

     TMOD=0x01;

     EA=1;

     ET0=1;

     TR0=1;

     TH0=(65536-10000)/256;          //Set the initial value of the timer

     TL0=(65536-10000)%6;          //12M crystal oscillator: ms

     while(1)

     {

     keyscan();

     displays(s);                 //Dynamic scanning of digital tubes to display seconds

     displayms(ms);                  //digital tube dynamic scanning milliseconds display

     displaym(m);                 //digital tube dynamic scanning seconds and minutes display

     displayh(h);                  //digital tube dynamic scanning second hour display

     }

}

 

void timer0() interrupt 1            //interrupt service routine

{

     TH0=(65536-10000)/256;

     TL0=(65536-10000)%6;

     ms++;

     if(ms==100)                      //Timer interrupt times is s

     {                              //Putting this part in the interrupt can reduce the impact of program execution time on interrupt time

         ms=0;

         s++;

         if(s==60)

         {

         s=0;

          m++;

         }

         if(m==60)

         {

              m=0;

              h++;

         }

         if(h==24)

         {

              h=0;

         }

     }   

}

 

Keywords:Timer Reference address:Design a stopwatch with digital tube display using timer interrupt

Previous article:Summary of electrical design work - MSP430G2553 study notes - 1
Next article:Design clock using timer interrupt and display using 1602 LCD

Recommended ReadingLatest update time:2024-11-15 17:00

C51中断(void timer1(void) interrupt 3 using 3)
interrupt indicates the interrupt priority, and using indicates the working register group used. interrupt x using y        The xx value after interrupt is the interrupt number, that is, which interrupt port this function corresponds to. Generally in 51,        0 is external interrupt 0          1 is timer 0        2
[Microcontroller]
STM32 Timer PWM_Output
The pulse width modulation mode can generate a signal with a frequency determined by the TIMx_ARR register and a duty cycle determined by the TIMx_CCRx register. The following is an example of PWM mode 1. The PWM signal reference OCxREF is high when TIMx_CNT   Library function STM32F10x_StdPeriph_Lib_V3.3.0\Proje
[Microcontroller]
STM32 Timer PWM_Output
Timer0 of PIC16F877
    Timer0 is an 8-bit timer counter with a programmable 8-bit prescaler. The hardware structure is shown in the figure below:   There are three registers associated with it: If counting mode is required, T0CKI should be set to input mode, that is, the corresponding position in TRISA is set to 1.     TMR
[Microcontroller]
PIC16F877A microcontroller (combined use of external interrupt and timer Timer0)
1 Implementation Principle See the previous content of timer 0 and external interrupt 2 Implementation circuit diagram 3 Source code /*----------------Function function:   Application of Timer 0+External Interrupt Function 1: LED0 keeps flashing; Function 2: When no button is pressed, LED1 is off. When a button
[Microcontroller]
PIC16F877A microcontroller (combined use of external interrupt and timer Timer0)
MSP430F149 TIMER_A (I)——16-bit timing counter
    TIMER_A has four optional clock sources. For convenience, ACLK and SMCLK are generally selected. A time base is generated through a frequency divider. Its structure diagram is as follows:   TIMER_A has three timing/counting modes: (1). Incremental counting mode: Counting cycle: TAR increases from 0 to TAC
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号