定时器的周期设置计算时间问题

2019-08-12 18:05发布

在原子的定时器历程中的定时时间TIM_Period 就需要+1,Tout= ((arr+1)*(psc+1))/Tclk  。而我在网上找了个用比较模式写的定时中的周期并没有+1,它是通过TIM_OCInitStructure.TIM_Pulse = CCR1_Val来改变。还有就是为什么我讲周期改成比65535小,就会使定时时间改变。完整历程如下:
TIM_TimeBaseStructure.TIM_Period = 65535;         
  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, 35999, TIM_PSCReloadMode_Immediate);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;//这个地方就是改比较模式的
   但是由于比较模式无论选哪个对于产生中断的作用是一样的,所以选TIMING都可以      
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;         
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
   
  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);//TIMx_CCRx寄存器能够在任何时候通过软件进行更新以控制输出波形,条件是未使用预装载寄存器(OCxPE=’0’,否则TIMx_CCRx影子寄存器只能在发生下一次更新事件时被更新)。这里设置为Disable就是为了后面在中断服务子程序可以修改TIMx_CCR实时起作用~
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_2;         
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;         
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_4;         
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);

  TIM_ARRPreloadConfig(TIM2, ENABLE);//TIM_OCPreload_Enable

  TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
    // STM3210B-LK1,  set PC.04 - PC.07
  GPIO_SetBits(GPIOC, GPIO_Pin_4 |GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);

  TIM_Cmd(TIM2, ENABLE);
  while (1)
  {
  }
}
中断服务子程序:
void TIM2_IRQHandler(void)
  {  u16 capture;
     u16 CCR1_Val = 1000;
     u16 CCR2_Val = 500;
     u16 CCR3_Val = 250;
     u16 CCR4_Val = 125;
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
   
    capture = TIM_GetCapture1(TIM2);
    TIM_SetCompare1(TIM2, capture + CCR1_Val);
   ////设置TIMx捕获比较1寄存器值然后动态修改其CCR的值 使整个程序一直进行下去
   
    // PC.04
    GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_4)));

  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
    capture = TIM_GetCapture2(TIM2);
    TIM_SetCompare2(TIM2, capture + CCR2_Val);

    // PC.05
    GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_5)));
   
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  {
   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
    capture = TIM_GetCapture3(TIM2);
    TIM_SetCompare3(TIM2, capture + CCR3_Val);
  
    // PC.06
    //GPIO_ResetBits(GPIOC, GPIO_Pin_6);
     GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
  }
  else
  {
   
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
    capture = TIM_GetCapture4(TIM2);
    TIM_SetCompare4(TIM2, capture + CCR4_Val);
     
    // PC.07
   // GPIO_ResetBits(GPIOC, GPIO_Pin_7);
     GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
  }
}

------------------------------------------------------------------------------------------------------------------

在STM32的某些应用中,用户有周期性执行某些程序的要求,使用定时器可以产生固定的时间周期,满足 这样的需求。STM32相关特征:
STM32高级定时器TIM1、TIM8,通用定时器TIM2、TIM3、TIM4、TIM5;
定时器最大时钟72MHz,配合预分频,提供灵活的时钟周期;
每个TIM有4个独立捕获/比较通道,DMA/中断功能;
通道工作在输出比较定时模式,一个TIM至多可以提供4个不同的定时周期。
原理
TIM某输出/捕获通道工作在输出比较定时模式
计数器计数至比较值时产生中断,在中断中刷新捕获比较寄存器,这样在相同时间间隔后可产生下一次中断

TIM2时钟设置为36MHz,预分频设置为2,使用输出比较-翻转模式(Output Compare Toggle Mode)。
TIM2计数器时钟可表达为:TIM2 counter clock = TIMxCLK / (Prescaler +1) = 12 MHz设置TIM2_CCR1寄存器值为32768,则CC1更新频率为TIM2计数器时钟频率除以CCR1寄存器值,为366.2 Hz。因此,TIM2通道1可产生一个频率为183.1 Hz的周期信号。同理,根据寄存器TIM2_CCR2 、TIM2_CCR3和 TIM2_CCR4的值,TIM2通道2可产生一个频率为366.3 Hz的周期信号;TIM2通道3可产生一个频率为732.4 Hz的周期信号;TIM2通道4可产生一个频率为1464.8 Hz的周期信号。#include "stm32f10x_lib.h"

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
vu16 CCR1_Val = 32768;
vu16 CCR2_Val = 16384;
vu16 CCR3_Val = 8192;
vu16 CCR4_Val = 4096;
ErrorStatus HSEStartUpStatus;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
   

int main(void)
{
#ifdef DEBUG
  debug();
#endif

  RCC_Configuration();

  NVIC_Configuration();


  GPIO_Configuration();

  TIM_TimeBaseStructure.TIM_Period = 65535;   //这里必须是65535
     
  TIM_TimeBaseStructure.TIM_Prescaler = 2;      
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;   
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;  //管脚输出模式:翻转(TIM输出比较触发模式)
   
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;         
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;   //翻转周期
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//TIM输出比较极性低  TIM_OCInit(TIM2, &TIM_OCInitStructure);  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);//失能TIMx在CCR1上的预装载寄存器
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_2;         
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;         
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_4;         
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);


  TIM_Cmd(TIM2, ENABLE);

  TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
  while (1)
  {
  }
}

void RCC_Configuration(void)
{  

  RCC_DeInit();

  RCC_HSEConfig(RCC_HSE_ON);

  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
   
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
   
    FLASH_SetLatency(FLASH_Latency_2);

   
    RCC_HCLKConfig(RCC_SYSCLK_Div1);

   
    RCC_PCLK2Config(RCC_HCLK_Div1);
   
    RCC_PCLK1Config(RCC_HCLK_Div4);
   
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
   
    RCC_PLLCmd(ENABLE);
   
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
   
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
   
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
#ifdef  VECT_TAB_RAM

  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else

  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
#endif


  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
#ifdef  DEBUG

void assert_failed(u8* file, u32 line)
{
  while (1)
  {
  }
}
#endif

中断服务:

void TIM2_IRQHandler(void)
{

  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//检查指定的TIM中断发生与否
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );//清除TIMx的中断待处理位
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture + CCR1_Val );//设置TIMx自动重装载寄存器值
        //将TIM2_CC1的值增加CCR1_Val,使得下一个TIM事件也需要CCR1_Val个脉冲,
  }


  if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
     TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM2);
    TIM_SetCompare2(TIM2, capture + CCR2_Val);
  }

  if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM2);
    TIM_SetCompare3(TIM2, capture + CCR3_Val);
  }

  if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM2);
    TIM_SetCompare4(TIM2, capture + CCR4_Val);
  }  
}


0条回答

一周热门 更多>