Maker Pro
Maker Pro

Servo motor control with POT

Cagatay_AKSOY

Nov 5, 2017
3
Joined
Nov 5, 2017
Messages
3
Hi friends, I can not speak English well. Sorry. I'm doing a project. My goal; Using 2 potentiometers to control 2 servo motors separately. Now I can control 1 servo motor with 1 potentiometer. 2. I can not add potentiometer and servo motor. I'm coding and uploading the proteus file. Thank you very much for your help. I use Mplab XC8 and pic 16f877a.Ekran Görüntüsü (8).png Ekran Görüntüsü (9).png Ekran Görüntüsü (10).png
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,715
Joined
Nov 17, 2011
Messages
12,715
2. I can not add potentiometer and servo motor.
You connect a second pot in the same way as the first, e.g. on AN1.
You connect a second servo in the same way as motor 1, e.g. on RB1.

I cannot help much with the coding of a PIC. However, You probably need to change the program structure. Currently you sare generating the PWM waveform in a subroutine using delay statements. This kind of delay is created by having the CPU running in a loop for a defined number of iterations to "burn" time. The CPU is not able to do usefil work during this time.
It is preferable to haev the PIC hardware generate the PWM signals autonomously as described e.g. here. You'll have to connect the servos to PC.1 and PC.2 for this to work.
The idea is that you set up the registers of the hardware PWM channels such that these hardware module generate the signals without further intervention. You'll have to change only the register settings for changing the ducty cycle.

As for the analog input you'll have to read both input channels to get the settings of the two potentiometers.

A rather general code could look like this (you'll have to work out the detailed code as I'm not familiar with PICs):
Code:
setup ();

while (1)
{
   ADC1 = read_ADC_channel_1();
   ADC2 = read_ADC_channel_2();
   set_PWM_channel_1(ADC1);
   set_PWM_channel_2(ADC2);
}

Alternatively, using parameterized routines for reading the ADC and setting the PWM:
Code:
setup ();

while (1)
{
   ADC1 = read_ADC_channel(1);
   ADC2 = read_ADC_channel(2);
   set_PWM_channel(ADC1, 1);
   set_PWM_channel(ADC2, 2);
}
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
If you insist on doing it with a program loop, you would turn both pins on, then, loop until the lower time is reached, turn it off, continue loop until the higher one is reached, turn it off.

Bob
 

Cagatay_AKSOY

Nov 5, 2017
3
Joined
Nov 5, 2017
Messages
3
First of all thank you for everything. I solved the problem by changing channels. Forced the Proteus simulation because I use PWM 2 also MAINE. @Harald Kapp and @BobK thank you. I am removing pictures because there is an error in the code. You can email to [email address removed for privacy] for information about this project.
 
Last edited by a moderator:

Minder

Apr 24, 2015
3,318
Joined
Apr 24, 2015
Messages
3,318
I presume these are RC servo's and not industrial versions?
M.
 
Top