Main
Main
Main
Page 1
main.c
Page 2
main.c
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
Page 7
main.c
Page 8
main.c
Page 9
main.c
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
Page 14