51 MCU timer T2 initialization settings

Source: Internet
Author: User

Single-Chip Microcomputer T2 timer for 1 second precise Timer Program

Single-Chip Microcomputer T2 timer for 1 second precise Timer Program

/*************************************** **************************************** *************

* File name: Test. c
* Function: Use the T2 timer to implement precise timing within one second and flash the light.
* 1. CPU model: AT89S52
* 2. Crystal Oscillator: 12.000 MHz
**************************************** **************************************** *************/

# Include "reg52.h" // include the header file
# Define uint unsigned int
# Define uchar unsigned char
Sbit p1_7 = p1 ^ 7; // defines the bit variable

/*************************************** **************************************** *************

* Function name: timer2_server ()
* Function: Timer 2 Overflow interrupt.
* Entry parameters: None
* Exit parameter: None
* Note: a static variable timer2_server_count is set in this function. The value of the static variable will not be
* The value is initialized, but the last value is retained. It is used to count the overflow times of the T2 timer (the number of incoming calls to this function), each
* If it overflows for 16 times, it controls the reverse display of one led11 event. At this time, the time is exactly 1 second, and it is accurate 1 second! Only with Crystal Oscillator
.
**************************************** **************************************** *************/

Void timer2_server (void) interrupt 5
{
Static uint timer2_server_count;
// Defines static variables to count the overflow times of the T2 timer (the number of times the timer enters the function)

TF2 = 0;
// When the T2 timer has an overflow interruption, you need to clear the overflow mark by yourself, and other timers of 51 are automatically cleared?

Timer2_server_count ++;
If (timer2_server_count = 16) // The pre-load value of the T2 timer is 0x0bdc, and 16 overflow times is 1 second.

{
Timer2_server_count = 0;
P1_7 = ~ P1_7; // reverse display of led11.
}
}

/*************************************** **************************************** *************

* Function name: Main ()
* Function: Use the T2 timer to implement precise timing for 1 second and enable the led11 to flash the light. That is, the led11 will be on for 1 second, off for 1 second, light for 1 second, and off for 1 second ......
* Note:
* 1. To perform precise timing, the automatic timer loading method must be used. This experiment uses the T2 timer to enable it to work on 16-bit automatic
* Loading mode. In this case, a 16-bit pre-load value is specially loaded in another location. When T2 overflows, the pre-load value is immediately loaded,
* This ensures accurate timing.
* 2. the T2 timer is a 16-bit timer. The maximum overflow time is dozens of milliseconds. To set the timer time to 1 second, you need to use a variable.
* To save the number of overflow times. After accumulating a certain number of times, an operation is executed. In this way, it can be accumulated to 1 second or more
* An operation takes a long time.
* 3. When the T2 timer overflows and interrupts, you need to clear the overflow mark by yourself, while other timers of 51 are automatically cleared.
* 4. T2 timer pre-load value calculation:
* If the crystal oscillator is set to 12 MHz, 1000000 (12000000/12) machine cycles can be performed per second. T2 has the most overflow each time.
* It has passed 65536 machine cycles. We should try to minimize the overflow interruption of the T2 timer, so as to interfere with the main program.
* It is the smallest.
* Select 14 interruptions per second. Each overflow is 1000000/14 = 71428.57 machine cycles, which is not an integer and exceeds 65536 machine cycles.
* Select 16 interruptions per second, with each overflow of 1000000/16 = 62500 machine cycles, less than 65536 machine cycles, valid.
* Select 20 interruptions per second, with each overflow of 1000000/20 = 50000 machine cycles, less than 65536 machine cycles, valid.
*.
*.
*.
* Through the above calculation, we can find that there are many ways to choose from, but the best is to interrupt 16 times per second, each time
* If 62500 machine cycles are exceeded, the initial value assigned to the T2 timer should be 65536-62500 = 3036 and be converted to the hexadecimal value
* 0x0bdc.
**************************************** **************************************** *************/

Void main (void)
{
P1_7 = 1; // led11 off

/* The T2 timer is assigned a pre-load value, and the overflow of 16 times is 1 second. */
Rcap2h = 0x0b;
Rcap2l = 0xdc;

Et2 = 1; // enable T2 timer interruption
Ea = 1; // total disconnection

Tr2 = 1; // start the T2 Timer

While (1); // endless loop, waiting for overflow interruption of T2 Timer
}

 

References: http://www.ctdisk.com/u/665442

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.