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
00038
00039 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
00040 #define LED1 _LATB14 //led1 state
00041
00043 inline void CONFIG_SW1() {
00044 CONFIG_RB13_AS_DIG_INPUT();
00045 ENABLE_RB13_PULLUP();
00046 }
00047
00048 #define SW1_RAW _RB13 //raw switch value
00049 #define SW1 u8_valueSW1 //switch state
00050 #define SW1_PRESSED() SW1==0 //switch test
00051 #define SW1_RELEASED() SW1==1 //switch test
00052
00054 inline void CONFIG_SW2() {
00055 CONFIG_RB12_AS_DIG_INPUT();
00056 ENABLE_RB12_PULLUP();
00057 }
00058
00059 #define SW2 _RB12 //switch state
00060
00061
00062 typedef enum {
00063 STATE_RESET = 0,
00064 STATE_WAIT_FOR_PRESS1,
00065 STATE_WAIT_FOR_RELEASE1,
00066 STATE_WAIT_FOR_PRESS2,
00067 STATE_WAIT_FOR_RELEASE2,
00068 STATE_BLINK,
00069 STATE_WAIT_FOR_RELEASE3
00070 } STATE;
00071
00072
00073 volatile uint8 u8_valueSW1 = 1;
00074 volatile uint8 doBlink = 0;
00075 STATE e_mystate;
00076
00077
00078 void _ISR _T3Interrupt (void) {
00079 u8_valueSW1 = SW1_RAW;
00080 switch (e_mystate) {
00081 case STATE_WAIT_FOR_PRESS1:
00082 LED1 = 0;
00083 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE1;
00084 break;
00085 case STATE_WAIT_FOR_RELEASE1:
00086 if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS2;
00087 break;
00088 case STATE_WAIT_FOR_PRESS2:
00089 LED1 = 1;
00090 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE2;
00091 break;
00092 case STATE_WAIT_FOR_RELEASE2:
00093 if (SW1_RELEASED()) {
00094
00095 if (SW2) e_mystate = STATE_BLINK;
00096 else e_mystate = STATE_WAIT_FOR_PRESS1;
00097 }
00098 break;
00099 case STATE_BLINK:
00100 doBlink = 1;
00101 if (SW1_PRESSED()) {
00102 doBlink = 0;
00103 e_mystate = STATE_WAIT_FOR_RELEASE3;
00104 }
00105 break;
00106 case STATE_WAIT_FOR_RELEASE3:
00107 LED1 = 1;
00108 if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS1;
00109 break;
00110 default:
00111 e_mystate = STATE_WAIT_FOR_PRESS1;
00112 }
00113 _T3IF = 0;
00114 }
00115
00116
00117
00118
00119 STATE e_LastState = STATE_RESET;
00120
00121 void printNewState (STATE e_currentState) {
00122 if (e_LastState != e_currentState) {
00123 switch (e_currentState) {
00124 case STATE_WAIT_FOR_PRESS1:
00125 outString("STATE_WAIT_FOR_PRESS1 - LED is off\n");
00126 break;
00127 case STATE_WAIT_FOR_RELEASE1:
00128 outString("STATE_WAIT_FOR_RELEASE1\n");
00129 break;
00130 case STATE_WAIT_FOR_PRESS2:
00131 outString("STATE_WAIT_FOR_PRESS2 - LED is on\n");
00132 break;
00133 case STATE_WAIT_FOR_RELEASE2:
00134 outString("STATE_WAIT_FOR_RELEASE2 - SW2 on goes to blink else go to PRESS1\n");
00135 break;
00136 case STATE_BLINK:
00137 outString("STATE_BLINK - LED blinks, waiting for SW1 press\n");
00138 break;
00139 case STATE_WAIT_FOR_RELEASE3:
00140 outString("STATE_WAIT_FOR_RELEASE3 - LED is on\n");
00141 break;
00142 default:
00143 break;
00144 }
00145 }
00146 e_LastState = e_currentState;
00147 }
00148
00149
00150 #define ISR_PERIOD 15 // in ms
00151 void configTimer3(void) {
00152
00153 T2CONbits.T32 = 0;
00154
00155
00156 T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF
00157 | T3_SOURCE_INT
00158 | T3_PS_1_64 ;
00159 PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
00160 TMR3 = 0;
00161 _T3IF = 0;
00162 _T3IP = 1;
00163 _T3IE = 1;
00164 T3CONbits.TON = 1;
00165 }
00166
00167 int main (void) {
00168
00169 configBasic(HELLO_MSG);
00171 CONFIG_SW1();
00172 CONFIG_SW2();
00173 CONFIG_LED1();
00175 configTimer3();
00176 e_mystate = STATE_WAIT_FOR_PRESS1;
00177
00178 while (1) {
00179 printNewState(e_mystate);
00180 if (doBlink) {
00181 LED1 = !LED1;
00182 DELAY_MS(100);
00183 }
00184 doHeartbeat();
00185 }
00186 }