00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 #include "pic24_all.h" 00030 #include <stdio.h> 00031 00039 void configTimer2(void) { 00040 T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF 00041 | T2_32BIT_MODE_OFF 00042 | T2_SOURCE_INT 00043 | T2_PS_1_256 ; //at 40 MHz, approx 420 ms period, 1 tick = 6.4 us 00044 PR2 = 0xFFFF; //maximum period 00045 TMR2 = 0; //clear timer2 value 00046 _T2IF = 0; //clear interrupt flag 00047 T2CONbits.TON = 1; //turn on the timer 00048 } 00049 00051 inline void CONFIG_SW1() { 00052 CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input 00053 ENABLE_RB13_PULLUP(); //enable the pullup 00054 } 00055 #define SW1 _RB13 //switch state 00056 #define SW1_PRESSED() SW1==0 //switch test 00057 #define SW1_RELEASED() SW1==1 //switch test 00058 00059 00060 int main (void) { 00061 uint16 u16_start, u16_delta; 00062 uint32 u32_pulseWidth; 00063 configBasic(HELLO_MSG); 00064 CONFIG_SW1(); //use RB13 00065 configTimer2(); 00066 while (1) { 00067 outString("Press button..."); 00068 while (SW1_RELEASED())doHeartbeat(); 00069 u16_start = TMR2; 00070 while (SW1_PRESSED())doHeartbeat(); 00071 u16_delta = TMR2 - u16_start; //works because using maximum PR2 value 00072 u32_pulseWidth = ticksToUs((uint32) u16_delta,getTimerPrescale(T2CONbits)); 00073 printf(" %ld us\n",u32_pulseWidth); 00074 } 00075 }