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