Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Main

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

main.

1 /* USER CODE BEGIN Header */


2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; 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 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <math.h>
30 /* USER CODE END Includes */
31
32 /* Private typedef -----------------------------------------------------------*/
33 /* USER CODE BEGIN PTD */
34
35 /* USER CODE END PTD */
36
37 /* Private define ------------------------------------------------------------*/
38 /* USER CODE BEGIN PD */
39 #define BIT0 1
40 #define BIT1 2
41 #define BIT2 4
42 #define BIT3 8
43 #define BIT4 16
44 #define BIT5 32
45 #define BIT6 64
46 #define BIT7 128
47 #define BIT8 256
48 #define BIT9 512
49 #define BIT10 1024
50 #define BIT11 2048
51 #define BIT12 4096
52 #define BIT13 8192
53 #define BIT14 16384
54 #define BIT15 32768
55
56 #define DEL 150
57 /*LCD Commands*/

Page 1
main.c

58 /*Command: Clear Screen*/


59 #define LCDCom_clear 0x01
60 /*Command: Cursor to Home*/
61 #define LCDCom_home 0x03
62 /*Command: 4 Bit Mode*/
63 #define LCDCom_fnset4 0x02
64 #define LCDCom_fnset42 0x28
65 /*Command: 4Bit, 2Lines, Display On, cursor ON, blink ON*/
66 #define LCDCom_enIncAuto 0x06
67 /*Command: Display Blink OFF*/
68 #define LCDCom_dispNob 0x0E
69 /*Command: Display Blink ON*/
70 #define LCDCom_dispB 0x0F
71
72 /* USER CODE END PD */
73
74 /* Private macro -------------------------------------------------------------*/
75 /* USER CODE BEGIN PM */
76
77 /* USER CODE END PM */
78
79 /* Private variables ---------------------------------------------------------*/
80 ADC_HandleTypeDef hadc1;
81 DMA_HandleTypeDef hdma_adc1;
82
83 I2C_HandleTypeDef hi2c1;
84
85 TIM_HandleTypeDef htim2;
86 TIM_HandleTypeDef htim4;
87
88 /* USER CODE BEGIN PV */
89
90 /* USER CODE END PV */
91
92 /* Private function prototypes -----------------------------------------------*/
93 void SystemClock_Config(void);
94 static void MX_GPIO_Init(void);
95 static void MX_DMA_Init(void);
96 static void MX_ADC1_Init(void);
97 static void MX_TIM2_Init(void);
98 static void MX_I2C1_Init(void);
99 static void MX_TIM4_Init(void);
100 /* USER CODE BEGIN PFP */
101 void lcdInit();
102 void lcdSendComm(uint8_t _com);
103 void lcdSendChar(uint8_t _char);
104 void lcdGoToxy(uint8_t x, uint8_t y);
105 void lcdClearScreen();
106 void lcdPrintS(char *_str);
107 void lcdPrintF(const char *fmt, ...);
108 void lcdENPulse();
109 void resistorSet(uint _setRes);
110 /* USER CODE END PFP */
111
112 /* Private user code ---------------------------------------------------------*/
113 /* USER CODE BEGIN 0 */
114

Page 2
main.c

115 uint processStep = 0;


