11 years, 1 month ago.

LPC11U24 PWM out for Servo control

Hi:

I just want to know how to use the pwm out to control the servo. I have build two codes already but none of them works. Please help! ps(these are continuous rotation servo which works with period of 20ms and pulse width between 1.3ms and 1.7ms.)

Servo control1

#include "mbed.h"
#include "Servo.h"

Serial pc(USBTX,USBRX);
Servo servo1(p25);

int main()
{
    float position=0.5;
    pc.printf("Servo speed control.\n");
    pc.printf("Use 'w' and 's' to control speed.");
    servo1.calibrate(0.0002,45);
    while(1){
        char input=pc.getc();
        if((input=='w') && (position<=1)){
            position+=0.1;
            servo1=position;
            }
            if((input=='s') && (position>=0)){
                position-=0.1;
                servo1=position;
                }
                }
                }

Servo control2

#include "mbed.h"

PwmOut servo1(p25);
Serial pc(USBTX,USBRX);

int main( ){
    float Pwidth=0.0015;
    servo1.period(0.020);
    pc.printf("Servo speed control.");
    pc.printf("Use 'w' and 's' to control speed.");
    while(1){
        char Iput=pc.getc();
        if((Iput=='w') && (Pwidth<=0.0017)){
            servo1.pulsewidth(Pwidth+=0.0001);
            }
        if((Iput=='s') && (Pwidth>=0.0013)){
            servo1.pulsewidth(Pwidth-=0.0001);
            }
            }
        }

Hi,

I just tried to use your test cases and both waveforms are correctly generated as far as I saw by oscilloscope.

I saw:

  • PWM cycle is fixed 20ms.
  • High pulse width can be changed from 1.3ms to 1.7ms using 's' and 'w' character input.

Can you please describe your problem more specific?

posted by Toyomasa Watarai 02 Dec 2014
Be the first to answer this question.