SNIU028D February 2016 – September 2020 UCD3138 , UCD3138064 , UCD3138064A , UCD3138128 , UCD3138A , UCD3138A64
The Prescaler for the 16 bit PWM Timer is always driven by ICLK. The clock divide is equal to PRESCALE + 1.
Here is C code which sets the prescale value to 3, for a divide of 4:
TimerRegs.T16PWM0CNTCTRL.bit.PRESCALE = 3; //divide by 4
To see the counter value, please follow the example below:
result = TimerRegs.T16PWM0CNTDAT.bit.CNT_DAT;
The counter is free-running and counts from 0x0000 after a reset. If the counter reaches 0xFFFF, it rolls over to 0x0000 and continues counting. This rollover causes the overflow flag to be set and an interrupt to be generated, if enabled. The interrupt and flag bits are well described in Section 11.22.
The timer can also be reset by a compare event on the compare 0 block. To enable this function, use this C code:
TimerRegs.T16PWM0CNTCTRL.bit.CMP_RESET_ENA = 1; //enable Comp 0 reset
If this bit is set the counter will be reset when the counter reaches the value in the T16PWM0CMP0DAT.bit.CMP_DAT register.
The 16 bit counter can also be reset by setting the SW_RESET bit:
TimerRegs.T16PWM0CNTCTRL.bit.SW_RESET = 0; //reset and stop counter
A zero in SW_RESET is the default state, so it is necessary to write a 1 to this bit in order for the counter to run. The compare and control registers should be initialized before writing to SW_RESET to enable the counter.