PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ledsw1_timer2.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 Demonstrates the use of a period interrupt to sample a switch
34 input, removes the need for debounce delays.
35 All of the FSM work is done in the ISR.
36 */
37 
38 /// LED1
39 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
40 #define LED1 _LATB14 //led1 state
41 
42 /// Switch1 configuration
43 inline void CONFIG_SW1() {
44  CONFIG_RB13_AS_DIG_INPUT(); //use RB13 for switch input
45  ENABLE_RB13_PULLUP(); //enable the pullup
46 }
47 
48 #define SW1_RAW _RB13 //raw switch value
49 #define SW1 u8_valueSW1 //switch state
50 #define SW1_PRESSED() (SW1==0) //switch test
51 #define SW1_RELEASED() (SW1==1) //switch test
52 
53 /// Switch2 configuration, does not have to be debounced
54 inline void CONFIG_SW2() {
55  CONFIG_RB12_AS_DIG_INPUT(); //use RB12 for switch input
56  ENABLE_RB12_PULLUP(); //enable the pullup
57 }
58 
59 #define SW2 _RB12 //switch state
60 
61 
62 typedef enum {
63  STATE_RESET = 0,
64  STATE_WAIT_FOR_PRESS1,
65  STATE_WAIT_FOR_RELEASE1,
66  STATE_WAIT_FOR_PRESS2,
67  STATE_WAIT_FOR_RELEASE2,
68  STATE_BLINK,
69  STATE_WAIT_FOR_RELEASE3
70 } STATE;
71 
72 
73 volatile uint8_t u8_valueSW1 = 1; //initially high
74 volatile uint8_t doBlink = 0;
75 volatile STATE e_mystate;
76 
77 //Interrupt Service Routine for Timer3
78 void _ISR _T3Interrupt (void) {
79  u8_valueSW1 = SW1_RAW; //sample the switch
80  switch (e_mystate) {
81  case STATE_WAIT_FOR_PRESS1:
82  LED1 = 0; //turn off the LED
83  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE1;
84  break;
85  case STATE_WAIT_FOR_RELEASE1:
86  if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS2;
87  break;
88  case STATE_WAIT_FOR_PRESS2:
89  LED1 = 1; //turn on the LED
90  if (SW1_PRESSED()) e_mystate = STATE_WAIT_FOR_RELEASE2;
91  break;
92  case STATE_WAIT_FOR_RELEASE2:
93  if (SW1_RELEASED()) {
94  //decide where to go
95  if (SW2) e_mystate = STATE_BLINK;
96  else e_mystate = STATE_WAIT_FOR_PRESS1;
97  }
98  break;
99  case STATE_BLINK:
100  doBlink = 1;
101  if (SW1_PRESSED()) {
102  doBlink = 0;
103  e_mystate = STATE_WAIT_FOR_RELEASE3;
104  }
105  break;
106  case STATE_WAIT_FOR_RELEASE3:
107  LED1 = 1; //Freeze LED1 at 1
108  if (SW1_RELEASED()) e_mystate = STATE_WAIT_FOR_PRESS1;
109  break;
110  default:
111  e_mystate = STATE_WAIT_FOR_PRESS1;
112  }
113  _T3IF = 0; //clear the timer interrupt bit
114 }
115 
116 
117 
118 
119 //print debug message for state when it changes
120 void printNewState (STATE e_currentState) {
121  static STATE e_LastState = STATE_RESET;
122  if (e_LastState != e_currentState) {
123  switch (e_currentState) {
124  case STATE_WAIT_FOR_PRESS1:
125  outString("STATE_WAIT_FOR_PRESS1 - LED is off\n");
126  break;
127  case STATE_WAIT_FOR_RELEASE1:
128  outString("STATE_WAIT_FOR_RELEASE1\n");
129  break;
130  case STATE_WAIT_FOR_PRESS2:
131  outString("STATE_WAIT_FOR_PRESS2 - LED is on\n");
132  break;
133  case STATE_WAIT_FOR_RELEASE2:
134  outString("STATE_WAIT_FOR_RELEASE2 - SW2 on goes to blink else go to PRESS1\n");
135  break;
136  case STATE_BLINK:
137  outString("STATE_BLINK - LED blinks, waiting for SW1 press\n");
138  break;
139  case STATE_WAIT_FOR_RELEASE3:
140  outString("STATE_WAIT_FOR_RELEASE3 - LED is on\n");
141  break;
142  default:
143  break;
144  }
145  }
146  e_LastState = e_currentState; //remember last state
147 }
148 
149 
150 #define ISR_PERIOD 15 // in ms
151 void configTimer3(void) {
152  //ensure that Timer2,3 configured as separate timers.
153  T2CONbits.T32 = 0; // 32-bit mode off
154  //T3CON set like this for documentation purposes.
155  //could be replaced by T3CON = 0x0020
156  T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF
157  | T3_SOURCE_INT
158  | T3_PS_1_64 ; //results in T3CON= 0x0020
159  PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
160  TMR3 = 0; //clear timer3 value
161  _T3IF = 0; //clear interrupt flag
162  _T3IP = 1; //choose a priority
163  _T3IE = 1; //enable the interrupt
164  T3CONbits.TON = 1; //turn on the timer
165 }
166 
167 int main (void) {
168 
169  configBasic(HELLO_MSG);
170  /** PIO config ******/
171  CONFIG_SW1(); //configure switch
172  CONFIG_SW2(); //configure switch
173  CONFIG_LED1(); //config the LED
174  /** Configure the Timer */
175  configTimer3();
176  e_mystate = STATE_WAIT_FOR_PRESS1;
177  /* While loop just checks the doBlink semaphore */
178  while (1) {
179  printNewState(e_mystate); //debug message when state changes
180  if (doBlink) {
181  LED1 = !LED1;
182  DELAY_MS(100);
183  }
184  doHeartbeat(); //ensure that we are alive
185  } // end while (1)
186 }