CH32V307定时器同步问题

我现在使用CH32V307RCT6,想实现TIM1和TIM2的同步计数以及pwm输出,现在正在写同步计数的部分代码,但是一直无法实现功能,特此请教。

代码如下

#include "debug.h"

void TIM1_Init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 999;
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
    TIM_Cmd(TIM1, ENABLE);
}

void TIM2_Init(void) {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 999;
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);
    TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);
    TIM_Cmd(TIM2, ENABLE);
}

int main(void) {
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(921600);
    printf("SystemClk:%d\r\n",SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );

    TIM1_Init();
    TIM2_Init();

    while (1) {
        printf("TIM1 Counter: %d, TIM2 Counter: %d\n", TIM_GetCounter(TIM1), TIM_GetCounter(TIM2));
        Delay_Ms(100);
    }
}

这样打印出来的counter值看起来差距很大,应该的没同步上,我应该如何修改代码。

[2024/07/11 10:27:51.797] → TIM1 Counter: 681, TIM2 Counter: 1?
TIM1 Counter: 531, TIM2 Counter: 2?

[2024/07/11 10:27:51.906] → TIM1 Counter: 381, TIM2 Counter: 2?

[2024/07/11 10:27:51.999] → TIM1 Counter: 231, TIM2 Counter: 2?

[2024/07/11 10:27:52.109] → TIM1 Counter: 81, TIM2 Counter: 2?

[2024/07/11 10:27:52.203] → TIM1 Counter: 907, TIM2 Counter: 2?

[2024/07/11 10:27:52.312] → TIM1 Counter: 757, TIM2 Counter: 2?

[2024/07/11 10:27:52.404] → TIM1 Counter: 607, TIM2 Counter: 2?

[2024/07/11 10:27:52.512] → TIM1 Counter: 457, TIM2 Counter: 2?

[2024/07/11 10:27:52.608] → TIM1 Counter: 307, TIM2 Counter: 3?

[2024/07/11 10:27:52.700] → TIM1 Counter: 157, TIM2 Counter: 3?

[2024/07/11 10:27:52.808] → TIM1 Counter: 7, TIM2 Counter: 3?

[2024/07/11 10:27:52.903] → TIM1 Counter: 807, TIM2 Counter: 3?

[2024/07/11 10:27:53.013] → TIM1 Counter: 657, TIM2 Counter: 3?

[2024/07/11 10:27:53.104] → TIM1 Counter: 507, TIM2 Counter: 3?

[2024/07/11 10:27:53.213] → TIM1 Counter: 357, TIM2 Counter: 3?

[2024/07/11 10:27:53.306] → TIM1 Counter: 207, TIM2 Counter: 3?

[2024/07/11 10:27:53.415] → TIM1 Counter: 57, TIM2 Counter: 4?

[2024/07/11 10:27:53.509] → TIM1 Counter: 883, TIM2 Counter: 4?

[2024/07/11 10:27:53.618] → TIM1 Counter: 733, TIM2 Counter: 4?

[2024/07/11 10:27:53.712] → TIM1 Counter: 583, TIM2 Counter: 4?

[2024/07/11 10:27:53.806] → TIM1 Counter: 433, TIM2 Counter: 4?

[2024/07/11 10:27:53.916] → TIM1 Counter: 283, TIM2 Counter: 4?

[2024/07/11 10:27:54.008] → TIM1 Counter: 133, TIM2 Counter: 4?

[2024/07/11 10:27:54.117] → TIM1 Counter: 983, TIM2 Counter: 4?

[2024/07/11 10:27:54.210] → TIM1 Counter: 833, TIM2 Counter: 5?


您好,定时器有一种模式-同步模式,可以用外部触发同步启动两个定时器,如下图,图中不一致是因为打印先后问题,实际计数器值是一样的。附件为参考例程,可以参考一下。

icon_rar.gifCH32V307_TIM同步模式.zip

image.png


好的,感谢,我稍微改了一下代码可以实现软件使能触发了。

void ExtTrigger_Start_Two_TIM( u16 arr, u16 psc)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );

    TIM_CounterModeConfig( TIM1, TIM_CounterMode_Up );

    TIM_TimeBaseInitStructure.TIM_Period = arr;
    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter =  0x00;
    TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
    TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure);

    TIM_SelectOutputTrigger( TIM1, TIM_TRGOSource_Enable );

    TIM_SelectInputTrigger( TIM2, TIM_TS_ITR0 );
    TIM_SelectSlaveMode( TIM2, TIM_SlaveMode_Trigger );

    TIM_Cmd(TIM1, ENABLE);
}

还想请问下,实现定时器同步后,如何实现对tim1的ch1-4和tim2的ch1-3,同时输出同占空比pwm波?感谢!


您好,输出同占空比的PWM波形,按照正常的PWM输出配置即可,注意TIM1的通道1用于触发输入,不能再用于PWM输出,可用其他通道。


只有登录才能回复,可以选择微信账号登录