TIM8输入捕获+TIM8更新计次数。为什么进不了更新中断?非常郁闷!请大神帮忙。
程序如下:
主程序:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "TIM8_20180111.h"
extern u16 TIM8CH1_CAPTURE_STA;
int main(void)
{
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init();
TIM8_Cap_Init(0XFFFF,72-1);
while(1)
{
LED0=!LED0;
delay_ms(500);
}
}
子程序:
#include "TIM8_20180111.h"
#include "led.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
/////定时器8通道1输入捕获配置
TIM_ICInitTypeDef TIM8_ICInitStructure;
void TIM8_Cap_Init(u16 arr,u16 psc)//2018011 TIM8 普通输入捕获模式CH1 PC6
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //使能TIM8时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIOC时钟//PC6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PC6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //输入 //PC6
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_6); //PC6
//初始化定时器8 TIM8
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM8输入捕获参数
TIM8_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM8_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM8_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM8_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM8_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM8, &TIM8_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_SetCounter(TIM8,0);
TIM_ITConfig(TIM8,TIM8_UP_IRQn,ENABLE);//允许中断 ,允许CC1IE捕获中断
TIM_ClearFlag(TIM8, TIM_FLAG_Update);//20180101
TIM_Cmd(TIM8,ENABLE ); //使能定时器
}
u16 TIM8CH1_CAPTURE_STA; //输入捕获状态
void TIM8_UP_IRQHandler (void)//20180111 普通定时器TIMER5改TIMER8 TIM8_UP_IRQHandler|TIM8_CC_IRQHandler
{
if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM8, TIM_IT_Update); //20171228
TIM8CH1_CAPTURE_STA++;
LED1=!LED1;
if((TIM8CH1_CAPTURE_STA&0X3FFF)==0X000A)
{
LED1=!LED1;
TIM8CH1_CAPTURE_STA=0;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
是AF, 不是IPD
一周热门 更多>