-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_pacemaker.c
203 lines (196 loc) · 6.05 KB
/
Simple_pacemaker.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 0.Documentation Section
// main.c
// Runs on LM4F120 or TM4C123 LaunchPad
// Input from PF4(SW1) is AS (atrial sensor),
// Output to PF3, Green LED, is Ready,
// Output to PF1, Red LED, is VT (ventricular trigger)
// Make PF4 input, PF3,PF1 output
// Initialize Ready to high and VT to low
// Repeat this sequence of operation over and over
// 1) Wait for AS to fall (touch SW1 switch)
// 2) Clear Ready low
// 3) Wait 10ms (debounces the switch)
// 4) Wait for AS to rise (release SW1)
// 5) Wait 250ms (simulates the time between atrial and ventricular contraction)
// 6) set VT high, which will pulse the ventricles
// 7) Wait 250ms
// 8) clear VT low
// 9) set Ready high
// 1. Pre-processor Directives Section
#include "TExaS.h"
// Constant declarations to access port registers using
// symbolic names instead of addresses
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
// 2. Declarations Section
// Global Variables
unsigned long SW1;
unsigned long ready = 1,out;
// Function Prototypes
void PortF_Init(void);
void Delay1ms(unsigned long msec);
void EnableInterrupts(void); // Enable interrupts
void WaitForASLow(void);
void WaitForASHigh(void);
void SetVT(void);
void ClearVT(void);
void SetReady(void);
void ClearReady(void);
// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
int main(void){
TExaS_Init(SW_PIN_PF40, LED_PIN_PF31,ScopeOn); // activate grader and set system clock to 80 MHz
PortF_Init(); // Init port PF4 PF3 PF1
EnableInterrupts(); // enable interrupts for the grader
GPIO_PORTF_DATA_R = 0x08; // initially the LED is Green
while(1){ // Follows the nine steps list above
// a) Ready signal goes high
// b) wait for switch to be pressed
// c) Ready signal goes low
// d) wait 10ms
// e) wait for switch to be released
// f) wait 250ms
// g) VT signal goes high
// h) wait 250ms
// i) VT signal goes low
SW1 = GPIO_PORTF_DATA_R & 0x10;
SetReady();
WaitForASLow();
ClearReady();
D1: SW1 = GPIO_PORTF_DATA_R & 0x10;
WaitForASHigh();
if(SW1 == 0x00)
{
goto D1; // You have to immediately check : if SW1 == 0x10,immediately goes to WaitForASHigh()
}
SetVT();
if(SW1 == 0x00)
{
goto D1;
}
ClearVT();
if(SW1 == 0x00)
{
goto D1;
}
}
}
// Subroutine to initialize port F pins for input and output
// PF4 is input SW1 and PF3-1 is output LEDs
// Inputs: None
// Outputs: None
// Notes: ...
void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock
delay = SYSCTL_RCGC2_R; // delay to allow clock to stabilize
GPIO_PORTF_AMSEL_R &= 0x00; // 2) disable analog function
GPIO_PORTF_PCTL_R &= 0x00000000; // 3) GPIO clear bit PCTL
GPIO_PORTF_DIR_R &= ~0x10; // 4.1) PF4 input,
GPIO_PORTF_DIR_R |= 0x0E; // 4.2) PF3,2,1 output
GPIO_PORTF_AFSEL_R &= 0x00; // 5) no alternate function
GPIO_PORTF_PUR_R |= 0x10; // 6) enable pullup resistor on PF4
GPIO_PORTF_DEN_R |= 0x1E; // 7) enable digital pins PF4-PF1
}
// Color LED(s) PortF
// dark --- 0
// red R-- 0x02
// blue --B 0x04
// green -G- 0x08
// yellow RG- 0x0A
// sky blue -GB 0x0C
// white RGB 0x0E
// Subroutine reads AS input and waits for signal to be low
// If AS is already low, it returns right away
// If AS is currently high, it will wait until it to go low
// Inputs: None
// Outputs: None
void WaitForASLow(void){
// write this function
if(ready == 1 && SW1 == 0x00)
{
GPIO_PORTF_DATA_R = 0x00;
}
}
// Subroutine reads AS input and waits for signal to be high
// If AS is already high, it returns right away
// If AS is currently low, it will wait until it to go high
// Inputs: None
// Outputs: None
void WaitForASHigh(void){
// write this function
if(ready == 0 && SW1 == 0x10)
{
Delay1ms(299);
}
}
// Subroutine sets VT high
// Inputs: None
// Outputs: None
// Notes: friendly means it does not affect other bits in the port
void SetVT(void){
// write this function
if(ready == 0 && SW1 == 0x10)
{
GPIO_PORTF_DATA_R = 0x02;
Delay1ms(250);
}
}
// Subroutine clears VT low
// Inputs: None
// Outputs: None
// Notes: friendly means it does not affect other bits in the port
void ClearVT(void){
// write this function
if(ready == 0 && SW1 == 0x10)
{
Delay1ms(50);
GPIO_PORTF_DATA_R = 0x08;
}
}
// Subroutine sets Ready high
// Inputs: None
// Outputs: None
// Notes: friendly means it does not affect other bits in the port
void SetReady(void){
// write this function
if(SW1 == 0x10 && ready == 0)
{
ready = 1;
}
}
// Subroutine clears Ready low
// Inputs: None
// Outputs: None
// Notes: friendly means it does not affect other bits in the port
void ClearReady(void){
// write this function
if(SW1 == 0x00 && ready == 1)
{
ready = 0;
}
}
// Subroutine to delay in units of milliseconds
// Inputs: Number of milliseconds to delay
// Outputs: None
// Notes: assumes 80 MHz clock
void Delay1ms(unsigned long msec){
// write this function
unsigned long i;
while(msec > 0)
{
i = (1333333 / 100); // 100ms = 1333333; 1ms = (1333333 / 100);
while(i >0)
{
i = (i - 1);
}
msec = (msec - 1);
}
}