📜 ⬆️ ⬇️

The counter on the microcontroller with an accuracy of 2/3 microseconds and overflow in several days. Answer

This post is the answer to the topic avn author about the software timer / counter in the microcontroller.
In his post, the author talks about software counters with acceptable accuracy and overflow time, but does not indicate the limitations that software counters implemented in this way introduce. Without being tied to the microcontroller architecture, I will try to present a different time counting algorithm in an accessible form.


Formulation of the problem

The avn version of the time account realization has the right to life in small programs, when the main task is time counting. In real life, “sometimes” has to perform differential calculations on low-power microcontrollers. The counting time of mathematics can take from two to several dozen increments of the program counter. In addition, the volume background problem of the mathematics counting is interrupted by requests from the periphery (ADC, DAC, PWM, asynchronous and synchronous communication channels).
If we consider that you may need to keep the program counter with even greater overflow and take into account that several dozens of assembler commands are executed each time you enter the timer (saving the stack, operations to increase the large timer, restore the stack), you can see the main task there is no time left.

What to do?

In such a situation, I proceed as follows. The frequency of the hardware timer overflow is selected from the software requirements (the accuracy of event binding to time, sufficient accuracy in calculating mathematics and the overflow margin of the software timer). I make the interrupt priority maximal (if there is no possibility to use the timer with the “author author”), and the interrupt itself is as low as possible. Ie in the interrupt I produce the increment of the variable delta on the bit width of the same bit width of the microcontroller (for eight-bit controllers, this is 1 byte). Thus, there is no need to additionally save registers to the stack.
In the main program I declare a global variable-copy of the program counter - delta_ . In the main program loop, I prohibit all interrupts, rewrite values ​​from delta to delta_ , reset delta , and allow interrupts. Thus, the global variable delta_ is the execution time of the previous main program loop. With this variable, you can perform further operations: add up with the current time, count down long time delays, and take into account when diffurs are counted.
')
What are the advantages here?

The undoubted advantage is the speed of the hardware timer interrupt, and therefore the possibility of setting a slightly higher frequency of interrupt operations.
No accumulation error when counting time.
Independence from the execution time of the main program and peripheral interrupts.

And cons?

In the minuses, the situation remains when the requirements on the accuracy of event binding are high, and the execution time of the main program cycle is long. In this case, the program counter delta may not be enough and you will have to increase its bit depth and, consequently, increase the execution time of the hardware timer interrupt.
Somewhat larger program code.

Closing words

I had to use the proposed time counting algorithm in software development, where the time reference accuracy was set to 1ms, the need for counting time exposures from 25ms to several minutes, integral links with integration time from 1 second to 1 hour, differential time constant from 1 second to 1 hour , and the execution time of the main program cycle ranged from 30 to 40 ms.

Thank you for attention.

Source: https://habr.com/ru/post/112698/


All Articles