摩尔芯闻 > 行业新闻 > 半导体 > stm32使用PWM波中断的方式精确的控制波的个数

stm32使用PWM波中断的方式精确的控制波的个数

eefocus ·2018-12-26 11:42·电子工程世界
阅读:2770

1,假如想要得到如下的波形:

每一个波的周期为25us,一组波的频率为10HZ

 

实现思路:

 

实现的时候使用TIM1的CH1和TIM2的CH1,使用TIM15的 中断 ,每100个ms来一次中断,在中断服务函数中以中断的方式打开 PWM波 的输出,在输出一个PWM波之后会进入PWM的中断服务函数,在中断服务函数中计数PWM波的个数,当达到8个的时候关闭PWM波的中断输出。

 

中断优先级的安排:

 

TIM15   1    0

 

TIM1     2    0

 

TIM2     3    0

 

2,在CUBE中的配置

 

TIM15的配置:

 

 

TIM1的配置:

TIM2的配置:

3,代码实现

 

/**

******************************************************************************

* File Name          : main.c

* Description        : Main program body

******************************************************************************

*

* COPYRIGHT(c) 2017 STMicroelectronics

*

* Redistribution and use in source and binary forms, with or without modification,

* are permitted provided that the following conditions are met:

*   1. Redistributions of source code must retain the above copyright notice,

*      this list of conditions and the following disclaimer.

*   2. Redistributions in binary form must reproduce the above copyright notice,

*      this list of conditions and the following disclaimer in the documentation

*      and/or other materials provided with the distribution.

*   3. Neither the name of STMicroelectronics nor the names of its contributors

*      may be used to endorse or promote products derived from this software

*      without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*

******************************************************************************

*/

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include " stm32 f0xx_hal.h"

 

/* USER CODE BEGIN Includes */

 

/* USER CODE END Includes */

 

/* Private variables ---------------------------------------------------------*/

TIM_HandleTypeDef htim1;

TIM_HandleTypeDef htim2;

TIM_HandleTypeDef htim15;

 

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

 

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_TIM1_Init(void);

static void MX_TIM2_Init(void);

static void MX_TIM15_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

 

 

 

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

 

/* USER CODE END PFP */

 

/* USER CODE BEGIN 0 */

 

/* USER CODE END 0 */

 

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();

 

/* Configure the system clock */

SystemClock_Config();

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_TIM1_Init();

MX_TIM2_Init();

MX_TIM15_Init();

 

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

 

}

/* USER CODE END 3 */

 

}

 

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

 

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 16;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;

RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

 

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK"RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

{

Error_Handler();

}

 

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

 

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

 

/* TIM1 init function */

static void MX_TIM1_Init(void)

{

 

TIM_MasterConfigTypeDef sMasterConfig;

TIM_OC_InitTypeDef sConfigOC;

TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

 

htim1.Instance = TIM1;

htim1.Init.Prescaler = 48-1;

htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

htim1.Init.Period = 25-1;

htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim1.Init.RepetitionCounter = 0;

htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)

{

Error_Handler();

}

 

sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

 

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 12;

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_PWM_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();

}

 

HAL_TIM_MspPostInit(&htim1);

 

HAL_NVIC_SetPriority(TIM1_CC_IRQn, 2, 0);

HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);

 

}

 

/* TIM2 init function */

static void MX_TIM2_Init(void)

{

 

TIM_MasterConfigTypeDef sMasterConfig;

TIM_OC_InitTypeDef sConfigOC;

 

htim2.Instance = TIM2;

htim2.Init.Prescaler = 48-1;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 25-1;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)

{

Error_Handler();

}

 

sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

 

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 12;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

 

HAL_TIM_MspPostInit(&htim2);

 

HAL_NVIC_SetPriority(T

分享到:
微信 新浪微博 QQ空间 LinkedIn

上一篇:5G时代,联发科能否用AI翻身?

下一篇:广汽集团拥有四大领先优势 着力打造世界级汽车硅谷

打开摩尔直播,更多新闻内容
半导体大咖直播分享高清观看
立即下载