引用: testd018 发表于 2019-5-14 18:54
怎么想的呀,高电平故障不是吗,那就是上升沿开始计数,下降沿复位计数,你可以设置超时中断,只要不超时(16.384Khz的半周期很容易算出来),所以只要超时了不就是有故障了吗,不过频繁中断会对cpu的消耗很大,所以要根据应用场景来看是否需要这么严格,从而设计一个比较宽裕的条件!
最靠谱和比较渐变的方法就是统计脉冲 ...
这样做:
Tim4_Init(void)
{ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB,&GPIO_InitStructure); //Max17830 Alarm
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 49999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //选择输入端 IC4映射到TI4上
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //上升沿下降沿都捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI4上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//配置输入分频,不分频
TIM_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4,TIM_IT_CC4,ENABLE);//允许CC4IE捕获中断
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//允许更新中断
TIM_ClearITPendingBit(TIM4,TIM_IT_CC4);
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
TIM_Cmd(TIM4,ENABLE);
}
中断函数
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_CC4) != RESET)
{
TIM_SetCounter(TIM4,0);
TIM_ClearITPendingBit(TIM4,TIM_IT_CC4);
}
if (TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源
}
}
main函数里判断
if(TIM_GetCounter(TIM4) >= 32) // (1/16.384KHz)/2= 30.517578125us
{
AlarmFlag = 1; // 故障标志
}
上升沿和下降沿都清零计数器,只要计数器大于32即认为有故障。但开启了溢出中断的话(TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE)),会频繁导致看门狗复位。
引用: testd018 发表于 2019-5-14 18:54
怎么想的呀,高电平故障不是吗,那就是上升沿开始计数,下降沿复位计数,你可以设置超时中断,只要不超时(16.384Khz的半周期很容易算出来),所以只要超时了不就是有故障了吗,不过频繁中断会对cpu的消耗很大,所以要根据应用场景来看是否需要这么严格,从而设计一个比较宽裕的条件!
最靠谱和比较渐变的方法就是统计脉冲 ...
这样做:
Tim4_Init(void)
{ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB,&GPIO_InitStructure); //Max17830 Alarm
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 49999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; //选择输入端 IC4映射到TI4上
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //上升沿下降沿都捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI4上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//配置输入分频,不分频
TIM_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4,TIM_IT_CC4,ENABLE);//允许CC4IE捕获中断
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//允许更新中断
TIM_ClearITPendingBit(TIM4,TIM_IT_CC4);
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
TIM_Cmd(TIM4,ENABLE);
}
中断函数
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_CC4) != RESET)
{
TIM_SetCounter(TIM4,0);
TIM_ClearITPendingBit(TIM4,TIM_IT_CC4);
}
if (TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源
}
}
main函数里判断
if(TIM_GetCounter(TIM4) >= 32) // (1/16.384KHz)/2= 30.517578125us
{
AlarmFlag = 1; // 故障标志
}
上升沿和下降沿都清零计数器,只要计数器大于32即认为有故障。但开启了溢出中断的话(TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE)),会频繁导致看门狗复位。
举报