完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
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); } } |
|
相关推荐
2个回答
|
|
我又改动了一下中断函数,得到清晰的结果
void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)//比较捕获中断 { TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);//清中断 GPIO_ResetBits(GPIOA,GPIO_Pin_8); //红灯亮 if(TIM_GetCounter(TIM2) ==0) //计数值为零 { TIM_SetCounter(TIM2,0x0000); //清零计数值 GPIO_ResetBits(GPIOD,GPIO_Pin_2); //黄灯亮 Delay(1000000); GPIO_SetBits(GPIOD,GPIO_Pin_2); } } } 当计数值为零时,黄灯能够亮灭。计数值小于比较捕获寄存器值,应该不会触发中断,但触发中断了,而且是在计数值为零时,实在搞不懂了, |
|
|
|
多看几遍手册,就懂了.
|
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1614 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1541 浏览 1 评论
970 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
682 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1592 浏览 2 评论
1863浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
644浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
515浏览 3评论
531浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
504浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 00:37 , Processed in 0.805170 second(s), Total 78, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号