-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_Functions.c
executable file
·148 lines (129 loc) · 3.92 KB
/
Final_Functions.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
#include "Final_Functions.h"
#include "Keypad.h"
// Stepper motor initialization
void stepper_motor_init(void){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // enable port B clock
Motor_Time_Init();
// set pins 0-3 as output
GPIOB->MODER |= GPIO_MODER_MODE11_0;
GPIOB->MODER |= GPIO_MODER_MODE12_0;
GPIOB->MODER |= GPIO_MODER_MODE13_0;
GPIOB->MODER |= GPIO_MODER_MODE14_0;
}
// 0=forward 1=backwards
void driveMotor(int direction){
if (direction == 0){
GPIOB->ODR |= (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR |= (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR |= (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR |= (1U<<14);
for(int i=0; i<2000; i++);
} else {
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR |= (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR |= (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR &=~ (1U<<11);
GPIOB->ODR |= (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
GPIOB->ODR |= (1U<<11);
GPIOB->ODR &=~ (1U<<12);
GPIOB->ODR &=~ (1U<<13);
GPIOB->ODR &=~ (1U<<14);
for(int i=0; i<2000; i++);
}
}
void water_level_init(void){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // enable port D clock
GPIOD->MODER &=~ GPIO_MODER_MODE6; // set PD6 to input
//*********************** ADC Setup ********************************//
// initialize clock for Port A and ADC
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
// intiialize Port A pin 1 as ADC pin
GPIOA->MODER |= GPIO_MODER_MODE1_Msk;
// CR1 register (default is 12 bit)
// Set Sampling time to 84 (100) cycles in SMPR2 register (default is 000)
ADC1->SMPR2 |= ADC_SMPR2_SMP0_2;
// Set Continuious mode ADC (CR2 register)
ADC1->CR2 |= ADC_CR2_CONT_Msk;
// Specify channel number 1 of the 1st conversion
ADC1->SQR1 &=~ ADC_SQR1_L;
ADC1->SQR3 |= ADC_SQR3_SQ1_0;
//Set end of conversion interrupt in CR1 register
ADC1->CR1 |= ADC_CR1_EOCIE;
}
// if it returns 1, water is low
int read_water_level(void){
int water_level;
// Turn on ADC conversion
ADC1->CR2 |= ADC_CR2_ADON;
// Start Conversion
ADC1->CR2 |= ADC_CR2_SWSTART;
while((ADC1->SR & ADC_SR_EOC) == 0);
water_level = ADC1->DR;
// Turn off ADC Conversion
ADC1->CR2 &=~ ADC_CR2_ADON;
if (water_level < 400){
return 1;
} else {
return 0;
}
}
void Motor_Time_Init(void){
SysTick->CTRL = 0; //disable SysTick
SysTick->LOAD = 15999-1; //setbuf reload register
//set interrupt priority of SysTick to less priority
NVIC_SetPriority (SysTick_IRQn, 6);
SysTick->VAL = 0; //reset the SysTick counter value
//select processor clock: 1 = processor clock; 0 = external clock
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
//enables SysTick interrupt, 1 = Enable, 0 = Disable
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
//enable SysTick
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}
//void systick_handler(void){ //systick interrupt service routine
// if (delaytime > 0){
// delaytime--;
// }
//}
void myDelay(uint32_t TimeToDelay){
// TimeToDelay specifies the delay time length
timerVal = TimeToDelay;
while(timerVal != 0); //busy wait until 0
}
void water_valve_init() {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // enable port A clock
GPIOA->MODER |= GPIO_MODER_MODE2_0; // use pin A2
}
void water_control(int mode){
if (mode == 0){
GPIOA->ODR &=~ GPIO_ODR_OD2;
} else {
GPIOA->ODR |= GPIO_ODR_OD2;
}
}