Mark Uckermann / DtPWM
Committer:
dontknowhow
Date:
Wed Mar 29 12:34:52 2017 +0000
Revision:
2:1ef7ff2f120e
Parent:
0:f4cf8380dbee
Child:
3:d7b9697768d8
Interrupt settings and fast disable/enable of PWM output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dontknowhow 0:f4cf8380dbee 1 #include "mbed.h"
dontknowhow 0:f4cf8380dbee 2
dontknowhow 0:f4cf8380dbee 3 #ifndef DTPWM_H
dontknowhow 0:f4cf8380dbee 4 #define DTPWM_H
dontknowhow 0:f4cf8380dbee 5
dontknowhow 2:1ef7ff2f120e 6 /**
dontknowhow 2:1ef7ff2f120e 7 * Library for fast and high resolution opposite PWM output with
dontknowhow 2:1ef7ff2f120e 8 * controllable dead times
dontknowhow 2:1ef7ff2f120e 9 */
dontknowhow 2:1ef7ff2f120e 10
dontknowhow 2:1ef7ff2f120e 11 /** definitions for interrupt flags, usage in IRS:
dontknowhow 2:1ef7ff2f120e 12 * if ( LPC_PWM1->IR & DTPWM_IF0 ) {
dontknowhow 2:1ef7ff2f120e 13 * // do something on MR0 interrupt
dontknowhow 2:1ef7ff2f120e 14 * LPC_PWM1->IR |= DTPWM_IF0; // reset interrupt flag
dontknowhow 2:1ef7ff2f120e 15 * }
dontknowhow 2:1ef7ff2f120e 16 */
dontknowhow 2:1ef7ff2f120e 17 #define DTPWM_IF0 (1 << 0U)
dontknowhow 2:1ef7ff2f120e 18 #define DTPWM_IF1 (1 << 1U)
dontknowhow 2:1ef7ff2f120e 19 #define DTPWM_IF2 (1 << 2U)
dontknowhow 2:1ef7ff2f120e 20 #define DTPWM_IF3 (1 << 3U)
dontknowhow 2:1ef7ff2f120e 21 #define DTPWM_IF4 (1 << 8U)
dontknowhow 2:1ef7ff2f120e 22 #define DTPWM_IF5 (1 << 9U)
dontknowhow 2:1ef7ff2f120e 23 #define DTPWM_IF6 (1 << 10U)
dontknowhow 2:1ef7ff2f120e 24
dontknowhow 2:1ef7ff2f120e 25 /** definitions for interrupt enable flags, usage:
dontknowhow 2:1ef7ff2f120e 26 * pwm.setInterrupts(DTPWM_SI0 | DTPWM_SI5); //to set MR0 & MR5 interrupts
dontknowhow 2:1ef7ff2f120e 27 */
dontknowhow 2:1ef7ff2f120e 28 #define DTPWM_SI0 (1 << 0U)
dontknowhow 2:1ef7ff2f120e 29 #define DTPWM_SI1 (1 << 3U)
dontknowhow 2:1ef7ff2f120e 30 #define DTPWM_SI2 (1 << 6U)
dontknowhow 2:1ef7ff2f120e 31 #define DTPWM_SI3 (1 << 9U)
dontknowhow 2:1ef7ff2f120e 32 #define DTPWM_SI4 (1 << 12U)
dontknowhow 2:1ef7ff2f120e 33 #define DTPWM_SI5 (1 << 15U)
dontknowhow 2:1ef7ff2f120e 34 #define DTPWM_SI6 (1 << 18U)
dontknowhow 2:1ef7ff2f120e 35
dontknowhow 2:1ef7ff2f120e 36 #define DTPWM_SIA (DTPWM_SI0 | DTPWM_SI1 | DTPWM_SI2 | DTPWM_SI3 | DTPWM_SI4 | DTPWM_SI5 | DTPWM_SI6)
dontknowhow 2:1ef7ff2f120e 37
dontknowhow 0:f4cf8380dbee 38 class DtPWM{
dontknowhow 0:f4cf8380dbee 39 public:
dontknowhow 0:f4cf8380dbee 40 /**
dontknowhow 0:f4cf8380dbee 41 * Create a dtPWM object connected to channel 2 and 4, optionally to LED 2&4
dontknowhow 0:f4cf8380dbee 42 *
dontknowhow 0:f4cf8380dbee 43 * @param led - false does not connect LEDs (default)
dontknowhow 0:f4cf8380dbee 44 */
dontknowhow 0:f4cf8380dbee 45 DtPWM(bool led = false);
dontknowhow 0:f4cf8380dbee 46 ~DtPWM();
dontknowhow 0:f4cf8380dbee 47
dontknowhow 2:1ef7ff2f120e 48
dontknowhow 2:1ef7ff2f120e 49 /**
dontknowhow 2:1ef7ff2f120e 50 * enable pin outputs and start the timer
dontknowhow 2:1ef7ff2f120e 51 */
dontknowhow 2:1ef7ff2f120e 52 void enable();
dontknowhow 2:1ef7ff2f120e 53
dontknowhow 2:1ef7ff2f120e 54 /**
dontknowhow 2:1ef7ff2f120e 55 * set output pins to low as fast as possible and stop the timer
dontknowhow 2:1ef7ff2f120e 56 */
dontknowhow 2:1ef7ff2f120e 57 void disable();
dontknowhow 2:1ef7ff2f120e 58
dontknowhow 0:f4cf8380dbee 59 /**
dontknowhow 0:f4cf8380dbee 60 * Set the period, duty cycle, and dead time.
dontknowhow 0:f4cf8380dbee 61 *
dontknowhow 0:f4cf8380dbee 62 * @param p_us - period in micro seconds
dontknowhow 0:f4cf8380dbee 63 * @param d - duty cycle from 0-1 as float
dontknowhow 0:f4cf8380dbee 64 * @param dt_us - dead time between switching in micro seconds
dontknowhow 0:f4cf8380dbee 65 */
dontknowhow 0:f4cf8380dbee 66 void setDtPWM(double p_us, float d, double dt_us);
dontknowhow 0:f4cf8380dbee 67
dontknowhow 0:f4cf8380dbee 68 /**
dontknowhow 2:1ef7ff2f120e 69 * Set which interrupts are enabled
dontknowhow 2:1ef7ff2f120e 70 *
dontknowhow 2:1ef7ff2f120e 71 * @param flags - OR'd bits for the interrupt register, 0 to disable interrupts
dontknowhow 2:1ef7ff2f120e 72 * use the definitions eg. pwm.setInterrupts(DTPWM_SI0 | DTPWM_SI5); to set MR0 & MR5
dontknowhow 2:1ef7ff2f120e 73 * @param priority - set interrupt priority level. -1 (default) leaves them unchanged
dontknowhow 2:1ef7ff2f120e 74 */
dontknowhow 2:1ef7ff2f120e 75 void setInterrupts(uint32_t flags, int prio = -1);
dontknowhow 2:1ef7ff2f120e 76
dontknowhow 2:1ef7ff2f120e 77 /**
dontknowhow 0:f4cf8380dbee 78 * Value for maximum duty cycle that fulfils minimum dead times
dontknowhow 0:f4cf8380dbee 79 */
dontknowhow 0:f4cf8380dbee 80 float d_max;
dontknowhow 0:f4cf8380dbee 81
dontknowhow 0:f4cf8380dbee 82 /**
dontknowhow 2:1ef7ff2f120e 83 * Function to just set the duty cycle.
dontknowhow 0:f4cf8380dbee 84 *
dontknowhow 0:f4cf8380dbee 85 * @param d - duty cycle from 0-1 as float no checking is done in this function
dontknowhow 0:f4cf8380dbee 86 */
dontknowhow 0:f4cf8380dbee 87 void setD(float d);
dontknowhow 0:f4cf8380dbee 88
dontknowhow 0:f4cf8380dbee 89 private:
dontknowhow 0:f4cf8380dbee 90 unsigned int p_tcks; // clock ticks in period
dontknowhow 0:f4cf8380dbee 91 unsigned int dt_tcks; // dead time ticks
dontknowhow 0:f4cf8380dbee 92 };
dontknowhow 0:f4cf8380dbee 93 #endif