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 support@ccsinfo.com

Pic Timer Calculator

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



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

Pic Timer Calculator
PostPosted: Mon Nov 24, 2014 6:34 pm     Reply with quote

Hi everyone,
I created a simple and beautiful calculator to calculate a prescaler and preload and generates a code.
Just put the crystal frequency and what frequency you want.

http://www.vinicius.info/PTC/

I hope help someone.
Thanks a lot.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Nov 27, 2014 11:31 am     Reply with quote

After some testing, caveat emptor:

For a very wide range of frequency combinations - no result is returned.

Sometimes making an adjustment to oscillator frequency of just 1 KHz or so
breaks it - and it also
fails nicely with every crystal value that is an exact power of 2.

Like 8388608 clock and 1000 hz desired.
0r 10100000 and 1000 hz
---
Here is the result for
10 Mhz and 1000 hz
Code:

setup_timer_0( RTCC_INTERNAL | RTCC_DIV_2 );
set_timer0( 64286 );

BUT to allow for the ISR timer reload instruction cycle and ISR context entry overhead time the value 64286 really should be more like 64287 or 88....
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Mon Dec 01, 2014 2:19 pm     Reply with quote

Hi asmboy. Thank you a lot for the post.

Well, I use the formula:

(crystal/4) / (PRE_SCALER*X) - 65536

with

X = [1,2,3,4...65536]
PRE_SCALER = [2,4,8,16,32,64,128,256]

is it wrong?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Dec 01, 2014 2:44 pm     Reply with quote

opvini wrote:
Hi asmboy. Thank you a lot for the post.

Well, I use the formula:

(crystal/4) / (PRE_SCALER*X) - 65536

with

X = [1,2,3,4...65536]
PRE_SCALER = [2,4,8,16,32,64,128,256]

is it wrong?


Simple answer. YES.

There is a delay from timer0 overflow to your set_timer0 instruction.
You are totally ignoring this delay.
Result, gross timing errors.

Mike

asmboy suspects there are other issues which I've not had time to investigate.

EDIT OK I've had time to look.
It appears that your algorithm only works when the crystal frequency is an integer multiple of 4*PRE-SCALER * desired output frequency.
Hence most combinations of crystal and output frequency (X) do not produce a result.
You claim that it works for all possible output frequencies (X) from 1 to 65536.
This is simply not correct.
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sun Dec 04, 2016 4:58 pm     Reply with quote

Cool!

I fixed it with:

Code:
 set_timer0( [VALUE] + get_timer0() );


About the multiple of 4*PRE-SCALER *, I will check it
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Dec 06, 2016 4:21 am     Reply with quote

opvini wrote:
Cool!

I fixed it with:

Code:
 set_timer0( [VALUE] + get_timer0() );



It takes time to perform the calculation, this makes your output frequency lower than expected.
You still need to correct for this error.
( The error is a constant time and has a bigger percentage effect high output frequencies. )

Mike

You've changed your calculator so previous replies are now rendered useless.
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