配置过程:
- 使能基本定时器的时钟;
- 设置自动装载值,APRE=0,TIMx_ARR寄存器没有缓冲;
- 设置预分频器系数;
- 清除中断标志位;
- 配置中断优先级并且使能该中断;
- 允许TIMx产生更新中断;
- 启动定时器;
basic_timer.h文件
#ifndef __BASIC_TIMER_H
#define __BASIC_TIMER_H
#include "stm32f10x.h"
#define My_BASIC_TIM TIM6
#define My_RCC_TIM6_EN (uint32_t)0x00000010
#define My_BASIC_UIF (uint32_t)0x00000001
#define My_BSIC_UIE (uint32_t)0x00000001
#define My_BASIC_CEN (uint32_t)0x00000001
void Base_Timer_Config(uint16_t arr, uint16_t psc);
#endif
basic_timer.c文件
#include "basic_timer.h"
void Base_Timer_Config(uint16_t arr, uint16_t psc)
{
RCC->APB1ENR |= My_RCC_TIM6_EN;
My_BASIC_TIM->ARR = arr-1;
My_BASIC_TIM->PSC = psc-1;
My_BASIC_TIM->SR &= ~My_BASIC_UIF;
NVIC->IP[TIM6_IRQn] = (uint8_t)0x00;
NVIC->ISER[TIM6_IRQn >> 0x05] = (uint32_t)0x01 << (TIM6_IRQn & (uint8_t)0x1F);
My_BASIC_TIM->DIER |= My_BSIC_UIE;
My_BASIC_TIM->CR1 |= My_BASIC_CEN;
}
备注:头文件及源文件再修改一下,便可兼容TIM7。 参考资料: 1.野火STM32教程; 2.STM32中文参考手册;
|