金牌会员
- 积分
- 1887
- 金钱
- 1887
- 注册时间
- 2012-10-28
- 在线时间
- 353 小时
|
发表于 2012-11-29 10:58:37
|
显示全部楼层
回复【100楼】骤雨龙元:
---------------------------------
你好,有个问题请教你一下:
关于tim3定时器的问题,问题如下:
//初始化时间比较模式
void TIM3_Cinit(u32 TIM_scale ,u16 CCR1 ,u16 CCR2 ,u16 CCR3)//CCR为16位的数
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0 /*TIM_CKD_DIV1*/; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM3, (TIM_scale-1), TIM_PSCReloadMode_Immediate);
//通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
//通道2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
//通道3
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
//中断使能
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
TIM3_Cinit(8400,10000,5000,2500); //(100us)10Khz计数频率 三个通道频率分别为1Hz,0.5Hz,0.25Hz
void TIM3_IRQHandler(void)
{
u16 capture=0;
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
LED1 = !LED1;
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3, capture + 10000);
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
//LED2 = !LED2;
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, capture + 5000);
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
LED2 = !LED2;
capture = TIM_GetCapture3(TIM3);
TIM_SetCompare3(TIM3, capture + 2500);
}
}
上面是你的原设置,我只是改了一下led与0.25hz的,其他都没有改变,你的这个设置定时还是蛮准的,但是我改成了下面的设置,定时就不准了,不知道何故???
TIM3_Cinit(84000,1000,500,250); //(1ms)1Khz计数频率 三个通道频率分别为1Hz,0.5Hz,0.25Hz
void TIM3_IRQHandler(void)
{
u16 capture=0;
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
LED1 = !LED1; //这个按照我上面的设置,应该也是1hz闪烁一次的,但是好像是现在成了 0.5hz闪烁一次了,其他的几个也相应的变快了
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3, capture + 1000);
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
//LED2 = !LED2;
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, capture + 500);
}
else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
LED2 = !LED2;
capture = TIM_GetCapture3(TIM3);
TIM_SetCompare3(TIM3, capture + 250);
}
}
|
|