NuMaker PWM1 drive +5V DC servo motor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // NuMaker-PFM-NUC472 : PWM1 output to drive DC servo motor
00002 #include "mbed.h"
00003 
00004 /* NOTE: Most targets has UNO D3 for PWM. Check it for supporting new targets */
00005 PwmOut pwm1(D3);
00006 
00007 int main() {
00008     
00009     int i=0;
00010     printf("...DCserv Start...\n\r");
00011 #ifdef MBED_MAJOR_VERSION
00012     printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00013 #endif
00014     pwm1.period_us(20000);      // set PWM period to 20ms (50Hz)
00015         
00016     for (i=500; i<=2500; i=i+200) { // from 0.5ms to 2.5ms
00017         pwm1.pulsewidth_us(i);      // set PWM pulse width to rotate motor
00018 #if MBED_MAJOR_VERSION >= 6
00019         ThisThread::sleep_for(1000);
00020 #else
00021         Thread::wait(1000);         // delay
00022 #endif
00023         printf("DCservo pulse width = %d\n\r", i);
00024     } 
00025     printf("...DCserv End.....\n\r");                             
00026 }