PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
incap_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 switching using input capture and Timer2
34  * For more accuracy, use an external crystal and define
35  * CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY in the MPLAB project.
36  * Remove this macro if you wish to use the internal oscillator.
37  * Typical crystal accuracy for through hole is +/-20 pmm, so for a 100000 us
38  * pulse width measurement this is +/- 2 us.
39  *
40  * This code works with PIC24E/dsPIC33 but a better way of measuring long capture periods
41  * with this family would be to use cascaded input captures to form
42  * a 32-bit input capture register.
43 */
44 
45 volatile uint16_t u16_oflowCount = 0;
46 
47 //Interrupt Service Routine for Timer2
48 void _ISRFAST _T2Interrupt (void) {
49  u16_oflowCount++;
50  _T2IF = 0; //clear the timer interrupt bit
51 }
52 
53 typedef enum {
54  STATE_WAIT_FOR_FALL_EDGE = 0,
55  STATE_WAIT_FOR_RISE_EDGE,
56 } ICSTATE;
57 
58 ICSTATE e_isrICState = STATE_WAIT_FOR_FALL_EDGE;
59 volatile uint8_t u8_captureFlag = 0;
60 volatile uint16_t u16_lastCapture;
61 volatile uint16_t u16_thisCapture;
62 volatile uint32_t u32_pulseWidth;
63 
64 void _ISRFAST _IC1Interrupt() {
65  _IC1IF = 0;
66  u16_thisCapture = IC1BUF; //always read the buffer to prevent overflow
67  switch (e_isrICState) {
68  case STATE_WAIT_FOR_FALL_EDGE:
69  if (u8_captureFlag == 0) {
70  if (u16_thisCapture == 0 && _T2IF) u16_oflowCount = 0 -1; //simultaneous timer with capture
71  else u16_oflowCount = 0;
72  u16_lastCapture = u16_thisCapture;
73  e_isrICState = STATE_WAIT_FOR_RISE_EDGE;
74  }
75  break;
76  case STATE_WAIT_FOR_RISE_EDGE:
77  if (u16_thisCapture == 0 && _T2IF) u16_oflowCount++; //simultaneous interrupt, increment oflow count
78  u32_pulseWidth = computeDeltaTicksLong(u16_lastCapture,u16_thisCapture,PR2, u16_oflowCount); //get delta ticks
79  u32_pulseWidth = ticksToUs(u32_pulseWidth,getTimerPrescale(T2CONbits)); //convert to microseconds
80  u8_captureFlag = 1;
81  e_isrICState = STATE_WAIT_FOR_FALL_EDGE;
82  break;
83  default:
84  e_isrICState = STATE_WAIT_FOR_FALL_EDGE;
85  }
86 }
87 
88 /// Switch1 configuration
89 inline void CONFIG_SW1() {
90  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
91  ENABLE_RB13_PULLUP(); //enable the pull-up
92  DELAY_US(1); //delay for pull-up
93 }
94 
95 void configInputCapture1(void) {
96 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
97  CONFIG_IC1_TO_RP(45); //map IC1 to RP45/RB13
98  IC1CON1 = IC_TIMER2_SRC | //Timer2 source
99  IC_INT_1CAPTURE | //Interrupt every capture
100  IC_EVERY_EDGE; //Capture every edge
101  IC1CON2 = 0x000C; //sync to timer2
102 #else
103  CONFIG_IC1_TO_RP(13); //map IC1 to RP13/RB13
104  IC1CON = IC_TIMER2_SRC | //Timer2 source
105  IC_INT_1CAPTURE | //Interrupt every capture
106  IC_EVERY_EDGE; //Capture every edge
107 #endif
108  _IC1IF = 0;
109  _IC1IP = 2; //higher than Timer2 so that Timer2 does not interrupt IC1
110  _IC1IE = 1; //enable
111 
112 }
113 
114 void configTimer2(void) {
115  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
116  | T2_32BIT_MODE_OFF
117  | T2_SOURCE_INT
118  | T2_PS_1_8 ; //1 tick = 0.2 us at FCY=40 MHz
119  PR2 = 0xFFFF; //maximum period
120  TMR2 = 0; //clear timer2 value
121  _T2IF = 0; //clear interrupt flag
122  _T2IP = 1; //choose a priority
123  _T2IE = 1; //enable the interrupt
124  T2CONbits.TON = 1; //turn on the timer
125 }
126 
127 int main (void) {
128  configBasic(HELLO_MSG);
129  CONFIG_SW1(); //use RB13
130  configInputCapture1();
131  configTimer2();
132 
133 
134  while (1) {
135  outString("Press button...");
136  while (!u8_captureFlag) doHeartbeat();
137  printf(" %ld us\n",u32_pulseWidth);
138  u8_captureFlag = 0;
139  }
140 }