/
main.c
104 lines (95 loc) · 3.19 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* File: main.c
* Author: gvanhoy
* Student: Toan Chu
* Description: lab 0.
* Created on August 27, 2015, 10:14 AM
*/
#include <xc.h>
#include <sys/attribs.h>
#include "switch.h"
#include "timer.h"
#include "led.h"
#include "interrupt.h"
#include "config.h"
#define OUTPUT 0
#define INPUT 1
//TODO: Define states of the state machine
typedef enum stateTypeEnum{
init, waitPush, waitRelease, prevLED, nextLED
} stateType;
stateType state = init;
int main() {
//This function is necessary to use interrupts.
enableInterrupts();
//TODO: Write each initialization function
initSwitch1();
initLEDs();
//initTimer2();
initTimer1();
int led;
int i;
while(1){
switch(state)
{
//init case
case init:
led = 0;
turnOnLED(0);//turn the top LED on
state = waitPush;//move on to the next state
break;
//waitPush state
case waitPush:
if(PORTDbits.RD6 == 0)//check if the switch is pushed
{
//for(i=0;i<10000;i++);
TMR1CLR = 0xFFFF;; // Clear the timer interrupt flag
state = waitRelease;
T1CONbits.TON = 1;//enable timer
IFS0bits.T1IF = 0;//put the flag down
}
else state = waitPush;//stay in waitPush if the switch is not pushed
break;
//waitRelease state
case waitRelease:
if(PORTDbits.RD6 == 1)//check if the switch is pushed
{
if(IFS0bits.T1IF == 1)//check if the flag is on
{
state = prevLED;//move to the previous state if the flag is on when hold more than 2 seconds
T1CONbits.TON = 0;//clear the timer when not in use
}
else{
state = nextLED;//move to the next state
T1CONbits.TON = 0;//clear the timer when not in use
}
}
else{
state = waitRelease;//move to waitRelease
}
break;
//go back to previous LED state
case prevLED:
if(led == 0)led=2;
else if(led == 1)led=0;
else if(led == 2)led=1;
turnOnLED(led);
IFS0bits.T1IF = 0;
state = waitPush;
T1CONbits.TON = 0;
break;
//go to next LED state
case nextLED:
if(led==0) led=1;
else if(led == 1)led=2;
else if (led == 2) led=0;
turnOnLED(led);
IFS0bits.T1IF = 0;
state = waitPush;
T1CONbits.TON = 0;
break;
default: break;
}
}
return 0;
}