Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
DtPWM.h@3:d7b9697768d8, 2017-04-04 (annotated)
- Committer:
- dontknowhow
- Date:
- Tue Apr 04 22:20:38 2017 +0000
- Revision:
- 3:d7b9697768d8
- Parent:
- 2:1ef7ff2f120e
- Child:
- 4:a490a36f1cb1
single channel enable;
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:d7b9697768d8 | 43 | * @param ebaled - start pwm enabled, output as soon as object is created (default) | 
| dontknowhow | 0:f4cf8380dbee | 44 | */ | 
| dontknowhow | 3:d7b9697768d8 | 45 | DtPWM(bool enable = true); | 
| dontknowhow | 2:1ef7ff2f120e | 46 | |
| dontknowhow | 2:1ef7ff2f120e | 47 | /** | 
| dontknowhow | 2:1ef7ff2f120e | 48 | * enable pin outputs and start the timer | 
| dontknowhow | 3:d7b9697768d8 | 49 | * | 
| dontknowhow | 3:d7b9697768d8 | 50 | * @channel - determines which channels are enabled. | 
| dontknowhow | 3:d7b9697768d8 | 51 | * 'h' for high (PWM1.2) | 
| dontknowhow | 3:d7b9697768d8 | 52 | * 'l' for low (PWM1.4) | 
| dontknowhow | 3:d7b9697768d8 | 53 | * 'a' for both (PWM1.2 & 1.4) (default) | 
| dontknowhow | 2:1ef7ff2f120e | 54 | */ | 
| dontknowhow | 3:d7b9697768d8 | 55 | void enable(char channel = 'a'); | 
| dontknowhow | 2:1ef7ff2f120e | 56 | |
| dontknowhow | 2:1ef7ff2f120e | 57 | /** | 
| dontknowhow | 2:1ef7ff2f120e | 58 | * set output pins to low as fast as possible and stop the timer | 
| dontknowhow | 2:1ef7ff2f120e | 59 | */ | 
| dontknowhow | 2:1ef7ff2f120e | 60 | void disable(); | 
| dontknowhow | 2:1ef7ff2f120e | 61 | |
| dontknowhow | 0:f4cf8380dbee | 62 | /** | 
| dontknowhow | 0:f4cf8380dbee | 63 | * Set the period, duty cycle, and dead time. | 
| dontknowhow | 0:f4cf8380dbee | 64 | * | 
| dontknowhow | 0:f4cf8380dbee | 65 | * @param p_us - period in micro seconds | 
| dontknowhow | 0:f4cf8380dbee | 66 | * @param d - duty cycle from 0-1 as float | 
| dontknowhow | 0:f4cf8380dbee | 67 | * @param dt_us - dead time between switching in micro seconds | 
| dontknowhow | 0:f4cf8380dbee | 68 | */ | 
| dontknowhow | 0:f4cf8380dbee | 69 | void setDtPWM(double p_us, float d, double dt_us); | 
| dontknowhow | 0:f4cf8380dbee | 70 | |
| dontknowhow | 0:f4cf8380dbee | 71 | /** | 
| dontknowhow | 2:1ef7ff2f120e | 72 | * Set which interrupts are enabled | 
| dontknowhow | 2:1ef7ff2f120e | 73 | * | 
| dontknowhow | 2:1ef7ff2f120e | 74 | * @param flags - OR'd bits for the interrupt register, 0 to disable interrupts | 
| dontknowhow | 2:1ef7ff2f120e | 75 | * use the definitions eg. pwm.setInterrupts(DTPWM_SI0 | DTPWM_SI5); to set MR0 & MR5 | 
| dontknowhow | 2:1ef7ff2f120e | 76 | * @param priority - set interrupt priority level. -1 (default) leaves them unchanged | 
| dontknowhow | 2:1ef7ff2f120e | 77 | */ | 
| dontknowhow | 2:1ef7ff2f120e | 78 | void setInterrupts(uint32_t flags, int prio = -1); | 
| dontknowhow | 2:1ef7ff2f120e | 79 | |
| dontknowhow | 2:1ef7ff2f120e | 80 | /** | 
| dontknowhow | 0:f4cf8380dbee | 81 | * Value for maximum duty cycle that fulfils minimum dead times | 
| dontknowhow | 0:f4cf8380dbee | 82 | */ | 
| dontknowhow | 0:f4cf8380dbee | 83 | float d_max; | 
| dontknowhow | 0:f4cf8380dbee | 84 | |
| dontknowhow | 0:f4cf8380dbee | 85 | /** | 
| dontknowhow | 2:1ef7ff2f120e | 86 | * Function to just set the duty cycle. | 
| dontknowhow | 0:f4cf8380dbee | 87 | * | 
| dontknowhow | 0:f4cf8380dbee | 88 | * @param d - duty cycle from 0-1 as float no checking is done in this function | 
| dontknowhow | 0:f4cf8380dbee | 89 | */ | 
| dontknowhow | 0:f4cf8380dbee | 90 | void setD(float d); | 
| dontknowhow | 0:f4cf8380dbee | 91 | |
| dontknowhow | 0:f4cf8380dbee | 92 | private: | 
| dontknowhow | 0:f4cf8380dbee | 93 | unsigned int p_tcks; // clock ticks in period | 
| dontknowhow | 0:f4cf8380dbee | 94 | unsigned int dt_tcks; // dead time ticks | 
| dontknowhow | 0:f4cf8380dbee | 95 | }; | 
| dontknowhow | 0:f4cf8380dbee | 96 | #endif |