计时器函数: #include "sys.h" #include "timer.h" #include "usart.h"
void TIM3_Cap_Init(uint16_t arr, uint16_tpsc) //TIM3用于输入捕获 { GPIO_InitTypeDefGPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM3_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能TIM3时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //选择6号引脚 GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPD; //输入下拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA6 GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA6下拉
//初始化定时器3 TIM3 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(TIM3,&TIM_TimeBaseStructure);
//初始化TIM3输入捕获参数 TIM3_ICInitStructure.TIM_Channel= TIM_Channel_1; //CC1S=01选择输入端 IC1映射到TI1上 TIM3_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising; //上升沿捕获 TIM3_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI; //映射到TI1上 TIM3_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM3_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 配置输入滤波器 不滤波 TIM_ICInit(TIM3,&TIM3_ICInitStructure);
//中断分组初始化 NVIC_InitStructure.NVIC_IRQChannel= TIM3_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0; //从优先级0级 NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM3,ENABLE ); //使能定时器3 }
uint8_t TIM3CH1_CAPTURE_STA; //输入捕获状态 uint16_t counter = 0; //记录捕获上升沿次数 uint8_t sec_cnt = 0; //毫秒计时 uint8_t flag = 0; //计时1s完成标志 uint32_t rising_time; //高电平时间 uint32_t falling_time; //低电平时间
//定时器3中断服务程序 void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) //发生捕获事件 { if(TIM3CH1_CAPTURE_STA& 0x80) //捕获下降沿(已经捕获到上升沿) { rising_time= TIM_GetCapture1(TIM3); //读取高电平时间(us)
TIM3CH1_CAPTURE_STA= 0; //清空 TIM_SetCounter(TIM3,0); TIM3CH1_CAPTURE_STA|= 0x40; //标记捕获到了下降沿 TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //设置为上升沿触发 } else //捕获上升沿 { falling_time= TIM_GetCapture1(TIM3); //低电平时间
TIM3CH1_CAPTURE_STA= 0; //清零 TIM_SetCounter(TIM3,0); TIM3CH1_CAPTURE_STA|=0X80; //标记捕获到了上升沿 TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //开始捕获下降沿 counter++; //频率计数加一 } }
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1); //清除中断标志位 }
void TIM4_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDefNVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period= arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler=psc; //设置用来作为TIMx时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision= 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel= TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1; //先占优先级1级 NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0; //从优先级0级 NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM4,ENABLE);
}
void TIM4_IRQHandler(void) //TIM3中断,10000us中断一次 { if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { sec_cnt++; if(sec_cnt== 100) //计时满1秒 { sec_cnt= 0; flag= 1; } TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源 } } LED初始化:#include "led.h" #include "sys.h"
void LED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //使能GPIOC端口时钟
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_13; //LED-->PC13 端口配置 GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; //IO口速度为50MHz GPIO_Init(GPIOC,&GPIO_InitStructure); //根据设定参数初始化GPIOB.5 GPIO_SetBits(GPIOC,GPIO_Pin_13); }
|