-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_to_blink.c
73 lines (67 loc) · 2.36 KB
/
push_to_blink.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
// 0. Documentation Section
// This program allows a user to push a button which causes an LED to blink.
// The LED is in the on state when the switch it not pressed.
// The LED blinks at ~5Hz or 100ms intervals while button is pressed.
// Author: Randy Shreeves
// Date: 10/30/2023
// 1. Pre-processor Directives Section
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTE_DATA_R (*((volatile unsigned long *)0x400243FC))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#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 NVIC_ST_CTRL_R (*((volatile unsigned long *)0xE000E010))
#define NVIC_ST_RELOAD_R (*((volatile unsigned long *)0xE000E014))
#define NVIC_ST_CURRENT_R (*((volatile unsigned long *)0xE000E018))
// 2. Global Declarations Section
void port_e_initialization(void);
void sys_tick_initialization(void);
void delay_1ms(unsigned long msec);
// 3. Subroutines Section
int main(void){
// Setup
unsigned long input_switch;
port_e_initialization();
GPIO_PORTE_DATA_R |= 0x02; // LED starts in the on state
// Loop
while(1){
input_switch = GPIO_PORTE_DATA_R&0x01;
delay_1ms(100);
if (input_switch == 0x01){ // if switch is pressed
GPIO_PORTE_DATA_R ^= 0x02; // toggle LED
}
else{
GPIO_PORTE_DATA_R |= 0x02; // else, turn LED on
}
}// end while loop
}// end main
void port_e_initialization(void){
unsigned long volatile delay; // delay for 3-5 bus cycles for clock to stabilize
SYSCTL_RCGC2_R |= 0x10;
delay = SYSCTL_RCGC2_R;
GPIO_PORTE_AMSEL_R &= ~0x03;
GPIO_PORTE_PCTL_R &= ~0x000000FF;
GPIO_PORTE_DIR_R |= 0x02;
GPIO_PORTE_DIR_R &= ~0x01;
GPIO_PORTE_AFSEL_R &= ~0x03;
GPIO_PORTE_DEN_R |= 0x03;
} // end portE_initialization
void sys_tick_initialization(void){
NVIC_ST_CTRL_R = 0;
NVIC_ST_RELOAD_R = 15999;
NVIC_ST_CURRENT_R = 0;
NVIC_ST_CTRL_R = 0x00000005;
}//end SysTick_initialization
void delay_1ms(unsigned long msec){
unsigned long n = msec;
while (n > 0){
sys_tick_initialization();
while((NVIC_ST_CTRL_R&0x00010000) == 0) {
// wait for count flag to be set
}// end inner while loop
NVIC_ST_CTRL_R = 0;
n--;
}// end outer while loop
}// end delay_1ms()