-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLab04_5.c
247 lines (200 loc) · 5.29 KB
/
Lab04_5.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Lab04_5.c
*
* Created on: Oct 24, 2019
* Author: hyperiontiger
*/
#include "init.h"
#include "math.h"
void configureDAC();
void configureADC();
void Init_Timer();
DAC_HandleTypeDef __hdac;
DAC_ChannelConfTypeDef dac_ch;
ADC_HandleTypeDef adc1;
ADC_HandleTypeDef adc3;
TIM_HandleTypeDef htim7;
int flag_tim6;
int f_adc1;
int f_adc3;
uint16_t raw_adc1;
uint16_t raw_adc3;
uint16_t x_t[5];
uint16_t y_t[5];
uint16_t tempx;
uint16_t tempy;
int main (void)
{
/*main code
*
*/
Sys_Init();
configureADC();
configureDAC();
Init_Timer();
printf("\r\033[2J\033[1;1HLab4-5---------------------------------!\r\n");
while(1){
}
}
void configureDAC()
{
/*configure the DAC
*
*/
__HAL_RCC_DAC_CLK_ENABLE();
__hdac.Instance = DAC1;
HAL_DAC_Init(&__hdac);
dac_ch.DAC_Trigger = DAC_TRIGGER_NONE;
dac_ch.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
HAL_DAC_ConfigChannel(&__hdac, &dac_ch, DAC_CHANNEL_1);
}
void configureADC()
{
// Enable the ADC Clock.ExternalTrigConv for ADC1 and ADC3 __HAL_ADC_ENABLE_IT
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
RCC->APB2ENR |= RCC_APB2ENR_ADC3EN;
NVIC->ISER[18 / 32] = (uint32_t) 1 << (18 % 32); //ADC Global, priority 25
HAL_NVIC_EnableIRQ(ADC_IRQn); // Enable IRQ
HAL_NVIC_SetPriority(ADC_IRQn, 1, 1);
//ADC_HandleTypeDef adc1;
adc1.Instance = ADC1;
adc1.Init.Resolution = ADC_RESOLUTION_12B;
adc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
adc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
adc1.Init.ContinuousConvMode = ENABLE;
adc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&adc1); // Initialize the ADC1
ADC_ChannelConfTypeDef _ADC1_IN6;
_ADC1_IN6.Channel = ADC_CHANNEL_6;
_ADC1_IN6.Offset = 0;
_ADC1_IN6.Rank = ADC_REGULAR_RANK_1;
_ADC1_IN6.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&adc1, &_ADC1_IN6);
//ADC_HandleTypeDef adc3;
adc1.Instance = ADC3;
adc1.Init.Resolution = ADC_RESOLUTION_12B;
adc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
adc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
adc1.Init.ContinuousConvMode = ENABLE;
adc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&adc3); // Initialize the ADC3
ADC_ChannelConfTypeDef _ADC3_IN8;
_ADC1_IN6.Channel = ADC_CHANNEL_8;
_ADC1_IN6.Offset = 0;
_ADC1_IN6.Rank = ADC_REGULAR_RANK_1;
_ADC1_IN6.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&adc3, &_ADC3_IN8);
}
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
{
/*configure the DAC GPIO
*
*/
GPIO_InitTypeDef GPIO_InitStruct;
__GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4; //Analog output PA4
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
{
// GPIO init
GPIO_InitTypeDef GPIO_InitStruct;
__GPIOA_CLK_ENABLE();
__GPIOC_CLK_ENABLE();
__GPIOF_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_10; //Analog input ADC3IN8
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6; //Analog input ADC1IN6
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_7; //Pushbutton C7
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Init_Timer()
{
//Enable Timer 7: overflow at 1.211E-5s
HAL_Init();
htim7.Instance = TIM7;
htim7.Init.Prescaler = 2;
htim7.Init.Period = 436;
__HAL_RCC_TIM7_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM7_IRQn);
HAL_TIM_Base_Init(&htim7);
HAL_TIM_Base_Start_IT(&htim7);
}
void TIM7_IRQHandler() {
//HAL_GPIO_EXTI_IRQHandler(???);
HAL_TIM_IRQHandler(&htim7);
}
void ADC_IRQHandler(void)
{
HAL_ADC_IRQHandler(&adc1);
HAL_ADC_IRQHandler(&adc3);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if (hadc->Instance == ADC1)
{
//adc1 value
printf("\r\n111\r\n");
f_adc1 = 1;
raw_adc1 = HAL_ADC_GetValue(&adc1);
//__HAL_ADC_CLEAR_FLAG(&adc1, ADC_SR_EOC);
// HAL_ADC_Stop_IT(&adc1);
}
if (hadc->Instance == ADC3)
{
//adc2 value
printf("\r\n333\r\n");
f_adc3 = 1;
raw_adc3 = HAL_ADC_GetValue(&adc3);
//__HAL_ADC_CLEAR_FLAG(&adc3, ADC_SR_EOC);
// HAL_ADC_Stop_IT(&adc3);
}
if (f_adc1 && f_adc3)
{
//multiply
f_adc1 = 0;
f_adc3 = 0;
tempx = x_t[3];
x_t[3] = x_t[2];
x_t[2] = x_t[1];
x_t[1] = x_t[0];
x_t[4] = tempx;
x_t[0] = (raw_adc1-620) * (raw_adc3-620);
//filter code
tempy = y_t[3];
y_t[3] = y_t[2];
y_t[2] = y_t[1];
y_t[1] = y_t[0];
y_t[4] = tempy;
y_t[0] = 0.001*x_t[0] - 0.002*x_t[2] + 0.001*x_t[4] + 3.166*y_t[1] - 4.418*y_t[2] + 3.028*y_t[3] - 0.915*y_t[4];
//DAC
HAL_DAC_Init(&__hdac);
HAL_DAC_Start(&__hdac, DAC_CHANNEL_1);
HAL_DAC_SetValue(&__hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (y_t[0]+620));
HAL_ADC_Stop_IT(&adc3);
HAL_ADC_Stop_IT(&adc1);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM7)
{
HAL_ADC_Start_IT(&adc1);
HAL_ADC_Start_IT(&adc3);
}
//counter_10++;
}