Mark Uckermann / DtPWM

DtPWM.h

Committer:
dontknowhow
Date:
2017-03-29
Revision:
2:1ef7ff2f120e
Parent:
0:f4cf8380dbee
Child:
3:d7b9697768d8

File content as of revision 2:1ef7ff2f120e:

#include "mbed.h"

#ifndef DTPWM_H
#define DTPWM_H

/** 
* Library for fast and high resolution opposite PWM output with 
* controllable dead times
*/

/** definitions for interrupt flags, usage in IRS:
* if ( LPC_PWM1->IR & DTPWM_IF0 ) {
*     // do something on MR0 interrupt
*     LPC_PWM1->IR |= DTPWM_IF0; // reset interrupt flag  
* }
*/
#define DTPWM_IF0 (1 << 0U)
#define DTPWM_IF1 (1 << 1U)
#define DTPWM_IF2 (1 << 2U)
#define DTPWM_IF3 (1 << 3U)
#define DTPWM_IF4 (1 << 8U)
#define DTPWM_IF5 (1 << 9U)
#define DTPWM_IF6 (1 << 10U)

/** definitions for interrupt enable flags, usage:
* pwm.setInterrupts(DTPWM_SI0 | DTPWM_SI5); //to set MR0 & MR5 interrupts
*/
#define DTPWM_SI0 (1 << 0U)
#define DTPWM_SI1 (1 << 3U)
#define DTPWM_SI2 (1 << 6U)
#define DTPWM_SI3 (1 << 9U)
#define DTPWM_SI4 (1 << 12U)
#define DTPWM_SI5 (1 << 15U)
#define DTPWM_SI6 (1 << 18U)

#define DTPWM_SIA (DTPWM_SI0 | DTPWM_SI1 | DTPWM_SI2 | DTPWM_SI3 | DTPWM_SI4 | DTPWM_SI5 | DTPWM_SI6)

class DtPWM{
public:
    /**
    * Create a dtPWM object connected to channel 2 and 4, optionally to LED 2&4
    *
    * @param led - false does not connect LEDs (default)
    */
    DtPWM(bool led = false);
    ~DtPWM(); 
    
    
    /**
    * enable pin outputs and start the timer
    */
    void enable();
    
    /**
    * set output pins to low as fast as possible and stop the timer
    */
    void disable();
    
    /**
    * Set the period, duty cycle, and dead time.
    *
    * @param p_us - period in micro seconds
    * @param d - duty cycle from 0-1 as float
    * @param dt_us - dead time between switching in micro seconds
    */
    void setDtPWM(double p_us, float d, double dt_us);
    
    /**
    * Set which interrupts are enabled
    *
    * @param flags - OR'd bits for the interrupt register, 0 to disable interrupts
    *   use the definitions eg. pwm.setInterrupts(DTPWM_SI0 | DTPWM_SI5); to set MR0 & MR5
    * @param priority - set interrupt priority level. -1 (default) leaves them unchanged
    */
    void setInterrupts(uint32_t flags, int prio = -1);
    
    /**
    * Value for maximum duty cycle that fulfils minimum dead times 
    */
    float d_max;
    
    /**
    * Function to just set the duty cycle.
    *
    * @param d - duty cycle from 0-1 as float no checking is done in this function
    */
    void setD(float d);
    
private:
    unsigned int p_tcks;        // clock ticks in period
    unsigned int dt_tcks;       // dead time ticks
};
#endif