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 00030 #include "pic24_all.h" 00031 00037 #define CONFIG_TOUT() CONFIG_RB9_AS_DIG_OUTPUT() 00038 #define TOUT _LATB9 //output state 00039 00040 #define TIN_RAW _RB8 //raw test in 00041 #define CONFIG_TIN() CONFIG_RB8_AS_DIG_INPUT(); 00042 00043 00044 #define ISR_PERIOD 1 // in ms 00045 #define MIN_STABLE 15 // in ms 00046 #define MIN_STABLECOUNT MIN_STABLE/ISR_PERIOD 00047 00048 00049 uint16 u16_stableCountTIN = 0; 00050 uint8 u8_rawTIN = 0; 00051 uint8 u8_oldrawTIN = 0; 00052 00053 00054 //debounced switch value that is set in the timer ISR 00055 //any variable written by an ISR, and accessed outside of the ISR 00056 //should be declared volatile 00057 volatile uint8 u8_valueTIN = 0; 00058 00059 //Interrupt Service Routine for Timer3 00060 void _ISRFAST _T3Interrupt (void) { 00061 u8_rawTIN = TIN_RAW; //sample the switch 00062 if (u8_rawTIN != u8_oldrawTIN) { 00063 //changed values, zero the stability counter 00064 u16_stableCountTIN = 0; 00065 u8_oldrawTIN = u8_rawTIN; 00066 } else { 00067 u16_stableCountTIN++; 00068 if (u16_stableCountTIN >= MIN_STABLECOUNT) { 00069 //new value is ready! 00070 u8_valueTIN = u8_rawTIN; 00071 } 00072 } 00073 _T3IF = 0; //clear the timer interrupt bit 00074 } 00075 00076 00077 00078 void configTimer3(void) { 00079 //ensure that Timer2,3 configured as separate timers. 00080 T2CONbits.T32 = 0; // 32-bit mode off 00081 //T3CON set like this for documentation purposes. 00082 T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF 00083 | T3_SOURCE_INT 00084 | T3_PS_1_1 ; 00085 PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1; 00086 TMR3 = 0; //clear timer3 value 00087 _T3IF = 0; //clear interrupt flag 00088 _T3IP = 1; //choose a priority 00089 _T3IE = 1; //enable the interrupt 00090 T3CONbits.TON = 1; //turn on the timer 00091 } 00092 00093 00094 uint8 u8_oldvalueTIN = 0; 00095 00096 #define TPW 20 // in ms, pulsewidth of TOUT 00097 00098 int main (void) { 00099 configBasic(HELLO_MSG); 00100 TOUT = 0; 00101 // TOUT drives TIN 00102 CONFIG_TIN(); 00103 CONFIG_TOUT(); 00104 configTimer3(); 00105 while (1) { 00106 TOUT = !TOUT; 00107 DELAY_MS(TPW); 00108 if (u8_valueTIN != u8_oldvalueTIN) { 00109 u8_oldvalueTIN = u8_valueTIN; 00110 outString("*"); 00111 } 00112 } 00113 }