Library that allows for higher resolution and speed than standard mbed PWM library. Based on FastPWM without floating point calculation in most functions

Fork of FastPWM by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HighPWM.cpp Source File

HighPWM.cpp

00001 #include "HighPWM.h"
00002 
00003 HighPWM::HighPWM(PinName pin) : PWMObject(pin){
00004     //Set clock source to CPU clock (without devider)
00005     LPC_SC->PCLKSEL0 |= 1<<12;
00006     
00007     _pulsewidth = 0;
00008     _period = 0;
00009     
00010     if (pin==p26||pin==LED1) {
00011         PWMUnit=1;
00012         MR=&LPC_PWM1->MR1;
00013         }
00014     else if (pin==p25||pin==LED2){
00015         PWMUnit=2;
00016         MR=&LPC_PWM1->MR2;
00017         }
00018     else if (pin==p24||pin==LED3){
00019         PWMUnit=3;
00020         MR=&LPC_PWM1->MR3;
00021         }
00022     else if (pin==p23||pin==LED4){
00023         PWMUnit=4;
00024         MR=&LPC_PWM1->MR4;
00025         }
00026     else if (pin==p22){
00027         PWMUnit=5;
00028         MR=&LPC_PWM1->MR5;
00029         }
00030     else if (pin==p21){
00031         PWMUnit=6;
00032         MR=&LPC_PWM1->MR6;
00033         }
00034     else
00035         error("Invalid hardware PWM pin\n\r");
00036     
00037     frequency(10000);   // 10kHz
00038 }
00039 
00040 void HighPWM::frequency(double frq) 
00041 {
00042     if ( frq>0.0 )
00043         period(1.0/frq);
00044 }
00045 
00046 void HighPWM::period(double seconds) 
00047 {
00048     period_ticks((unsigned int)(seconds*s_2_F_CLK+0.5));
00049 }
00050 
00051 void HighPWM::period_us(double us) 
00052 {
00053     period_ticks((unsigned int)(us*us_2_F_CLK+0.5));
00054 }
00055 
00056 void HighPWM::period_ms(unsigned int ms) 
00057 {
00058     period_ticks(ms*ms_2_F_CLK);
00059 }
00060 
00061 void HighPWM::period_us(unsigned int us) 
00062 {
00063     period_ticks(us*us_2_F_CLK);
00064 }
00065 
00066 void HighPWM::period_ticks(unsigned int ticks) 
00067 {
00068     double duty = (double)_pulsewidth/(double)_period;
00069     
00070     LPC_PWM1->MR0 = ticks;
00071     LPC_PWM1->LER |= 1;
00072     _period = ticks;
00073     pulsewidth_ticks((int)(duty*(double)_period+0.5));
00074 }
00075 
00076 void HighPWM::pulsewidth(double seconds) 
00077 {
00078     pulsewidth_ticks((unsigned int)(seconds*s_2_F_CLK+0.5));
00079 }
00080 
00081 void HighPWM::pulsewidth_us(double us) 
00082 {
00083     pulsewidth_ticks((unsigned int)(us*us_2_F_CLK+0.5));
00084 }
00085 
00086 void HighPWM::pulsewidth_ms(unsigned int ms) 
00087 {
00088     pulsewidth_ticks(ms*ms_2_F_CLK);
00089 }
00090 
00091 void HighPWM::pulsewidth_us(unsigned int us) 
00092 {
00093     pulsewidth_ticks(us*us_2_F_CLK);
00094 }
00095 
00096 void HighPWM::pulsewidth_ticks(unsigned int ticks) 
00097 {
00098     *MR = ticks;
00099     LPC_PWM1->LER |= 1<<PWMUnit;
00100     _pulsewidth = ticks;
00101 }
00102 
00103 void HighPWM::write(double duty) 
00104 {
00105     pulsewidth_ticks((int)(duty*(double)_period+0.5));
00106 }
00107 
00108 double HighPWM::read( void ) 
00109 {
00110     return (double)_pulsewidth/(double)_period;
00111 }
00112     
00113 HighPWM & HighPWM::operator= (double value) 
00114 {
00115     write(value);
00116     return(*this);
00117 }
00118     
00119 HighPWM::operator double() 
00120 {
00121     return read();
00122 }
00123