Mid-term exam, 15 seconds stopwatch production

First, the requirements are as follows

Making a stopwatch timer, the recording time of 0-15 seconds. It requires accurate and controllable.

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.

Second, the circuit diagram of a full complement, as shown below.

 

 

 Third, enter the following code in keil4

#include<reg52.h>
#define LED P0
unsigned int i = 0;
unsigned int j =0;
sbit KEY1 = P1^0;        //启动/暂停按钮
sbit KEY2 = P1^2;          //复位按钮
unsigned char NUM[]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,\
     0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E};
void INT_T0(void)
{
 TMOD = 0X01;       //打开定时器0 ,使用模式1定时
 TH0 = 0XFC;
 TL0 = 0X18;     //初始值
 EA = 1;      //打开总开关
 ET0 = 1;      //打开中断T0开关
}
void main(void)

 INT_T0();
 TR0 = 0;       //定时功能打开
 LED = NUM[0];
 while(1)
 {   
  if(KEY1 == 0)
  {
   TR0= 1 ;
  }
  if(KEY1 == 1)
  {
   TR0=0;
  }
  if(KEY2 == 0)
  {
    LED = NUM[0]; 
  } 
 }
}
 
void time0(void) interrupt 1 
{
 TH0 = 0XFC;
 TL0 = 0X18;     //初始值
 if(i < 1000)
 {
  i++;
 }
 else
 {
   LED= NUM[j] ;
  if(j < 16)
  {
   j++;
  }
  else
  {
    j=0;
  }
  i = 0;
 }
 
}

四、完成如上任务,第一个按钮负责开始和停止,第二个按钮负责归零。

 

 

 

Guess you like

Origin www.cnblogs.com/fgbcfdv/p/11818069.html