Content originally posted in LPCWare by sns22 on Fri Feb 07 05:39:04 MST 2014
Hello everyone.
I am a newbie in lpc devices. I have been trying to interface the PWM based max6672 sensor with the PWM capture pin. I want to read the both the low time and high time of the PWM pulse. I am recieving an interrupt when the PWm input is received. But some how I am not able to calculate the rise and fall time correctly. I need this info to calculate the temperature of the sensor.
Steps in my code:
1. set up the PWM capture pin.
2. set the PWM capture registers.
3. Wait for interrupt on rise time. save the PWM0CR0 rise time value
4. set the register to low time interrupt.
5. wait for interrupt on low time. save the PWM0CR0 low time value = PWM0CR0 - high time.
6. calculate the temp using the high and low time of the pulse.
I am putting up my code here.
void Capture0IntrHandler(void)
{
if ((PWM0IR == 0x10)){
if((PWM0CCR_bit.CAP0RE == 1)){ //low to high transition rising edge
period = PWM0CR0 - rising_edg;
rising_edg = PWM0CR0;
PWM0CCR = 0x6;
}else{
duty = PWM0CR0 - rising_edg;
PWM0CCR = 0x5;
PWM0_Done =1;
}
}
PWM0IR=0xFF; //Reset the interrupt*/
VICADDRESS = 0;
}
void init_timer(void) // Timer 0 used as a counter
{
// PWM PCAP0.0 Pin: P1.12
// pclk = cclk/2 = 30MHz (cclk=ARM7 core clock)
unsigned int IRQ_priority= 0x1; // '0' = highest, '0xF' = lowest IRQ priority
PINSEL2 = 0x03000000; //PWM PCAP0.0 Pin: P1.12 //1 = pclk = 60 MHz or 72MHz
PCONP_bit.PCPWM0 |=1;
PCLKSEL0_bit.PCLK_PWM0 = 1; //pclk is set to 2 = pclk/2 = 36 MHz or 30MHz
//RISING EDGE CAPTURE
PWM0TCR_bit.CR = 0; //disable PWM counter reset
PWM0TCR_bit.CR = 1; //Reset PWM counter
PWM0TCR_bit.CR = 0; //disable PWM counter reset
PWM0CCR_bit.CAP0RE = 1; //capture of rising edge
PWM0CCR_bit.CAP0FE = 0; //disable falling edge capture
PWM0CCR_bit.CAP0INT = 1; //enable interrupt rising edge
PWM0CTCR_bit.CM = 0; //PWM0 is set in timer mode
PWM0CTCR_bit.CIS = 0;
PWM0PCR =0; //no external interrupt
PWM0PR = 59; //resolution of 1 us for a 60MHz cclk = pclk
PWM0IR = 0xFF; //reset the interrupts
VIC_SetVectoredIRQ(Capture0IntrHandler, IRQ_priority, VIC_PWM01); // setup PWM IRQ
PWM0TCR_bit.CE =1; //enable the PWM Capture timer
}
int main (void)
{
LPC2468_Init();
ConfigurePLL();
LPC2468GPIOInit();
__disable_interrupt();
LPC2468VICInit();
__enable_interrupt();
period = 0;
falling_edg = 0;
rising_edg =0;
duty = 0;
init_timer();
PWM0_Done =0;
while (PWM0_Done == 0){};
y = 3;
t1 = duty;
t2 = rising_edg;
k = (double)t1/t2;
x11 = (temp_coeff - k);
x1 = (double)powf(x11,y);
x2 = ((double)(temp_coeff2 * k)) - temp_coeff3;
temp_deg = ((double)((-temp_coeff1)* x1)) + x2 ;
peri = rising_edg + duty;
return 0;
}
Thanks
Sneha