116
117 unsigned short flag = 1; //1: Rising Edge; 0: Falling Edge
118 uint32_t timeA = 0; //Rising Edge Capture
119 uint32_t timeB = 0; //Falling Edge CApture
120 double pulseWidth = 0; //Difference
121 double phaseAngle = 0; //Impedance Phase Angle
122
123 uint resSelect = 0; //Resistor Value Selected Index
124 uint frecSelect = 0; //Frequency VAlue Selected Index
125 //Resistor Array
126 uint resistor[] = { 1, 10, 100, 1000, 10000, 15000, 20000, 22000 };
127 //Frequency Array
128 uint32_t frequency[] = { 100, 1000, 10000, 100000 };
129 double periodnS[] = { 10000000.0, 1000000.0, 100000.0, 1000.0 };
130
131 uint32_t adcVal[2] = { 0, 0 };
132 uint32_t coso = 100;
133
134 double v0 = 0; //Vdiff
135 double v1 = 0; //Vz
136 double i = 0;
137 double z = 0;
138 double c = 0;
139
140 /* USER CODE END 0 */
141
142 /**
143 * @brief The application entry point.
144 * @retval int
145 */
146 int main(void) {
147 /* USER CODE BEGIN 1 */
148 /* USER CODE END 1 */
149
150 /* MCU Configuration--------------------------------------------------------*/
151
152 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
153 HAL_Init();
154
155 /* USER CODE BEGIN Init */
156
157 /* USER CODE END Init */
158
159 /* Configure the system clock */
160 SystemClock_Config();
161
162 /* USER CODE BEGIN SysInit */
163
164 /* USER CODE END SysInit */
165
166 /* Initialize all configured peripherals */
167 MX_GPIO_Init();
168 MX_DMA_Init();
169 MX_ADC1_Init();
170 MX_TIM2_Init();
171 MX_I2C1_Init();

Page 3
main.c

172 MX_TIM4_Init();
173 /* USER CODE BEGIN 2 */
174
175 /* USER CODE END 2 */
176
177 /* Infinite loop */
178 /* USER CODE BEGIN WHILE */
179 HAL_ADC_Start_DMA(&hadc1, adcVal, 2);
180 lcdInit();
181 lcdPrintF("Huevos a los %s", "sordos");
182 short int isMagnitudeCorrect = 0;
183 frecSelect = 3;
184 while (1) {
185 resSelect = 0;
186 pulseWidth = 0;
187 phaseAngle = 0;
188 /* USER CODE END WHILE */
189
190 /* USER CODE BEGIN 3 */
191 while (!isMagnitudeCorrect) {
192 resistorSet(resSelect++);
193 HAL_Delay(5);
194 isMagnitudeCorrect = fabs(
195 100.0 * (adcVal[1] - adcVal[2]) / (adcVal[1] + adcVal[2]))
196 < 80;
197 if (resSelect > 7) {
198 lcdClearScreen();
199 lcdGoToxy(0, 1);
200 lcdPrintF("Out of Range");
201 }
202 }
203 HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
204 HAL_Delay(5);
205 if (pulseWidth > 0) { //Valid condition
206 phaseAngle = pulseWidth * 13.88889 / periodnS[frecSelect]; //10k
207 }
208
209 v0 = 3.3 * adcVal[0] / 4095; //Vdiff
210 i = 1000 * v0 / resistor[resSelect]; //mA
211 v1 = 3.3 * adcVal[1] / 4095; //Vcap
212
213 z = v1 / i;
214 c = 1000 / (6.2831 * z);
215
216 }
217 /* USER CODE END 3 */
218 }
219
220 /**
221 * @brief System Clock Configuration
222 * @retval None
223 */
224 void SystemClock_Config(void) {
225 RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
226 RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
227 RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
228

Page 4
main.c

229 /** Initializes the CPU, AHB and APB busses clocks
230 */
231 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
232 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
233 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
234 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
235 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
236 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
237 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
238 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
239 Error_Handler();
240 }
241 /** Initializes the CPU, AHB and APB busses clocks
242 */
243 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
244 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
245 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
246 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
247 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
248 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
249
250 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
251 Error_Handler();
252 }
253 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
254 PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
255 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
256 Error_Handler();
257 }
258 }
259
260 /**
261 * @brief ADC1 Initialization Function
262 * @param None
263 * @retval None
264 */
265 static void MX_ADC1_Init(void) {
266
267 /* USER CODE BEGIN ADC1_Init 0 */
268
269 /* USER CODE END ADC1_Init 0 */
270
271 ADC_ChannelConfTypeDef sConfig = { 0 };
272
273 /* USER CODE BEGIN ADC1_Init 1 */
274
275 /* USER CODE END ADC1_Init 1 */
276 /** Common config
277 */
278 hadc1.Instance = ADC1;
279 hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
280 hadc1.Init.ContinuousConvMode = DISABLE;
281 hadc1.Init.DiscontinuousConvMode = DISABLE;
282 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
283 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
284 hadc1.Init.NbrOfConversion = 2;
285 if (HAL_ADC_Init(&hadc1) != HAL_OK) {

Page 5
main.c

286 Error_Handler();
287 }
288 /** Configure Regular Channel
289 */
290 sConfig.Channel = ADC_CHANNEL_0;
291 sConfig.Rank = ADC_REGULAR_RANK_1;
292 sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
293 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
294 Error_Handler();
295 }
296 /** Configure Regular Channel
297 */
298 sConfig.Channel = ADC_CHANNEL_1;
299 sConfig.Rank = ADC_REGULAR_RANK_2;
300 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
301 Error_Handler();
302 }
303 /* USER CODE BEGIN ADC1_Init 2 */
304
305 /* USER CODE END ADC1_Init 2 */
306
307 }
308
309 /**
310 * @brief I2C1 Initialization Function
311 * @param None
312 * @retval None
313 */
314 static void MX_I2C1_Init(void) {
315
316 /* USER CODE BEGIN I2C1_Init 0 */
317
318 /* USER CODE END I2C1_Init 0 */
319
320 /* USER CODE BEGIN I2C1_Init 1 */
321
322 /* USER CODE END I2C1_Init 1 */
323 hi2c1.Instance = I2C1;
324 hi2c1.Init.ClockSpeed = 100000;
325 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
326 hi2c1.Init.OwnAddress1 = 0;
327 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
328 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
329 hi2c1.Init.OwnAddress2 = 0;
330 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
331 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
332 if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
333 Error_Handler();
334 }
335 /* USER CODE BEGIN I2C1_Init 2 */
336
337 /* USER CODE END I2C1_Init 2 */
338
339 }
340
341 /**
342 * @brief TIM2 Initialization Function

Page 6
main.c

343 * @param None


344 * @retval None
345 */
346 static void MX_TIM2_Init(void) {
347
348 /* USER CODE BEGIN TIM2_Init 0 */
349
350 /* USER CODE END TIM2_Init 0 */
351
352 TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
353 TIM_MasterConfigTypeDef sMasterConfig = { 0 };
354
355 /* USER CODE BEGIN TIM2_Init 1 */
356
357 /* USER CODE END TIM2_Init 1 */
358 htim2.Instance = TIM2;
359 htim2.Init.Prescaler = 0;
360 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
361 htim2.Init.Period = 65535 - 1;
362 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
363 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
364 if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {
365 Error_Handler();
366 }
367 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
368 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
369 Error_Handler();
370 }
371 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
372 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
373 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig)
374 != HAL_OK) {
375 Error_Handler();
376 }
377 /* USER CODE BEGIN TIM2_Init 2 */
378
379 /* USER CODE END TIM2_Init 2 */
380
381 }
382
383 /**
384 * @brief TIM4 Initialization Function
385 * @param None
386 * @retval None
387 */
388 static void MX_TIM4_Init(void) {
389
390 /* USER CODE BEGIN TIM4_Init 0 */
391
392 /* USER CODE END TIM4_Init 0 */
393
394 TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
395 TIM_MasterConfigTypeDef sMasterConfig = { 0 };
396 TIM_IC_InitTypeDef sConfigIC = { 0 };
397
398 /* USER CODE BEGIN TIM4_Init 1 */
399

Page 7
main.c

400 /* USER CODE END TIM4_Init 1 */


401 htim4.Instance = TIM4;
402 htim4.Init.Prescaler = 0;
403 htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
404 htim4.Init.Period = 0;
405 htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
406 htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
407 if (HAL_TIM_Base_Init(&htim4) != HAL_OK) {
408 Error_Handler();
409 }
410 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
411 if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) {
412 Error_Handler();
413 }
414 if (HAL_TIM_IC_Init(&htim4) != HAL_OK) {
415 Error_Handler();
416 }
417 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
418 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
419 if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig)
420 != HAL_OK) {
421 Error_Handler();
422 }
423 sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
424 sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
425 sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
426 sConfigIC.ICFilter = 0;
427 if (HAL_TIM_IC_ConfigChannel(&htim4, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) {
428 Error_Handler();
429 }
430 /* USER CODE BEGIN TIM4_Init 2 */
431
432 /* USER CODE END TIM4_Init 2 */
433
434 }
435
436 /**
437 * Enable DMA controller clock
438 */
439 static void MX_DMA_Init(void) {
440
441 /* DMA controller clock enable */
442 __HAL_RCC_DMA1_CLK_ENABLE()
443 ;
444
445 /* DMA interrupt init */
446 /* DMA1_Channel1_IRQn interrupt configuration */
447 HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
448 HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
449
450 }
451
452 /**
453 * @brief GPIO Initialization Function
454 * @param None
455 * @retval None
456 */

