Hi, my first topic.
I'm trying to measure rise fall time for a signal at 16K. Time measurement is defined between 2V and 5V for rise and vv for fall.
My idea is use comparator to detect level (2V and 5V) and generate interrupt to save TMR2 value. I'm using DAC to provide for refer....but observed measured value are not dependent from signal (I'm using wave generator to test)..I think there is problem in interrupt management.
/*measure setup tu execute before every measure*/
void LnMeterTime_Setup(void)
{
CM1CON0bits.C1CH = LnMeterSts_CONCh;
Lm_Time_DATA_REDY = Lm_DATA_REDY_Empty;
VREFCON2 = T_High_Th_DACR;
CM1CON0bits.C1POL = TRUE;
CM1CON0bits.CON = TRUE; /*Comparatore abilitato*/
PIR2bits.C1IF = FALSE;
PIE2bits.C1IE = TRUE;
}
/*Interrupt code*/
void LnMeterTimeMeasureIsr(void)
{
static BYTE TMR2_Old = 0;
static BYTE TMR2_Current = 0;
static BYTE temp = 0;
static RiseFallTimeSts_t RiseFallTimeSts = RiseFallTimeSts_Idle;
TMR2_Current = TMR2;
if(Lm_Time_DATA_REDY == Lm_DATA_REDY_Empty)
{
Lm_Time_DATA_REDY = Lm_DATA_REDY_Init;
RiseFallTimeSts = RiseFallTimeSts_Init;
}
switch(RiseFallTimeSts)
{
case RiseFallTimeSts_Idle:
break;
case RiseFallTimeSts_Init: /*Cerco il fronte di discesa*/
RiseFallTimeSts = RiseFallTimeSts_FallHigh_Th;
VREFCON2 = T_High_Th_DACR;
CM1CON0bits.C1POL = FALSE;
break;
case RiseFallTimeSts_FallHigh_Th: /*fronte di discesa trovato, cerco la soglia minima*/
RiseFallTimeSts = RiseFallTimeSts_FallLow_Th;
VREFCON2 = T_Low_Th_DACR;
TMR2_Old = TMR2_Current;
break;
case RiseFallTimeSts_FallLow_Th: /*soglia minima trovata, cerco fronte di salita*/
RiseFallTimeSts = RiseFallTimeSts_RiseLow_Th;
T_Fall = (BYTE)(TMR2_Current - TMR2_Old);
//lo č gią VREFCON2 = T_Low_Th_DACR;
CM1CON0bits.C1POL = TRUE;
break;
case RiseFallTimeSts_RiseLow_Th: /*fronte di salita trovato, cerco soglia massima*/
RiseFallTimeSts = RiseFallTimeSts_RiseHigh_Th;
TMR2_Old = TMR2_Current;
VREFCON2 = T_High_Th_DACR;
break;
case RiseFallTimeSts_RiseHigh_Th: /*soglia massima trovata*/
RiseFallTimeSts = RiseFallTimeSts_End;
T_Rise =(BYTE)(TMR2_Current - TMR2_Old);
CM1CON0bits.CON = FALSE; /*Comparatore disabilitato*/
PIE2bits.C1IE = FALSE;
Lm_Time_DATA_REDY = Lm_DATA_REDY_Full;
break;
case RiseFallTimeSts_End: /*letture disponibili*/
break;
default:
RiseFallTimeSts = RiseFallTimeSts_Idle;
break;
}
temp = CM1CON0; /*clear latch*/
PIR2bits.C1IF = FALSE;
}