|
|
|
|
/* USER CODE BEGIN Header */
|
|
|
|
|
/**
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* @file : main.c
|
|
|
|
|
* @brief : Main program body
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* @attention
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2023 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"
|
|
|
|
|
|
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
|
|
|
/* USER CODE BEGIN Includes */
|
|
|
|
|
#include "stm_gen.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 ---------------------------------------------------------*/
|
|
|
|
|
SPI_HandleTypeDef hspi1;
|
|
|
|
|
|
|
|
|
|
TIM_HandleTypeDef htim1;
|
|
|
|
|
TIM_HandleTypeDef htim2;
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
|
|
int channel = 1;
|
|
|
|
|
int flag = 0;
|
|
|
|
|
/* USER CODE END PV */
|
|
|
|
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
|
void SystemClock_Config(void);
|
|
|
|
|
static void MX_GPIO_Init(void);
|
|
|
|
|
static void MX_SPI1_Init(void);
|
|
|
|
|
static void MX_TIM2_Init(void);
|
|
|
|
|
static void MX_TIM1_Init(void);
|
|
|
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
|
/* 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 */
|
|
|
|
|
/* 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_SPI1_Init();
|
|
|
|
|
MX_TIM2_Init();
|
|
|
|
|
MX_TIM1_Init();
|
|
|
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
|
struct mode modes[CHANNELS];
|
|
|
|
|
//----------------КАНАЛ 1---------------
|
|
|
|
|
modes[0].time_mode = 5; // время работы в данном режиме, секунды (задается)
|
|
|
|
|
modes[0].pwm_value = 35000; // скважность для данного режими (задается)
|
|
|
|
|
modes[0].f = 40; // частота импульсной последовательности, Гц (задается)
|
|
|
|
|
|
|
|
|
|
//----------------КАНАЛ 2---------------
|
|
|
|
|
modes[1].time_mode = 10;
|
|
|
|
|
modes[1].pwm_value = 35000;
|
|
|
|
|
modes[1].f = 40;
|
|
|
|
|
|
|
|
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET);
|
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
|
|
|
|
|
|
/* Infinite loop */
|
|
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* USER CODE END WHILE */
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
|
|
|
if (modes[0].time_mode >= 0 && modes[0].pwm_value > 0 && modes[0].f > 0) { // проверяем введенные данные
|
|
|
|
|
uint8_t T = 1000 / modes[0].f; // период следования импульсов
|
|
|
|
|
|
|
|
|
|
// Считаем, какая частота таймера нужна для получения заданного периода T
|
|
|
|
|
uint32_t freq_T_check = (F_CPU * T) / 1000;
|
|
|
|
|
|
|
|
|
|
// Если частота больше максимально допустимой 65536 - добавляем предделитель
|
|
|
|
|
if (freq_T_check >= MAX_PWM_FREQ) {
|
|
|
|
|
// Делим полученную выше частоту на максимально допустимую (65536) - получаем значение предделителя
|
|
|
|
|
modes[0].coef = freq_T_check / MAX_PWM_FREQ;
|
|
|
|
|
|
|
|
|
|
// Округляем предделитель в большую сторону для запаса, если необходимо
|
|
|
|
|
if (freq_T_check % MAX_PWM_FREQ != 0) {
|
|
|
|
|
modes[0].coef++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Частота от процессора после прохождения предделителя
|
|
|
|
|
int F_tmp = F_CPU / modes[0].coef;
|
|
|
|
|
|
|
|
|
|
// Считаем частоту таймера нужна для получения заданного периода T (с новым значением частоты процессора)
|
|
|
|
|
modes[0].freq_pwm_new = (F_tmp * T) / 1000;
|
|
|
|
|
|
|
|
|
|
// Если все успешно - получим значение меньше 65536
|
|
|
|
|
if (modes[0].freq_pwm_new <= MAX_PWM_FREQ && channel == 1 && flag == 0) {
|
|
|
|
|
modes[0].pwm_value_res = (modes[0].pwm_value * modes[0].freq_pwm_new) / MAX_PWM_FREQ;
|
|
|
|
|
PWM_Init(modes[0].coef-1, modes[0].freq_pwm_new-1, modes[0].pwm_value_res);
|
|
|
|
|
|
|
|
|
|
uint16_t time_to_tick = modes[0].time_mode * F_CPU_TIM1; // время * 366 (24 000 000 / 65 536)
|
|
|
|
|
|
|
|
|
|
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // запуск PWM на таймер 2 канал 1
|
|
|
|
|
|
|
|
|
|
__HAL_TIM_SET_AUTORELOAD(&htim1, time_to_tick); // перенастройка period под заданное пользователем время
|
|
|
|
|
|
|
|
|
|
__HAL_TIM_SET_COUNTER(&htim1, time_to_tick - 1); // начинаем счет не с 0, а с (max - 1) значения - избавляемся от прерывания сразу после старта
|
|
|
|
|
HAL_TIM_Base_Start_IT(&htim1); // запускает таймер 1 для прерываний
|
|
|
|
|
|
|
|
|
|
flag = 1;
|
|
|
|
|
} else if (channel == 2) {
|
|
|
|
|
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1); // запуск PWM на таймер 2 канал 1
|
|
|
|
|
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
|
|
|
|
|
}
|
|
|
|
|
else if(channel == 1 && flag == 1){
|
|
|
|
|
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 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
|
|
|
|
|
*/
|
|
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
|
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
|
|
|
|
|
|
/** 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_BYPASS;
|
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
|
|
|
|
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_HSE;
|
|
|
|
|
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_0) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief SPI1 Initialization Function
|
|
|
|
|
* @param None
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
static void MX_SPI1_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN SPI1_Init 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END SPI1_Init 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN SPI1_Init 1 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END SPI1_Init 1 */
|
|
|
|
|
/* SPI1 parameter configuration*/
|
|
|
|
|
hspi1.Instance = SPI1;
|
|
|
|
|
hspi1.Init.Mode = SPI_MODE_SLAVE;
|
|
|
|
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
|
|
|
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
|
|
|
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
|
|
|
|
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
|
|
|
|
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
|
|
|
|
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
|
|
|
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
|
|
|
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
|
|
|
hspi1.Init.CRCPolynomial = 10;
|
|
|
|
|
if (HAL_SPI_Init(&hspi1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE BEGIN SPI1_Init 2 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END SPI1_Init 2 */
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief TIM1 Initialization Function
|
|
|
|
|
* @param None
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
static void MX_TIM1_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM1_Init 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 0 */
|
|
|
|
|
|
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
|
TIM_OC_InitTypeDef sConfigOC = {0};
|
|
|
|
|
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM1_Init 1 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 1 */
|
|
|
|
|
htim1.Instance = TIM1;
|
|
|
|
|
htim1.Init.Prescaler = 65535;
|
|
|
|
|
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
|
htim1.Init.Period = 1830;
|
|
|
|
|
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
|
htim1.Init.RepetitionCounter = 0;
|
|
|
|
|
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
|
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
|
|
|
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
|
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sConfigOC.OCMode = TIM_OCMODE_TIMING;
|
|
|
|
|
sConfigOC.Pulse = 0;
|
|
|
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
|
|
|
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
|
|
|
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
|
|
|
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
|
|
|
|
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
|
|
|
|
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
|
|
|
|
|
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
|
|
|
|
|
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
|
|
|
|
|
sBreakDeadTimeConfig.DeadTime = 0;
|
|
|
|
|
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
|
|
|
|
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
|
|
|
|
|
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
|
|
|
|
|
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE BEGIN TIM1_Init 2 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 2 */
|
|
|
|
|
HAL_TIM_MspPostInit(&htim1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief TIM2 Initialization Function
|
|
|
|
|
* @param None
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
static void MX_TIM2_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 0 */
|
|
|
|
|
/* USER CODE END TIM2_Init 0 */
|
|
|
|
|
|
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
|
TIM_OC_InitTypeDef sConfigOC = {0};
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 1 */
|
|
|
|
|
/* USER CODE END TIM2_Init 1 */
|
|
|
|
|
htim2.Instance = TIM2;
|
|
|
|
|
htim2.Init.Prescaler = 0;
|
|
|
|
|
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
|
htim2.Init.Period = 65535;
|
|
|
|
|
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
|
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
|
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
|
|
|
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
|
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
|
|
|
|
sConfigOC.Pulse = 10000;
|
|
|
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
|
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 2 */
|
|
|
|
|
/* USER CODE END TIM2_Init 2 */
|
|
|
|
|
HAL_TIM_MspPostInit(&htim2);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief GPIO Initialization Function
|
|
|
|
|
* @param None
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
static void MX_GPIO_Init(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
|
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
|
|
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
|
|
|
__HAL_RCC_GPIOH_CLK_ENABLE();
|
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
|
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
|
|
|
|
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
|
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET);
|
|
|
|
|
|
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
|
|
|
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_3, GPIO_PIN_RESET);
|
|
|
|
|
|
|
|
|
|
/*Configure GPIO pin : PC1 */
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
|
/*Configure GPIO pin : PA1 */
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
|
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
|
/*Configure GPIO pin : PA3 */
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_3;
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
|
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
|
void PWM_Init(uint8_t prescaler, uint16_t period, uint16_t pwm_value) {
|
|
|
|
|
|
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
|
TIM_OC_InitTypeDef sConfigOC = {0};
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 1 */
|
|
|
|
|
/* USER CODE END TIM2_Init 1 */
|
|
|
|
|
htim2.Instance = TIM2;
|
|
|
|
|
htim2.Init.Prescaler = prescaler;
|
|
|
|
|
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
|
htim2.Init.Period = period;
|
|
|
|
|
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
|
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
|
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
|
|
|
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
|
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
|
|
|
|
sConfigOC.Pulse = pwm_value;
|
|
|
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
|
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 2 */
|
|
|
|
|
//TIM2->CCR1 = pwmValue;
|
|
|
|
|
/* USER CODE END TIM2_Init 2 */
|
|
|
|
|
HAL_TIM_MspPostInit(&htim2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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 */
|