STM32 tiM1重复计数器+单脉冲模式 产生精确个数脉冲 程序如下:void TIM1_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE ); //--TIM1更新中断配置------------------------------------- //NVIC_InitStructure.NVIC_IRQChannel= TIM1_UP_IRQn; //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1; //NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0; //NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; //NVIC_Init(&NVIC_InitStructure); TIM_DeInit(TIM1); //--TIM1基本配置----------------------------------------- TIM_TimeBaseStructure.TIM_Period = 11; //设置周期 ,频率=72MHz/11=6.5MHz TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频(时钟分频) 不分频 TIM_TimeBaseStructure.TIM_ClockDivision = 0x00; //设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //设置向上计数模式 TIM_TimeBaseStructure.TIM_RepetitionCounter= 0x0A; //重复计数器计数值 TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化函数 TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Single); //设置单脉冲模式,在发生下一次更新事件时计数器停止 TIM_ARRPreloadConfig(TIM1,DISABLE); //失能在ARR上的预装载寄存器,没有缓冲 TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Disable); //失能在CCR1上的预装载寄存器 //--TIM1 PWM输出OC1配置---------------------------------------------- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出比较状态 TIM_OCInitStructure.TIM_OutputNState= TIM_OutputNState_Enable; //使能互补输出状态 TIM_OCInitStructure.TIM_Pulse = 3; //设置待装入捕获比较寄存器的脉冲值 占空时间 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补 输出极性高 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //MOE=0 设置 TIM1输出比较空闲状态 TIM_OCInitStructure.TIM_OCNIdleState= TIM_OCIdleState_Set; //MOE=0 重置 TIM1输出比较空闲状态 TIM_OC1Init(TIM1,&TIM_OCInitStructure); //函数初始化 //--TIM1 PWM输出BDTR配置---------------------------------------------- TIM_BDTRInitStructure.TIM_OSSRState= TIM_OSSRState_Enable; //OSSR TIM_BDTRInitStructure.TIM_OSSIState= TIM_OSSIState_Enable; //OSSI TIM_BDTRInitStructure.TIM_LOCKLevel= TIM_LOCKLevel_1; //锁定级别1 TIM_BDTRInitStructure.TIM_DeadTime = 0; //死区时间0 TIM_BDTRInitStructure.TIM_Break= TIM_Break_Disable; //空闲模式不允许 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //空闲电平极性高 TIM_BDTRInitStructure.TIM_AutomaticOutput= TIM_AutomaticOutput_Enable; //自动输出使能 TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //TIM_ITConfig(TIM1,TIM_IT_Update , ENABLE); //使能更新中断 TIM_ITConfig(TIM1,TIM_IT_Update , DISABLE); //失能中断 TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Global); //设置更新请求源 0 TIM_GenerateEvent(TIM1,TIM_EventSource_Update); //设置由软件产生一个TIM1更新事件 __nop(); TIM_ClearFlag(TIM1,TIM_IT_Update); //清除待处理标志位 TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular); //设置更新请求源 1,只有溢出才能触发DMA // TIM_CtrlPWMOutputs(TIM1, DISABLE); //主输出失能 TIM_Cmd(TIM1, DISABLE); //使能TIM外设 }
产生脉冲波形是这样的。 重复计数器RCR计数值设置为0x0A。手册上说当重复计数器RCR值为0且同时出现计数器溢出时,才会有更新请求。现在,在第一次溢出时产生更新,但是此时RCR不为0????? 为什么??
单脉冲模式到底怎么用??求教。
一周热门 更多>