PWM with regular digital IO and with digital pin that supports PWM generation
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:ee9aab897b59
- Child:
- 1:56cc90ae5080
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 20 15:07:18 2015 +0000 @@ -0,0 +1,147 @@ +/********************************************************************************** +* @file main.cpp +* @author Petr Douša +* @version V1.0 +* @date 20-April-2015 +* @brief This program use digital output and regular PWM output to modulate PWM +***********************************************************************************/ + +/**********************************************************************************/ +/* Table of PWM pins on Nucleo F303 (LQFP64) */ +/**********************************************************************************/ +/* LQFP64 pin | Nucleo pin | ST Pin | PWM number | Channel */ +/* 15 | A1 | PA_1 | PWM15 | 1 */ +/* 26 | A3 | PB_0 | PWM1 | 2 */ +/* 9 | A4 | PC_1 | PWM1 | 2 */ +/* 8 | A5 | PC_0 | PWM1 | 1 */ +/* 17 | D0 | PA_3 | PWM15 | 2 */ +/* 16 | D1 | PA_2 | PWM15 | 1 */ +/* 43 | D2 | PA_10 | PWM1 | 3 */ +/* 55 | D3 | PB_3 | PWM8 | 1 */ +/* 57 | D4 | PB_5 | PWM17 | 1 */ +/* 56 | D5 | PB_4 | PWM16 | 1 */ +/* 41 | D7 | PA_8 | PWM1 | 1 */ +/* 42 | D8 | PA_9 | PWM1 | 2 */ +/* 38 | D9 | PC_7 | PWM3 | 2 */ +/* 58 | D10 | PB_6 | PWM16 | 1 */ +/* 23 | D11 | PA_7 | PWM15 | 2 */ +/* 22 | D12 | PA_6 | PWM15 | 1 */ +/* 62 | D14 | PB_9 | PWM17 | 1 */ +/* 61 | D15 | PB_8 | PWM16 | 1 */ +/* 46 | | PA_13 | PWM16 | 1 */ +/* 49 | | PA_14 | PWM8 | 2 */ +/* 59 | | PB_7 | PWM17 | 1 */ +/* 2 | | PC_13 | PWM1 | 1 */ +/* 10 | | PC_2 | PWM1 | 3 */ +/* 11 | | PC_3 | PWM1 | 4 */ +/* 40 | | PC_9 | PWM3 | 4 */ +/* 39 | | PC_8 | PWM3 | 3 */ +/* 37 | | PC_6 | PWM3 | 1 */ +/* 45 | | PA_12 | PWM16 | 1 */ +/* 44 | | PA_11 | PWM1 | 4 */ +/* 27 | | PB_1 | PWM1 | 3 */ +/* 36 | | PB_15 | PWM17 | 1 */ +/* 35 | | PB_14 | PWM16 | 1 */ +/**********************************************************************************/ + +/* Includes ----------------------------------------------------------------------*/ +#include "mbed.h" + +/* Defines -----------------------------------------------------------------------*/ + +/* Function prototypes -----------------------------------------------------------*/ +void toggleOff(void); + +/* Variables ---------------------------------------------------------------------*/ +int on_delay = 0; +int off_delay = 0; + +//mbed - initialization of peripherals +Timeout timer; // timer use with digital IO +DigitalOut my_pwm(LED1); // digital IO used by pwm_io function +PwmOut real_pwm(PA_8); // regular PWM output + +/* Functions----------------------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : toggleOn. +* Description : Set high on digital pin and set and start timer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void toggleOn(void) +{ + my_pwm = 1; + timer.attach_us(toggleOff, on_delay); +} + +/******************************************************************************* +* Function Name : toggleOff. +* Description : Set low on digital pin and set and start timer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void toggleOff(void) +{ + my_pwm = 0; + timer.attach_us(toggleOn, off_delay); +} + +/******************************************************************************* +* Function Name : pwm_io. +* Description : Set variables and start PWM on digital output. +* Input : p_us: signal period in micro_seconds ,dc: signal duty-cycle (0.0 to 1.0) . +* Output : on_delay: time to be digital output on high ,off_delay: time to be digital output on low: . +* Return : None. +*******************************************************************************/ +void pwm_io(int p_us, float dc) +{ + timer.detach(); // shutdown timer + if ((p_us == 0) || (dc == 0)) { // if duty-cycle is 0 + my_pwm = 0; // set low regular IO to low + return; + } + if (dc >= 1) { // if duty-cycle is 1 and more + my_pwm = 1; // set high regular IO to low + return; + } + on_delay = (int)(p_us * dc); // calculate time to be digital output on high and low + off_delay = p_us - on_delay; + toggleOn(); // call function toggleOn to start PWM +} + +/*********************************************************************************** +* Function Name : main. +* Description : Main routine. +* Input : None. +* Output : None. +* Return : None. +***********************************************************************************/ +int main() +{ + double a=0; // variable to set duty-cycle + + real_pwm.period_us(2000); // set period of real PWM to 2 milliseconds + real_pwm.write(a); // set duty-cycle as it is in variable a (0 at this point) + + + pwm_io(2000, a); // set period of digital IO PWM to 2 ms and duty-cycle as in variable a (0 at this point) + + + while(1) { // main cykl of program + + if(a>=1) { // if variable is grater then 1 set it 0 else attributable 0.01 + a=0; + } else { + a=a+0.01; + } + + real_pwm.write(a); // set duty-cycle of real PWM as as it is in variable a + + pwm_io(2000, a); // set duty-cycle of digital IO PWM as as it is in variable a and period to 2 ms + + wait(0.05); // wait some time in milliseconds + } +}