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

Dependencies:   mbed

Committer:
Foxnec
Date:
Tue May 12 09:46:06 2015 +0000
Revision:
1:56cc90ae5080
Parent:
0:ee9aab897b59
Changes to comments

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