tim setcompare
时间: 2023-08-28 10:09:28 浏览: 138
TIM_SetCompare函数是用来设置TIMx的比较寄存器的值的函数。其中,TIM_SetCompareX()函数有四个,分别为TIM_SetCompare1、TIM_SetCompare2、TIM_SetCompare3、TIM_SetCompare4,对应着TIMx的通道CH1、CH2、CH3、CH4。这些函数的定义如下:
```c
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1) {
/* Check the parameters */
assert_param(IS_TIM_LIST8_PERIPH(TIMx));
/* Set the Capture Compare1 Register value */
TIMx->CCR1 = Compare1;
}
```
这个函数的作用是设置TIMx的比较寄存器的值,通过改变比较寄存器的值可以实现PWM输出。比如,TIM_SetCompare1函数就是用来设置TIMx的通道1的比较寄存器的值。[1][2][3]
相关问题
Tim setcompare
TIM_SetCompare函数用于设置TIMx的Capture Compare寄存器的值。其中,TIM_SetCompare函数有四个,分别为TIM_SetCompare1、TIM_SetCompare2、TIM_SetCompare3、TIM_SetCompare4,对应着TIM的CH1、CH2、CH3、CH4通道。当调用TIM_SetCompare1(TIMx, Compare1)函数时,TIMx为TIM的实例,x可以取1到17,并且不能为6和7;Compare1为要与TIMx进行比较的值。这个函数的作用是根据Compare1的值来确定TIMx周期的前部分和后部分的电平状态,如果前部分是高电平,则Compare1段所在时间为低电平;如果前部分是低电平,则Compare1段所在时间为高电平。
void TIM3_Int_lnit(u16 arr,u16 psc)TIM TimeBaselnitTypeDef TIM TimeBaseStructureRCC_APB1PeriphClockCmd(RCC APB1Periph_TIM3,ENABLE);TIM_TimeBaseStructure.TIM_Period = arr;TIM TimeBaseStructure.TIM Prescaler =psc;TIM_TimeBaseStructure.TIM ClockDivision = 0TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaselnit(TIM3, &TIM_TimeBaseStructure);TIM Cmd(TIM3,ENABLE);//TIM3的 PWM输出初始化void TIM3_PWM_Init(u16 arr,u16 psc)TIM_TimeBaselnitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OClnitStructure;TIM TimeBaseStructure.TIM Period = arrTIM_TimeBaseStructure.TIM_Prescaler =pscTIM TimeBaseStructure.TIM_ClockDivision = 0;TIM TimeBaseStructure.TIM CounterMode = TIM CounterMode Up;TIM_TimeBaselnit(TIM3, &TIM_TimeBaseStructure); TIM OCInitStructure.TIM OCMode = TIM OCMode PWM2; TIM OCInitStructure.TIM OutputState = TIM OutputState Enable TIM OCInitStructure.TIM OCPolarity = TIMOCPolarity_High; TIM_OC2Init(TIM3,&TIM_OCInitStructure);TIM OC2PreloadConfig(TIM3, TIM OCPreload Enable);TIM Cmd(TIM3.ENABLE): 请说明:(1) 若设置PWM 频率为10K,则TIM3 PWM nit() 如何设置形参?(2)若利用 PA8,PA9,PA10 输出频率为 10K,占空比分别为20%,35%,50%的脉冲信号(利用定时器 1),请参考 函数TIM3_PWM_Init(u16 arr,u16 psc)和 调用函数TIM SetCompare1(),TIM SetCompare2( )TIM SetCompare3()来完成编程。
(1) 若设置PWM频率为10K,则arr和psc的取值分别为:
arr = 84000000 / (10 * 1000 * psc) - 1
其中,84000000是STM32的主频,10是要求的PWM频率,1000是转换为kHz的系数,psc是预分频系数。解上式可得:
psc = 83
arr = 99
因此,TIM3_PWM_Init函数的形参应设置为:
TIM3_PWM_Init(99, 83)
(2) 利用定时器1,PA8、PA9、PA10分别对应TIM1_CH1、TIM1_CH2、TIM1_CH3,因此需要在TIM1_PWM_Init函数中设置对应通道的PWM输出。具体实现代码如下:
void TIM1_PWM_Init(u16 arr, u16 psc) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = arr * 0.2; // 20% 占空比
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = arr * 0.35; // 35% 占空比
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = arr * 0.5; // 50% 占空比
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1, ENABLE);
}
然后,在需要输出PWM信号的地方,调用TIM_SetCompare函数即可。例如,要输出20%占空比的PWM信号,可以调用:
TIM_SetCompare1(TIM1, arr * 0.2);
注意,arr和psc的取值仍然需要根据要求的PWM频率来计算。
阅读全文