Пример #1
0
//*********************************//2ms
void tmi1_ini(void)
{
    TIM1_TimeBaseInit(8,TIM1_COUNTERMODE_UP,2000,0);//设置分频数,设置定时周期
    TIM1_ARRPreloadConfig(ENABLE);//使能预装载
    TIM1_ITConfig(TIM1_IT_UPDATE , ENABLE);//使能中断
    TIM1_Cmd(ENABLE); //开启定时器
}
Пример #2
0
/**
  * @brief  Configure TIM1 to allow sine wave generation
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  TIM1_DeInit();
  /* Time base configuration */
  /*
       TIM1_Period = INIT_TIM1_ARR
          TIM1_Prescaler = 0
       TIM1_RepetitionCounter=0
      */
  TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, INIT_TIM1_ARR, 0);
  
  /* PWM configuration */
  /*
       TIM1_OCMode = TIM1_OCMODE_PWM1
       TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
       TIM1_Pulse = INIT_TIM1_CCR1
       TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
  */
  TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE,
               INIT_TIM1_CCR1, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, 
               TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
  
  /* Update Interrupt Enable */
  TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
  
  /* ARRPreload Enable */
  TIM1_ARRPreloadConfig(ENABLE);
  
  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);
  
  /* Enable CC1 output*/
  TIM1_CtrlPWMOutputs(ENABLE);
}
Пример #3
0
/**
  * @brief  Configure TIM1 to generate 6 Steps PWM signal
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  /* TIM1 Peripheral Configuration */ 
  TIM1_DeInit();

  /* Time Base configuration */
  TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0);

  /* Channel 1, 2 and 3 Configuration in TIMING mode */  
  
  /* TIM1_Pulse = 2047 */
  TIM1_OC1Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
               2047, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
               TIM1_OCNIDLESTATE_SET);  

  /* TIM1_Pulse = 1023 */
  TIM1_OC2Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 1023,
               TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, 
               TIM1_OCNIDLESTATE_SET); 

  /* TIM1_Pulse = 511 */
  TIM1_OC3Init(TIM1_OCMODE_TIMING, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
               511, TIM1_OCPOLARITY_HIGH, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
               TIM1_OCNIDLESTATE_SET); 

  /* Automatic Output enable, Break, dead time and lock configuration*/
  TIM1_BDTRConfig( TIM1_OSSISTATE_ENABLE,  TIM1_LOCKLEVEL_OFF, 1,  TIM1_BREAK_DISABLE,
                   TIM1_BREAKPOLARITY_LOW,  TIM1_AUTOMATICOUTPUT_ENABLE);
  TIM1_CCPreloadControl(ENABLE);
  TIM1_ITConfig(TIM1_IT_COM, ENABLE);

  /* TIM1 counter enable */
  TIM1_Cmd(ENABLE);
}
Пример #4
0
Файл: workMode.c Проект: wantf/C
void initTimer(void)
{
		TIM1_DeInit();
		TIM1_TimeBaseInit(15, TIM1_COUNTERMODE_UP,5000-1, 0);	//5MS
		TIM1_ARRPreloadConfig(ENABLE);
		TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
		TIM1_Cmd(ENABLE);
}
Пример #5
0
@far @interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)
{	
	//sen
	CLAMPMOTORWORK();
	TIM1_Cmd(DISABLE); 
	TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
	
}
Пример #6
0
/**
  * @brief  Configure TIM1 to generate 3 complementary signals, to insert a 
  *         defined dead time value, to use the break feature and to lock the 
  *         desired parameters
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  /* TIM1 Peripheral Configuration */ 
  TIM1_DeInit();

  /* Time Base configuration */
	/*
  TIM1_Prescaler = 0
  TIM1_CounterMode = TIM1_COUNTERMODE_UP
  TIM1_Period = 65535
  TIM1_RepetitionCounter = 0
	*/

  TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 65535,0);

  /* Channel 1, 2 and 3 Configuration in PWM mode */
  /*
	TIM1_OCMode = TIM1_OCMODE_PWM2
  TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
  TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
  TIM1_Pulse = CCR1_Val
  TIM1_OCPolarity = TIM1_OCPOLARITY_LOW 
  TIM1_OCNPolarity = TIM1_OCNPOLARITY_LOW        
  TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
  TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
	*/
  TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
               CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET,
               TIM1_OCNIDLESTATE_RESET);

  /* TIM1_Pulse = CCR2_Val */
  TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
               CCR2_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET,
               TIM1_OCNIDLESTATE_RESET);

  /* TIM1_Pulse = CCR3_Val */
	TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
               CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET,
               TIM1_OCNIDLESTATE_RESET);

  /* Automatic Output enable, Break, dead time and lock configuration */
  /*
	TIM1_OSSIState = TIM1_OSSISTATE_ENABLE
  TIM1_LockLevel = TIM1_LOCKLEVEL_1
  TIM1_DeadTime = 117
  TIM1_Break = TIM1_BREAK_ENABLE
  TIM1_BreakPolarity = TIM1_BREAKPOLARITY_HIGH
  TIM1_AutomaticOutput = TIM1_AUTOMATICOUTPUT_ENABLE
	*/
	TIM1_BDTRConfig( TIM1_OSSISTATE_ENABLE,  TIM1_LOCKLEVEL_1, 117, TIM1_BREAK_ENABLE,
                   TIM1_BREAKPOLARITY_HIGH, TIM1_AUTOMATICOUTPUT_ENABLE);

  /* TIM1 counter enable */
  TIM1_Cmd(ENABLE);

  /* Main Output Enable */
  TIM1_CtrlPWMOutputs(ENABLE);
}
Пример #7
0
void led_off(void)
{
  TIM1_ITConfig((TIM1_IT_TypeDef)(TIM1_IT_UPDATE | TIM1_IT_CC1), DISABLE);
  TIM1_ClearITPendingBit((TIM1_IT_TypeDef)(TIM1_IT_UPDATE | TIM1_IT_CC1));
  TIM1_Cmd(DISABLE);
  // Display Off
  _led_write_segments(0x7f);
  _led_write_com(0);
}
Пример #8
0
/**
  * @brief  Configure TIM1 to generate 7 PWM signals with 4 different duty cycles
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{

    TIM1_DeInit();

    /* Time Base configuration */
    /*
    TIM1_Period = 4095
    TIM1_Prescaler = 0
    TIM1_CounterMode = TIM1_COUNTERMODE_UP
    TIM1_RepetitionCounter = 0
    */

    TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0);

    /* Channel 1, 2,3 and 4 Configuration in PWM mode */

    /*
    TIM1_OCMode = TIM1_OCMODE_PWM2
    TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
    TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
    TIM1_Pulse = CCR1_Val
    TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
    TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
    TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
    TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET

    */
    TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
                 CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                 TIM1_OCNIDLESTATE_RESET);

    /*TIM1_Pulse = CCR2_Val*/
    TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val,
                 TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                 TIM1_OCNIDLESTATE_RESET);

    /*TIM1_Pulse = CCR3_Val*/
    TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
                 CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                 TIM1_OCNIDLESTATE_RESET);

    /*TIM1_Pulse = CCR4_Val*/
    TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, CCR4_Val, TIM1_OCPOLARITY_LOW,
                 TIM1_OCIDLESTATE_SET);

    /* TIM1 counter enable */
    TIM1_Cmd(ENABLE);

    /* TIM1 Main Output Enable */
    TIM1_CtrlPWMOutputs(ENABLE);
}
Пример #9
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{

   /* CLK configuration -------------------------------------------*/
  CLK_Config(); 

  /* Init the Eval board LCD */
  STM8_EVAL_LCD_Init();

  /* Clear  LCD */
  LCD_Clear();

  /* Print the Voltage on the LCD*/
  LCD_SetCursorPos(LCD_LINE1, 0);
  LCD_Print("  POT      BNC  ");

   /* ADC configuration -------------------------------------------*/
  ADC_Config();

   /* DMA configuration -------------------------------------------*/
  DMA_Config();
    
  /* Enable ADC1 DMA requests*/
  ADC_DMACmd(ADC1, ENABLE);

  /* Start ADC1 Conversion using TIM1 TRGO*/
  ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_Trigger2,
                         ADC_ExtTRGSensitivity_Rising);

  /* Master Mode selection: Update event */
  TIM1_SelectOutputTrigger(TIM1_TRGOSource_Update);

  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);

  /* Enable Interrupts */
  enableInterrupts();

  /* Infinite loop*/
  while (1)
  {
    if (DisplayStatus != DISABLE)
    {
      /* Display Potentiometer RV and BNC voltage values on LCD*/
      ShowVoltages(PotVoltage, BNCVoltage);
      /* Disable displaying voltages on LCD until next DMA Channel0 Transfer complete occurs */
      DisplayStatus = DISABLE;
    }
  }
}
void HalLightClose(void)
{
  TIM1_SetCompare1(10);
  HalLightDelay();
//  GPIO_SetBits(GPIOD, GPIO_Pin_3);
  GPIO_ResetBits(GPIOD, GPIO_Pin_3);
  
  HalLightDelay();
  TIM1_Cmd(DISABLE);
  TIM1_CtrlPWMOutputs(DISABLE);
  GPIO_SetBits(GPIOD, GPIO_Pin_2);
  
  HalLightOn = FALSE;
  HalLigthPWM = 0;     /* 定时器关闭,无PWM输出 */
}
void HalLightOpen(void)
{
  TIM1_SetCompare1(5);
  TIM1_Cmd(ENABLE);
  TIM1_CtrlPWMOutputs(ENABLE);
  HalLightDelay();
  // GPIO_ResetBits(GPIOD, GPIO_Pin_3);
//  GPIO_SetBits(GPIOD, GPIO_Pin_3);
  HalLightDelay();
  for (int i = 5; i <= HalLigthPWM; i++)
  {
    HalLightDelay();
    TIM1_SetCompare1(i); 
  }
  HalLightOn = TRUE;
}
Пример #12
0
/**
  * @brief  Configure TIM1 peripheral 
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  /* configure TIM1 channel 1 to capture a PWM signal */
  TIM1_PWMIConfig(TIM1_Channel_1, TIM1_ICPolarity_Rising, TIM1_ICSelection_DirectTI,
                  TIM1_ICPSC_DIV1, ICFilter);

  /* Select the TIM1 Input Trigger: TI1FP1 */
  TIM1_SelectInputTrigger(TIM1_TRGSelection_TI1FP1);
  TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset);

  /* Enable CC1 interrupt request */
  TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
  enableInterrupts();

  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);
}
Пример #13
0
/**
  * @brief  Configure TIM1 peripheral
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  /* configure TIM1 channel 1 as PWM Output */
  TIM1_OC1Init(TIM1_OCMode_PWM1, TIM1_OutputState_Enable, TIM1_OutputNState_Disable,
               0x7FF/* TIM1_Pulse */, TIM1_OCPolarity_Low, TIM1_OCNPolarity_Low,
               TIM1_OCIdleState_Reset, TIM1_OCNIdleState_Reset);

  /* Set TIM1 Autoreload value*/
  TIM1_SetAutoreload(0xFFF);

  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);

  /* GPIO configuration: TIM1 channel 1 (PD2)*/
  GPIO_Init(GPIOD, GPIO_Pin_2 , GPIO_Mode_Out_PP_Low_Fast);

  /* Enable TIM1 Outputs*/
  TIM1_CtrlPWMOutputs(ENABLE);  
}
Пример #14
0
/**
  * @brief  Configure TIM1 to to capture the internal clock source (LSI)
  * @param  None
  * @retval None
  */
