-
Notifications
You must be signed in to change notification settings - Fork 0
/
VendingMachine.c
96 lines (86 loc) · 3.64 KB
/
VendingMachine.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
#include "PLL.h"
#include "SysTick.h"
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTE_DIR_R (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define COINS (*((volatile unsigned long *)0x4002400C))
#define SODA (*((volatile unsigned long *)0x40005004))
#define CHANGE (*((volatile unsigned long *)0x40005008))
#define T10ms 800000
#define T20ms 1600000
void FSM_Init(void){ volatile unsigned long delay;
PLL_Init(); // 80 MHz
SysTick_Init();
SYSCTL_RCGC2_R |= 0x12; // 1) B E
delay = SYSCTL_RCGC2_R; // 2) no need to unlock
GPIO_PORTE_AMSEL_R &= ~0x03; // 3) disable analog function on PE1-0
GPIO_PORTE_PCTL_R &= ~0x000000FF; // 4) enable regular GPIO
GPIO_PORTE_DIR_R &= ~0x03; // 5) inputs on PE1-0
GPIO_PORTE_AFSEL_R &= ~0x03; // 6) regular function on PE1-0
GPIO_PORTE_DEN_R |= 0x03; // 7) enable digital on PE1-0
GPIO_PORTB_AMSEL_R &= ~0x03; // 3) disable analog function on PB1-0
GPIO_PORTB_PCTL_R &= ~0x000000FF; // 4) enable regular GPIO
GPIO_PORTB_DIR_R |= 0x03; // 5) outputs on PB1-0
GPIO_PORTB_AFSEL_R &= ~0x03; // 6) regular function on PB1-0
GPIO_PORTB_DEN_R |= 0x03; // 7) enable digital on PB1-0
SODA = 0; CHANGE = 0;
}
unsigned long Coin_Input(void){
return COINS; // PE1,0 can be 0, 1, or 2
}
void Solenoid_None(void){
};
void Solenoid_Soda(void){
SODA = 0x01; // activate solenoid
SysTick_Wait(T10ms); // 10 msec, dispenses a delicious soda
SODA = 0x00; // deactivate
}
void Solenoid_Change(void){
CHANGE = 0x02; // activate solenoid
SysTick_Wait(T10ms); // 10 msec
CHANGE = 0x00; // deactivate
}
struct State {
void (*CmdPt)(void); // output function
unsigned long Time; // wait time, 12.5ns units
unsigned long Next[3];};
typedef const struct State StateType;
#define M0 0
#define W5 1
#define M5 2
#define W10 3
#define M10 4
#define W15 5
#define M15 6
#define W20 7
#define M20 8
StateType FSM[9]={
{&Solenoid_None, T20ms,{M0,W5,W10}}, // M0, no money
{&Solenoid_None, T20ms,{M5,W5,W5}}, // W5, seeing a nickel
{&Solenoid_None, T20ms,{M5,W10,W15}}, // M5, have 5 cents
{&Solenoid_None, T20ms,{M10,W10,W10}}, // W10, seeing a dime
{&Solenoid_None, T20ms,{M10,W15,W20}}, // M10, have 10 cents
{&Solenoid_None, T20ms,{M15,W15,W15}}, // W15, seeing something
{&Solenoid_Soda, T20ms,{M0,M0,M0}}, // M15, have 15 cents
{&Solenoid_None, T20ms,{M20,W20,W20}}, // W20, seeing dime
{&Solenoid_Change,T20ms,{M15,M15,M15}}}; // M20, have 20 cents
unsigned long S; // index into current state
unsigned long Input;
int main(void){
FSM_Init();
S = M0; // Initial State
while(1){
(FSM[S].CmdPt)(); // call output function
SysTick_Wait(FSM[S].Time); // wait Program 10.2
Input = Coin_Input(); // input can be 0,1,2
S = FSM[S].Next[Input]; // next
}
}