View previous topic :: View next topic |
Author |
Message |
neopolythe
Joined: 31 Jan 2011 Posts: 2
|
Accurate delays using while loops |
Posted: Mon Jan 31, 2011 12:31 am |
|
|
Hi,
I have been trying for some time now to learn how to calculate the exact number of clock cycles a while loop will take to execute. I have stepped through the EX_FREQC.C example. In terms of clock cycles versus code they state that the following should be true:
Please note that the number to the left is the clock cycles:
0: set_timer0(0);
0: delay_cycles(2);
0:
1: timer0 = get_timer0();
2:
3: while (cycles != 1){ //true this time so 3 cycles
4:
5:
6: cycles++;
7: } //jump back to top of while loop 2 cycles
8:
9: while (cycles !=1){ //false this time so 4 cycles
10:
11:
12:
13: timer1 = get_timer0();
14:
According to the example we do the following:
(3 + 1 + 2) * n + 4
where 3 is for while being evaluated as true
1 is for cycles++
2 is for jump back to top of while loop
n is for number of iterations through while loop
4 is for while being evaluated as false.
When I compile this example on a 16F84 running a 10Mhz external clock and simulate it in proteus, out putting the measured values to an LCD I get the following:
n timer1 timer2 calculated error
1 0 11 10 -1
2 0 20 16 -4
3 0 27 22 -5
4 0 34 28 -6
5 0 41 34 -7
6 0 48 40 -8
7 0 55 46 -9
8 0 62 52 -10
9 0 69 58 -11
10 0 76 64 -12
Where timer1 and timer2 are the two measured values.
Calculated is what the equation (3+1+2) * n + 4 says the answer should be. Error is the error between the measured and calculated values.
Could someone please assist me in telling me why the equation does not work in this instance?
Thank you. |
|
|
neopolythe
Joined: 31 Jan 2011 Posts: 2
|
|
Posted: Mon Jan 31, 2011 12:36 am |
|
|
My apologies the table did not align properly:
n - timer1 - timer2 - calculated - error
1 - 0 - 11 - 10 - -1
2 - 0 - 20 - 16 - -4
3 - 0 - 27 - 22 - -5
4 - 0 - 34 - 28 - -6
5 - 0 - 41 - 34 - -7
6 - 0 - 48 - 40 - -8
7 - 0 - 55 - 46 - -9
8 - 0 - 62 - 52 - -10
9 - 0 - 69 - 58 - -11
10 - 0 - 76 - 64 - -12 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Jan 31, 2011 3:26 am |
|
|
'Getting' a sixteen bit value from a timer, does not take two clock cycles, but four. The processor has to read a byte (one cycle), write this to an eight bit register (one cycle), read the next byte (one cycle), and write this to another location.
Also, you are assuming that everything is in one bank. There is no guarantee of this, so a bank switch may be needed.
Look at the .lst file. In this, you can see the C instructions, and the assembler they generate.
As posted, the original counts are right, but what you post is not from the original...
If you still can't get the counts to work out, simplify your code, but leave the bare minimum present (timer setups, variable declarations etc.), and post this, we can then have a hope of actually working out what values should be seen and why.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Jan 31, 2011 6:12 am |
|
|
There's also NO guarantee that Proteus is giving you the correct numbers! After all it is a simulator.
The listing, the datasheet (instruction set chapter) and a calculator will tell you what you want to know. If you do the simple math, you'll have a better understanding of the PIC! |
|
|
|