static void TIM1_Config(void)
{
  TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI,
               TIM1_ICPSC_DIV8, 0x0);
  
  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);

  /* Clear CC1 Flag*/
  TIM1_ClearFlag(TIM1_FLAG_CC1);
  
  /* wait a capture on CC1 */
  while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
  /* Get CCR1 value*/
  ICValue1 = TIM1_GetCapture1();
  TIM1_ClearFlag(TIM1_FLAG_CC1);
  
  /* wait a capture on cc1 */
  while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
  /* Get CCR1 value*/
  ICValue2 = TIM1_GetCapture1();
  TIM1_ClearFlag(TIM1_FLAG_CC1);
}
Пример #15
0
void led_on(void)
{
   TIM1_DeInit();
  
  /* TIM1 configuration:
   * TIM1 in PWM mode without output disabled. We only interest in the interrupts.
   * Period of PWM is 1024us; Duty cycle adjustable from 1/16 - 15/16.
   * With 16Mhz clock, we use:
   * TIM1_Period = 16 - 1
   * TIM1_Prescaler = 1024 - 1 
   * TIM1_Pulse = [0..15]
   */  
  TIM1_TimeBaseInit(1023, TIM1_COUNTERMODE_UP, 15, 0);
  /*
  TIM1_OCMode = TIM1_OCMODE_PWM1
  TIM1_OutputState = TIM1_OUTPUTSTATE_DISABLED
  TIM1_OutputNState = TIM1_OUTPUTNSTATE_DISABLED
  TIM1_Pulse = [1..16], change using TIM1_SetCompare1
  TIM1_OCPolarity = TIM1_OCPOLARITY_LOW; Doesn't matter as output is disabled
  TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH; Doesn't matter as output is disabled
  TIM1_OCIdleState = TIM1_OCIDLESTATE_SET; Doesn't matter as output is disabled
  TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET; Doesn't matter as output is disabled
  */
  TIM1_OC1Init
  (
    TIM1_OCMODE_PWM1,
    TIM1_OUTPUTSTATE_DISABLE, TIM1_OUTPUTNSTATE_DISABLE,
    _duty,
    TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH,
    TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET
  ); 
  TIM1_ITConfig((TIM1_IT_TypeDef)(TIM1_IT_UPDATE | TIM1_IT_CC1), ENABLE);
  /* TIM1 Main Output disabled */
  TIM1_CtrlPWMOutputs(DISABLE);  
  /* TIM1 counter enable */
  TIM1_Cmd(ENABLE);
}
Пример #16
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
    /* CLK configuration -------------------------------------------*/
    CLK_Config();

    /* GPIO configuration -------------------------------------------*/
    GPIO_Config();

    /* TIM1 configuration -------------------------------------------*/
    TIM1_Config();

    /* DAC configuration -------------------------------------------*/
    DAC_Config();

    /* COMP configuration -------------------------------------------*/
    COMP_Config();

    /* TIM1 counter enable */
    TIM1_Cmd(ENABLE);

    /* Infinite loop */
    while (1)
    {}
}
Пример #17
0
/**
  * @brief Measure the LSI frequency using timer IC1 and update the calibration registers.
  * @par Parameters:
  * None
  * @retval 
	* None
  * @par Required preconditions:
  * It is recommanded to use a timer clock frequency of at least 10MHz in order 
	* to obtain a better in the LSI frequency measurement.
  */
u32 LSIMeasurment(void)
{

  u32 lsi_freq_hz = 0x0;
  u32 fmaster = 0x0;
  u16 ICValue1 = 0x0;
  u16 ICValue2 = 0x0;

  /* Get master frequency */
  fmaster = CLK_GetClockFreq();

  /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */
  AWU->CSR |= AWU_CSR_MSR;

#if defined (STM8S903) || defined (STM8S103)
  /* Measure the LSI frequency with TIMER Input Capture 1 */
  
  /* Capture only every 8 events!!! */
  /* Enable capture of TI1 */
	TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0);
	
  /* Enable TIM1 */
  TIM1_Cmd(ENABLE);
  
  /* wait a capture on cc1 */
  while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
  /* Get CCR1 value*/
  ICValue1 = TIM1_GetCapture1();
  TIM1_ClearFlag(TIM1_FLAG_CC1);
  
  /* wait a capture on cc1 */
  while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
  /* Get CCR1 value*/
  ICValue2 = TIM1_GetCapture1();
  TIM1_ClearFlag(TIM1_FLAG_CC1);
  
  /* Disable IC1 input capture */
  TIM1->CCER1 &= (u8)(~TIM1_CCER1_CC1E);
  /* Disable timer2 */
  TIM1_Cmd(DISABLE);
  
#else  
  /* Measure the LSI frequency with TIMER Input Capture 1 */
  
  /* Capture only every 8 events!!! */
  /* Enable capture of TI1 */
  TIM3_ICInit(TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, TIM3_ICPSC_DIV8, 0);

  /* Enable TIM3 */
  TIM3_Cmd(ENABLE);

	/* wait a capture on cc1 */
  while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1);
	/* Get CCR1 value*/
  ICValue1 = TIM3_GetCapture1();
  TIM3_ClearFlag(TIM3_FLAG_CC1);

  /* wait a capture on cc1 */
  while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1);
    /* Get CCR1 value*/
  ICValue2 = TIM3_GetCapture1();
	TIM3_ClearFlag(TIM3_FLAG_CC1);

  /* Disable IC1 input capture */
  TIM3->CCER1 &= (u8)(~TIM3_CCER1_CC1E);
  /* Disable timer3 */
  TIM3_Cmd(DISABLE);
#endif

  /* Compute LSI clock frequency */
  lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1);
  
  /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */
  AWU->CSR &= (u8)(~AWU_CSR_MSR);

 return (lsi_freq_hz);
}
Пример #18
0
/**
  * @brief  Configure TIM1,2,3 peripherals 
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  /* TIM1 configuration:
   - TIM1CLK is set to 2 MHz, the TIM2 Prescaler is equal to 15 so the TIM1 counter
   clock used is 2 MHz / (15 + 1) = 125 000 Hz
   - TIM1 Channel 1 output frequency = TIM1CLK / (TIM1_PERIOD + 1) * (TIM1_PRESCALER + 1)
                                    = 2 000 000 / 256 * 16 = 488.28 Hz */
  /* Time Base configuration */
  TIM1_TimeBaseInit(TIM1_PRESCALER, TIM1_CounterMode_Up, TIM1_PERIOD, TIM1_REPETITION_COUNTER);

  /* TIM1 Channel 1 Configuration in PWM2 mode */
  TIM1_OC1Init(TIM1_OCMode_PWM2, TIM1_OutputState_Enable, TIM1_OutputNState_Disable, TIM1_CCR1_VAL,
               TIM1_OCPolarity_Low, TIM1_OCNPolarity_Low, TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set);

  /* Master Mode selection: Update event */
  TIM1_SelectOutputTrigger(TIM1_TRGOSource_Update);

  /* TIM2 configuration:
   - TIM2 is connected to TIM1 Update so TIM2CLK is equal to 
     TIM1 output clock / (TIM1_REPETITION_COUNTER + 1) = 488.28 / 5 = 97.65 Hz
    - TIM2 Prescaler is equal to 1 so the TIM2 counter clock used is 97.65 / 1 = 97.65 Hz
    - TIM2 Channel 1 output frequency = TIM2CLK / (TIM2_PERIOD + 1) * TIM2_Prescaler
                                     = 97.65 / 3 * 1 = 32.55 Hz */
  /* Time Base configuration */
  TIM2_TimeBaseInit(TIM2_Prescaler_1, TIM2_CounterMode_Up, TIM2_PERIOD);

  TIM2_OC1Init(TIM2_OCMode_PWM2, TIM2_OutputState_Enable, TIM2_CCR1_VAL,
               TIM2_OCPolarity_Low, TIM2_OCIdleState_Reset);

  /* TIM2 Slave Mode selection: Gated mode */
  TIM2_SelectSlaveMode(TIM2_SlaveMode_Gated);
  TIM2_SelectInputTrigger(TIM2_TRGSelection_TIM1);

  /* TIM3 configuration:
   - TIM3 is connected to TIM1 Update so TIM3CLK is equal to 
     TIM1 output clock / (TIM1_REPETITION_COUNTER + 1) = 488.28 / 5 = 97.65 Hz
    - TIM3 Prescaler is equal to 1 so the TIM3 counter clock used is 97.65 / 1 = 97.65 Hz
    - TIM3 Channel 1 output frequency = TIM3CLK / (TIM3_PERIOD + 1) * TIM3Prescaler
                                     = 97.65 / 4 * 1 = 24.41 Hz */
  /* Time Base configuration */
  TIM3_TimeBaseInit(TIM3_Prescaler_1, TIM3_CounterMode_Up, TIM3_PERIOD);

  TIM3_OC1Init(TIM3_OCMode_PWM2, TIM3_OutputState_Enable, TIM3_CCR1_VAL,
               TIM3_OCPolarity_Low, TIM3_OCIdleState_Reset);

  /* TIM3 Slave Mode selection: Gated mode */
  TIM3_SelectSlaveMode(TIM3_SlaveMode_Gated);
  TIM3_SelectInputTrigger(TIM3_TRGSelection_TIM1);

  /* Main Output Enable */
  TIM1_CtrlPWMOutputs(ENABLE);
  TIM2_CtrlPWMOutputs(ENABLE);
  TIM3_CtrlPWMOutputs(ENABLE);

  /* TIM2 counter enable */
  TIM2_Cmd(ENABLE);
  /* TIM3 counter enable */
  TIM3_Cmd(ENABLE);
  /* TIM1 counter enable */
  TIM1_Cmd(ENABLE);
}
Пример #19
0
/* Configures the used Timers. ----------------------------------------------*/
void TIM_Configuration(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure2;
    TIM_OCInitTypeDef  TIM_OCInitStructure3;
    TIM_OCInitTypeDef  TIM_OCInitStructure8;

    /* TIM2 configuration for LED -------------------------------------------*/
    TIM_TimeBaseStructure.TIM_Period = 0x12B; 				//299
    TIM_TimeBaseStructure.TIM_Prescaler = 0xEA5F;       	//59999
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    /* Outoput Compare Init */
    TIM_OCStructInit(&TIM_OCInitStructure2);
    /* Output Compare Timing Mode configuration: Channel1 */
    TIM_OCInitStructure2.TIM_OCMode = TIM_OCMode_Timing;
    TIM_OCInitStructure2.TIM_Pulse = 0x0;
    TIM_OCInit(TIM2, &TIM_OCInitStructure2);
    /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);
    /* Immediate load of TIM2 Precaler value */
    TIM_PrescalerConfig(TIM2, 0xEA5F, TIM_PSCReloadMode_Immediate);
    /* Clear TIM2 update pending flag */
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    /* Enable TIM2 Update interrupt */
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    /* TIM3 configuration for Statemachine (main control loop) --------------*/
    TIM_TimeBaseStructure.TIM_Period = 1000; 				//alle 1ms
    TIM_TimeBaseStructure.TIM_Prescaler = 0x48;       	//72
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    /* Outoput Compare Init */
    TIM_OCStructInit(&TIM_OCInitStructure3);
    /* Output Compare Timing Mode configuration: Channel1 */
    TIM_OCInitStructure3.TIM_OCMode = TIM_OCMode_Timing;
    TIM_OCInitStructure3.TIM_Pulse = 0x0;
    TIM_OCInit(TIM3, &TIM_OCInitStructure3);
    /* TIM3 enable counter */
    TIM_Cmd(TIM3, ENABLE);
    /* Immediate load of TIM3 Precaler value */
    TIM_PrescalerConfig(TIM3, 0x48, TIM_PSCReloadMode_Immediate);
    /* Clear TIM3 update pending flag */
    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    /* Enable TIM3 Update interrupt */
    // enable in main after powerup
    //TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);


    /* Timer for PPM decode ---------------------------------------------------*/
    TIM1_ICInitTypeDef TIM1_ICInitStructure;
    //TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP;
#ifndef PPM_PA11
    TIM1_ICInitStructure.TIM1_Channel = TIM1_Channel_1;   //Pin: PA8
#else
    TIM1_ICInitStructure.TIM1_Channel = TIM1_Channel_4;   //Pin: PA11
#endif
    TIM1_ICInitStructure.TIM1_ICPolarity = TIM1_ICPolarity_Falling;
    TIM1_ICInitStructure.TIM1_ICSelection = TIM1_ICSelection_DirectTI;
    TIM1_ICInitStructure.TIM1_ICPrescaler = TIM1_ICPSC_DIV1;
    TIM1_ICInitStructure.TIM1_ICFilter = 0x0;
    TIM1_ICInit(&TIM1_ICInitStructure);

    TIM1_SelectInputTrigger(TIM1_TS_TI2FP2);

    TIM1_InternalClockConfig();
    TIM1_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
    TIM1_TimeBaseStructure.TIM1_Period = 0xFFFF;
    TIM1_TimeBaseStructure.TIM1_Prescaler = 17; // fCK_PSC / (17 + 1) 1ms = 4000
    TIM1_TimeBaseStructure.TIM1_ClockDivision = TIM1_CKD_DIV1;
    TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;
    TIM1_TimeBaseInit(& TIM1_TimeBaseStructure);

    /* TIM enable counter */
    TIM1_Cmd(ENABLE);
    /* Enable the CC1 Interrupt Request */
#ifndef PPM_PA11
    TIM1_ITConfig(TIM_IT_CC1, ENABLE);
#else
    TIM1_ITConfig(TIM_IT_CC4, ENABLE);
#endif

    /* Timer for Servo 0-3 --------------------------------------------------*/
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 5999;
    TIM_TimeBaseStructure.TIM_Prescaler = 72;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure8.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure8.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure8.TIM_Channel = TIM_Channel_1;
    TIM_OCInitStructure8.TIM_Pulse = 1500;
    TIM_OCInitStructure8.TIM_OCPolarity = TIM_OCPolarity_High; // maybee it works with TIM_OCPolarity_LOW???

    TIM_OCInit(TIM8, &TIM_OCInitStructure8);

    TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel2 */
    TIM_OCInitStructure8.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure8.TIM_Channel = TIM_Channel_2;
    TIM_OCInitStructure8.TIM_Pulse = 1500;

    TIM_OCInit(TIM8, &TIM_OCInitStructure8);

    TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel3 */
    TIM_OCInitStructure8.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure8.TIM_Channel = TIM_Channel_3;
    TIM_OCInitStructure8.TIM_Pulse = 1500;

    TIM_OCInit(TIM8, &TIM_OCInitStructure8);

    TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel4 */
    TIM_OCInitStructure8.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure8.TIM_Channel = TIM_Channel_4;
    TIM_OCInitStructure8.TIM_Pulse = 1500;

    TIM_OCInit(TIM8, &TIM_OCInitStructure8);

    TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM8, ENABLE);

    /* Enable the TIM Main Output only needed for timer 1 and 8*/
    TIM8->BDTR |= ((u16)0x8000); //BDTR_MOE_Set;

    /* TIM8 enable counter */
    TIM_Cmd(TIM8, ENABLE);
    /* Immediate load of TIM8 Precaler value */
    //TIM_PrescalerConfig(TIM8, 72, TIM_PSCReloadMode_Immediate);
    /* Clear TIM8 update pending flag */
    //TIM_ClearFlag(TIM8, TIM_FLAG_Update);
}