I am making use of TIMERs in STM32, and my doubt lies in its settings.
It is known the formula to calculate the update event is given by:
Considering that the TimerClock is 48MHz and I wish an Update Event of 0.5s. In theory, it can be achieved with different combinations of prescaler and period values.
My question is, how does the choice of the Prescaler and Period influence the system, is it better to have a higher Prescaler and a lower Period or vice-versa or it does not have a relevant impact at all?
A bit extra on timers before the actual answer:
I always find that formula counterintuitive, because it teaches you nothing, explains nothing about the logic inside STM32 timers. If you understand the logic, however, everything just falls in place.
First, we need to define what a timer clock is. A timer clock is characterized by timer counter frequency - how many times per second does Timer's counter increment (or decrement) with a prescaler of 1. This frequency is NOT equal to CPU clock frequency. It can be, but not always. Timer clock frequency is complicated. Typically it depends on frequency of the APB bus the timer lives on, multiplied by 2, but even that is not fixed (it can be 2xAPBx freq. with certain APB prescalers, and 1xAPBx with others, it can also be 4xAHB freq). You can find more info about it in the reference manual, in Timer section, as well as RCC section. However, for many default configurations, Timer clock is simply 2xAPBx frequency, which often happens to be equal to CPU frequency. If you debug your project and you find your timer is off by a factor of 2, you should look into that first.
Next comes the timer prescaler, which, as you can guess, simply divides the timer clock so that instead of timer counter incrementing/decrementing on every timer clock tick, it only does so on every "prescalerth" timer clock tick.
Then there is a reload value, a value which defines how many counter ticks make a full "revolution", and trigger an update event, and start the counter over.
An intuitive way to think about it is to consider timer clock, then consider a prescaler (such as 2 - every second timer clock tick increments the counter), and then you set the reload value, which simply defines how many counter ticks you will have in one period.
For example:
Imagine Timer Clock is 100MHz (for easy math).
A period is then 10ns.
I set prescaler to 4, so the timer counter increments at 25MHz, or (25*10^6) = 40ns per timer counter increment.
I want a timer to trigger every 1us (microsecond), so I need 1000ns/40ns = 25 counter ticks. This will be my reload value.
Alternatively, I can have a prescaler of 2 instead of 4 (20ns per timer counter increment), which means I need to double the reload value to 50, and I will get the same 20ns/tick*50ticks = 1000ns/counter reload. Halve the one, double the other.
*I omitted all these +1 that go into registers, this is about logic.
Answering the actual question:
Now, back to the question of whether it matters if I change prescaler and reload value while keeping the same counter period.
First, you have to consider how many bits wide your prescaler and reload value are. Timers often have 16-bit prescalers and/or 16-bit reload values. This obviously limits your range. This is individual for every timer, some have 32-bit registers for these values, some have 16-bit registers. So not every combination of prescaler/reload value will fit into every timer.
Second of all, the smaller the prescaler, the more granularity of counter period you have. With small prescaler, you can change reload value by 1, and the change in period will be very tiny, while if your prescaler is large, every change in reload value will change counter period more. So if you want fine control (such as PWM), you want to push prescaler as low as possible, while also making sure the reload value still fits into reload value register.
Thirdly, if you just want a fixed frequency tick from a timer and you don't need any fine control (such as just a tick every 0.5s and nothing else), then the exact values of prescaler and reload value don't matter indeed. You can double the prescaler, halve the reload value, you will get exactly the same timer counter period, there is no practical difference there. Again, as long as both fit into their registers.