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.
Previous article:MSP430 Learning Experience---Clock
Next article:MSP430 MCU RTC-Real Time Clock
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- [Perf-V Evaluation] Exception (interrupt) handling of Hummingbird E203
- Calculation and setting of baud rate of msp430 microcontroller
- RSL10 rich board set!
- What challenges do you face in powering handling and harvesting robots? Let’s talk with Vicor engineers!
- What is the continuity threshold used for? Analysis of the wiring harness tester function
- Does DDR design require back drilling?
- [Integrated subway security control system based on Raspberry Pi 400] Material unboxing - Raspberry Pi 400
- Single chip microcomputer CPU card programming system main frequency setting
- Award-winning live broadcast: Microchip's trusted platform for the CryptoAuthentication family
- GD32307E-START IO analog SPI control RGBLED (03)