Best Practices to Reload the Compare and Period Values of Timer/PWM/TCPWM/Counter Component - KBA90554

Version: **

 

Question:

What is the best practice to be followed while reloading the compare and period values of a timer, PWM, TCPWM, or counter Component? 

 

Answer:

The best way to reload the compare or period value of a timer, counter, PWM, or TCPWM through firmware is to stop the Component, load the new compare or period value, and then start the component. Counter_WriteCounter(uint8/16/32 count), Counter_WritePeriod(uint8/16/32 period), and Counter_WriteCompare(uint8/16/32 compare) are the APIs to load the counter value, period value, and compare value respectively.

It is not recommended to reload the compare value on-the-fly because if the new period/ compare value is smaller than the current value in the counter, the counter will run until the maximum possible value and then roll-over to zero. Only then the new period and compare value would come into effect. For example, if your period value is 2000, your new period value is 1000 and if the present count value is greater than 1000, then the count will go until 65534 and then roll back. So, it is better to stop the Component and then reload the new period or compare value.