PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
timer32bit_switch_pulse_measure.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33  * Measures the pulse width of pushbutton switch using Timer2/3 in 32-bit mode with INT1 for edge detection.
34  * For more accuracy, use an external crystal and define
35  * CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY in your MPLAB project.
36  * Typical crystal accuracy for through hole is +/-20 pmm, so for a 100000 us
37  * pulse width measurement this is +/- 2 us.
38 */
39 
40 typedef enum {
41  STATE_WAIT_FOR_FALL_EDGE = 0,
42  STATE_WAIT_FOR_RISE_EDGE,
43 } INT1STATE;
44 
45 INT1STATE e_isrINT1State = STATE_WAIT_FOR_FALL_EDGE;
46 volatile uint8_t u8_captureFlag = 0;
47 volatile union32 u32_lastCapture; //union32 declared in stdint.h
48 volatile union32 u32_thisCapture;
49 volatile int32_t u32_delta;
50 volatile int32_t u32_pulseWidth;
51 
52 //Interrupt Service Routine for INT1
53 void _ISRFAST _INT1Interrupt (void) {
54  _INT1IF = 0; //clear the interrupt bit
55  switch (e_isrINT1State) {
56  case STATE_WAIT_FOR_FALL_EDGE:
57  if (u8_captureFlag == 0) {
58  u32_lastCapture.u16.ls16 = TMR2;
59  u32_lastCapture.u16.ms16 = TMR3HLD;
60  _INT1EP = 0; //configure for rising edge
61  e_isrINT1State = STATE_WAIT_FOR_RISE_EDGE;
62  }
63  break;
64  case STATE_WAIT_FOR_RISE_EDGE:
65  u32_thisCapture.u16.ls16 = TMR2;
66  u32_thisCapture.u16.ms16 = TMR3HLD;
67  u32_delta = u32_thisCapture.u32 - u32_lastCapture.u32;
68  u32_pulseWidth = ticksToUs(u32_delta, getTimerPrescale(T2CONbits));
69  u8_captureFlag = 1;
70  _INT1EP = 1; //configure for falling edge
71  e_isrINT1State = STATE_WAIT_FOR_FALL_EDGE;
72  break;
73  default:
74  e_isrINT1State= STATE_WAIT_FOR_FALL_EDGE;
75  }
76 }
77 
78 /// Switch1 configuration, use RB13
79 inline void CONFIG_SW1() {
80  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
81  ENABLE_RB13_PULLUP(); //enable the pullup
82 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
83  CONFIG_INT1_TO_RP(45); //map INT1 to RP45/RB13
84 #else
85  CONFIG_INT1_TO_RP(13); //map INT1 to RP13/RB13
86 #endif
87  DELAY_US(1); //Wait for pullup
88  /** Configure INT1 interrupt */
89  _INT1IF = 0; //Clear the interrupt flag
90  _INT1IP = 1; //Choose a priority
91  _INT1EP = 1; //negative edge triggerred
92  _INT1IE = 1; //enable INT1 interrupt
93 }
94 
95 //Timer2/3 used as single 32-bit timer, control word of Timer2 controls timer,
96 //interrupt status of Timer3 used for the combined timer
97 void configTimer23(void) {
98  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
99  | T2_32BIT_MODE_ON
100  | T2_SOURCE_INT
101  | T2_PS_1_1 ;
102  PR2 = 0xFFFF; //maximum period
103  PR3 = 0xFFFF; //maximum period
104  TMR3HLD = 0; //write MSW first
105  TMR2 = 0; //then LSW
106  _T3IF = 0; //clear interrupt flag
107  T2CONbits.TON = 1; //turn on the timer
108 }
109 
110 
111 int main (void) {
112  configBasic(HELLO_MSG);
113  CONFIG_SW1(); //use RB13
114  configTimer23();
115  while (1) {
116  outString("Press button...");
117  while (!u8_captureFlag) doHeartbeat();
118  printf(" %ld us\n",u32_pulseWidth);
119  u8_captureFlag = 0;
120  }
121 }
122