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.
Dependents: PWMAverage_test pwm_duty_measurement
PWMAverage.cpp
00001 #include "PWMAverage.h" 00002 00003 #define PWMA_PCLK 96000000 00004 00005 00006 PWMAverage * PWMAverage::instance; 00007 00008 00009 void PWMAverage::_tisr() 00010 { 00011 //printf("."); 00012 int cr0 = LPC_TIM2->CR0; 00013 int cr1 = LPC_TIM2->CR1; 00014 00015 LPC_TIM2->IR = 0x3F; 00016 00017 LPC_TIM2->TCR = 3; 00018 LPC_TIM2->TCR = 1; 00019 00020 //LPC_NVIC->ICPR0 |= (1<<3) 00021 00022 if(!instance->starting) 00023 { 00024 instance->total += cr1 + 82; 00025 instance->totalup += cr0 + 82; 00026 instance->count_++; 00027 } 00028 else instance->starting = false; 00029 00030 } 00031 00032 PWMAverage::PWMAverage(PinName cap0, PinName cap1) 00033 { 00034 prescaler_point = 0x0; 00035 configure(); 00036 stop(); 00037 00038 timeMult = (1/PWMA_PCLK)*(prescaler_point+1); 00039 timeDiv = PWMA_PCLK/(prescaler_point+1); 00040 instance = this; 00041 } 00042 00043 void PWMAverage::reset() 00044 { 00045 count_ = 0; 00046 total = 0; 00047 totalup = 0; 00048 LPC_TIM2->TCR = 2; 00049 LPC_TIM2->TCR = 0; 00050 } 00051 00052 void PWMAverage::start() 00053 { 00054 //reset(); 00055 starting = true; 00056 enable(true); 00057 NVIC_EnableIRQ(TIMER2_IRQn); 00058 } 00059 00060 void PWMAverage::stop() 00061 { 00062 enable(false); 00063 NVIC_DisableIRQ(TIMER2_IRQn); 00064 } 00065 00066 float PWMAverage::read() 00067 { 00068 if(period() != 0) return float(avg_up()/period()); 00069 else return 0; 00070 } 00071 00072 double PWMAverage::avg_up() 00073 { 00074 if(count_!= 0) return (double(totalup)/timeDiv)/double(count_); 00075 else return 0; 00076 00077 } 00078 00079 float PWMAverage::avg_UP() 00080 { 00081 if(count_!= 0) return float((double(totalup)/timeDiv)/double(count_)); 00082 else return 0; 00083 } 00084 00085 double PWMAverage::avg_down() 00086 { 00087 if(count_!= 0) return (double(total-totalup)/timeDiv)/double(count_); 00088 else return 0; 00089 } 00090 00091 double PWMAverage::period() 00092 { 00093 if(count_!= 0) return (double(total)/timeDiv)/double(count_); 00094 else return 0; 00095 } 00096 00097 int PWMAverage::count() 00098 { 00099 return count_; 00100 } 00101 00102 void PWMAverage::enable(bool yn) 00103 { 00104 LPC_TIM2->TCR = yn; 00105 } 00106 00107 void PWMAverage::configure() 00108 { 00109 //Power Periferal 00110 00111 LPC_SC->PCONP |= (1<<22); 00112 00113 //Setup Pins 00114 00115 LPC_PINCON->PINSEL0 |= (0x3<<8); 00116 LPC_PINCON->PINSEL0 |= (0x3<<10); 00117 00118 //Setup clock source 00119 00120 LPC_SC->PCLKSEL1 |= (0x1<<12); 00121 00122 //Setup PC 00123 00124 LPC_TIM2->PR = prescaler_point; 00125 00126 //Setup CAP0 - Store on fall 00127 00128 LPC_TIM2->CCR |= 0x2; 00129 00130 //Setup CAP1 - Store on rise, interrupt 00131 00132 LPC_TIM2->CCR |= 0x5<<3; 00133 00134 //Setup IRQs 00135 NVIC_DisableIRQ(TIMER2_IRQn); 00136 NVIC_SetVector(TIMER2_IRQn, (uint32_t)&_tisr); 00137 NVIC_EnableIRQ(TIMER2_IRQn); 00138 // 00139 }
Generated on Tue Jul 12 2022 20:22:51 by
