12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #include "timer.h"
- volatile uint32_t delay_counter = 0; // 延时计数器
- void timer5_init(){
- rcu_periph_clock_enable(RCU_TIMER5); // 使能Timer5时钟
- timer_parameter_struct timer_init_struct;
- timer_deinit(TIMER5); // 复位Timer5配置
- timer_struct_para_init(&timer_init_struct);
- timer_init_struct.prescaler = 119; // 定时器时钟为120MHz
- timer_init_struct.alignedmode = TIMER_COUNTER_EDGE; // 边沿对齐模式
- timer_init_struct.counterdirection = TIMER_COUNTER_UP; // 向上计数
- timer_init_struct.period = 999; // 计数器周期为1000,即10kHz下1秒钟
- timer_init_struct.clockdivision = TIMER_CKDIV_DIV1; // 时钟分频1
- timer_init_struct.repetitioncounter = 0; // 重复计数器值
- timer_init(TIMER5, &timer_init_struct);
- timer_interrupt_enable(TIMER5, TIMER_INT_UP); // 使能Timer5更新中断
- nvic_irq_enable(TIMER5_IRQn, 0, 0); // 使能Timer5中断,并设置优先级为0
- timer_enable(TIMER5); // 启动Timer5
- }
- //启动定时器
- void timer5_start(void){
- timer_enable(TIMER5);
- }
- //关闭定时器
- void timer5_stop(void){
- timer_disable(TIMER5);
- }
- void TIMER5_IRQHandler(void)
- {
- if (timer_interrupt_flag_get(TIMER5, TIMER_INT_UP) != RESET)
- {
-
- timer_interrupt_flag_clear(TIMER5, TIMER_INT_UP); // 清除中断标志
- delay_counter++; // 延时计数器加1
- }
- }
|