//TIM4_CH4用来输入捕获
void TIM4_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM4_ICInitStructure ;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9 清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB9 输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_9); //PB9 下拉
//初始化定时器4 TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM5输入捕获参数
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_4; //CC4S=01 选择输入端 IC3映射到TI3上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
//TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI ;//映射到TI3上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4,&TIM4_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC4IE捕获中断
TIM_Cmd(TIM4,ENABLE ); //使能定时器4
}
u8 TIM4CH4_CAPTURE_STA=0; //输入捕获状态
u16 TIM4CH4_CAPTURE_VAL; //输入捕获值
//定时器4中断服务程序
void TIM4_IRQHandler(void)
{
if((TIM4CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
{
if(TIM4CH4_CAPTURE_STA&0X40)//已经捕获到高电平了
{
if((TIM4CH4_CAPTURE_STA&0X3F)==0X3F)//高电平太长了
{
TIM4CH4_CAPTURE_STA|=0X80;//标记成功捕获了一次
TIM4CH4_CAPTURE_VAL=0XFFFF;
}else TIM4CH4_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)//捕获3发生捕获事件
{
if(TIM4CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM4CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次高电平脉宽
TIM4CH4_CAPTURE_VAL=TIM_GetCapture4(TIM4);
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC4P=0 设置为上升沿捕获
}else //还未开始,第一次捕获上升沿
{
TIM4CH4_CAPTURE_STA=0; //清空
TIM4CH4_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC4P=1 设置为下降沿捕获
}
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
}
extern u8 TIM5CH1_CAPTURE_STA; //输入捕获状态
extern u16 TIM5CH1_CAPTURE_VAL; //输入捕获值
int main(void)
{
u32 temp=0;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
TIM3_PWM_Init(899,0); //不分频。PWM频率=72000/(899+1)=80Khz
TIM5_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数
while(1)
{
delay_ms(10);
TIM_SetCompare2(TIM3,TIM_GetCapture2(TIM3)+1);
if(TIM_GetCapture2(TIM3)==300)TIM_SetCompare2(TIM3,0);
if(TIM5CH1_CAPTURE_STA&0X80)//成功捕获到了一次上升沿
{
temp=TIM5CH1_CAPTURE_STA&0X3F;
temp*=65536;//溢出时间总和
temp+=TIM5CH1_CAPTURE_VAL;//得到总的高电平时间
printf("HIGH:%d us
",temp);//打印总的高点平时间
TIM5CH1_CAPTURE_STA=0;//开启下一次捕获
}
}
}
原封不动修改的原子哥的代码啊.
将PA0(按键开关)连接到PB9,按键没有输出.按键用电压表测过,正常输出.
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>