STM32F103定时器TIM2输出比较无中断产生
时间:10-02
整理:3721RD
点击:
TIM2的输出比较模式的一个简单实验,要求比较成功后产生中断,在中断服务程序里翻转PF6。
其中系统时钟72MHz
实验中计数器工作正常,CCR1值正常。
但是实验中中断并未产生,用软件模拟产生中断后,一切正常。
所以问题应该出在比较的时候。就是说:当计数器CNT的计数值与CCR1的值相等时,没有中断产生
但是我没有发现问题。请各位帮忙看看,源码如下:
main.c
#include "stm32f10x.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
__IO uint16_t CCR1_Val = 40961;
uint16_t PrescalerValue = 0;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 65535; //最大计数值65536
TIM_TimeBaseStructure.TIM_Prescaler = 0; //不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //不滤波
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);//计数器时钟为6MHz
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //冻结,比较成功后OC1状态不变
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //OC1输出使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //给CCR1赋值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //不使用预装载寄存器
TIM_ITConfig(TIM2, TIM_IT_CC1 , ENABLE);//使能CC1中断
TIM_Cmd(TIM2, ENABLE); //开启定时器
while (1);
}
void RCC_Configuration(void)
{
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);//优先级分组3:7个抢占优先级,2个响应优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //TIM2全局中断使能
NVIC_Init(&NVIC_InitStructure);
}
stm32f10x_it.c
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
GPIO_WriteBit(GPIOF, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_6)));//ò???×¢
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture + CCR1_Val);
}
其中系统时钟72MHz
实验中计数器工作正常,CCR1值正常。
但是实验中中断并未产生,用软件模拟产生中断后,一切正常。
所以问题应该出在比较的时候。就是说:当计数器CNT的计数值与CCR1的值相等时,没有中断产生
但是我没有发现问题。请各位帮忙看看,源码如下:
main.c
#include "stm32f10x.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
__IO uint16_t CCR1_Val = 40961;
uint16_t PrescalerValue = 0;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 65535; //最大计数值65536
TIM_TimeBaseStructure.TIM_Prescaler = 0; //不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //不滤波
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);//计数器时钟为6MHz
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //冻结,比较成功后OC1状态不变
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //OC1输出使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //给CCR1赋值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //不使用预装载寄存器
TIM_ITConfig(TIM2, TIM_IT_CC1 , ENABLE);//使能CC1中断
TIM_Cmd(TIM2, ENABLE); //开启定时器
while (1);
}
void RCC_Configuration(void)
{
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);//优先级分组3:7个抢占优先级,2个响应优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //TIM2全局中断使能
NVIC_Init(&NVIC_InitStructure);
}
stm32f10x_it.c
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
GPIO_WriteBit(GPIOF, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOF, GPIO_Pin_6)));//ò???×¢
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture + CCR1_Val);
}
弄了几天,自己解决了。
禁止了TIM_OC1PreloadConfig(),就要在中断重新设置捕获寄存器值,这样才能不断有捕获中断产生。
今天看到这个帖子,刚开始学习STM32,写的程序和你基本一模一样,也出现了同样的问题,不过没看明白你的解决方法,TIM_SetCompare1(TIM2, capture + CCR1_Val);这句不是重新设置了比较寄存器的新值吗,我现在的程序一样进不去中断,能不能说详细点,或者传个到我邮箱365283796@qq.com,谢了
已解决了,不是程序的原因,是软件仿真的原因,可能是软件不能仿真定时中断,将程序改到另外一块板子,同样的程序,运行正常
不错,解决了
丁二狗的猎艳人生http://92ks.com/20253/