-----------
源码
void tim3_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//使能TIM3的硬件时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//配置TIM3的定时时间
TIM_TimeBaseStructure.TIM_Period = 10000-1; //自动重载
TIM_TimeBaseStructure.TIM_Prescaler = 8400-1; //预分频值为8400 84000000/8400=10000(1S)
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //从0开始计数,然后计数到TIM_Period这个值就触发中断
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//配置TIM3的中断触发:时间更新
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
//配置TIM3的优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开通道给NVIC管理
NVIC_Init(&NVIC_InitStructure);
//使能TIM3工作
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
PFout(9)^=1;
//告诉CPU,已经完成接收中断请求,可以响应新的接收中断请求
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
其他外设定时器使用方法
打开对应时钟供电