The difference between timer and counter in microcontroller

Publisher:技术掌门Latest update time:2017-01-04 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A timer is actually a counter, but it counts pulses with a fixed period.

The timer/counter is easy to understand.

The timer actually works in counting mode, but it counts pulses with a fixed period. Since the pulse period is fixed, the time can be calculated by the count value, which has a timing function.

Timing and counting are the same only in terms of triggering sources (clock signal and external pulse).
The registers in a microcontroller can be seen as electronic switches that are used to switch different functions and signals.

In 51, the source of the counting signal is switched by the T/C bit in TMOD.
When T/C works in the timer, the pulses of the oscillator source divided by 12 are counted, that is, the count value is increased by 1 in each machine cycle, and the counting frequency is 1/12fosc. When the crystal frequency is 6MHZ , the counting frequency is 500KHz, and the count value is increased by 1 every 2us; the crystal oscillator is 12MHZ , which means it is increased by 1 every 1us.

When T/C works in the counter, the counting pulse comes from the external pulse input pin T0 (P3.4) or T1 (P3.5). When the T0 or T1 pin has a negative transition, the count value is increased by 1. It takes 2 machine cycles, that is, 24 oscillation cycles, to identify the negative transition of the pin. Therefore, the highest frequency of the countable external pulse input to the T0 or T1 pin is 1/24fosc. When the crystal oscillator is 12MHZ , the highest counting frequency is 500KHz. If the frequency is higher than this, the counting will be wrong.


As for the initial value, it is the cup principle. Since 51 can only count up, and can only send an interrupt at the moment when the cup is just full, triggering the interrupt program, we put a certain amount of beans in the cup first, and then the corresponding number of beans will be full, and then the interrupt program will work automatically.

Note:
Timing and counting are just different in triggering source (clock signal and external pulse), and other aspects are the same.
Suppose we want to time a certain time (100 machine cycles), we just set the initial value to (overflow value-100), and suppose we want to count 100 pieces, in fact, we also set the initial value to (overflow value-100), and then set the input pulse to external input. In other words
: timing and counting are just different in triggering source (clock signal and external pulse), and other aspects are the same.
The initial value in the interrupt is to prepare for the next cycle. There is nothing much to say, just set it according to the needs.
Since the value of the timer counter can also be read at any time, we can also start counting from 0 to calculate the number of a period of time or a certain number of pulses. This can still open the interrupt. When the interrupt occurs, it means that (timer overflow value) pulses have been counted again. By performing overflow processing in the interrupt, you can calculate a number much larger than (timer overflow value).
At this time, you should also pay attention to one point: when reading 51, in addition to the capture function of T/C2, directly reading TH and TL will change continuously. For details, please read the book. When you understand the timer counter, we can even set the count value to (overflow value-1) to achieve automatic single step (timing mode) or use it as an external interrupt (counting mode). Think about it carefully, hehe;-)
As for the reason why the interrupt needs to protect the scene, it is to prevent the parameters of other programs from being accidentally modified, thereby affecting the operation of other programs. Therefore, as long as the data moved by the interrupt program itself is protected, the moved memories will be restored to the state when entering when the interrupt is exited, so as not to affect the interrupted program.
You need to save the scene yourself for assembly, since you write the program yourself anyway, so everything is under your control;
for C, the compiler will automatically perform coverage analysis and automatically save the variables that need to be saved, so you can rest assured when using it. Of course, if you know what the compiler does, you can also embed assembly and play around with it freely, but this is not recommended for novices, so leave it to the compiler.
In fact, these are all basic knowledge. If you don’t understand, it must be because you didn’t read the book carefully enough.


Keywords:MCU Reference address:The difference between timer and counter in microcontroller

Previous article:MSP430 Learning Experience---Clock
Next article:MSP430 MCU RTC-Real Time Clock

Latest Microcontroller Articles
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号