PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
manual_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 a pushbutton switch using Timer2 without input capture.
34  * CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY is defined in the MPLAB project.
35  * Remove this macro if you wish to use the internal oscillator.
36 */
37 
38 
39 void configTimer2(void) {
40  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
41  | T2_32BIT_MODE_OFF
42  | T2_SOURCE_INT
43  | T2_PS_1_256 ; //at 40 MHz, approx 420 ms period, 1 tick = 6.4 us
44  PR2 = 0xFFFF; //maximum period
45  TMR2 = 0; //clear timer2 value
46  _T2IF = 0; //clear interrupt flag
47  T2CONbits.TON = 1; //turn on the timer
48 }
49 
50 /// Switch1 configuration
51 inline void CONFIG_SW1() {
52  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
53  ENABLE_RB13_PULLUP(); //enable the pullup
54 }
55 #define SW1 _RB13 //switch state
56 #define SW1_PRESSED() SW1==0 //switch test
57 #define SW1_RELEASED() SW1==1 //switch test
58 
59 
60 int main (void) {
61  uint16_t u16_start, u16_delta;
62  uint32_t u32_pulseWidth;
63  configBasic(HELLO_MSG);
64  CONFIG_SW1(); //use RB13
65  configTimer2();
66  while (1) {
67  outString("Press button...");
68  while (SW1_RELEASED())doHeartbeat();
69  u16_start = TMR2;
70  while (SW1_PRESSED())doHeartbeat();
71  u16_delta = TMR2 - u16_start; //works because using maximum PR2 value
72  u32_pulseWidth = ticksToUs((uint32_t) u16_delta,getTimerPrescale(T2CONbits));
73  printf(" %ld us\n",u32_pulseWidth);
74  }
75 }