00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031
00037
00038 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
00039 #define LED1 _LATB14 //led1 state
00040
00042 inline void CONFIG_SW1() {
00043 CONFIG_RB13_AS_DIG_INPUT();
00044 ENABLE_RB13_PULLUP();
00045 }
00046
00047 #define SW1_RAW _RB13 //raw switch value
00048 #define SW1 u8_valueSW1 //switch state
00049 #define SW1_PRESSED() SW1==0 //switch test
00050 #define SW1_RELEASED() SW1==1 //switch test
00051
00052
00053
00054 volatile uint8 u8_valueSW1 = 1;
00055
00056
00057 void _ISRFAST _T3Interrupt (void) {
00058 u8_valueSW1 = SW1_RAW;
00059 _T3IF = 0;
00060 }
00061
00062 typedef enum {
00063 STATE_RESET = 0,
00064 STATE_WAIT_FOR_PRESS,
00065 STATE_WAIT_FOR_RELEASE
00066 } STATE;
00067
00068 STATE e_LastState = STATE_RESET;
00069
00070 void printNewState (STATE e_currentState) {
00071 if (e_LastState != e_currentState) {
00072 switch (e_currentState) {
00073 case STATE_WAIT_FOR_PRESS:
00074 outString("STATE_WAIT_FOR_PRESS\n");
00075 break;
00076 case STATE_WAIT_FOR_RELEASE:
00077 outString("STATE_WAIT_FOR_RELEASE\n");
00078 break;
00079 default:
00080 break;
00081 }
00082 }
00083 e_LastState = e_currentState;
00084 }
00085
00086 #define ISR_PERIOD 15 // in ms
00087 void configTimer3(void) {
00088
00089 T2CONbits.T32 = 0;
00090
00091
00092 T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF
00093 | T3_SOURCE_INT
00094 | T3_PS_1_64 ;
00095 PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
00096 TMR3 = 0;
00097 _T3IF = 0;
00098 _T3IP = 1;
00099 _T3IE = 1;
00100 T3CONbits.TON = 1;
00101 }
00102
00103 int main (void) {
00104 STATE e_mystate;
00105
00106 configBasic(HELLO_MSG);
00108 CONFIG_SW1();
00109 CONFIG_LED1();
00110
00111 configTimer3();
00112
00113 e_mystate = STATE_WAIT_FOR_PRESS;
00114
00115
00116
00117
00118
00119 while (1) {
00120 printNewState(e_mystate);
00121 switch (e_mystate) {
00122 case STATE_WAIT_FOR_PRESS:
00123 if (SW1_PRESSED()) {
00124 e_mystate = STATE_WAIT_FOR_RELEASE;
00125 }
00126 break;
00127 case STATE_WAIT_FOR_RELEASE:
00128 if (SW1_RELEASED()) {
00129 LED1 = !LED1;
00130 e_mystate = STATE_WAIT_FOR_PRESS;
00131 }
00132 break;
00133 default:
00134 e_mystate = STATE_WAIT_FOR_PRESS;
00135 }
00136 doHeartbeat();
00137 }
00138
00139 }