- Thu Aug 12, 2010 1:24 pm
#107257
This low level coding shouldn't be required by everyone to learn MCU's. Besides I'd rather learn by coding something that hasn't already been done-- more product for everything.
Anyhow, I have been reading about all of that, but it's far more difficult then you make it out. Quote, see bold text -->
+++
It's possbile, but not as easy as on the PC where the Pentium processor has an internal tick counter.
You'll need to set SMCLK or ACLK to the same clock frequency as MCLK (for ACLK it is only possible on newer devices), then set up a timer to be clocked by SMCLK or ACLK, enable overflow interrupt and write an ISR which counts the overflows. If you never disable interrupts for longer than ~65500 clock cycles, the overflow counter will hold the upper 16 bit (or 32 bits if you use a long int) and the timer counter register the lower 16 bits of the passed cpu ticks since starting the timer. It is a bit difficult, however, to retrieve the current value, as it is possible that an overflow occurs between collecting the two 16bit values (orbetween reading the upper and lower 16 bit of the long counter, if used).
Also, this approach consumes some processor time too, as every 65536 cpu cycles the ISR is entered and consumes 20-30 cycles for updating the overflow counter.
You can, however, use the dividers for SM/ACLK and/or timer to break down the count frequency by a factor of up to 64. This reduces the resolution of the tick counter by 64, but also reduces the cpu usage by the same factor. And maybe eliminates the need of using a long value for the overflow counter.
+++