PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
incap_freqmeasure.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 
30 #include "pic24_all.h"
31 #include <stdio.h>
32 
33 /** \file
34  * Measures the square wave frequency using input capture and Timer2
35  * This projects uses an external crystal for accuracy.
36  * CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY is defined in the MPLAB project.
37  * Remove this macro if you wish to use the internal oscillator.
38 */
39 
40 uint8_t getPeriodAdjust (uint8_t ICMbits) {
41  if (ICMbits == IC_EVERY_16_RISE_EDGE) return 16;
42  else if (ICMbits == IC_EVERY_4_RISE_EDGE) return 4;
43  else return 1;
44 }
45 
46 volatile uint8_t u8_captureFlag = 0;
47 volatile uint16_t u16_lastCapture;
48 volatile uint16_t u16_thisCapture;
49 volatile uint32_t u32_period;
50 
51 void _ISRFAST _IC1Interrupt() {
52  _IC1IF = 0;
53  u16_thisCapture = IC1BUF; //always read the buffer to prevent overflow
54  if (u8_captureFlag == 0) {
55  u32_period = (uint32_t) computeDeltaTicks(u16_lastCapture,u16_thisCapture,PR2);
56  u32_period = ticksToNs (u32_period, getTimerPrescale(T2CONbits));
57  //adjust period if necessary
58 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
59  u32_period = u32_period/getPeriodAdjust(IC1CON1bits.ICM);
60 #else
61  u32_period = u32_period/getPeriodAdjust(IC1CONbits.ICM);
62 #endif
63  u8_captureFlag = 1;
64  }
65  u16_lastCapture = u16_thisCapture;
66 }
67 
68 void configInputCapture1(void) {
69  CONFIG_RB13_AS_DIG_INPUT();
70 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
71  CONFIG_IC1_TO_RP(45); //map IC1 to RP45/RB13
72  IC1CON1 = IC_TIMER2_SRC | //Timer2 source
73  IC_INT_1CAPTURE | //Interrupt every capture
74  IC_EVERY_EDGE; //Capture every edge
75  IC1CON2 = 0x000C; //sync to timer2
76 #else
77  CONFIG_IC1_TO_RP(13); //map IC1 to RP13/RB13
78  IC1CON = IC_TIMER2_SRC | //Timer2 source
79  IC_INT_1CAPTURE | //Interrupt every capture
80  IC_EVERY_16_RISE_EDGE; //capture every 16th edge
81 #endif
82  _IC1IF = 0;
83  _IC1IP = 1; //pick a priority
84  _IC1IE = 1; //enable
85 }
86 
87 void configTimer2(void) {
88  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
89  | T2_32BIT_MODE_OFF
90  | T2_SOURCE_INT
91  | T2_PS_1_64 ; //1 tick = 1.6 us at FCY=40 MHz
92  PR2 = 0xFFFF; //maximum period
93  TMR2 = 0; //clear timer2 value
94  _T2IF = 0; //clear interrupt flag
95  T2CONbits.TON = 1; //turn on the timer
96 }
97 
98 int main (void) {
99  uint32_t u32_maxPeriodNs;
100  configBasic(HELLO_MSG);
101  configTimer2();
102  configInputCapture1();
103 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
104  u32_maxPeriodNs = ticksToNs (65536, getTimerPrescale(T2CONbits))/getPeriodAdjust(IC1CON1bits.ICM);
105 #else
106  u32_maxPeriodNs = ticksToNs (65536, getTimerPrescale(T2CONbits))/getPeriodAdjust(IC1CONbits.ICM);
107 #endif
108  printf("Maximum period is %ld ns\n",u32_maxPeriodNs);
109  while (1) {
110  outString("Press button...");
111  while (!u8_captureFlag) doHeartbeat();
112  printf(" %ld ns\n",u32_period);
113  u8_captureFlag = 0;
114  }
115 }