1 | /* USER CODE BEGIN Header */
|
2 | /**
|
3 | ******************************************************************************
|
4 | * @file : main.c
|
5 | * @brief : Main program body
|
6 | ******************************************************************************
|
7 | * @attention
|
8 | *
|
9 | * <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
10 | * All rights reserved.</center></h2>
|
11 | *
|
12 | * This software component is licensed by ST under BSD 3-Clause license,
|
13 | * the "License"; You may not use this file except in compliance with the
|
14 | * License. You may obtain a copy of the License at:
|
15 | * opensource.org/licenses/BSD-3-Clause
|
16 | *
|
17 | ******************************************************************************
|
18 | */
|
19 | /* USER CODE END Header */
|
20 | |
21 | /* Includes ------------------------------------------------------------------*/
|
22 | #include "main.h"
|
23 | |
24 | /* Private includes ----------------------------------------------------------*/
|
25 | /* USER CODE BEGIN Includes */
|
26 | |
27 | /* USER CODE END Includes */
|
28 | |
29 | /* Private typedef -----------------------------------------------------------*/
|
30 | /* USER CODE BEGIN PTD */
|
31 | |
32 | /* USER CODE END PTD */
|
33 | |
34 | /* Private define ------------------------------------------------------------*/
|
35 | /* USER CODE BEGIN PD */
|
36 | |
37 | /* USER CODE END PD */
|
38 | |
39 | /* Private macro -------------------------------------------------------------*/
|
40 | /* USER CODE BEGIN PM */
|
41 | |
42 | /* USER CODE END PM */
|
43 | |
44 | /* Private variables ---------------------------------------------------------*/
|
45 | TIM_HandleTypeDef htim2;
|
46 | |
47 | /* USER CODE BEGIN PV */
|
48 | uint8_t ui8TimPulse = 50; //Set duty cycle to 50 %
|
49 | |
50 | /* USER CODE END PV */
|
51 | |
52 | /* Private function prototypes -----------------------------------------------*/
|
53 | void SystemClock_Config(void);
|
54 | static void MX_GPIO_Init(void);
|
55 | static void MX_TIM2_Init(void);
|
56 | /* USER CODE BEGIN PFP */
|
57 | |
58 | /* USER CODE END PFP */
|
59 | |
60 | /* Private user code ---------------------------------------------------------*/
|
61 | /* USER CODE BEGIN 0 */
|
62 | |
63 | |
64 | |
65 | void heizen(uint16_t value){
|
66 | |
67 | HAL_GPIO_WritePin(nSLEEP_GPIO_Port,nSLEEP_Pin, 1);
|
68 | HAL_GPIO_WritePin(DISABLE_GPIO_Port,DISABLE_Pin, 0);
|
69 | |
70 | TIM_OC_InitTypeDef sConfigOC = {0};
|
71 | sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
72 | |
73 | sConfigOC.Pulse = value;
|
74 | |
75 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
76 | |
77 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
78 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
79 | {
|
80 | Error_Handler();
|
81 | }
|
82 | sConfigOC.Pulse = 101;
|
83 | sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
|
84 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
85 | {
|
86 | Error_Handler();
|
87 | }
|
88 | |
89 | |
90 | HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
|
91 | HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
|
92 | |
93 | |
94 | |
95 | |
96 | }
|
97 | |
98 | void cooling(uint16_t value){
|
99 | |
100 | HAL_GPIO_WritePin(nSLEEP_GPIO_Port,nSLEEP_Pin, 1);
|
101 | HAL_GPIO_WritePin(DISABLE_GPIO_Port,DISABLE_Pin, 0);
|
102 | |
103 | TIM_OC_InitTypeDef sConfigOC = {0};
|
104 | sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
105 | |
106 | sConfigOC.Pulse = 101;
|
107 | |
108 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
109 | |
110 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
111 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
112 | {
|
113 | Error_Handler();
|
114 | }
|
115 | sConfigOC.Pulse = value;
|
116 | sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
|
117 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
118 | {
|
119 | Error_Handler();
|
120 | }
|
121 | |
122 | |
123 | HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
|
124 | HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
|
125 | }
|
126 | |
127 | //value=PWM Value
|
128 | void peltier(int16_t value ){
|
129 | if (value>0){
|
130 | heizen(value);
|
131 | }
|
132 | if (value <0){
|
133 | cooling( (uint16_t)(value*-1) );
|
134 | }
|
135 | if (value ==0){
|
136 | HAL_GPIO_WritePin(nSLEEP_GPIO_Port,nSLEEP_Pin, 0);
|
137 | }
|
138 | |
139 | }
|
140 | /* USER CODE END 0 */
|
141 | |
142 | /**
|
143 | * @brief The application entry point.
|
144 | * @retval int
|
145 | */
|
146 | int main(void)
|
147 | {
|
148 | /* USER CODE BEGIN 1 */
|
149 | |
150 | /* USER CODE END 1 */
|
151 |
|
152 | |
153 | /* MCU Configuration--------------------------------------------------------*/
|
154 | |
155 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
156 | HAL_Init();
|
157 | |
158 | /* USER CODE BEGIN Init */
|
159 | |
160 | /* USER CODE END Init */
|
161 | |
162 | /* Configure the system clock */
|
163 | SystemClock_Config();
|
164 | |
165 | /* USER CODE BEGIN SysInit */
|
166 | |
167 | /* USER CODE END SysInit */
|
168 | |
169 | /* Initialize all configured peripherals */
|
170 | MX_GPIO_Init();
|
171 | MX_TIM2_Init();
|
172 | /* USER CODE BEGIN 2 */
|
173 | |
174 | HAL_TIM_Base_Start_IT(&htim2);//Startet Zeitbasis des TIMERS
|
175 | |
176 | /* USER CODE END 2 */
|
177 | |
178 | /* Infinite loop */
|
179 | /* USER CODE BEGIN WHILE */
|
180 | while (1)
|
181 | {
|
182 | /* USER CODE END WHILE */
|
183 | peltier(50);
|
184 | HAL_Delay(5000);
|
185 | peltier(-50);
|
186 | HAL_Delay(5000);
|
187 | |
188 | |
189 | /* USER CODE BEGIN 3 */
|
190 | }
|
191 | /* USER CODE END 3 */
|
192 | }
|
193 | |
194 | /**
|
195 | * @brief System Clock Configuration
|
196 | * @retval None
|
197 | */
|
198 | void SystemClock_Config(void)
|
199 | {
|
200 | RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
201 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
202 | |
203 | /** Initializes the CPU, AHB and APB busses clocks
|
204 | */
|
205 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
206 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
207 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
208 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
209 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
210 | {
|
211 | Error_Handler();
|
212 | }
|
213 | /** Initializes the CPU, AHB and APB busses clocks
|
214 | */
|
215 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
216 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
217 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
|
218 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
219 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
220 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
221 | |
222 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
|
223 | {
|
224 | Error_Handler();
|
225 | }
|
226 | }
|
227 | |
228 | /**
|
229 | * @brief TIM2 Initialization Function
|
230 | * @param None
|
231 | * @retval None
|
232 | */
|
233 | static void MX_TIM2_Init(void)
|
234 | {
|
235 | |
236 | /* USER CODE BEGIN TIM2_Init 0 */
|
237 | |
238 | /* USER CODE END TIM2_Init 0 */
|
239 | |
240 | TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
241 | TIM_MasterConfigTypeDef sMasterConfig = {0};
|
242 | TIM_OC_InitTypeDef sConfigOC = {0};
|
243 | |
244 | /* USER CODE BEGIN TIM2_Init 1 */
|
245 | |
246 | /* USER CODE END TIM2_Init 1 */
|
247 | htim2.Instance = TIM2;
|
248 | htim2.Init.Prescaler = 4;
|
249 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
250 | htim2.Init.Period = 100;
|
251 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
252 | htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
253 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
254 | {
|
255 | Error_Handler();
|
256 | }
|
257 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
258 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
259 | {
|
260 | Error_Handler();
|
261 | }
|
262 | if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
|
263 | {
|
264 | Error_Handler();
|
265 | }
|
266 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
267 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
268 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
269 | {
|
270 | Error_Handler();
|
271 | }
|
272 | sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
273 | sConfigOC.Pulse = 0;
|
274 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
275 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
276 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
277 | {
|
278 | Error_Handler();
|
279 | }
|
280 | sConfigOC.Pulse = 50;
|
281 | sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
|
282 | if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
283 | {
|
284 | Error_Handler();
|
285 | }
|
286 | /* USER CODE BEGIN TIM2_Init 2 */
|
287 | |
288 | /* USER CODE END TIM2_Init 2 */
|
289 | HAL_TIM_MspPostInit(&htim2);
|
290 | |
291 | }
|
292 | |
293 | /**
|
294 | * @brief GPIO Initialization Function
|
295 | * @param None
|
296 | * @retval None
|
297 | */
|
298 | static void MX_GPIO_Init(void)
|
299 | {
|
300 | GPIO_InitTypeDef GPIO_InitStruct = {0};
|
301 | |
302 | /* GPIO Ports Clock Enable */
|
303 | __HAL_RCC_GPIOA_CLK_ENABLE();
|
304 | __HAL_RCC_GPIOC_CLK_ENABLE();
|
305 | __HAL_RCC_GPIOD_CLK_ENABLE();
|
306 | __HAL_RCC_GPIOB_CLK_ENABLE();
|
307 | |
308 | /*Configure GPIO pin Output Level */
|
309 | HAL_GPIO_WritePin(DISABLE_GPIO_Port, DISABLE_Pin, GPIO_PIN_RESET);
|
310 | |
311 | /*Configure GPIO pin Output Level */
|
312 | HAL_GPIO_WritePin(nSLEEP_GPIO_Port, nSLEEP_Pin, GPIO_PIN_RESET);
|
313 | |
314 | /*Configure GPIO pin : DISABLE_Pin */
|
315 | GPIO_InitStruct.Pin = DISABLE_Pin;
|
316 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
317 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
318 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
319 | HAL_GPIO_Init(DISABLE_GPIO_Port, &GPIO_InitStruct);
|
320 | |
321 | /*Configure GPIO pin : nSLEEP_Pin */
|
322 | GPIO_InitStruct.Pin = nSLEEP_Pin;
|
323 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
324 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
325 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
326 | HAL_GPIO_Init(nSLEEP_GPIO_Port, &GPIO_InitStruct);
|
327 | |
328 | }
|
329 | |
330 | /* USER CODE BEGIN 4 */
|
331 | |
332 | /* USER CODE END 4 */
|
333 | |
334 | /**
|
335 | * @brief This function is executed in case of error occurrence.
|
336 | * @retval None
|
337 | */
|
338 | void Error_Handler(void)
|
339 | {
|
340 | /* USER CODE BEGIN Error_Handler_Debug */
|
341 | /* User can add his own implementation to report the HAL error return state */
|
342 | |
343 | /* USER CODE END Error_Handler_Debug */
|
344 | }
|
345 | |
346 | #ifdef USE_FULL_ASSERT
|
347 | /**
|
348 | * @brief Reports the name of the source file and the source line number
|
349 | * where the assert_param error has occurred.
|
350 | * @param file: pointer to the source file name
|
351 | * @param line: assert_param error line source number
|
352 | * @retval None
|
353 | */
|
354 | void assert_failed(uint8_t *file, uint32_t line)
|
355 | {
|
356 | /* USER CODE BEGIN 6 */
|
357 | /* User can add his own implementation to report the file name and line number,
|
358 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
359 | /* USER CODE END 6 */
|
360 | }
|
361 | #endif /* USE_FULL_ASSERT */
|
362 | |
363 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|