#include "STM32f10x.h"
int main(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInitStruct.TIM_Period = 10000-1; 定时周期为10k/10k=1s
TIM_TimeBaseInitStruct.TIM_Prescaler = 7200-1; // 对时钟进行分频 72M分为10k
//APB1时钟72MHz,具体看时钟配置部分代码,以上配置为定时1000ms
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInit( TIM2, & TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);
TIM_ClearITPendingBit( TIM2,TIM_IT_Update);
NVIC_Init( &NVIC_InitStruct);
TIM_Cmd( TIM2, ENABLE);
GPIO_Init( GPIOC, & GPIO_InitStruct);
while(1)
{
}
}
void TIM2_IRQHandler(void)
{
if(RESET != TIM_GetITStatus( TIM2,TIM_IT_Update))
{
GPIOC->ODR ^=(1<<13); // 可以实现中断进入反转点评。
TIM_ClearITPendingBit( TIM2,TIM_IT_Update);
//发生定时器中断啦
}
}
2019.2.18 亲测可用 keil5.23 stm32f103C8