PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
softfilt_test.c
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 
32 /**
33 Implements a software filter for input pulses less than a specified duration.
34 
35 */
36 
37 #define CONFIG_TOUT() CONFIG_RB9_AS_DIG_OUTPUT()
38 #define TOUT _LATB9 //output state
39 
40 #define TIN_RAW _RB8 //raw test in
41 #define CONFIG_TIN() CONFIG_RB8_AS_DIG_INPUT();
42 
43 
44 #define ISR_PERIOD 1 // in ms
45 #define MIN_STABLE 15 // in ms
46 #define MIN_STABLECOUNT MIN_STABLE/ISR_PERIOD
47 
48 
49 uint16_t u16_stableCountTIN = 0;
50 uint8_t u8_rawTIN = 0;
51 uint8_t u8_oldrawTIN = 0;
52 
53 
54 //debounced switch value that is set in the timer ISR
55 //any variable written by an ISR, and accessed outside of the ISR
56 //should be declared volatile
57 volatile uint8_t u8_valueTIN = 0;
58 
59 //Interrupt Service Routine for Timer3
60 void _ISRFAST _T3Interrupt (void) {
61  u8_rawTIN = TIN_RAW; //sample the switch
62  if (u8_rawTIN != u8_oldrawTIN) {
63  //changed values, zero the stability counter
64  u16_stableCountTIN = 0;
65  u8_oldrawTIN = u8_rawTIN;
66  } else {
67  u16_stableCountTIN++;
68  if (u16_stableCountTIN >= MIN_STABLECOUNT) {
69  //new value is ready!
70  u8_valueTIN = u8_rawTIN;
71  }
72  }
73  _T3IF = 0; //clear the timer interrupt bit
74 }
75 
76 
77 
78 void configTimer3(void) {
79  //ensure that Timer2,3 configured as separate timers.
80  T2CONbits.T32 = 0; // 32-bit mode off
81  //T3CON set like this for documentation purposes.
82  T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF
83  | T3_SOURCE_INT
84  | T3_PS_1_1 ;
85  PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
86  TMR3 = 0; //clear timer3 value
87  _T3IF = 0; //clear interrupt flag
88  _T3IP = 1; //choose a priority
89  _T3IE = 1; //enable the interrupt
90  T3CONbits.TON = 1; //turn on the timer
91 }
92 
93 
94 uint8_t u8_oldvalueTIN = 0;
95 
96 #define TPW 20 // in ms, pulsewidth of TOUT
97 
98 int main (void) {
99  configBasic(HELLO_MSG);
100  TOUT = 0;
101  // TOUT drives TIN
102  CONFIG_TIN();
103  CONFIG_TOUT();
104  configTimer3();
105  while (1) {
106  TOUT = !TOUT;
107  DELAY_MS(TPW);
108  if (u8_valueTIN != u8_oldvalueTIN) {
109  u8_oldvalueTIN = u8_valueTIN;
110  outString("*");
111  }
112  }
113 }