/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "spi.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "lcd.h" #include "sys.h" #include #include #include "servo.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ char rxdata[52]; uint16_t rxlen=52; uint8_t LED_B_ON=0,LED1_ON=0,LED2_ON=0,LED3_ON=0; int cnt = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; } int fgetc(FILE *f) { int ch; // while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) == RESET); HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return (ch); } void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { printf("rx\r\n"); if (huart == &huart1) { rxdata[Size] = '\0'; if (strcmp(rxdata, "LED1") == 0) { LED1_ON=!LED1_ON; HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin); } else if (strcmp(rxdata, "LED2") == 0) { LED2_ON=!LED2_ON; HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); } else if (strcmp(rxdata, "LED3") == 0) { LED_B_ON=!LED_B_ON; LED3_ON=!LED3_ON; } else { cnt=0; for(int i=0;i 1.7) { cnt++; } else { cnt = 0; } if (cnt == 50) { HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); HAL_Delay(100); HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); HAL_Delay(100); HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); HAL_Delay(100); HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); HAL_Delay(100); HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); HAL_Delay(100); HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); } LCD_ShowxNum(50, 200, cnt, 4, 16, 0); HAL_GPIO_TogglePin(LED_B_GPIO_Port,LED_B_Pin); // } HAL_Delay(100); #endif /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ //cnt=(cnt+30)%(180+30); LCD_ShowxNum(50, 200, cnt, 4, 16, 0); LCD_ShowxNum(50, 150, 2000*cnt/180+500, 4, 16, 0); Servo_SetAngle(cnt); HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin); HAL_Delay(1000); if(!LED_B_ON) { continue; } for(int i=0;i<=100;i++) { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,100-i); HAL_Delay(10); } for(int i=0;i<=100;i++) { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,i); HAL_Delay(10); } //HAL_Delay(500); } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 18; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */