timer32bit_switch_pulse_measure.c - Uses 32-bit timer to measure pulse widthΒΆ

32-bit Timer example: Measures the pulse width of pushbutton switch using Timer2/3 in 32-bit mode with INT1 for edge detection. To configure this example to run with an external 8 MHz crystal for for a FCY=40MHz, define the C preprocessor macro: CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY and have an external crysal + 2 capacitors on the OSC1/OSC2 pins. Typical crystal accuracy for through hole is +/-20 pmm, so for a 100000 us pulse width measurement this is +/- 2 us.

#include "pic24_all.h"
#include <stdio.h>


typedef enum  {
  STATE_WAIT_FOR_FALL_EDGE = 0,
  STATE_WAIT_FOR_RISE_EDGE,
} INT1STATE;

INT1STATE e_isrINT1State = STATE_WAIT_FOR_FALL_EDGE;
volatile uint8_t u8_captureFlag = 0;
volatile union32 u32_lastCapture; //union32 declared in stdint.h
volatile union32 u32_thisCapture;
volatile int32_t u32_delta;
volatile int32_t u32_pulseWidth;

//Interrupt Service Routine for INT1
void _ISRFAST _INT1Interrupt (void) {
  _INT1IF = 0;    //clear the interrupt bit
  switch (e_isrINT1State) {
    case STATE_WAIT_FOR_FALL_EDGE:
      if (u8_captureFlag == 0) {
        u32_lastCapture.u16.ls16 = TMR2;
        u32_lastCapture.u16.ms16 = TMR3HLD;
        _INT1EP = 0;  //configure for rising edge
        e_isrINT1State = STATE_WAIT_FOR_RISE_EDGE;
      }
      break;
    case STATE_WAIT_FOR_RISE_EDGE:
      u32_thisCapture.u16.ls16 = TMR2;
      u32_thisCapture.u16.ms16 = TMR3HLD;
      u32_delta = u32_thisCapture.u32 - u32_lastCapture.u32;
      u32_pulseWidth = ticksToUs(u32_delta, getTimerPrescale(T2CONbits));
      u8_captureFlag = 1;
      _INT1EP = 1;     //configure for falling edge
      e_isrINT1State = STATE_WAIT_FOR_FALL_EDGE;
      break;
    default:
      e_isrINT1State= STATE_WAIT_FOR_FALL_EDGE;
  }
}

/// Switch1 configuration, use RB13
inline void CONFIG_SW1()  {
  CONFIG_RB13_AS_DIG_INPUT();   //use RB13 for switch input
  ENABLE_RB13_PULLUP();         //enable the pullup
  CONFIG_INT1_TO_RP(RB13_RP);   //map INT1 to RB13
  DELAY_US(1);             //Wait for pullup
  /** Configure INT1 interrupt  */
  _INT1IF = 0;     //Clear the interrupt flag
  _INT1IP = 1;     //Choose a priority
  _INT1EP = 1;     //negative edge triggerred
  _INT1IE = 1;     //enable INT1 interrupt
}

//Timer2/3 used as single 32-bit timer, control word of Timer2 controls timer,
//interrupt status of Timer3 used for the combined timer
void  configTimer23(void) {
  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
          | T2_32BIT_MODE_ON
          | T2_SOURCE_INT
          | T2_PS_1_1 ;
  PR2 = 0xFFFF;                    //maximum period
  PR3 = 0xFFFF;                    //maximum period
  TMR3HLD = 0;                     //write MSW first
  TMR2  = 0;                       //then LSW
  _T3IF = 0;                       //clear interrupt flag
  T2CONbits.TON = 1;               //turn on the timer
}


int main (void) {
  configBasic(HELLO_MSG);
  CONFIG_SW1();    //use RB13
  configTimer23();
  while (1) {
    outString("Press button...");
    while (!u8_captureFlag) doHeartbeat();
    printf(" %ld us\n",u32_pulseWidth);
    u8_captureFlag = 0;
  }
}