Timer Loading Approaches Comparison

Thread Starter

MTech1

Joined Feb 15, 2023
147
I've come across some c code for a PIC timer where we can either load a value directly into the timer, define a 16-bit variable and split its high and low bytes into their respective registers (TMRH and TMRL), or define another uint16_t variable like this:

load a value directly into the timer
Code:
uint16_t TMR = 1110;     // Define a 16-bit variable with an initial value of 1110
Split its high and low bytes into their respective registers (TMRH and TMRL),

Code:
uint16_t values = 1110;  // Define another 16-bit variable with an initial value of 1110

// Split the high and low bytes into their respective registers
TMRH = (values >> 8) & 0xFF; // Load the high byte of 'values' with 4
TMRL = values & 0xFF;        // Load the low byte of 'values' with 74
Could you explain the benefits of each approach and when it's preferable to use one over the other?
 

MrChips

Joined Oct 2, 2009
29,832
You have defined TMR as 16 bits. The compiler will do the work for you but it still has to read/write two bytes.
On some MCUs, the order by which you read/write the bytes is important. Check the MCU User Manual.
 
Top