PWM with regular digital IO and with digital pin that supports PWM generation

Dependencies:   mbed

Committer:
dousape2
Date:
Mon Apr 20 15:07:18 2015 +0000
Revision:
0:ee9aab897b59
Child:
1:56cc90ae5080
verze 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dousape2 0:ee9aab897b59 1 /**********************************************************************************
dousape2 0:ee9aab897b59 2 * @file main.cpp
dousape2 0:ee9aab897b59 3 * @author Petr Douša
dousape2 0:ee9aab897b59 4 * @version V1.0
dousape2 0:ee9aab897b59 5 * @date 20-April-2015
dousape2 0:ee9aab897b59 6 * @brief This program use digital output and regular PWM output to modulate PWM
dousape2 0:ee9aab897b59 7 ***********************************************************************************/
dousape2 0:ee9aab897b59 8
dousape2 0:ee9aab897b59 9 /**********************************************************************************/
dousape2 0:ee9aab897b59 10 /* Table of PWM pins on Nucleo F303 (LQFP64) */
dousape2 0:ee9aab897b59 11 /**********************************************************************************/
dousape2 0:ee9aab897b59 12 /* LQFP64 pin | Nucleo pin | ST Pin | PWM number | Channel */
dousape2 0:ee9aab897b59 13 /* 15 | A1 | PA_1 | PWM15 | 1 */
dousape2 0:ee9aab897b59 14 /* 26 | A3 | PB_0 | PWM1 | 2 */
dousape2 0:ee9aab897b59 15 /* 9 | A4 | PC_1 | PWM1 | 2 */
dousape2 0:ee9aab897b59 16 /* 8 | A5 | PC_0 | PWM1 | 1 */
dousape2 0:ee9aab897b59 17 /* 17 | D0 | PA_3 | PWM15 | 2 */
dousape2 0:ee9aab897b59 18 /* 16 | D1 | PA_2 | PWM15 | 1 */
dousape2 0:ee9aab897b59 19 /* 43 | D2 | PA_10 | PWM1 | 3 */
dousape2 0:ee9aab897b59 20 /* 55 | D3 | PB_3 | PWM8 | 1 */
dousape2 0:ee9aab897b59 21 /* 57 | D4 | PB_5 | PWM17 | 1 */
dousape2 0:ee9aab897b59 22 /* 56 | D5 | PB_4 | PWM16 | 1 */
dousape2 0:ee9aab897b59 23 /* 41 | D7 | PA_8 | PWM1 | 1 */
dousape2 0:ee9aab897b59 24 /* 42 | D8 | PA_9 | PWM1 | 2 */
dousape2 0:ee9aab897b59 25 /* 38 | D9 | PC_7 | PWM3 | 2 */
dousape2 0:ee9aab897b59 26 /* 58 | D10 | PB_6 | PWM16 | 1 */
dousape2 0:ee9aab897b59 27 /* 23 | D11 | PA_7 | PWM15 | 2 */
dousape2 0:ee9aab897b59 28 /* 22 | D12 | PA_6 | PWM15 | 1 */
dousape2 0:ee9aab897b59 29 /* 62 | D14 | PB_9 | PWM17 | 1 */
dousape2 0:ee9aab897b59 30 /* 61 | D15 | PB_8 | PWM16 | 1 */
dousape2 0:ee9aab897b59 31 /* 46 | | PA_13 | PWM16 | 1 */
dousape2 0:ee9aab897b59 32 /* 49 | | PA_14 | PWM8 | 2 */
dousape2 0:ee9aab897b59 33 /* 59 | | PB_7 | PWM17 | 1 */
dousape2 0:ee9aab897b59 34 /* 2 | | PC_13 | PWM1 | 1 */
dousape2 0:ee9aab897b59 35 /* 10 | | PC_2 | PWM1 | 3 */
dousape2 0:ee9aab897b59 36 /* 11 | | PC_3 | PWM1 | 4 */
dousape2 0:ee9aab897b59 37 /* 40 | | PC_9 | PWM3 | 4 */
dousape2 0:ee9aab897b59 38 /* 39 | | PC_8 | PWM3 | 3 */
dousape2 0:ee9aab897b59 39 /* 37 | | PC_6 | PWM3 | 1 */
dousape2 0:ee9aab897b59 40 /* 45 | | PA_12 | PWM16 | 1 */
dousape2 0:ee9aab897b59 41 /* 44 | | PA_11 | PWM1 | 4 */
dousape2 0:ee9aab897b59 42 /* 27 | | PB_1 | PWM1 | 3 */
dousape2 0:ee9aab897b59 43 /* 36 | | PB_15 | PWM17 | 1 */
dousape2 0:ee9aab897b59 44 /* 35 | | PB_14 | PWM16 | 1 */
dousape2 0:ee9aab897b59 45 /**********************************************************************************/
dousape2 0:ee9aab897b59 46
dousape2 0:ee9aab897b59 47 /* Includes ----------------------------------------------------------------------*/
dousape2 0:ee9aab897b59 48 #include "mbed.h"
dousape2 0:ee9aab897b59 49
dousape2 0:ee9aab897b59 50 /* Defines -----------------------------------------------------------------------*/
dousape2 0:ee9aab897b59 51
dousape2 0:ee9aab897b59 52 /* Function prototypes -----------------------------------------------------------*/
dousape2 0:ee9aab897b59 53 void toggleOff(void);
dousape2 0:ee9aab897b59 54
dousape2 0:ee9aab897b59 55 /* Variables ---------------------------------------------------------------------*/
dousape2 0:ee9aab897b59 56 int on_delay = 0;
dousape2 0:ee9aab897b59 57 int off_delay = 0;
dousape2 0:ee9aab897b59 58
dousape2 0:ee9aab897b59 59 //mbed - initialization of peripherals
dousape2 0:ee9aab897b59 60 Timeout timer; // timer use with digital IO
dousape2 0:ee9aab897b59 61 DigitalOut my_pwm(LED1); // digital IO used by pwm_io function
dousape2 0:ee9aab897b59 62 PwmOut real_pwm(PA_8); // regular PWM output
dousape2 0:ee9aab897b59 63
dousape2 0:ee9aab897b59 64 /* Functions----------------------------------------------------------------------*/
dousape2 0:ee9aab897b59 65
dousape2 0:ee9aab897b59 66 /*******************************************************************************
dousape2 0:ee9aab897b59 67 * Function Name : toggleOn.
dousape2 0:ee9aab897b59 68 * Description : Set high on digital pin and set and start timer.
dousape2 0:ee9aab897b59 69 * Input : None.
dousape2 0:ee9aab897b59 70 * Output : None.
dousape2 0:ee9aab897b59 71 * Return : None.
dousape2 0:ee9aab897b59 72 *******************************************************************************/
dousape2 0:ee9aab897b59 73 void toggleOn(void)
dousape2 0:ee9aab897b59 74 {
dousape2 0:ee9aab897b59 75 my_pwm = 1;
dousape2 0:ee9aab897b59 76 timer.attach_us(toggleOff, on_delay);
dousape2 0:ee9aab897b59 77 }
dousape2 0:ee9aab897b59 78
dousape2 0:ee9aab897b59 79 /*******************************************************************************
dousape2 0:ee9aab897b59 80 * Function Name : toggleOff.
dousape2 0:ee9aab897b59 81 * Description : Set low on digital pin and set and start timer.
dousape2 0:ee9aab897b59 82 * Input : None.
dousape2 0:ee9aab897b59 83 * Output : None.
dousape2 0:ee9aab897b59 84 * Return : None.
dousape2 0:ee9aab897b59 85 *******************************************************************************/
dousape2 0:ee9aab897b59 86 void toggleOff(void)
dousape2 0:ee9aab897b59 87 {
dousape2 0:ee9aab897b59 88 my_pwm = 0;
dousape2 0:ee9aab897b59 89 timer.attach_us(toggleOn, off_delay);
dousape2 0:ee9aab897b59 90 }
dousape2 0:ee9aab897b59 91
dousape2 0:ee9aab897b59 92 /*******************************************************************************
dousape2 0:ee9aab897b59 93 * Function Name : pwm_io.
dousape2 0:ee9aab897b59 94 * Description : Set variables and start PWM on digital output.
dousape2 0:ee9aab897b59 95 * Input : p_us: signal period in micro_seconds ,dc: signal duty-cycle (0.0 to 1.0) .
dousape2 0:ee9aab897b59 96 * Output : on_delay: time to be digital output on high ,off_delay: time to be digital output on low: .
dousape2 0:ee9aab897b59 97 * Return : None.
dousape2 0:ee9aab897b59 98 *******************************************************************************/
dousape2 0:ee9aab897b59 99 void pwm_io(int p_us, float dc)
dousape2 0:ee9aab897b59 100 {
dousape2 0:ee9aab897b59 101 timer.detach(); // shutdown timer
dousape2 0:ee9aab897b59 102 if ((p_us == 0) || (dc == 0)) { // if duty-cycle is 0
dousape2 0:ee9aab897b59 103 my_pwm = 0; // set low regular IO to low
dousape2 0:ee9aab897b59 104 return;
dousape2 0:ee9aab897b59 105 }
dousape2 0:ee9aab897b59 106 if (dc >= 1) { // if duty-cycle is 1 and more
dousape2 0:ee9aab897b59 107 my_pwm = 1; // set high regular IO to low
dousape2 0:ee9aab897b59 108 return;
dousape2 0:ee9aab897b59 109 }
dousape2 0:ee9aab897b59 110 on_delay = (int)(p_us * dc); // calculate time to be digital output on high and low
dousape2 0:ee9aab897b59 111 off_delay = p_us - on_delay;
dousape2 0:ee9aab897b59 112 toggleOn(); // call function toggleOn to start PWM
dousape2 0:ee9aab897b59 113 }
dousape2 0:ee9aab897b59 114
dousape2 0:ee9aab897b59 115 /***********************************************************************************
dousape2 0:ee9aab897b59 116 * Function Name : main.
dousape2 0:ee9aab897b59 117 * Description : Main routine.
dousape2 0:ee9aab897b59 118 * Input : None.
dousape2 0:ee9aab897b59 119 * Output : None.
dousape2 0:ee9aab897b59 120 * Return : None.
dousape2 0:ee9aab897b59 121 ***********************************************************************************/
dousape2 0:ee9aab897b59 122 int main()
dousape2 0:ee9aab897b59 123 {
dousape2 0:ee9aab897b59 124 double a=0; // variable to set duty-cycle
dousape2 0:ee9aab897b59 125
dousape2 0:ee9aab897b59 126 real_pwm.period_us(2000); // set period of real PWM to 2 milliseconds
dousape2 0:ee9aab897b59 127 real_pwm.write(a); // set duty-cycle as it is in variable a (0 at this point)
dousape2 0:ee9aab897b59 128
dousape2 0:ee9aab897b59 129
dousape2 0:ee9aab897b59 130 pwm_io(2000, a); // set period of digital IO PWM to 2 ms and duty-cycle as in variable a (0 at this point)
dousape2 0:ee9aab897b59 131
dousape2 0:ee9aab897b59 132
dousape2 0:ee9aab897b59 133 while(1) { // main cykl of program
dousape2 0:ee9aab897b59 134
dousape2 0:ee9aab897b59 135 if(a>=1) { // if variable is grater then 1 set it 0 else attributable 0.01
dousape2 0:ee9aab897b59 136 a=0;
dousape2 0:ee9aab897b59 137 } else {
dousape2 0:ee9aab897b59 138 a=a+0.01;
dousape2 0:ee9aab897b59 139 }
dousape2 0:ee9aab897b59 140
dousape2 0:ee9aab897b59 141 real_pwm.write(a); // set duty-cycle of real PWM as as it is in variable a
dousape2 0:ee9aab897b59 142
dousape2 0:ee9aab897b59 143 pwm_io(2000, a); // set duty-cycle of digital IO PWM as as it is in variable a and period to 2 ms
dousape2 0:ee9aab897b59 144
dousape2 0:ee9aab897b59 145 wait(0.05); // wait some time in milliseconds
dousape2 0:ee9aab897b59 146 }
dousape2 0:ee9aab897b59 147 }