小学生
最后登录1970-1-1
在线时间 小时
注册时间2014-7-29
|
我写了一个用定时器3的1通道捕获一个PWM的周期,但是其得到的数据和实际相去甚远!
static void TIM4_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 100-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
......}
这是定时器4用来产生一个PWM信号来给定时器3捕获,其频率即为:(72M/100)/1000=720Hz,T=1/f=1.38ms 用示波器观察其信号没有错误。
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* éèÖÃTIM3CLK Îa 72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Config_TIM3Capture(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 5000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 719; //FreÎa20Hz
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0; // ¶ÔêäèëDÅoÅÂË2¨
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // éÏéyÑØ′¥·¢
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //¶Ôía2¿DÅoÅ·ÖÆμ
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //2»ê1óý»2æ′¥·¢
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
NVIC_Config_TIM3Capture();
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_Capture_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
这是捕获定时器3的设置。
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
if(flag == 0)
{
TIM_SetCounter(TIM3, 0);
flag=1;
}
else if(flag == 1)
{
flag=0;
printf("%d\n",TIM_GetCapture1(TIM3));
}
}
这是中断函数,按道理说第一次上升沿我将CNT置0,第二次上升沿采集到CCR的值TIM_GetCapture1(TIM3)应该等于定时器4产生的周期1.38ms,即CCR的值应该等于138,但是CCR取出来的值每次都等于88或89,为什么相差这么多呢? |
|