PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ledsw1.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 implementing switch/LED input/output.
35 */
36 
37 /// LED1
38 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
39 #define LED1 _LATB14 //led1 state
40 
41 /// Switch1 configuration
42 inline void CONFIG_SW1() {
43  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
44  ENABLE_RB13_PULLUP(); //enable the pullup
45 }
46 #define SW1 _RB13 //switch state
47 #define SW1_PRESSED() (SW1==0) //switch test
48 #define SW1_RELEASED() (SW1==1) //switch test
49 
50 /// Switch2 configuration
51 inline void CONFIG_SW2() {
52  CONFIG_RB12_AS_DIG_INPUT(); //use RB12 for switch input
53  ENABLE_RB12_PULLUP(); //enable the pullup
54 }
55 
56 #define SW2 _RB12 //switch state
57 
58 
59 typedef enum {
60  STATE_RESET = 0,
61  STATE_WAIT_FOR_PRESS1,
62  STATE_WAIT_FOR_RELEASE1,
63  STATE_WAIT_FOR_PRESS2,
64  STATE_WAIT_FOR_RELEASE2,
65  STATE_BLINK,
66  STATE_WAIT_FOR_RELEASE3
67 } STATE;
68 
69 STATE e_LastState = STATE_RESET;
70 //print debug message for state when it changes
71 void printNewState (STATE e_currentState) {
72  if (e_LastState != e_currentState) {
73  switch (e_currentState) {
74  case STATE_WAIT_FOR_PRESS1:
75  outString("STATE_WAIT_FOR_PRESS1 - LED is off\n");
76  break;
77  case STATE_WAIT_FOR_RELEASE1:
78  outString("STATE_WAIT_FOR_RELEASE1\n");
79  break;
80  case STATE_WAIT_FOR_PRESS2:
81  outString("STATE_WAIT_FOR_PRESS2 - LED is on\n");
82  break;
83  case STATE_WAIT_FOR_RELEASE2:
84  outString("STATE_WAIT_FOR_RELEASE2 - SW2 on goes to blink else go to PRESS1\n");
85  break;
86  case STATE_BLINK:
87  outString("STATE_BLINK - LED blinks, waiting for SW1 press\n");
88  break;
89  case STATE_WAIT_FOR_RELEASE3:
90  outString("STATE_WAIT_FOR_RELEASE3 - LED is on\n");
91  break;
92 
93  default:
94  break;
95  }
96  }
97  e_LastState = e_currentState; //remember last state
98 }
99 
100 int main (void) {
101  STATE e_mystate;
102 
103  configBasic(HELLO_MSG); // Set up heartbeat, UART, print hello message and diags
104 
105  /** GPIO config ***************************/
106  CONFIG_SW1(); //configure switch
107  CONFIG_SW2(); //configure switch
108  CONFIG_LED1(); //config the LED
109  DELAY_US(1); //give pullups a little time
110  /*****Toggle LED each time switch is pressed and released ******************************/
111  e_mystate = STATE_WAIT_FOR_PRESS1;
112 
113  while (1) {
114  printNewState(e_mystate); //debug message when state changes
115  switch (e_mystate) {
116  case STATE_WAIT_FOR_PRESS1:
117  LED1 = 0; //turn off the LED
118  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE1;
119  break;
120  case STATE_WAIT_FOR_RELEASE1:
121  if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS2;
122  break;
123  case STATE_WAIT_FOR_PRESS2:
124  LED1 = 1; //turn on the LED
125  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE2;
126  break;
127  case STATE_WAIT_FOR_RELEASE2:
128  if (SW1_RELEASED()) {
129  //decide where to go
130  if (SW2) e_mystate = STATE_BLINK;
131  else e_mystate = STATE_WAIT_FOR_PRESS1;
132  }
133  break;
134  case STATE_BLINK:
135  LED1 = !LED1; //blink while not pressed
136  DELAY_MS(100); //blink delay
137  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE3;
138  break;
139  case STATE_WAIT_FOR_RELEASE3:
140  LED1 = 1; //Freeze LED1 at 1
141  if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS1;
142  break;
143  default:
144  e_mystate = STATE_WAIT_FOR_PRESS1;
145  }//end switch(e_mystate)
146  DELAY_MS(DEBOUNCE_DLY); //Debounce
147  doHeartbeat(); //ensure that we are alive
148  } // end while (1)
149 }