新手入门
- 积分
- 1
- 金钱
- 1
- 注册时间
- 2023-4-24
- 在线时间
- 0 小时
|
1金钱
本帖最后由 gx99 于 2023-4-24 15:26 编辑
void IC_Init(void)
{
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//配置GPIO
GPIO_InitTypeDef GPIO_Structure;
GPIO_Structure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Structure.GPIO_Pin = GPIO_Pin_6;
GPIO_Structure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_Structure);
//配置时基单元
TIM_InternalClockConfig(TIM3);
TIM_TimeBaseInitTypeDef Timer_Structure;
Timer_Structure.TIM_ClockDivision = TIM_CKD_DIV1;
Timer_Structure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
Timer_Structure.TIM_Period = 65536 - 1 ;//设置ARR的值,设置最大防止计数溢出
Timer_Structure.TIM_Prescaler = 7200 - 1;//设置预分频值,即fc
Timer_Structure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &Timer_Structure);
//配置输入捕获通道
TIM_ICInitTypeDef TIM_Structure;
TIM_Structure.TIM_Channel = TIM_Channel_1;
TIM_Structure.TIM_ICFilter = 0xF;
TIM_Structure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_Structure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_Structure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_PWMIConfig(TIM3, &TIM_Structure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);//选择触发源,TI1FP1作为触发源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//设置从模式,从模式设置为Reset
TIM_Cmd(TIM3, ENABLE);
}
uint32_t IC_GetFreq(void)
{
return 10000/(TIM_GetCapture1(TIM3) + 1);
}
uint32_t IC_GetDuty(void)
{
return (TIM_GetCapture2(TIM3) + 1) * 100 / (TIM_GetCapture1(TIM3));
}
|
|