STM32CubeMX learning - (2) Timer

The timer interrupt function

Timer Configuration

  • Select TIM6 in Timers, check Activated.
  • Set parameters:
    the Prescaler = 83, the division ratio, the frequency count = APB1 / psc.
    Mode = the UP Counter,
    Counter = Period 499, auto-reload value.
    = PRELOAD reload the Enable-Auto,
    the Trigger the Event Selection = the Reset.
  • Enable interrupt vector table
    check Enable NVIC Settings in.
  • Modify the program
    click the generated code, modify the code. Timer configuration has been configured, simply run this program to open a timer.
HAL_TIM_Base_Start_IT(&htim6);

The foregoing is provided for the timing frequency is calculated into the interrupt 0.5ms. Specific interrupt handler as follows:

//回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim==&htim6)
	{
		/*********APP********/
	}
}

So far timer interrupt function is complete.

Timer PWM Output Configuration

  • Timers selected in TIM3, and the Channel1 Channel2 into a PWM Generation CHx mode.
  • Parameter Settings set parameters:
    the Prescaler = 83, the division ratio, the frequency count = APB1 / psc.
    Mode = the UP Counter,
    Counter = Period 1000, auto-reload value.
    Master / = the Disable the Slave Mode,
    Auto-reload the Enable = PRELOAD,
    the Trigger the Event Selection = the Reset,
    the PWM Channel Generation X set:
    Mode = the PWM MODE. 1
    Pulse 0 =
    Fast Mode the Disable =
    CH Polarity = High, the output level of the polarity.
  • Modify the code necessary to add the following code in the code, the PWM output opening,
	HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_2);

Add the following code to set the duty ratio = 700/1000 = 70%.

__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,700);
__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,700);

Thus, to the corresponding output pin PWM waveform.

Released six original articles · won praise 1 · views 139

Guess you like

Origin blog.csdn.net/weixin_43482414/article/details/103938834