以下是定时器向上溢出示例代码: C语言: TIM1模块产生向上溢出事件 //Step1.时钟设置:启动TIM1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //Step2.中断NVIC设置:允许中断,设置优先级 NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel; //更新事件 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //允许中断 NVIC_Init(&NVIC_InitStructure); //写入设置 //Step3.TIM1模块设置 void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_BaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //TIM1 使用内部时钟 //TIM_InternalClockConfig(TIM1); //TIM1基本设置 //设置预分频器分频系数71,即APB2=72M, TIM1_CLK=72/72=1MHz //TIM_Period(TIM1_ARR)=1000,计数器向上计数到1000后产生更新事件,计数值归零 //向上计数模式 //TIM_RepetitionCounter(TIM1_RCR)=0,每次向上溢出都产生更新事件 TIM_BaseInitStructure.TIM_Period = 1000; TIM_BaseInitStructure.TIM_Prescaler = 71; TIM_BaseInitStructure.TIM_ClockDivision = 0; TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure); //清中断,以免一启用中断后立即产生中断 TIM_ClearFlag(TIM1, TIM_FLAG_Update); //使能TIM1中断源 TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //TIM1总开关:开启 TIM_Cmd(TIM1, ENABLE); } //Step4.中断服务子程序: void TIM1_UP_IRQHandler(void) { GPIOC->ODR ^= (1<<4); //闪灯 TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //清中断 } 下面是输出比较功能实现TIM1_CH1管脚输出指定频率的脉冲: C语言: TIM1模块实现输出比较,自动翻转并触发中断 //Step1.启动TIM1,同时还要注意给相应功能管脚启动时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //Step2. PA.8口设置为TIM1的OC1输出口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //Step3.使能TIM1的输出比较匹配中断 NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //Step4. TIM模块设置 void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_BaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //TIM1基本计数器设置 TIM_BaseInitStructure.TIM_Period = 0xffff; //这里必须是65535 TIM_BaseInitStructure.TIM_Prescaler = 71; //预分频71,即72分频,得1M TIM_BaseInitStructure.TIM_ClockDivision = 0; TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure); //TIM1_OC1模块设置 TIM_OCStructInit(& TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //管脚输出模式:翻转 TIM_OCInitStructure.TIM_Pulse = 2000; //翻转周期:2000个脉冲 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能TIM1_CH1通道 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出为正逻辑 TIM_OC1Init(TIM1, &TIM_OCInitStructure); //写入配置 //清中断 TIM_ClearFlag(TIM1, TIM_FLAG_CC1); //TIM1中断源设置,开启相应通道的捕捉比较中断 TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE); //TIM1开启 TIM_Cmd(TIM1, ENABLE); //通道输出使能 TIM_CtrlPWMOutputs(TIM1, ENABLE); } |