PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ledtoggle.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 
32 /** \file
33 A program that uses a finite state machine approach for
34 toggling an LED whenever a pushbutton switch is pressed
35 and released. Demonstrates the use of debounce delays when
36 polling a switch input.
37 */
38 
39 /// LED1
40 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
41 #define LED1 _LATB14 //led1 state
42 
43 /// Switch1 configuration
44 inline void CONFIG_SW1() {
45  CONFIG_RB13_AS_DIG_INPUT(); //use RB10 for switch input
46  ENABLE_RB13_PULLUP(); //enable the pullup
47 }
48 
49 
50 #define SW1 _RB13 //switch state
51 #define SW1_PRESSED() (SW1==0) //switch test
52 #define SW1_RELEASED() (SW1==1) //switch test
53 
54 typedef enum {
55  STATE_RESET = 0,
56  STATE_WAIT_FOR_PRESS,
57  STATE_WAIT_FOR_RELEASE
58 } STATE;
59 
60 STATE e_lastState = STATE_RESET;
61 //print debug message for state when it changes
62 void printNewState (STATE e_currentState) {
63  if (e_lastState != e_currentState) {
64  switch (e_currentState) {
65  case STATE_WAIT_FOR_PRESS:
66  outString("STATE_WAIT_FOR_PRESS\n");
67  break;
68  case STATE_WAIT_FOR_RELEASE:
69  outString("STATE_WAIT_FOR_RELEASE\n");
70  break;
71  default:
72  outString("Unexpected state\n");
73  break;
74  }
75  }
76  e_lastState = e_currentState; //remember last state
77 }
78 
79 int main (void) {
80  STATE e_mystate;
81 
82  configBasic(HELLO_MSG); // Set up heartbeat, UART, print hello message and diags
83  /** GPIO config ***************************/
84  CONFIG_SW1(); //configure switch
85  CONFIG_LED1(); //config the LED
86  DELAY_US(1); //give pullups a little time
87  /*****Toggle LED each time switch is pressed and released ******************************/
88  e_mystate = STATE_WAIT_FOR_PRESS;
89 
90  while (1) {
91  printNewState(e_mystate); //debug message when state changes
92  switch (e_mystate) {
93  case STATE_WAIT_FOR_PRESS:
94  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE;
95  break;
96  case STATE_WAIT_FOR_RELEASE:
97  if (SW1_RELEASED()) {
98  LED1 = !LED1; //toggle LED
99  e_mystate = STATE_WAIT_FOR_PRESS;
100  }
101  break;
102  default:
103  e_mystate = STATE_WAIT_FOR_PRESS;
104  }//end switch(e_mystate)
105  DELAY_MS(DEBOUNCE_DLY); //Debounce
106  doHeartbeat(); //ensure that we are alive
107  } // end while (1)
108 }