View previous topic :: View next topic |
Author |
Message |
iam6me
Joined: 29 May 2011 Posts: 3
|
help please servo controlled by potentiometer code |
Posted: Sun May 29, 2011 7:40 am |
|
|
hello there guys
how is it going!!
I need help if you may help me please. I am building a robotic arm using multi servos and i need to control them through 1K potentiometers. 20KHz crystal, 16f877a pic.
Can you help me trouble-shooting my code ?
There it is
Code: |
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=20000000)
|
Code: |
int16 value=0;
int16 x1=0;
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(1);
delay_us(20);
while (1)
{
value = read_adc();
x1 = value+1000;
output_high(PIN_b1);
delaY_us(x1);
output_low(PIN_b1);
delay_ms(18);
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun May 29, 2011 11:30 am |
|
|
Ok...looks like you're using the RC hobby type of servo...
Break your program down into 3 new programs..
1) use 'fixed' delays of 1000, 1500 and 2000 us as your x1 value( servo position) and see what happens.
2) if they work correctly( full left, center, full right) then you know you've wired up the protoboard correctly.
3) now add the 'read adc and send data to servo delay_us(x1)...
if that doesn't work, reread the ADC section or look at the examples in the CCS folder labelled 'examples' for your compiler.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun May 29, 2011 2:37 pm |
|
|
Question - what compiler version?.
There have been three different 'versions' of the delay_us code.
Oldest wouldn't accept a variable (you obviously have not got this).
Then next allowed a constant from 0-65535, or a variable from 0-255. With this later you would only get a small amount of movement (1/4 travel), happening four times as you move up the pot range.
Current version allows 65535 for both variable and constant.
So your code has a problem if you have an old compiler....
Best Wishes |
|
|
iam6me
Joined: 29 May 2011 Posts: 3
|
|
Posted: Mon May 30, 2011 11:57 am |
|
|
Hello and thanx for the replies. In fact i tried just what you said and used fixed values instead of the variables but still the servo didn't work.
While I am sure the connection is right cause i have a micro ship where it has a program that works almost right.
:s
So i tried even using 4MHZ clock but still the same problem ?
And am using version 4.105 so i guess it is suitable and up to date. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon May 30, 2011 2:47 pm |
|
|
So have you actually proved the processor is running?.
Silly things like MCLR pin not pulled high, or wildly wrong loading capacitors on the crystal (had one guy who accidentally used 22nF, instead of 22pF).
A simple 'flash an LED at 1second intervals' test, will first prove the processor is running, and also allow you to check that it is running at the right speed (which is of course critical for time based things like running a servo....), is the first place to start.
Best Wishes |
|
|
iam6me
Joined: 29 May 2011 Posts: 3
|
|
Posted: Mon May 30, 2011 6:01 pm |
|
|
Yea every thing you said is just right I've just checked :S |
|
|
|