CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

timers configuration issue

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dpechman



Joined: 04 Dec 2007
Posts: 43

View user's profile Send private message Visit poster's website

timers configuration issue
PostPosted: Fri Nov 19, 2010 6:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 19, 2010 9:31 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Nov 20, 2010 4:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 20, 2010 6:00 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group