Can anyone give me some insight into how the 16-bit timer on Padauk MCUs works?
What I want to do is create a timeout timer of around 500 msec. I've figured I can do this by setting the T16 clock source to SYSCLK (which will be IHRC/16 = 1 MHz), dividing by 16 for 62.5 kHz tick rate, then setting the interrupt source to be bit 15 of the counter (count of 32,768). This should give me a timeout period of approximately 524 msec, which is good enough for my purposes.
But, I only want to enable this timeout timer on-demand, and wait for either an I/O pin to toggle or the timeout to expire (i.e. I don't want something to
repeat every 500 msec).
I'm thinking there's two potential ways I can go about this. When I want to start the timeout, I could either:
- Reset the timer counter to zero, then enable the interrupt for T16 (
INTEN |= INTEN_T16). Disable the interrupt at all other times.
- Enable T16 by setting its clock source, hopefully the counter is reset to zero whenever the timer is re-enabled. Keep T16 disabled (i.e.
T16M_CLK_DISABLE) at all other times. Interrupt is enabled at all times.
The first method is difficult because the T16 counter can only be written or read with specific assembly instructions,
STT16 and
LDT16, and these instructions only read/write from/to a fixed memory address. To reset the counter to zero, I would have to do something like declare a global variable with a value of zero, then use inline assembly to write that value to the counter.
static uint16_t t16_reset_val = 0;
/* Wherever I need to reset T16 counter: */
__asm__("stt16 _t16_reset_val\n");
Also, the
Free-PDK website detailing the PDK13 instruction set says that the memory address for
STT16 and
LDT16 must be word-aligned. I don't know how to ensure that with SDCC, or if it's even possible.
The second method, I don't know if the counter value gets reset whenever the T16 is disabled/enabled. If it does, this approach would work, but if not it's not viable. Anyone know?