View previous topic :: View next topic |
Author |
Message |
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
Pic Timer Calculator |
Posted: Mon Nov 24, 2014 6:34 pm |
|
|
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
|
|
Posted: Thu Nov 27, 2014 11:31 am |
|
|
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
|
|
Posted: Mon Dec 01, 2014 2:19 pm |
|
|
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
|
|
Posted: Mon Dec 01, 2014 2:44 pm |
|
|
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
|
|
Posted: Sun Dec 04, 2016 4:58 pm |
|
|
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
|
|
Posted: Tue Dec 06, 2016 4:21 am |
|
|
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. |
|
|
|