Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: roi_tau on December 04, 2008, 01:32:17 AM

Title: How to calculate the time for one clock cycle
Post by: roi_tau on December 04, 2008, 01:32:17 AM
Hi everyone

In the 50 $ robot,there was the formula that 23 cycles are approx 0.992ms

How can I calculate it in PIC16F887 with 8MHz ?

And I was told that if the servo doesn't get exact pulses ("1" and "0" ) it can damage the servo (I think I did it to mine...)

How much is crucial that the off pulse would be 20ms minus the on pulse ?

In the 50 $ code example , the off pulse was const.


All the best


Roi
Title: Re: How to calculate the time for one clock cycle
Post by: Webbot on December 04, 2008, 03:57:22 AM
Quote
In the 50 $ robot,there was the formula that 23 cycles are approx 0.992ms

You can either: do it by trial and error ie use a stopwatch and write a program to turn on and LED, call the loop 10000 times, turn off the LED. Then fiddle with the delay loop until it 10000 times longer than you need. OR, the more accurate, is to look at the assembly language that has been produced (look at the listing files created by the compiler) and then look at the controllers datasheet to see how many cycle each assembly instruction takes. An 8MHz processor will do 8 million cycles per second.

Quote
And I was told that if the servo doesn't get exact pulses ("1" and "0" ) it can damage the servo (I think I did it to mine...)
The only wat I can think that it will damage an un-modified servo is that if you try to push it beyond its endstops and the current goes up to high.

Quote
How much is crucial that the off pulse would be 20ms minus the on pulse ?
In the 50 $ code example , the off pulse was const.
Not very crucial. If you wait too long between the pulses then the motion will be jerky. If you send the pulses too quickly then it can confuse the servo a bit. But a few ms either way wont make much difference.
Title: Re: How to calculate the time for one clock cycle
Post by: hudbrog on December 04, 2008, 04:18:30 AM
Actually, it's just 1/8000000 of a second for 8mhz =)
To calculate timing precisely without using timers, you have to write using assembly. for example in avr:
ldi R17, 16     ' takes 1 clock cycle
b: dec r17     ' takes 1 clock cycle
brne b          ' takes 2 cycle to branch, and 1 otherwise

so whole program will take 1+1+16*2+1=35 cycles, or 4.37 usec


Title: Re: How to calculate the time for one clock cycle
Post by: Webbot on December 04, 2008, 04:34:54 AM
Yes one cycle takes 1/8000000 of a second or, as I said, 8000000 cycles per second.

Don't forget that the 'dec r17' instruction is also in the loop so it actually takes:-

1 + 16*(1 + 2) + 1 = 50 cycles or 6.25 usec
Title: Re: How to calculate the time for one clock cycle
Post by: cosminprund on December 04, 2008, 04:40:38 AM
"roi_tau" is talking about an PIC16F887 - that's an 8 bit PICMicro, it uses 4 clock cycles per instruction, so he only gets 2 MIPS with an 8Mhz clock :)
Title: Re: How to calculate the time for one clock cycle
Post by: hudbrog on December 04, 2008, 05:31:49 AM
Don't forget that the 'dec r17' instruction is also in the loop so it actually takes:-
yep, my bad =)

"roi_tau" is talking about an PIC16F887 - that's an 8 bit PICMicro, it uses 4 clock cycles per instruction, so he only gets 2 MIPS with an 8Mhz clock :)
afaik there are tables for PICs too... 4 clock cycles for usual instructions, more for branching... don't really remember =(
Title: Re: How to calculate the time for one clock cycle
Post by: cosminprund on December 04, 2008, 07:00:32 AM
PIC's use 4 clock cycles for all usual instructions and twice as many for instructions that affect the program counter (jumps, goto's skips).
Title: Re: How to calculate the time for one clock cycle
Post by: Admin on December 04, 2008, 09:45:00 PM
http://www.societyofrobots.com/microcontroller_time_cycles.shtml