Microcontroller C language programming: 10s stopwatch

Publisher:光明2599Latest update time:2018-10-10 Source: 21icKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include


#define uchar unsigned char


#define uint unsigned int


sbit K1=P3^7;


fly


i,Second_Counts,Key_Flag_Idx;


bit Key_State;


fly

 

DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


//Delay


void DelayMS(uint ms)


{


flying t;


while(ms--) for(t=0;t<120;t );


}


//Handle key events


void Key_Event_Handle()


{


if(Key_State==0)


{


Key_Flag_Idx=(Key_Flag_Idx 1)%3;


switch(Key_Flag_Idx)


{


case 1:EA=1;ET0=1;TR0=1;break;


case 2:EA=0;ET0=0;TR0=0;break;


case 0:P0=0x3f;P2=0x3f;i=0;Second_Counts=0;


}


}


}


//Main program


void main()


{

 

P0=0x3f; //display 00


P2=0x3f;


i=0;


Second_Counts=0;


Key_Flag_Idx=0; //Number of key presses (values ​​0, 1, 2, 3)


Key_State=1; //Key status

 


TMOD=0x01; //Timer 0 mode 1

 


TH0=(65536-50000)/256; //Timer 0: 15ms


TL0=(65536-50000)%6;


while(1)


{


if(Key_State!=K1)


{


DelayMS(10);


Key_State=K1;


Key_Event_Handle();


}


}


}

 


/T0 interrupt function


void DSY_Refresh() interrupt 1


{


TH0=(65536-50000)/256; //Restore the initial value of timer 0


TL0=(65536-50000)%6;


if( i==2)

 

{


i=0;


Second_Counts; //50ms*2=0.1s conversion state


P0=DSY_CODE[Second_Counts/10];


P2=DSY_CODE[Second_Counts];


if(Second_Counts==100) Second_Counts=0; //Display 00 after reaching 100 (10s)


}


}

 

Microcontroller C language programming: 10s stopwatch


Keywords:MCU Reference address:Microcontroller C language programming: 10s stopwatch

Previous article:Have you ever thought about the issue of program efficiency?
Next article:A brief comparison between 89C51 and STM8

Recommended ReadingLatest update time:2024-11-16 14:57

Detailed explanation of the pin functions of the 80C51 microcontroller
    MCS-51 is a standard 40-pin dual in-line integrated circuit chip. Please refer to the figure for the pin arrangement.     P0.0 ~ P0.7: P0 port 8-bit bidirectional port line.     P1.0 ~ P1.7: P1 port 8-bit bidirectional port line.     P2.0 ~ P2.7: P2 port 8-bit bidirectional port line.     P3.0 ~ P3.7:
[Microcontroller]
Detailed explanation of the pin functions of the 80C51 microcontroller
Understanding Pulse Width Modulation (PWM) in Atmega16/32 AVR Microcontrollers
Pulse Width Modulation (PWM) is a powerful technique that varies the pulse width by keeping the frequency constant. This technique is currently used in many control systems. The applications of PWM are not limited, it is widely used in motor speed control, measurement, power control, and communication, etc. In PWM tec
[Microcontroller]
Understanding Pulse Width Modulation (PWM) in Atmega16/32 AVR Microcontrollers
"Whether learning single-chip microcomputers or embedded systems has a future" 1
     In early June, Wu Jianhong published an article titled "Wu Jianhong on whether learning single-chip microcomputers or embedded systems has a future" , which was later forwarded to the blog by andy1989 and caused a great response among enthusiasts. Wu Jianhong responded to many comments from enthusiasts on the foru
[Microcontroller]
Design of smart IC card gas meter electronic control system based on PIC microcontroller
    Abstract: The design of a smart IC card gas meter electronic control system with PIC microcontroller as the core is given. The control mode of the system, the hardware circuit composition of the electronic control system, the system software design and its practical application are introduced. Keywords: PIC mic
[Test Measurement]
MCU: IIC bus communication
1. IIC bus communication 1 Introduction:  IIC stands for Inter-Integrated Circuit (IC bus). I2C bus is a serial bus launched by PHLIPS. I2C bus has only two bidirectional signal lines. One is the data line SDA and the other is the clock line SCL.  Each device connected to the I2C bus has a unique address. Data transm
[Microcontroller]
51 Single-chip multi-machine communication principle
1. Principle of multi-machine communication In multi-machine communication, the host must be able to identify each slave. In the 51 series microcontroller, this can be achieved through the SM2 bit of the SCON register. When the serial port sends data in mode 2 or mode 3, each frame of information is 11 bits, and the 9
[Microcontroller]
51 Single-chip multi-machine communication principle
Five low power modes of MSP430 microcontroller
The five low power modes are LPM0~LPM4 (LOW POWER MODE), and the active state of the CPU is called AM (ACTIVE MODE). AM consumes the most power, and LPM4 consumes the least power, only 0.1uA. In addition, the influence of working voltage on power consumption: the lower the voltage, the lower the power consumption.
[Microcontroller]
YuanCheng communication between microcontroller and PC based on MODEM
    Abstract: The public telephone network technology is mature and covers a wide range. It is a very effective method to use the existing public telephone network to realize remote communication between microcontrollers and PCs. This article introduces in detail the hardware, software design and implementation metho
[Industrial Control]
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号