/* 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" #include "key.h" #include "motor.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 */ void Servo_test(void); int fputc(int ch, FILE *f); int fgetc(FILE *f); void Breathing_light(void); void ADC_BEEP(void); void Key_get(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ uint32_t ADC_Value; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); MX_SPI3_Init(); MX_TIM1_Init(); MX_TIM15_Init(); MX_TIM7_Init(); /* USER CODE BEGIN 2 */ LCD_Init(); HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); HAL_UARTEx_ReceiveToIdle_IT(&huart1, (uint8_t *)rxdata, sizeof(rxdata)); HAL_ADC_Start(&hadc1); POINT_COLOR = BLUE; // ��������Ϊ��ɫ HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2); HAL_TIMEx_PWMN_Start(&htim15, TIM_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED_B_GPIO_Port, LED_B_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(BIN1_GPIO_Port,BIN1_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(BIN2_GPIO_Port, BIN2_Pin, GPIO_PIN_SET); printf("Hello!"); //__HAL_TIM_SET_COMPARE(&htim15,TIM_CHANNEL_2,5); HAL_TIM_Base_Start_IT(&htim7); while (1) { ADC_BEEP(); LCD_ShowxNum(50, 50, Servo_Speed, 4, 16, 0); LCD_ShowxNum(150, 50, Motor_Speed, 4, 16, 0); LCD_ShowxNum(50, 100, HAL_GPIO_ReadPin(PB8_EXTI_GPIO_Port,PB8_EXTI_Pin), 4, 16, 0); if(Motor_time>=0) LCD_ShowxNum(150, 100, Motor_time, 4, 16, 0); LCD_ShowxNum(50, 200, Servo_goal, 4, 16, 0); LCD_ShowxNum(50, 150, Servo_position, 4, 16, 0); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ Key_get(); // Servo_test(); //Breathing_light(); } /* 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 */ void Servo_test(void) { // cnt=(cnt+30)%(180+30); LCD_ShowxNum(50, 200, cnt, 4, 16, 0); LCD_ShowxNum(50, 150, ANGLETOCCR(cnt), 4, 16, 0); Servo_SetAngle(cnt); HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin); } 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 < Size; i++) { cnt *= 10; cnt += rxdata[i] - '0'; } Servo_goal = cnt; } HAL_UARTEx_ReceiveToIdle_IT(&huart1, (uint8_t *)rxdata, sizeof(rxdata)); } } 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); } int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; } // LED_B的呼吸灯 void Breathing_light(void) { if (!LED_B_ON) { return; } 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); } } void ADC_BEEP(void) { #ifdef ONE_ADC_BEEP ADC_Value = Get_Adc_Average(ADC_CHANNEL_3, 5); //??AD? LCD_ShowxNum(50, 100, ADC_Value, 4, 16, 0); if (ADC_Value * 3.3 / 4095 > 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 } void Key_get(void) { if (HAL_GPIO_ReadPin(KEY_LEFT_GPIO_Port, KEY_LEFT_Pin) == KEY_PRESSED) { HAL_Delay(15); LCD_ShowxNum(50, 150, Servo_position, 4, 16, 0); while (HAL_GPIO_ReadPin(KEY_LEFT_GPIO_Port, KEY_LEFT_Pin) == KEY_PRESSED) ; Servo_goal = (Servo_goal + 90) % (180 + 90); LCD_ShowxNum(50, 200, Servo_goal, 4, 16, 0); HAL_Delay(15); } if (HAL_GPIO_ReadPin(KEY_RIGHT_GPIO_Port, KEY_RIGHT_Pin) == KEY_PRESSED) { HAL_Delay(15); while (HAL_GPIO_ReadPin(KEY_RIGHT_GPIO_Port, KEY_RIGHT_Pin) == KEY_PRESSED) ; Servo_Speed = Servo_Speed % 3 + 1; LCD_ShowxNum(50, 50, Servo_Speed, 4, 16, 0); HAL_Delay(15); } if (HAL_GPIO_ReadPin(KEY_UP_GPIO_Port, KEY_UP_Pin) == KEY_UP_PRESSED) { HAL_Delay(15); while (HAL_GPIO_ReadPin(KEY_UP_GPIO_Port, KEY_UP_Pin) == KEY_UP_PRESSED); Motor_Speed = NewCCRValue(Motor_Speed,+1,20); if(Motor_Speed==1) { Motor_time = 0; Motor_start=1; } HAL_Delay(15); HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); Motor_reset_ornot(); __HAL_TIM_SET_COMPARE(&htim15,TIM_CHANNEL_1,MOTORCCR(Motor_Speed)); } if (HAL_GPIO_ReadPin(KEY_DOWN_GPIO_Port, KEY_DOWN_Pin) == KEY_PRESSED) { HAL_Delay(15); while (HAL_GPIO_ReadPin(KEY_DOWN_GPIO_Port, KEY_DOWN_Pin) == KEY_PRESSED); Motor_Speed = NewCCRValue(Motor_Speed,-1,20); HAL_Delay(15); HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin); Motor_reset_ornot(); __HAL_TIM_SET_COMPARE(&htim15,TIM_CHANNEL_1,MOTORCCR(Motor_Speed)); } } /* 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 */