Nuvoton
/
NuMaker-mbed-PWM1_DCservo
NuMaker PWM1 drive +5V DC servo motor
main.cpp@12:c7ca6b6c7fbc, 2021-02-25 (annotated)
- Committer:
- SHLIU1@OANBE02333.nuvoton.com
- Date:
- Thu Feb 25 11:24:43 2021 +0800
- Revision:
- 12:c7ca6b6c7fbc
- Parent:
- 9:64a0098bb8df
Support the both V5.X and V6.X for mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkuo2000 | 0:68283046ceea | 1 | // NuMaker-PFM-NUC472 : PWM1 output to drive DC servo motor |
rkuo2000 | 0:68283046ceea | 2 | #include "mbed.h" |
rkuo2000 | 0:68283046ceea | 3 | |
ccli8 |
9:64a0098bb8df | 4 | /* NOTE: Most targets has UNO D3 for PWM. Check it for supporting new targets */ |
ccli8 |
9:64a0098bb8df | 5 | PwmOut pwm1(D3); |
rkuo2000 | 0:68283046ceea | 6 | |
rkuo2000 | 0:68283046ceea | 7 | int main() { |
rkuo2000 | 0:68283046ceea | 8 | |
rkuo2000 | 0:68283046ceea | 9 | int i=0; |
rkuo2000 | 0:68283046ceea | 10 | printf("...DCserv Start...\n\r"); |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 11 | #ifdef MBED_MAJOR_VERSION |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 12 | printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 13 | #endif |
rkuo2000 | 0:68283046ceea | 14 | pwm1.period_us(20000); // set PWM period to 20ms (50Hz) |
rkuo2000 | 0:68283046ceea | 15 | |
rkuo2000 | 0:68283046ceea | 16 | for (i=500; i<=2500; i=i+200) { // from 0.5ms to 2.5ms |
rkuo2000 | 0:68283046ceea | 17 | pwm1.pulsewidth_us(i); // set PWM pulse width to rotate motor |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 18 | #if MBED_MAJOR_VERSION >= 6 |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 19 | ThisThread::sleep_for(1000); |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 20 | #else |
rkuo2000 | 0:68283046ceea | 21 | Thread::wait(1000); // delay |
SHLIU1@OANBE02333.nuvoton.com | 12:c7ca6b6c7fbc | 22 | #endif |
rkuo2000 | 0:68283046ceea | 23 | printf("DCservo pulse width = %d\n\r", i); |
rkuo2000 | 0:68283046ceea | 24 | } |
rkuo2000 | 0:68283046ceea | 25 | printf("...DCserv End.....\n\r"); |
rkuo2000 | 0:68283046ceea | 26 | } |