2

### Author Topic: How to calculate the time for one clock cycle  (Read 15188 times)

0 Members and 1 Guest are viewing this topic.

#### roi_tau

• Jr. Member
• Posts: 19
• Helpful? 0
##### How to calculate the time for one clock cycle
« 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

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132
• Helpful? 109
##### Re: How to calculate the time for one clock cycle
« Reply #1 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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

#### hudbrog

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: How to calculate the time for one clock cycle
« Reply #2 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

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132
• Helpful? 109
##### Re: How to calculate the time for one clock cycle
« Reply #3 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
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

#### cosminprund

• Robot Overlord
• Posts: 284
• Helpful? 8
##### Re: How to calculate the time for one clock cycle
« Reply #4 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

#### hudbrog

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: How to calculate the time for one clock cycle
« Reply #5 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 =(

#### cosminprund

• Robot Overlord
• Posts: 284
• Helpful? 8
##### Re: How to calculate the time for one clock cycle
« Reply #6 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).

#### Admin

• Administrator
• Supreme Robot
• Posts: 11,658
• Helpful? 169
##### Re: How to calculate the time for one clock cycle
« Reply #7 on: December 04, 2008, 09:45:00 PM »

Get Your Ad Here