Nuvoton
/
NuMaker-mbed-PWM1_DCservo
NuMaker PWM1 drive +5V DC servo motor
main.cpp@4:cae45e2ca73a, 2016-12-22 (annotated)
- Committer:
- shliu1
- Date:
- Thu Dec 22 15:18:23 2016 +0800
- Revision:
- 4:cae45e2ca73a
- Parent:
- 0:68283046ceea
- Child:
- 7:cd68421d2a50
Support M453, target is numaker_pfm_m453
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 | |
shliu1 |
4:cae45e2ca73a | 4 | #if defined(TARGET_NUMAKER_PFM_NUC472) |
rkuo2000 | 0:68283046ceea | 5 | PwmOut pwm1(PF_10); // PWM1 pinname |
shliu1 |
4:cae45e2ca73a | 6 | #elif defined(TARGET_NUMAKER_PFM_M453) |
shliu1 |
4:cae45e2ca73a | 7 | PwmOut pwm1(PC_7); |
shliu1 |
4:cae45e2ca73a | 8 | #endif |
rkuo2000 | 0:68283046ceea | 9 | |
rkuo2000 | 0:68283046ceea | 10 | int main() { |
rkuo2000 | 0:68283046ceea | 11 | |
rkuo2000 | 0:68283046ceea | 12 | int i=0; |
rkuo2000 | 0:68283046ceea | 13 | printf("...DCserv Start...\n\r"); |
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 |
rkuo2000 | 0:68283046ceea | 18 | Thread::wait(1000); // delay |
rkuo2000 | 0:68283046ceea | 19 | printf("DCservo pulse width = %d\n\r", i); |
rkuo2000 | 0:68283046ceea | 20 | } |
rkuo2000 | 0:68283046ceea | 21 | printf("...DCserv End.....\n\r"); |
rkuo2000 | 0:68283046ceea | 22 | } |