/*############################################################# Program Name : ex3_PWM_motor Author : Grant Phillips Date Modified : 13/01/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that demonstrates the use of a PWM output for the control of a DC motor. By default the period for an PwmOut class is 20ms (0.02s). This example will show how we can change this to 50us (or 20 Khz) to properly do PWM control on a DC motor. More precise methods for controlling the duty cycle will also be demonstrated. Requirements : * STM32F4-Discovery Board Circuit : * DC Motor connected to PB4 using an H-Bridge (EN pin) * The H-Bridge's REV pin connected to PC5 * The H-Bridge's FWD pin connected to PC4 * Connect PB4 to an oscilloscope to observe the effect of an PWM output graphically ##############################################################*/ #include "mbed.h" PwmOut motorpwd(PB_4); //default period at this stage is 20ms (50Hz) DigitalOut motorfwd(PC_4); DigitalOut motorrev(PC_5); int main() { // set direction pins on H-Bridge to run motor forward // fwd rev result // 0 0 stop // 0 1 reverse // 1 0 forward // 1 1 stop / brake motorfwd = 1; motorrev = 0; // change period to 50us (20Khz) motorpwd.period(0.00005); // you can also use more precise methods: // motorpwd.period_us(50); //set period in us // motorpwd.period_ms(integer value); //set period in ms while(1) { motorpwd = 0.5; //set duty cycle to 50% // you can also use more precise methods: //motorpwd.pulsewidth(0.000025); //set high pulse in sec to 0.000025s (50% duty) //motorpwd.pulsewidth_us(25); //set high pulse in us to 25us (50% duty) //motorpwd.pulsewidth_ms(integer value); //set high pulse in ms } }