CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

servo control

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

servo control
PostPosted: Tue Jun 26, 2018 6:42 am     Reply with quote

Hello,
I want control servo "standard" 50 Hz frequency...

My code works well with 50 % duty cycle but frequency is far too
high. It's about 1.49 kHz. It should be 50 Hz.
Please help me to get PWM control for 50 Hz.
Sad

all the best for you all

Code:

#include<18F452.h>
//#include <string.h>
#fuses HS,NOLVP,NOWDT,PUT


//#FUSES H4 //This is HS, with 4* PLL - H4 config bit.....
//#use delay(clock=8M)

#use delay(clock=8000000)


//************************************************************************************************
 void main(void){
 
 while(1){
           
      // C1 = pin 16 =  CCP2, C2 = pin 17 = CCP1
     
     
      setup_timer_2(T2_DIV_BY_16, 255, 16);            //this is for PWM 1 & PWM 2
      setup_ccp1(CCP_PWM);                            // Configure CCP1 as a PWM
      set_pwm1_duty(127);
 
 
      stop:
     
      ;
     
     
      goto stop;
 
 }
 
 }
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Jun 26, 2018 7:03 am     Reply with quote

Look in the code library.
There are multiple examples there of how to control servos.

The PWM is _not_ normally the way. Problem is if you wanted 50Hz, you would need to program the PWM to give 50Hz. Then the resolution would be very low.

If fact though 'high speed' servo update algorithms use 400Hz, not 50Hz. The actual 'update rate' for servos does not matter at all. You can leave most servos for several seconds without an update. Or update then at much higher rates. Only the pulse width actually matters. The reason for '50Hz', was that old radio systems used a 'frame' of pulses sent one after the other. The first pulse was for the first servo, the second for the second etc.. The whole frame usually comprised perhaps ten servo pulses, and then a pause to allow the internal counter to synchronise. The radio had simple logic that looked for the pause, and then gated the first pulse to the first servo, etc.. Modern digital radios no longer do this, and these will often simply send pulses to each servo at 400Hz.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jun 26, 2018 8:39 am     Reply with quote

yeesh, sad thing is I have some of the original RC servo chips that Signetics made in the mid 70s(?) in a drawer here....wonder if there's a market for them Laughing
blowtorch



Joined: 11 Jun 2013
Posts: 35
Location: Cape Town

View user's profile Send private message

PostPosted: Thu Jun 28, 2018 3:15 am     Reply with quote

Some time back I had a project where I needed to occasionally drive a servo to a preset position (open / closed). The mechanical situation was such that the open position did not exert any force on the servo, but the closed position did. So I ensured that in the closed position the mechanical alignment would put the forces in a straight line, and therefore not "load" the servo. Therefore, after having arrived at either of the 2 preset conditions, I simply stopped sending pulses to the servo.

This code ran fine at a clock speed of 4MHz. Note the resolution was poor, but for my needs perfectly adequate given that I only needed an open / closed position. It was a very simple but effective approach that has worked for years.

Posted here in case it helps...



Code:
#define SERVO_OPEN             44   // used to set servo position1 via pwm2 duty
#define SERVO_CLOSED           134  // used to set servo position2 via pwm2 duty


void do_servo(uint16 servo_pos) {
    setup_ccp2(CCP_PWM);  // set CCP2 to PWM mode
    setup_timer_2(T2_DIV_BY_64,255,1); //
    set_pwm2_duty(servo_pos);
     
    delay_ms(1500); // allow 1.5 seconds for servo to move into position
    set_pwm2_duty(0L); // specific sequence to shut down PWM in low position
    delay_ms(25);
    setup_ccp2(CCP_OFF); // turn off the PWM...
    output_low(SERVO);  // ensure the PWM line low, probably not required
    setup_timer_2(T2_DISABLED,255,1); // turn off the timer...save power
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group