MCU Lesson 10th: timer interruption

Source: Internet
Author: User

Jp3 is connected to P0 port.

# Include <reg51.h> # define uchar unsigned charuchar flag, num; uchar code SMG [10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x90}; // declare the subfunction void Init (); void inittimer0 (); void main () {Init (); inittimer0 (); While (1) {// none of the Code} void timer0 () interrupt 1 {// re-install the initial value, because the cycle time is required, the time is 50 milliseconds th0 = (65536-50000) /256; tl0 = (65536-50000) % 256; flag ++; If (flag = 20) {// Add 1 flag = 0 every second; p0 = SMG [num]; num ++; If (num = 10) {num = 0 ;}} void inittimer0 () {// initial value, timing 50 ms th0 = (65536-50000)/256; tl0 = (65536-50000) % 256; // select the timer t0 Mode 1 tmod = 0x01; // enable/disable et0 = 1; // enable/disable total CPU Ea = 1; // start the timer tr0 = 1;} void Init () {flag = 0; num = 0 ;}
The two timers are used in the same way. Now let's summarize the usage steps of the Timer:

1. Initial Values

2. Determine the working method. The related register is tmod.

3. Enable the et0 or ET1, and EA

4. Start the timer

5. Write interrupt service programs

Void function name () interrupt 1 (if timer 1 is selected, the code is 3)

{

// Reinstall the Initial Value

}

MCU Lesson 10th: timer interruption

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.