Page 8
main.c

457 static void MX_GPIO_Init(void) {


458 GPIO_InitTypeDef GPIO_InitStruct = { 0 };
459
460 /* GPIO Ports Clock Enable */
461 __HAL_RCC_GPIOC_CLK_ENABLE()
462 ;
463 __HAL_RCC_GPIOD_CLK_ENABLE()
464 ;
465 __HAL_RCC_GPIOA_CLK_ENABLE()
466 ;
467 __HAL_RCC_GPIOB_CLK_ENABLE()
468 ;
469
470 /*Configure GPIO pin Output Level */
471 HAL_GPIO_WritePin(GPIOA,
472 OUT_R0_Pin | OUT_R1_Pin | OUT_R2_Pin | OUT_F0_Pin | OUT_F1_Pin,
473 GPIO_PIN_RESET);
474
475 /*Configure GPIO pin Output Level */
476 HAL_GPIO_WritePin(GPIOB,
477 LCD_RS_Pin | LCD_EN_Pin | LCD_D4_Pin | LCD_D5_Pin | LCD_D6_Pin | LCD_D7_Pin,
478 GPIO_PIN_RESET);
479
480 /*Configure GPIO pins : OUT_R0_Pin OUT_R1_Pin OUT_R2_Pin OUT_F0_Pin
481 OUT_F1_Pin */
482 GPIO_InitStruct.Pin = OUT_R0_Pin | OUT_R1_Pin | OUT_R2_Pin | OUT_F0_Pin
483 | OUT_F1_Pin;
484 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
485 GPIO_InitStruct.Pull = GPIO_NOPULL;
486 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
487 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
488
489 /*Configure GPIO pins : LCD_RS_Pin LCD_EN_Pin LCD_D4_Pin LCD_D5_Pin
490 LCD_D6_Pin LCD_D7_Pin */
491 GPIO_InitStruct.Pin = LCD_RS_Pin | LCD_EN_Pin | LCD_D4_Pin | LCD_D5_Pin
492 | LCD_D6_Pin | LCD_D7_Pin;
493 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
494 GPIO_InitStruct.Pull = GPIO_NOPULL;
495 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
496 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
497
498 /*Configure GPIO pins : IN_100_Pin IN_1k_Pin IN_10k_Pin IN_100k_Pin */
499 GPIO_InitStruct.Pin = IN_100_Pin | IN_1k_Pin | IN_10k_Pin | IN_100k_Pin;
500 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
501 GPIO_InitStruct.Pull = GPIO_PULLUP;
502 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
503
504 /*Configure GPIO pin : IN_Signo_Pin */
505 GPIO_InitStruct.Pin = IN_Signo_Pin;
506 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
507 GPIO_InitStruct.Pull = GPIO_NOPULL;
508 HAL_GPIO_Init(IN_Signo_GPIO_Port, &GPIO_InitStruct);
509
510 /* EXTI interrupt init*/
511 HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
512 HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
513

Page 9
main.c

514 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);


