本帖最后由 一路向北lm 于 2022-3-8 21:44 编辑
杂谈5 定时器来输出一个PWM吧!
上次我们验证了定时器中断功能,这次我们来验证定时器的PWM输出功能,同样写了一个定时器定时器输出PWM的例子,这次我用Timer3吧,省得大家又说我瞎操作哈! - int main(void)
- {
- system_clock_config();
- /* peripheral clocks configuration */
- crm_configuration();
- /* gpio configuration */
- gpio_configuration();
- /* compute the prescaler value */
- prescaler_value = (uint16_t)(system_core_clock / 96000000) - 1;
- /* tmr3 time base configuration */
- tmr_base_init(TMR3, 500, prescaler_value);
- tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
- tmr_clock_source_div_set(TMR3, TMR_CLOCK_DIV1);
- tmr_output_default_para_init(&tmr_oc_init_structure);
- tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
- tmr_oc_init_structure.oc_idle_state = FALSE;
- tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
- tmr_oc_init_structure.oc_output_state = TRUE;
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_1, &tmr_oc_init_structure);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, c1dt_val);
- tmr_output_channel_buffer_enable(TMR3, TMR_SELECT_CHANNEL_1, TRUE);
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_2, &tmr_oc_init_structure);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, c2dt_val);
- tmr_output_channel_buffer_enable(TMR3, TMR_SELECT_CHANNEL_2, TRUE);
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_3, &tmr_oc_init_structure);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_3, c3dt_val);
- tmr_output_channel_buffer_enable(TMR3, TMR_SELECT_CHANNEL_3, TRUE);
- tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_4, &tmr_oc_init_structure);
- tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_4, c4dt_val);
- tmr_output_channel_buffer_enable(TMR3, TMR_SELECT_CHANNEL_4, TRUE);
- tmr_period_buffer_enable(TMR3, TRUE);
- /* tmr enable counter */
- tmr_counter_enable(TMR3, TRUE);
- while(1)
- {
- }
- }
复制代码在PA6端子上输出了 192k 的PWM波形
|