EDF - OnePIC MCU  v1.1.0
source/base/OnePIC_switch.c
Go to the documentation of this file.
00001 
00006 #include "TimeDelay.h"
00007 #include "OnePIC_switch.h"
00008 #include "OnePIC_int.h"
00009 
00010 #ifdef __PIC24FJ256GA106__
00011 //#include "Controller.h"
00012 #endif
00013 
00014 uint8_t _pbState; 
00015 uint8_t ON_ENTRY; 
00016 swState _swState; 
00024 uint8_t switchPressed(void) {
00025     uint8_t rVal = 0;
00026     bool sw1_state;
00027 
00028     sw1_state = SW1_LATCH; //save existing LED state
00029     SW1_LATCH = 0;        //stop driving the LED/SW1 pin
00030     SW1_TRIS = 1;
00031     Delay10us(1);
00032     if (SW1_PORT) { //read from port....if held down, the pin is connected to VDD
00033         Delay10us(1); //Debounce if switch is held down
00034         if(SW1_PORT) //still down?
00035             rVal = SW1_PORT;
00036     }
00037     SW1_TRIS = 0;   //configure as output
00038     SW1_LATCH = sw1_state;  //reload prior state
00039 
00040     return rVal;
00041 }
00042 
00048 void switchTasks(void) {
00049     switch (_swState) {
00050         case SW_PRESSED:
00051             if (ON_ENTRY) {
00052                 _pbState ^= 1;
00053                 ON_ENTRY = 0;
00054             }
00055             if (switchPressed() == 0) {
00056                 _swState = SW_RELEASED;
00057             }
00058             break;
00059         case SW_RELEASED:
00060             if (switchPressed() == 1) {
00061                 ON_ENTRY = 1;
00062                 _swState = SW_PRESSED;
00063             }
00064             break;
00065     }
00066 }
00067