View previous topic :: View next topic |
Author |
Message |
dpechman
Joined: 04 Dec 2007 Posts: 43
|
timers configuration issue |
Posted: Fri Nov 19, 2010 6:57 pm |
|
|
I was making some tests with timers. First I would like to have a pin toggling at 17us each semicycle. Using @20MHz, I set timer1 to 1:1 and reload him with 65451 each interruption.
Measuring the output pin with the scope it reaches 24us.
My program just reach 17us at the output pin when I set this with a theoretical 9.8us (65487).
Then I tried with other values and also with timer 0 where this difference is showing up.
My question is, where am I misinterpreting the evidence? |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Fri Nov 19, 2010 9:31 pm |
|
|
if you want a square-wave output at that fast of a frequency, why not just use the PWM?
What you did makes sense. You can just throw a guess at the timer, measure it and compensate for the error. The error is due to the housekeeping instructions that the compiler must execute to get to the actual interrupt code. (saving registers, stack, etc.)
A 20MHz chip will execute 5 instructions per microsecond. you want to toggle the pin via the interrupt every 5*17 = 85 instructions. I'm a bit surprised it even worked. There certainly won't be any time left to do anything else.
Again, why not use the PWM and let the hardware do it?
One trick I've used to set the timer more accurately is to just set it back instead of trying to calc a fixed value. For instance, when I set up a 10mS interrupt on a 4MHz chip (1uS per timer count) I just do something like this:
Code: | set_timer1(get_timer1()-10000);
|
That backs up the timer while taking into account the instruction cycles that have elapsed since rollover.
I don't think that would work on such a tight interrupt cycle - not enough time to do it.
Hey, how bout using the PWM? |
|
|
dpechman
Joined: 04 Dec 2007 Posts: 43
|
|
Posted: Sat Nov 20, 2010 4:43 am |
|
|
I'm not using this with PWM because it is for communications purposes.
Just trying to make a second UART port.
It will run in a 18F4550 later at 48 MHz. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Sat Nov 20, 2010 6:00 am |
|
|
Use the CCS software UART, and ensure you disable interrupts while the character is sent (disable_ints in the #use RS232).
The problem is that the overhead of entering and leaving an interrupt, is typically something around 65 instruction times. Add some code to calculate what bit you want to send, and it just isn't do-able. At 57600bps, it is only going to take 174uSec to send a byte, so disabling the interrupts and using the software UART, shouldn't be a problem.
Best Wishes |
|
|
|