318 lines
8.8 KiB
C
318 lines
8.8 KiB
C
/* 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 <stdio.h>
|
||
#include <string.h>
|
||
#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<Size;i++)
|
||
{
|
||
cnt*=10;
|
||
cnt+=rxdata[i]-'0';
|
||
}
|
||
Servo_goal=cnt;
|
||
}
|
||
HAL_UARTEx_ReceiveToIdle_IT(&huart1, (uint8_t *)rxdata, sizeof(rxdata));
|
||
}
|
||
}
|
||
/* 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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ɫ
|
||
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
|
||
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_2);
|
||
/* 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);
|
||
printf("Hello!");
|
||
//__HAL_TIM_SET_COMPARE(&htim15,TIM_CHANNEL_2,5);
|
||
HAL_TIM_Base_Start_IT(&htim7);
|
||
while (1)
|
||
{ //??ADC??
|
||
// HAL_ADC_PollForConversion(&hadc1, 50); //??????,50???????,???ms
|
||
// if (HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
|
||
// {
|
||
#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
|
||
/* 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 */
|