NUCLEO-L053R8 | TIM定时器 PWM输出
TIM2 PWM输出测试
今天给大伙分享一个TIM2 PWM输出小实验。
实验开发板:Nucleo-L053R8,即STM32L053R8T6。
开发环境:MDK5
图1 - 工程界面
本次实验测试的是通用定时器TIM2,PWM输出模式。
附上代码:
bsp_tim.h
1 /** 2 ****************************************************************************** 3 * File Name : bsp_tim.h 4 * Description : This file provides code for the configuration 5 * of the TIM instances. 6 ****************************************************************************** 7 */ 8 #ifndef __BSP_TIM_H 9 #define __BSP_TIM_H 10 11 #include "stm32l0xx_hal.h" 12 13 // 使用宏定义,代码易于移植 14 15 #define TIM_CH1_PORT GPIOA 16 #define TIM_CH1_PIN GPIO_PIN_0 17 18 #define TIM_x TIM2 19 #define TIM_CHANNEL_x TIM_CHANNEL_1 20 #define TIM_CLK_ENABLE __HAL_RCC_TIM2_CLK_ENABLE() 21 22 // PWM信号的频率 F = TIM_CLK/((ARR+1)*(PSC+1)) 23 #define TIM_PSC (1-1) 24 #define TIM_PERIOD (200-1) 25 #define TIM_PULSE 100 26 27 extern void _Error_Handler(void); 28 void HAL_TIM2_Init(void); 29 30 #endif /* __BSP_TIM_H */
bsp_tim.c
1 /** 2 ****************************************************************************** 3 * File Name : bsp_tim.c 4 * Description : This file provides code for the configuration 5 * of the TIM instances. 6 ****************************************************************************** 7 */ 8 9 #include "bsp_tim.h" 10 11 12 static void HAL_TIM2_GPIO_Config(void) 13 { 14 GPIO_InitTypeDef GPIO_InitStruct; 15 16 // TIM2_PWM输出GPIO初始化 PA0 ----> TIM2_CH1 17 GPIO_InitStruct.Pin = TIM_CH1_PIN; 18 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 19 GPIO_InitStruct.Pull = GPIO_NOPULL; 20 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 21 GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; 22 HAL_GPIO_Init(TIM_CH1_PORT, &GPIO_InitStruct); 23 } 24 25 static void HAL_TIM2_Mode_Config(void) 26 { 27 TIM_HandleTypeDef TIM_HandleStruct; 28 TIM_MasterConfigTypeDef TIM_MasterStruct; 29 TIM_OC_InitTypeDef TIM_OC_InitStruct; 30 31 // 开启定时器时钟 32 TIM_CLK_ENABLE; 33 34 TIM_HandleStruct.Instance = TIM_x; 35 TIM_HandleStruct.Init.Prescaler = TIM_PSC; 36 TIM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP; 37 TIM_HandleStruct.Init.Period = TIM_PERIOD; 38 TIM_HandleStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 39 if (HAL_TIM_PWM_Init(&TIM_HandleStruct) != HAL_OK) 40 { 41 _Error_Handler(); 42 } 43 44 TIM_MasterStruct.MasterOutputTrigger = TIM_TRGO_RESET; 45 TIM_MasterStruct.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 46 if (HAL_TIMEx_MasterConfigSynchronization(&TIM_HandleStruct, &TIM_MasterStruct) != HAL_OK) 47 { 48 _Error_Handler(); 49 } 50 51 TIM_OC_InitStruct.OCMode = TIM_OCMODE_PWM1; 52 TIM_OC_InitStruct.Pulse = TIM_PULSE; 53 TIM_OC_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH; 54 TIM_OC_InitStruct.OCFastMode = TIM_OCFAST_DISABLE; 55 if (HAL_TIM_PWM_ConfigChannel(&TIM_HandleStruct, &TIM_OC_InitStruct, TIM_CHANNEL_x) != HAL_OK) 56 { 57 _Error_Handler(); 58 } 59 60 HAL_TIM_PWM_Start(&TIM_HandleStruct, TIM_CHANNEL_x); 61 } 62 63 void HAL_TIM2_Init(void) 64 { 65 HAL_TIM2_GPIO_Config(); 66 HAL_TIM2_Mode_Config(); 67 }
main.c
1 /** 2 ****************************************************************************** 3 * @file main.c 4 * @author Tuple 5 * @brief Main program body 6 ****************************************************************************** 7 */ 8 9 #include "stm32l0xx_hal.h" 10 #include "bsp_tim.h" 11 12 void _Error_Handler(void); 13 void SystemClock_Config(void); 14 15 int main(void) 16 { 17 //重置所有外围设备,初始化Flash接口和Systick 18 HAL_Init(); 19 20 //系统时钟配置 21 SystemClock_Config(); 22 23 //TIM2 PWM输出配置 24 HAL_TIM2_Init(); 25 26 } 27 28 29 void SystemClock_Config(void) 30 { 31 RCC_OscInitTypeDef RCC_OscInitStruct; 32 RCC_ClkInitTypeDef RCC_ClkInitStruct; 33 34 //Configure the main internal regulator output voltage 35 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); 36 37 //Initializes the CPU, AHB and APB busses clocks 38 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 39 RCC_OscInitStruct.HSIState = RCC_HSI_ON; 40 RCC_OscInitStruct.HSICalibrationValue = 16; 41 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 42 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; 43 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4; 44 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2; 45 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 46 { 47 _Error_Handler(); 48 } 49 50 //Initializes the CPU, AHB and APB busses clocks 51 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 52 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 53 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 54 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 55 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 56 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 57 58 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) 59 { 60 _Error_Handler(); 61 } 62 63 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); 64 65 //Configure the Systick interrupt time 66 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 67 68 //Configure the Systick 69 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 70 71 //SysTick_IRQn interrupt configuration 72 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 73 } 74 75 void _Error_Handler() 76 { 77 while(1) 78 { 79 } 80 }
如果你不清楚Nucleo板子上PA0在哪个引脚,请查阅RCC时钟配置实验的Figure 20 --> Figure 20.NUCLEO-L053R8
用两根杜邦线从Nucleo板子上引出PA0、GND,然后用数字示波器来监控PWM输出。实物连接如下:
图2 - 实物连接图
把编译好的程序下载到Nucleo开发板,PWM输出设置成不同的占空比,用示波器监控 PWM输出的波形。下面给大伙展示3组不同占空比的PWM输出波形:
图3 - 占空比25%的PWM波形
图4 - 占空比50%的PWM波形
图5 - 占空比75%的PWM波形
以上总结仅供参考,若有不对之处,敬请谅解。
未经本人同意,不得转载,谢谢合作!