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
00039
00040 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
00041 #define LED1 _LATB14 //led1 state
00042
00044 inline void CONFIG_SW1() {
00045 CONFIG_RB13_AS_DIG_INPUT();
00046 ENABLE_RB13_PULLUP();
00047 }
00048
00049
00050 #define SW1 _RB13 //switch state
00051 #define SW1_PRESSED() SW1==0 //switch test
00052 #define SW1_RELEASED() SW1==1 //switch test
00053
00054 typedef enum {
00055 STATE_RESET = 0,
00056 STATE_WAIT_FOR_PRESS,
00057 STATE_WAIT_FOR_RELEASE
00058 } STATE;
00059
00060 STATE e_lastState = STATE_RESET;
00061
00062 void printNewState (STATE e_currentState) {
00063 if (e_lastState != e_currentState) {
00064 switch (e_currentState) {
00065 case STATE_WAIT_FOR_PRESS:
00066 outString("STATE_WAIT_FOR_PRESS\n");
00067 break;
00068 case STATE_WAIT_FOR_RELEASE:
00069 outString("STATE_WAIT_FOR_RELEASE\n");
00070 break;
00071 default:
00072 outString("Unexpected state\n");
00073 break;
00074 }
00075 }
00076 e_lastState = e_currentState;
00077 }
00078
00079 int main (void) {
00080 STATE e_mystate;
00081
00082 configBasic(HELLO_MSG);
00084 CONFIG_SW1();
00085 CONFIG_LED1();
00086 DELAY_US(1);
00087
00088 e_mystate = STATE_WAIT_FOR_PRESS;
00089
00090 while (1) {
00091 printNewState(e_mystate);
00092 switch (e_mystate) {
00093 case STATE_WAIT_FOR_PRESS:
00094 if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE;
00095 break;
00096 case STATE_WAIT_FOR_RELEASE:
00097 if (SW1_RELEASED()) {
00098 LED1 = !LED1;
00099 e_mystate = STATE_WAIT_FOR_PRESS;
00100 }
00101 break;
00102 default:
00103 e_mystate = STATE_WAIT_FOR_PRESS;
00104 }
00105 DELAY_MS(DEBOUNCE_DLY);
00106 doHeartbeat();
00107 }
00108 }