新手上路
- 积分
- 37
- 金钱
- 37
- 注册时间
- 2020-5-10
- 在线时间
- 7 小时
|
5金钱
使用的定时器3和定时器4的ETR引脚,定时器6开250ms中断,中断里读定时器3和4的计数值并清零,乘以4得到频率。
问题:不准,500HZ得到的有580、560等等,示波器开着波形很好是500HZ。
- static void mTIM3_ETR_cfg(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //PORTD 时钟使能
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3 时钟使能
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //GPIOD2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度 100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //外部已有100K上拉
- GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化 PD2
-
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3); //PD2 复用为定时器3
-
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //自动重装载值
- TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //定时器分频
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
-
- TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0);
- TIM_SetCounter(TIM3, 0);
- TIM_Cmd(TIM3, ENABLE); //使能定时器
- }
- static void mTIM4_ETR_cfg(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //PORTE 时钟使能
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4 时钟使能
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOE0
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度 100MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //外部已有100K上拉
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化 PE0
-
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_TIM4); //PE0 复用为定时器4
-
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //自动重装载值
- TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //定时器分频
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0);
- TIM_SetCounter(TIM4, 0);
- TIM_Cmd(TIM4, ENABLE); //使能定时器
- }
- void mTIM6_Config(u16 TIM_period, u16 TIM_prescaler, u8 TIM_clockDivision)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
- TIM_TimeBaseInitStructure.TIM_Period = TIM_period; //自动重装载值
- TIM_TimeBaseInitStructure.TIM_Prescaler = TIM_prescaler; //定时器分频
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_clockDivision;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStructure);
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
- TIM_Cmd(TIM6, ENABLE); //使能TIMx外设
- TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //使能指定的TIM中断
- }
- static u32 pulse1_250ms_Cnt = 0, pulse2_250ms_Cnt = 0;
- void TIM6_DAC_IRQHandler(void) //定时器6中断函数,不能重命名
- {
- if(TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //清中断标志
- pulse1_250ms_Cnt = TIM3->CNT;
- pulse2_250ms_Cnt = TIM4->CNT;
- TIM3->CNT = 0;
- TIM4->CNT = 0;
- }
- }
- void mDIN_pulseMeasInit(void)
- {
- mTIM3_ETR_cfg();
- mTIM4_ETR_cfg();
- mTIM6_Config(2500 - 1, 8400 - 1, 0); //250ms中断一次(1000/250*65535>200000)
- }
- void mDIN_pulseGetFre(u32 fre[2])
- {
- fre[0] = pulse1_250ms_Cnt * 4; //转为hz
- fre[1] = pulse2_250ms_Cnt * 4;
- }
复制代码
|
|