stm32的定时器输出比较实验,计数值比比较捕获定时器值小,能触发中断?应该不能啊
现在我的程序一直在触发中断,不知道为什么
没有开溢出中断
#include "stm32f10x.h"
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,ENABLE); //外设要开复用时钟?
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //LED0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM2的CH1是PA0的复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void TIMx_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//时基单元初始化
TIM_TimeBaseStructure.TIM_Period = 8000; //计数值8000
TIM_TimeBaseStructure.TIM_Prescaler = 999; //8000000/1000=8000Hz,周期为1/8000,计数器周期为1s
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 15100;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1); //使能输出比较中断
TIM_Cmd(TIM2,ENABLE);
}
void Delay(vu32 times)
{
for(;times!=0;times--);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIMx_Configuration();
NVIC_Configuration();
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
Delay(100000);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
Delay(100000);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);
TIM_SetCounter(TIM2,0x0000);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
Delay(1000000);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}
} |