-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.c
139 lines (117 loc) · 3.04 KB
/
button.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
#include "button.h"
#include "pwm.h"
#include "adc.h"
#include "stm32f303xe.h"
#define ZERO 1023
#define HALF 512
#define FULL 0
void EXTI0_IRQHandler(void)
{
if (inADCMode == 0)
{
// Sets duty cycle to 100% (zero power to motor)
update_duty_cycle(ZERO);
}
EXTI -> PR |= EXTI_PR_PR0;
}
void EXTI1_IRQHandler(void)
{
if (inADCMode == 0)
{
// Sets duty cycle to 50% (half power to motor)
update_duty_cycle(HALF);
}
EXTI -> PR |= EXTI_PR_PR1;
}
void EXTI2_TSC_IRQHandler(void)
{
if (inADCMode == 0)
{
// Sets duty cycle to zero (full power to motor)
update_duty_cycle(FULL);
}
EXTI -> PR |= EXTI_PR_PR2;
}
// ISR for button 3 (to enable/disable ADC mode)
void EXTI3_IRQHandler(void)
{
if (!inADCMode)
{
// Enables ADC and blocks other buttons
start_ADC1();
inADCMode = 1;
}
else
{
// Disables ADC - control back to other buttons
disable_ADC1();
inADCMode = 0;
}
EXTI -> PR |= EXTI_PR_PR3;
}
// Sets up button 0
void setup_button_0(void)
{
// Sets up PC0 as input
RCC -> AHBENR |= RCC_AHBENR_GPIOCEN;
GPIOC -> MODER &= ~(GPIO_MODER_MODER0_Msk);
// Configures interrupt line for button
RCC -> APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG -> EXTICR[0] &= ~(SYSCFG_EXTICR1_EXTI0_Msk);
SYSCFG -> EXTICR[0] |= SYSCFG_EXTICR1_EXTI0_PC;
// Sets up EXTI0 interrupt line
EXTI -> IMR |= EXTI_IMR_MR0;
EXTI -> FTSR |= EXTI_FTSR_FT0; // Set up to trigger on falling edge
// Enable interrupt
NVIC_EnableIRQ(EXTI0_IRQn);
}
// Sets up button 1
void setup_button_1(void)
{
// Sets up PC1 as input
RCC -> AHBENR |= RCC_AHBENR_GPIOCEN;
GPIOC -> MODER &= ~(GPIO_MODER_MODER1_Msk);
// Configures interrupt line for button
RCC -> APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG -> EXTICR[0] &= ~(SYSCFG_EXTICR1_EXTI1_Msk);
SYSCFG -> EXTICR[0] |= SYSCFG_EXTICR1_EXTI1_PC;
// Sets up EXTI1 interrupt line
EXTI -> IMR |= EXTI_IMR_MR1;
EXTI -> FTSR |= EXTI_FTSR_FT1; // Set up to trigger on falling edge
// Enable interrupt
NVIC_EnableIRQ(EXTI1_IRQn);
}
// Sets up button 2
void setup_button_2(void)
{
// Sets up PC2 as input
RCC -> AHBENR |= RCC_AHBENR_GPIOCEN;
GPIOC -> MODER &= ~(GPIO_MODER_MODER2_Msk);
// Configures interrupt line for button
RCC -> APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG -> EXTICR[0] &= ~(SYSCFG_EXTICR1_EXTI2_Msk);
SYSCFG -> EXTICR[0] |= SYSCFG_EXTICR1_EXTI2_PC;
// Sets up EXTI2 interrupt line
EXTI -> IMR |= EXTI_IMR_MR2;
EXTI -> FTSR |= EXTI_FTSR_FT2; // Set up to trigger on falling edge
// Enable interrupt
NVIC_EnableIRQ(EXTI2_TSC_IRQn);
}
// Sets up button 3
void setup_button_3(void)
{
// Fan not in ADC mode by default
inADCMode = 0;
// Sets up PC3 as input
RCC -> AHBENR |= RCC_AHBENR_GPIOCEN;
GPIOC -> MODER &= ~(GPIO_MODER_MODER3_Msk);
// Configures interrupt line for button
RCC -> APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG -> EXTICR[0] &= ~(SYSCFG_EXTICR1_EXTI3_Msk);
SYSCFG -> EXTICR[0] |= SYSCFG_EXTICR1_EXTI3_PC;
// Sets up EXTI2 interrupt line
EXTI -> IMR |= EXTI_IMR_MR3;
EXTI -> FTSR |= EXTI_FTSR_FT3; // Set up to trigger on falling edge
// Enable interrupt
NVIC_EnableIRQ(EXTI3_IRQn);
}