1、单片机:STM32L031G6U6
2、使用IO:PA6 TIM22 CH1 PA7 TIM22 CH2
3、时钟配置:1.048M
TIM22配置频率:晶振频率1.048Mhz 预分频系数为 0,自动重装载值为 99,定时器溢出频率,即PWM的周期,就是 1.048MHz/(0+1)/(99+1) = 10.048kHz
4、问题:不能输出PWM波形,示波器测试的
使能时输出高电平,不使能时输出低电平
HAL_TIM_PWM_Start(&htim22,TIM_CHANNEL_1); //开启PWM输出
HAL_TIM_PWM_Start(&htim22,TIM_CHANNEL_2); //开启PWM输出
TIM22初始化的代码
- static void MX_TIM22_Init(void)
- {
- /* USER CODE BEGIN TIM22_Init 0 */
- __HAL_RCC_TIM22_CLK_ENABLE();
- /* USER CODE END TIM22_Init 0 */
- __HAL_RCC_TIM22_CLK_ENABLE();
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- /* USER CODE BEGIN TIM22_Init 1 */
- /* USER CODE END TIM22_Init 1 */
- htim22.Instance = TIM22;
- htim22.Init.Prescaler = 0;//晶振频率1.048Mhz 预分频系数为 0,自动重装载值为 99,定时器溢出频率,即PWM的周期,就是 1.048MHz/(0+1)/(99+1) = 10.048kHz
- htim22.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim22.Init.Period = 99;
- htim22.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim22.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;//自动重装载
- if (HAL_TIM_Base_Init(&htim22) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim22, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_Init(&htim22) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_OnePulse_Init(&htim22, TIM_OPMODE_SINGLE) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim22, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;// PWM模式1
- sConfigOC.Pulse = 60;//初始化占空比
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;//当定时器计数值小于CCR1_Val时为高电平*/
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim22, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim22, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM22_Init 2 */
- /* USER CODE END TIM22_Init 2 */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**TIM22 GPIO Configuration
- PA6 ------> TIM22_CH1
- PA7 ------> TIM22_CH2
- */
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF5_TIM22;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-
- HAL_TIM_PWM_Start(&htim22,TIM_CHANNEL_1); //开启PWM输出
- //HAL_TIM_PWM_Start(&htim22,TIM_CHANNEL_2); //开启PWM输出
- __HAL_TIM_SET_COMPARE(&htim22,TIM_CHANNEL_1,50); //设置默认的占空比值
- //__HAL_TIM_SET_COMPARE(&htim22,TIM_CHANNEL_2,50); //设置默认的占空比值
- }
复制代码 主函数调用初始化
- int main(void)
- {
- static U8 LedTimer;
- U8 temp;
- HAL_Init();
- SystemClock_Config();
- MX_ADC_Init();
- MX_GPIO_Init();
- MX_TIM2_Init();
- MX_TIM21_Init();
- MX_TIM22_Init();
复制代码 主循环中持续输出:
- while (1)
- {
- __HAL_TIM_SET_COMPARE(&htim22,TIM_CHANNEL_1,50); //设置默认的占空比值
- led_blink();
复制代码
|