having trouble, need a push
science_geek
Posts: 247
i am having troubles getting servos to work with my propellor, its not a hardware problem, its a software problem, i have servos working,· but only with the help of the object exchange, i have tried various versions to get a servo to go from 0 to 180 and then changing code to get it back to 0, i have the clock set to its highest setting of 80_000_000 and just want a little help with the code to make a servo work, i tried this, and it would go a certain distance and then put out a high pitch tone, here is the code, dont ask me where i got the numbers, i was working on this with my uncle and this is the last we got to before i had to go
CON
··
· _clkmode = xtal1 + pll16x··········· ' Set up clkfreq = 80 MHz.
· _xinfreq = 5_000_000
PUB TestFrequency
· 'Configure ctra module
· ctra[noparse][[/noparse]30..26] := %00100·············· ' Set ctra for "NCO single-ended"
· ctra[noparse][[/noparse]5..0] := 27···················· ' Set APIN to P27
· frqa := 1_000_000····················· ' Set frqa for 2093 Hz (C7 note) using:
······································ ' frqa/b = frequency × (232 ÷ clkfreq)
· 'Broadcast the signal for 1 s
· dira[noparse][[/noparse]27]~~·························· ' Set P27 to output
· waitcnt(clkfreq + cnt /40)
·
CON
··
· _clkmode = xtal1 + pll16x··········· ' Set up clkfreq = 80 MHz.
· _xinfreq = 5_000_000
PUB TestFrequency
· 'Configure ctra module
· ctra[noparse][[/noparse]30..26] := %00100·············· ' Set ctra for "NCO single-ended"
· ctra[noparse][[/noparse]5..0] := 27···················· ' Set APIN to P27
· frqa := 1_000_000····················· ' Set frqa for 2093 Hz (C7 note) using:
······································ ' frqa/b = frequency × (232 ÷ clkfreq)
· 'Broadcast the signal for 1 s
· dira[noparse][[/noparse]27]~~·························· ' Set P27 to output
· waitcnt(clkfreq + cnt /40)
·
Comments
If that is correct (which it probably is) then i have to ask why your waitcnt line reads "waitcnt(clkfreq + cnt/40)"? If you want to wait for a full second your code should read "waitcnt(clkfreq + cnt)". Using your mathematics, you should have probably written "waitcnt(clkfreq/40 + cnt)". Again, just advice.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Some men see things as they are and ask "why?"
I dream of things that never were and ask "why not?"~
Graham
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Some men see things as they are and ask "why?"
I dream of things that never were and ask "why not?"~
As Graham mentioned. First consider exactly what signal a servo needs in order to operate.
Then design an highlevel approach to providing that signal
Then build the code to produce the signal that is needed. Often is small steps that build on each other.
Here is one typical specification but you should find the actual specifications for the servo you are using.
1. A Servo requires a square wave pulse from 1ms to 2ms long with the actual lenght based on where you want to possition it
2. The pulse should be repeated no more often than once every 20ms and typcally every 20ms
pseudocode
Loop every 20 mS
· Start Delay
··· Output high
··· Time for 1.5ms
··· output low
· end Delay
End loop
This would center the servo with a 1.5 ms duration.
Develop the code to loop every 20 ms. Once you have that working then move on to adde the next complexity.
Think about how to modify this to move the servo from one stop (1ms) to the other stop (2ms)
Then what actual spin code would be needed to accomplish this.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Searider
·
CLKFREQ / 1000 tells you how many are in a millisecond.· You can multiply this value to get pretty much any time you want.· If you're dividing up the 1-2ms range for a servo into 100 units, you'd have
(CLKFREQ / 100000) * X + (CLKFREQ / 1000) where X is a number between 0 and 99.· The result is a clock cycle count for 1ms to 2ms.
It is well structured and can give you ideas to try out. In any case it never hurts to take a peek, after all it's there to be used and
no one would call it cheating (well I wouldn't anyway, can't speak for others )
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jac {^[noparse]:)[/noparse]}
Never underestimate the power of human stupidity! (It bites me all the time!)