Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

servo motor control through potentiomer code error

Status
Not open for further replies.

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
i am supposed to control a servo motor movement using this code through the resistance change of a potentiometer
am using a pic 16f877A and ccs pic compiler

The code i've reached so far is

void main() {

int value,x1;

setup_adc_ports(ALL_ANALOG); setup_adc(ADC_CLOCK_INTERNAL); setup_psp(PSP_DISABLED); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE);

while (1) { set_adc_channel(0); delay_us(10);

value=read_adc();

x1 = value +1000 ;

output_high(PIN_b0); delAY_us(x1); output_low(PIN_b0); delay_ms(20); } }

any ideas guys ?please :S
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
yea there is no actual error it's just that it's not working :S
but i don't know why do u have any idea ?
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
Is the output switching at all? To check your code, we would need the full source, including all #fuses and preprocesor commnds. You should also tell the CCS C version.
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
these are the fuses :


#include <16F877A.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#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 WRT_50% //Lower half of Program Memory is Write Protected

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

the whole source is uploaded here
ex.rar
and the ccs v is 4
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
The oscillator configuration and #use delay statement don't fit. What's the real oscillator configuaration? You can check the processor speed with a simple LED blink program.
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
sorry but i didn't understand what did u mean ?
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
You have set low power oscillator and 20 MHz frequency. So all timings will be completely wrong.
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
thanx for ur help but still not the answer :S
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
thanx for ur help but still not the answer
The answer to which question?

You should correct your code according to the requirements of PIC processor hardware and CCS clock setup statements. And then show it again.
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
void main()
{
int value , x1 ;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_64);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

while (1)
{
set_adc_channel(1);
delay_us(20);
value = read_adc();

x1 = value+1000 ;

output_high(PIN_b1);
delAY_us(x1);
output_low(PIN_b1);
delay_ms(20);
}
}


#include <16F877A.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#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 WRT_50% //Lower half of Program Memory is Write Protected

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
this is the final code :S
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
O.K. I assume, that you have connected a 20 MHz crystal with respective capacitors and verified that the circuit is correct.

There's still a problem, because int has 8 Bit size in CCS C by default. You should write int16 instead.
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
i think that the problem is in variable declaration cause when i tried sitting the x1=1500;
it gave me the same problem so most likely the problem is in using the variable :S i think
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
void main()
{
#include <16F877A.h>
#device *=16
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#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 WRT_50% //Lower half of Program Memory is Write Protected

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

int16 value , x1;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

while (1)
{
set_adc_channel(1);
delay_us(10);
value = read_adc();

x1 = value+990 ;

output_high(PIN_b1);
delAY_us(x1);
output_low(PIN_b1);
delay_ms(19);
}
}
it worked just fine in simulation but when i programed the chip and tried it the servo would go to 90+ and stop there on it's own and the potentiometer doesn't work at all
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,174
Helped
14,198
Reputation
28,655
Reaction score
12,887
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,122
I don't see a reason at first view. Does the servo control work with fixed delays?
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
while(1){
output_high(PIN_b1);
delAY_us(1200);
output_low(PIN_b1);
delay_ms(19);

delay_ms (1500) ;

}

}
when i used this code the servo would rotate one step and stop every time i connect the circuit :S
 

BradtheRad

Super Moderator
Staff member
Joined
Apr 1, 2011
Messages
13,972
Helped
2,784
Reputation
5,566
Reaction score
2,691
Trophy points
1,393
Location
Minneapolis, Minnesota, USA
Activity points
104,220
Question:

Is this the same kind of servo used in radio control cars/ boats/ airplanes?

You control its position according to the length of a pulse sent to an input wire (which makes 3 wires in addition to the two supply wires).

Each pulse must be between 1-2 milliseconds long. The middle position is 1.5 mSec.

Pulses do not need to be any particular frequency.

That is how the servos I've worked with operate.

Or is it a stepper motor?
 

e.hamza

Newbie level 5
Joined
May 20, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
yes it's a aservo motor just like the one u 've described :)
 

mac231991

Newbie level 3
Joined
Jul 4, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,296
what servo motor are you using? can you post your schematic for the whole project? i am new on pic programming
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top