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);
}
一周热门 更多>