515 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
516
517 }
518
519 /* USER CODE BEGIN 4 */
520
521 void lcdInit() {
522 //Clear Data Pins
523 GPIOB->ODR &= ~( BIT15 | BIT14 | BIT13 | BIT12);
524 lcdSendComm(LCDCom_fnset4);
525 lcdSendComm(LCDCom_fnset42);
526 lcdSendComm(LCDCom_dispB);
527 lcdSendComm(LCDCom_enIncAuto);
528 lcdSendComm(LCDCom_clear);
529 lcdSendComm(LCDCom_home);
530
531 }
532
533 void lcdSendComm(uint8_t _comm) {
534 //Clear RS & EN
535 GPIOB->ODR &= ~( LCD_RS_Pin | LCD_EN_Pin);
536
537 //Clear Data Pins
538 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
539
540 //High-Side
541 GPIOB->ODR |= (_comm & 0x00F0) << 8;
542
543 lcdENPulse();
544
545 //Clear Data Pins
546 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
547
548 //Low-Side
549 GPIOB->ODR |= (_comm & 0x000F) << 12;
550
551 lcdENPulse();
552
553 //Clear Data Bits
554 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
555
556 //Delay to Process everything
557 HAL_Delay(DEL);
558 }
559
560 void lcdGoToxy(uint8_t x, uint8_t y) {
561 unsigned char dir = 0x00;
562 if (y == 1) { //Caso para la primera línea [0x80]
563 dir |= 0x80;//No se necesita realizar nada más que levantar el bit del comando para
mover el cursor
564 } else if (y == 2) { //Caso para la segunda línea [0x80 + 0x60]
565 dir |= 0xC0;//Se levanta el bit del comando para mover el cursor y el bit para la
segunda línea
566 }
567 if (x > 0 && x <= 0x3F) {//Se tienen del 0x00 al 0xBF[63] localidades de memoria por cada
línea [64 en total]

Page 10
main.c

568 dir |= x - 0x01;//Dado que se toman del 1 al 63, restamos 1 así el 3, por ejemplo,
(0b011) sería 2 (0b010)
569 }
570 lcdSendComm(dir);
571 }
572
573 void lcdSendChar(uint8_t _char) {
574 //Set RS for Char send
575 GPIOB->ODR |= LCD_RS_Pin;
576 //Clear EN
577 GPIOB->ODR |= LCD_EN_Pin;
578
579 //Clear Data Pins
580 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
581 //High Side First
582 GPIOB->ODR |= (_char & 0x00F0) << 8;
583
584 lcdENPulse();
585
586 //Clear Data Pins
587 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
588
589 //Low Side Second
590 GPIOB->ODR |= (_char & 0x000F) << 12;
591
592 lcdENPulse();
593
594 //Clear Data Pins
595 GPIOB->ODR &= ~( LCD_D7_Pin | LCD_D6_Pin | LCD_D5_Pin | LCD_D4_Pin);
596
597 //Delay To process everything
598 HAL_Delay(DEL);
599 }
600
601 void lcdClearScreen() {
602 lcdSendComm(LCDCom_clear);
603 }
604
605 void lcdPrintS(char *_str) {
606 uint8_t in = 0; //Counter
607 for (in = 0; in < strlen(_str); in++) {
608 lcdSendChar(_str[in]);
609 if (in == 15) {
610 lcdGoToxy(0, 2);
611 }
612 }
613 }
614
615 void lcdPrintF(const char *fmt, ...) {
616 char tmp[32] =
617 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
618 va_list args; // Argument List
619 va_start(args, fmt);
620 //Write in tmp, with MAX_CADENA size the fmt format with 'args' as arugments
621 vsnprintf(tmp, 32, fmt, args);
622 va_end(args);
623 lcdPrintS(tmp);

Page 11
main.c

624 }
625
626 void lcdENPulse() {
627 //EN Pulse
628 GPIOB->ODR |= LCD_EN_Pin;
629 HAL_Delay(1);
630 GPIOB->ODR &= ~LCD_EN_Pin;
631 }
632
633 void resistorSet(uint _setRes) {
634 GPIOA->ODR &= ~(OUT_R2_Pin | OUT_R1_Pin | OUT_R0_Pin);
635 switch (_setRes) {
636 case 0:
637 //R2 = 0; R1 = 0; R0 = 0;
638 break;
639 case 1:
640 //R2 = 0; R1 = 0; R0 = 1;
641 GPIOA->ODR |= (OUT_R0_Pin);
642 break;
643 case 2:
644 //R2 = 0; R1 = 1; R0 = 0;
645 GPIOA->ODR |= (OUT_R1_Pin);
646 break;
647 case 3:
648 //R2 = 0; R1 = 1; R0: = 1;
649 GPIOA->ODR |= (OUT_R1_Pin | OUT_R0_Pin);
650 break;
651 case 4:
652 //R2 = 1; R1 = 0; R0 = 0;
653 GPIOA->ODR |= (OUT_R2_Pin);
654 break;
655 case 5:
656 //R2 = 1; R1 = 0; R0 = 1;
657 GPIOA->ODR |= (OUT_R2_Pin | OUT_R0_Pin);
658 break;
659 case 6:
660 //R2 = 1; R1 = 1; R0 = 0;
661 GPIOA->ODR |= (OUT_R2_Pin | OUT_R1_Pin);
662 break;
663 case 7:
664 //R2 = 1; R1 = 1; R0 = 1;
665 GPIOA->ODR |= (OUT_R2_Pin | OUT_R1_Pin | OUT_R0_Pin);
666 break;
667 }
668 }
669
670 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
671 GPIOA->ODR &= ~(OUT_F0_Pin | OUT_F1_Pin); //Clear All Pins
672 switch (GPIO_Pin) {
673 case IN_100_Pin:
674 // F1 = 0; F0 = 0;
675 frecSelect = 0;
676 break;
677 case IN_1k_Pin:
678 frecSelect = 1;
679 //F1 = 0; F0 = 1;
680 GPIOA->ODR |= OUT_F0_Pin;

Page 12
main.c

681 break;
682 case IN_10k_Pin:
683 frecSelect = 2;
684 //F1 = 1; F0 = 0;
685 GPIOA->ODR |= OUT_F1_Pin;
686 break;
687 case IN_100k_Pin:
688 frecSelect = 3;
689 //F1 = 0; F0 = 1;
690 GPIOA->ODR |= OUT_F1_Pin | OUT_F0_Pin;
691 break;
692 }
693
694 }
695
696 void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
697 //TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_1,TIM_INPUTCHANNELPOLARITY_RISING);
698
699 if (flag) {
700 timeA = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
701 flag = 0;
702 __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1,
703 TIM_INPUTCHANNELPOLARITY_FALLING);
704
705 } else {
706 timeB = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
707 flag = 1;
708 if (timeB > timeA) {
709 pulseWidth = timeB - timeA;
710 pulseWidth = pulseWidth / 72.0; //to test
711 HAL_TIM_IC_Stop_IT(&htim2, TIM_CHANNEL_1);
712 }
713
714 __HAL_TIM_SET_COUNTER(htim, 0);
715 __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_4,
716 TIM_INPUTCHANNELPOLARITY_RISING);
717 }
718 }
719
720 /* USER CODE END 4 */
721
722 /**
723 * @brief This function is executed in case of error occurrence.
724 * @retval None
725 */
726 void Error_Handler(void) {
727 /* USER CODE BEGIN Error_Handler_Debug */
728 /* User can add his own implementation to report the HAL error return state */
729
730 /* USER CODE END Error_Handler_Debug */
731 }
732
733 #ifdef USE_FULL_ASSERT
734 /**
735 * @brief Reports the name of the source file and the source line number
736 * where the assert_param error has occurred.
737 * @param file: pointer to the source file name

Page 13
main.c

738 * @param line: assert_param error line source number


739 * @retval None
740 */
741 void assert_failed(uint8_t *file, uint32_t line)
742 {
743 /* USER CODE BEGIN 6 */
744 /* User can add his own implementation to report the file name and line number,
745 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
746 /* USER CODE END 6 */
747 }
748 #endif /* USE_FULL_ASSERT */
749
750 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
751

Page 14

You might also like