📄 stm8l15x_tim3.c
字号:
/**
******************************************************************************
* @file stm8l15x_tim3.c
* @author MCD Application Team
* @version V1.5.0
* @date 13-May-2011
* @brief This file provides firmware functions to manage the following
* functionalities of the TIM3 peripheral:
* - TimeBase management
* - Output Compare management
* - Input Capture management
* - Interrupts, DMA and flags management
* - Clocks management
* - Synchronization management
* - Specific interface management
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
* This driver provides functions to configure and initialise the TIM3
* peripheral
* These functions are split in 7 groups:
*
* 1. TIM3 TimeBase management: this group includes all needed functions
* to configure the TIM Timebase unit:
* - Set/Get Prescaler
* - Set/Get Autoreload
* - Counter modes configuration
* - Select the One Pulse mode
* - Update Request Configuration
* - Update Disable Configuration
* - Auto-Preload Configuration
* - Enable/Disable the counter
*
* 2. TIM3 Output Compare management: this group includes all needed
* functions to configure the Capture/Compare unit used in Output
* compare mode:
* - Configure each channel, independently, in Output Compare mode
* - Select the output compare modes
* - Select the Polarities of each channel
* - Set/Get the Capture/Compare register values
* - Select the Output Compare Fast mode
* - Select the Output Compare Forced mode
* - Output Compare-Preload Configuration
* - Enable/Disable the Capture/Compare Channels
*
* 3. TIM3 Input Capture management: this group includes all needed
* functions to configure the Capture/Compare unit used in
* Input Capture mode:
* - Configure each channel in input capture mode
* - Configure Channel1/2 in PWM Input mode
* - Set the Input Capture Prescaler
* - Get the Capture/Compare values
*
* 4. TIM3 interrupts, DMA and flags management
* - Enable/Disable interrupt sources
* - Get flags status
* - Clear flags/ Pending bits
* - Enable/Disable DMA requests
* - Select CaptureCompare DMA request
*
* 5. TIM3 clocks management: this group includes all needed functions
* to configure the clock controller unit:
* - Select internal/External clock
* - Select the external clock mode: ETR(Mode1/Mode2) or TIx
*
* 6. TIM3 synchronization management: this group includes all needed
* functions to configure the Synchronization unit:
* - Select Input Trigger
* - Select Output Trigger
* - Select Master Slave Mode
* - ETR Configuration when used as external trigger
*
* 7. TIM3 specific interface management, this group includes all
* needed functions to use the specific TIM3 interface:
* - Encoder Interface Configuration
* - Select Hall Sensor
*
* @endverbatim
*
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x_TIM3.h"
/** @addtogroup STM8L15x_StdPeriph_Driver
* @{
*/
/** @defgroup TIM3
* @brief TIM3 driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
uint8_t TIM3_ICFilter);
static void TI2_Config(TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
uint8_t TIM3_ICFilter);
/** @defgroup TIM3_Private_Functions
* @{
*/
/** @defgroup TIM3_Group1 TimeBase management functions
* @brief TimeBase management functions
*
@verbatim
===============================================================================
TimeBase management functions
===============================================================================
===================================================================
TIM3 Driver: how to use it in Timing(Time base) Mode
===================================================================
To use the Timer in Timing(Time base) mode, the following steps are mandatory:
1. Enable TIM3 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE) function.
2. Call TIM3_TimeBaseInit() to configure the Time Base unit with the
corresponding configuration.
3. Enable global interrupts if you need to generate the update interrupt.
4. Enable the corresponding interrupt using the function TIM3_ITConfig(TIM3_IT_Update)
5. Call the TIM3_Cmd(ENABLE) function to enable the TIM3 counter.
Note1: All other functions can be used separately to modify, if needed,
a specific feature of the Timer.
@endverbatim
* @{
*/
/**
* @brief Deinitialize the TIM3 peripheral registers to their default reset values.
* @param None
* @retval None
*/
void TIM3_DeInit(void)
{
TIM3->CR1 = TIM_CR1_RESET_VALUE;
TIM3->CR2 = TIM_CR2_RESET_VALUE;
TIM3->SMCR = TIM_SMCR_RESET_VALUE;
TIM3->ETR = TIM_ETR_RESET_VALUE;
TIM3->IER = TIM_IER_RESET_VALUE;
TIM3->SR2 = TIM_SR2_RESET_VALUE;
/* Disable channels */
TIM3->CCER1 = TIM_CCER1_RESET_VALUE;
/* Configure channels as inputs: it is necessary if lock level is equal to 2 or 3 */
TIM3->CCMR1 = 0x01;/*TIM3_ICxSource_TIxFPx */
TIM3->CCMR2 = 0x01;/*TIM3_ICxSource_TIxFPx */
/* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
TIM3->CCER1 = TIM_CCER1_RESET_VALUE;
TIM3->CCMR1 = TIM_CCMR1_RESET_VALUE;
TIM3->CCMR2 = TIM_CCMR2_RESET_VALUE;
TIM3->CNTRH = TIM_CNTRH_RESET_VALUE;
TIM3->CNTRL = TIM_CNTRL_RESET_VALUE;
TIM3->PSCR = TIM_PSCR_RESET_VALUE;
TIM3->ARRH = TIM_ARRH_RESET_VALUE;
TIM3->ARRL = TIM_ARRL_RESET_VALUE;
TIM3->CCR1H = TIM_CCR1H_RESET_VALUE;
TIM3->CCR1L = TIM_CCR1L_RESET_VALUE;
TIM3->CCR2H = TIM_CCR2H_RESET_VALUE;
TIM3->CCR2L = TIM_CCR2L_RESET_VALUE;
TIM3->OISR = TIM_OISR_RESET_VALUE;
TIM3->EGR = 0x01;/* TIM_EGR_UG */
TIM3->BKR = TIM_BKR_RESET_VALUE;
TIM3->SR1 = TIM_SR1_RESET_VALUE;
}
/**
* @brief Initializes the TIM3 Time Base Unit according to the specified parameters.
* @param TIM3_Prescaler: Prescaler
* This parameter can be one of the following values:
* @arg TIM3_Prescaler_1: Time base Prescaler = 1 (No effect)
* @arg TIM3_Prescaler_2: Time base Prescaler = 2
* @arg TIM3_Prescaler_4: Time base Prescaler = 4
* @arg TIM3_Prescaler_8: Time base Prescaler = 8
* @arg TIM3_Prescaler_16: Time base Prescaler = 16
* @arg TIM3_Prescaler_32: Time base Prescaler = 32
* @arg TIM3_Prescaler_64: Time base Prescaler = 64
* @arg TIM3_Prescaler_128: Time base Prescaler = 128
* @param TIM3_CounterMode: Counter mode
* This parameter can be one of the following values:
* @arg TIM3_CounterMode_Up: Counter Up Mode
* @arg TIM3_CounterMode_Down: Counter Down Mode
* @arg TIM3_CounterMode_CenterAligned1: Counter Central aligned Mode 1
* @arg TIM3_CounterMode_CenterAligned2: Counter Central aligned Mode 2
* @arg TIM3_CounterMode_CenterAligned3: Counter Central aligned Mode 3
* @param TIM3_Period: This parameter must be a value between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM3_TimeBaseInit(TIM3_Prescaler_TypeDef TIM3_Prescaler,
TIM3_CounterMode_TypeDef TIM3_CounterMode,
uint16_t TIM3_Period)
{
assert_param(IS_TIM3_PRESCALER(TIM3_Prescaler));
assert_param(IS_TIM3_COUNTER_MODE(TIM3_CounterMode));
/* Set the Autoreload value */
TIM3->ARRH = (uint8_t)(TIM3_Period >> 8) ;
TIM3->ARRL = (uint8_t)(TIM3_Period);
/* Set the Prescaler value */
TIM3->PSCR = (uint8_t)(TIM3_Prescaler);
/* Select the Counter Mode */
TIM3->CR1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS)) & ((uint8_t)(~TIM_CR1_DIR));
TIM3->CR1 |= (uint8_t)(TIM3_CounterMode);
/* Generate an update event to reload the Prescaler value immediately */
TIM3->EGR = TIM3_EventSource_Update;
}
/**
* @brief Configures the TIM3 Prescaler.
* @param Prescaler: Specifies the Prescaler Register value
* This parameter can be one of the following values:
* @arg TIM3_Prescaler_1: Time base Prescaler = 1 (No effect)
* @arg TIM3_Prescaler_2: Time base Prescaler = 2
* @arg TIM3_Prescaler_4: Time base Prescaler = 4
* @arg TIM3_Prescaler_8: Time base Prescaler = 8
* @arg TIM3_Prescaler_16: Time base Prescaler = 16
* @arg TIM3_Prescaler_32: Time base Prescaler = 32
* @arg TIM3_Prescaler_64: Time base Prescaler = 64
* @arg TIM3_Prescaler_128: Time base Prescaler = 128
* @param TIM3_PSCReloadMode: Specifies the TIM3 Prescaler Reload mode.
* This parameter can be one of the following values:
* @arg TIM3_PSCReloadMode_Update: Prescaler value is reloaded at every update
* @arg TIM3_PSCReloadMode_Immediate: Prescaler value is reloaded at every update
* @retval None
*/
void TIM3_PrescalerConfig(TIM3_Prescaler_TypeDef Prescaler,
TIM3_PSCReloadMode_TypeDef TIM3_PSCReloadMode)
{
/* Check the parameters */
assert_param(IS_TIM3_PRESCALER(Prescaler));
assert_param(IS_TIM3_PRESCALER_RELOAD(TIM3_PSCReloadMode));
/* Set the Prescaler value */
TIM3->PSCR = (uint8_t)(Prescaler);
/* Set or reset the UG Bit */
if (TIM3_PSCReloadMode == TIM3_PSCReloadMode_Immediate)
{
TIM3->EGR |= TIM_EGR_UG ;
}
else
{
TIM3->EGR &= (uint8_t)(~TIM_EGR_UG) ;
}
}
/**
* @brief Specifies the TIM3 Counter Mode to be used.
* @param TIM3_CounterMode: Specifies the Counter Mode to be used
* This parameter can be one of the following values:
* @arg TIM3_CounterMode_Up: Counter Up Mode
* @arg TIM3_CounterMode_Down: Counter Down Mode
* @arg TIM3_CounterMode_CenterAligned1: Counter Central aligned Mode 1
* @arg TIM3_CounterMode_CenterAligned2: Counter Central aligned Mode 2
* @arg TIM3_CounterMode_CenterAligned3: Counter Central aligned Mode 3
* @retval None
*/
void TIM3_CounterModeConfig(TIM3_CounterMode_TypeDef TIM3_CounterMode)
{
uint8_t tmpcr1 = 0;
/* Check the parameters */
assert_param(IS_TIM3_COUNTER_MODE(TIM3_CounterMode));
tmpcr1 = TIM3->CR1;
/* Reset the CMS and DIR Bits */
tmpcr1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS) & (uint8_t)(~TIM_CR1_DIR));
/* Set the Counter Mode */
tmpcr1 |= (uint8_t)TIM3_CounterMode;
TIM3->CR1 = tmpcr1;
}
/**
* @brief Sets the TIM3 Counter Register value.
* @param Counter: Specifies the Counter register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM3_SetCounter(uint16_t Counter)
{
/* Set the Counter Register value */
TIM3->CNTRH = (uint8_t)(Counter >> 8);
TIM3->CNTRL = (uint8_t)(Counter);
}
/**
* @brief Sets the TIM3 Autoreload Register value.
* @param Autoreload: Specifies the Autoreload register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM3_SetAutoreload(uint16_t Autoreload)
{
/* Set the Autoreload Register value */
TIM3->ARRH = (uint8_t)(Autoreload >> 8);
TIM3->ARRL = (uint8_t)(Autoreload);
}
/**
* @brief Gets the TIM3 Counter value.
* @param None
* @retval Counter Register value.
*/
uint16_t TIM3_GetCounter(void)
{
uint16_t tmpcnt = 0;
uint8_t tmpcntrl, tmpcntrh;
tmpcntrh = TIM3->CNTRH;
tmpcntrl = TIM3->CNTRL;
tmpcnt = (uint16_t)(tmpcntrl);
tmpcnt |= (uint16_t)((uint16_t)tmpcntrh << 8);
/* Get the Counter Register value */
return ((uint16_t)tmpcnt);
}
/**
* @brief Gets the TIM3 Prescaler value.
* @param None
* @retval TIM3 Prescaler, it can be one of the following values:
* - TIM3_Prescaler_1: Time base Prescaler = 1 (No effect)
* - TIM3_Prescaler_2: Time base Prescaler = 2
* - TIM3_Prescaler_4: Time base Prescaler = 4
* - TIM3_Prescaler_8: Time base Prescaler = 8
* - TIM3_Prescaler_16: Time base Prescaler = 16
* - TIM3_Prescaler_32: Time base Prescaler = 32
* - TIM3_Prescaler_64: Time base Prescaler = 64
* - TIM3_Prescaler_128: Time base Prescaler = 128
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -