コード例 #1
0
ファイル: stm8l15x_tim3.c プロジェクト: ezhov/stm8l-discovery
/**
  * @brief  Configures the TIM3 Trigger as External Clock.
  * @param  TIM3_TIxExternalCLKSource : Specifies Trigger source.
  *   This parameter can be one of the @ref TIM3_TIxExternalCLK1Source_TypeDef enumeration.
  * @param  TIM3_ICPolarity : Specifies the TIx Polarity.
  *   This parameter can be @ref TIM3_ICPolarity_TypeDef enumeration.
  * @param ICFilter : Specifies the filter value.
  *   This parameter must be a value between 0x00 and 0x0F
  * @retval None
  */
void TIM3_TIxExternalClockConfig(TIM3_TIxExternalCLK1Source_TypeDef TIM3_TIxExternalCLKSource,
                                 TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
                                 uint8_t ICFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM3_TIXCLK_SOURCE(TIM3_TIxExternalCLKSource));
  assert_param(IS_TIM3_IC_POLARITY(TIM3_ICPolarity));
  assert_param(IS_TIM3_IC_FILTER(ICFilter));

  /* Configure the TIM3 Input Clock Source */
  if (TIM3_TIxExternalCLKSource == TIM3_TIxExternalCLK1Source_TI2)
  {
    TI2_Config(TIM3_ICPolarity, TIM3_ICSelection_DirectTI, ICFilter);
  }
  else
  {
    TI1_Config(TIM3_ICPolarity, TIM3_ICSelection_DirectTI, ICFilter);
  }

  /* Select the Trigger source */
  TIM3_SelectInputTrigger((TIM3_TRGSelection_TypeDef)TIM3_TIxExternalCLKSource);

  /* Select the External clock mode1 */
  TIM3->SMCR |= (uint8_t)(TIM3_SlaveMode_External1);
}
コード例 #2
0
ファイル: config.c プロジェクト: cristi85/RemoteSwitch_v1
void Config()
{
  //System clock at power up is HSI/8 = 16MHz/8 = 2MHz
  //CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);  //set HSI/4 = 4MHz SysClk to Core and Memory, minimum clock = 125KHz for CLK_SYSCLKDiv_128
  //PWR_PVDCmd(ENABLE);  //Power voltage detector and brownout Reset unit supply current 2,6uA
  //PWR_PVDLevelConfig(PWR_PVDLevel_2V26); //set Programmable voltage detector threshold to 2,26V
  //PWR_GetFlagStatus(PWR_FLAG_PVDOF);  //checks whether the specified PWR flag is set or not
  
  //Configure external interrupts - BTN1 and BTN2 presses
  //EXTI_SetPinSensitivity(EXTI_Pin_2, EXTI_Trigger_Falling_Low);
  //EXTI_SetPinSensitivity(EXTI_Pin_4, EXTI_Trigger_Falling_Low);
  //EXTI_SelectPort(EXTI_Port_B);
  //EXTI_SetHalfPortSelection(EXTI_HalfPort_B_MSB, ENABLE);
  
  // Timer 4 Configuration
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE);   // 8bit: used for system periodic flags and timeouts
  TIM4_TimeBaseInit(TIM4_Prescaler_16, 125);                // Overflow at 1ms, 2MHz/16/125 = 1000
  TIM4_ITConfig(TIM4_IT_Update, ENABLE);
  TIM4_Cmd(ENABLE);

  // Timer 2 Configuration
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);           // 16bit: used for runtime measurement
  TIM2_TimeBaseInit(TIM2_Prescaler_2, TIM2_CounterMode_Up, 0xFFFF); // timebase of 1us, 2MHz/2=1M
  TIM2_Cmd(ENABLE);
  
  // Timer3 Configuration
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE);
  TIM3_TimeBaseInit(TIM3_Prescaler_2, TIM3_CounterMode_Up, 0xFFFF);  // 1us timebase @ 2MHz system clock
  TIM3_ICInit(TIM3_Channel_2,
              TIM3_ICPolarity_Falling,
              TIM3_ICSelection_IndirectTI,
              TIM3_ICPSC_Div1,
              0);
  TIM3_ICInit(TIM3_Channel_1,
              TIM3_ICPolarity_Rising,
              TIM3_ICSelection_DirectTI,
              TIM3_ICPSC_Div1,
              0);
  TIM3_SelectInputTrigger(TIM3_TRGSelection_TI1FP1);
  TIM3_SelectSlaveMode(TIM3_SlaveMode_Reset);  // Reset timer on selected trigger signal
  TIM3_ITConfig(TIM3_IT_CC1, ENABLE);
  TIM3_ITConfig(TIM3_IT_CC2, ENABLE);
  TIM3_Cmd(ENABLE);
  
  /* Output PP High - bicolor LED to GND */
  GPIO_Init(LED_PORT, LED_PIN1, GPIO_Mode_Out_PP_Low_Slow);
  GPIO_Init(LED_PORT, LED_PIN2, GPIO_Mode_Out_PP_Low_Slow);

  /* Input pull up no IT - onboard Button, external pull-up */
  GPIO_Init(BTN1_PORT, BTN1_PIN, GPIO_Mode_In_FL_No_IT);
  
  /* RFM85W pins */
  GPIO_Init(RFM_DATA_PORT, RFM_DATA_PIN, GPIO_Mode_In_FL_No_IT);
  
  /* DEBUG pin */
  GPIO_Init(DEBUG_PIN_PORT, DEBUG_PIN_PIN, GPIO_Mode_Out_PP_Low_Fast);

  /* H bridge outputs */
  GPIO_Init(PMOS_L_PORT,  PMOS_L_PIN,    GPIO_Mode_Out_PP_High_Fast);
  GPIO_Init(PMOS_R_PORT,  PMOS_R_PIN,    GPIO_Mode_Out_PP_High_Fast);
  GPIO_Init(NMOS_L_PORT,  NMOS_L_PIN,    GPIO_Mode_Out_PP_Low_Fast);
  GPIO_Init(NMOS_R_PORT,  NMOS_R_PIN,    GPIO_Mode_Out_PP_Low_Fast);
}
コード例 #3
0
ファイル: main.c プロジェクト: glockwork/ev-stm8l151k6-emotor
/**
  * @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);
}