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 #define SW1 _RB13 //switch state
00047 #define SW1_PRESSED() SW1==0 //switch test
00048 #define SW1_RELEASED() SW1==1 //switch test
00049
00051 inline void CONFIG_SW2() {
00052 CONFIG_RB12_AS_DIG_INPUT();
00053 ENABLE_RB12_PULLUP();
00054 }
00055
00056 #define SW2 _RB12 //switch state
00057
00058
00059 typedef enum {
00060 STATE_RESET = 0,
00061 STATE_WAIT_FOR_PRESS1,
00062 STATE_WAIT_FOR_RELEASE1,
00063 STATE_WAIT_FOR_PRESS2,
00064 STATE_WAIT_FOR_RELEASE2,
00065 STATE_BLINK,
00066 STATE_WAIT_FOR_RELEASE3
00067 } STATE;
00068
00069 STATE e_LastState = STATE_RESET;
00070
00071 void printNewState (STATE e_currentState) {
00072 if (e_LastState != e_currentState) {
00073 switch (e_currentState) {
00074 case STATE_WAIT_FOR_PRESS1:
00075 outString("STATE_WAIT_FOR_PRESS1 - LED is off\n");
00076 break;
00077 case STATE_WAIT_FOR_RELEASE1:
00078 outString("STATE_WAIT_FOR_RELEASE1\n");
00079 break;
00080 case STATE_WAIT_FOR_PRESS2:
00081 outString("STATE_WAIT_FOR_PRESS2 - LED is on\n");
00082 break;
00083 case STATE_WAIT_FOR_RELEASE2:
00084 outString("STATE_WAIT_FOR_RELEASE2 - SW2 on goes to blink else go to PRESS1\n");
00085 break;
00086 case STATE_BLINK:
00087 outString("STATE_BLINK - LED blinks, waiting for SW1 press\n");
00088 break;
00089 case STATE_WAIT_FOR_RELEASE3:
00090 outString("STATE_WAIT_FOR_RELEASE3 - LED is on\n");
00091 break;
00092
00093 default:
00094 break;
00095 }
00096 }
00097 e_LastState = e_currentState;
00098 }
00099
00100 int main (void) {
00101 STATE e_mystate;
00102
00103 configBasic(HELLO_MSG);
00105 CONFIG_SW1();
00106 CONFIG_SW2();
00107 CONFIG_LED1();
00108 DELAY_US(1);
00109
00110 e_mystate = STATE_WAIT_FOR_PRESS1;
00111
00112 while (1) {
00113 printNewState(e_mystate);
00114 switch (e_mystate) {
00115 case STATE_WAIT_FOR_PRESS1:
00116 LED1 = 0;
00117 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE1;
00118 break;
00119 case STATE_WAIT_FOR_RELEASE1:
00120 if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS2;
00121 break;
00122 case STATE_WAIT_FOR_PRESS2:
00123 LED1 = 1;
00124 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE2;
00125 break;
00126 case STATE_WAIT_FOR_RELEASE2:
00127 if (SW1_RELEASED()) {
00128
00129 if (SW2) e_mystate = STATE_BLINK;
00130 else e_mystate = STATE_WAIT_FOR_PRESS1;
00131 }
00132 break;
00133 case STATE_BLINK:
00134 LED1 = !LED1;
00135 DELAY_MS(100);
00136 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE3;
00137 break;
00138 case STATE_WAIT_FOR_RELEASE3:
00139 LED1 = 1;
00140 if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS1;
00141 break;
00142 default:
00143 e_mystate = STATE_WAIT_FOR_PRESS1;
00144 }
00145 DELAY_MS(DEBOUNCE_DLY);
00146 doHeartbeat();
00147 }
00148 }