STM8S/A Standard Peripherals Drivers: stm8s_tim4.c Source File

STM8S/A

stm8s_tim4.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8s_tim4.c
00004   * @author  MCD Application Team
00005   * @version V2.3.0
00006   * @date    16-June-2017
00007   * @brief   This file contains all the functions for the TIM4 peripheral.
00008    ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s_tim4.h"
00030 
00031 /** @addtogroup STM8S_StdPeriph_Driver
00032   * @{
00033   */
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 /* Private function prototypes -----------------------------------------------*/
00039 /**
00040   * @addtogroup TIM4_Public_Functions
00041   * @{
00042   */
00043 
00044 /**
00045   * @brief  Deinitializes the TIM4 peripheral registers to their default reset values.
00046   * @param  None
00047   * @retval None
00048   */
00049 void TIM4_DeInit(void)
00050 {
00051   TIM4->CR1 = TIM4_CR1_RESET_VALUE;
00052   TIM4->IER = TIM4_IER_RESET_VALUE;
00053   TIM4->CNTR = TIM4_CNTR_RESET_VALUE;
00054   TIM4->PSCR = TIM4_PSCR_RESET_VALUE;
00055   TIM4->ARR = TIM4_ARR_RESET_VALUE;
00056   TIM4->SR1 = TIM4_SR1_RESET_VALUE;
00057 }
00058 
00059 /**
00060   * @brief  Initializes the TIM4 Time Base Unit according to the specified parameters.
00061   * @param    TIM4_Prescaler specifies the Prescaler from TIM4_Prescaler_TypeDef.
00062   * @param    TIM4_Period specifies the Period value.
00063   * @retval None
00064   */
00065 void TIM4_TimeBaseInit(TIM4_Prescaler_TypeDef TIM4_Prescaler, uint8_t TIM4_Period)
00066 {
00067   /* Check TIM4 prescaler value */
00068   assert_param(IS_TIM4_PRESCALER_OK(TIM4_Prescaler));
00069   /* Set the Prescaler value */
00070   TIM4->PSCR = (uint8_t)(TIM4_Prescaler);
00071   /* Set the Autoreload value */
00072   TIM4->ARR = (uint8_t)(TIM4_Period);
00073 }
00074 
00075 /**
00076   * @brief  Enables or disables the TIM4 peripheral.
00077   * @param   NewState new state of the TIM4 peripheral. This parameter can
00078   * be ENABLE or DISABLE.
00079   * @retval None
00080   */
00081 void TIM4_Cmd(FunctionalState NewState)
00082 {
00083   /* Check the parameters */
00084   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00085   
00086   /* set or Reset the CEN Bit */
00087   if (NewState != DISABLE)
00088   {
00089     TIM4->CR1 |= TIM4_CR1_CEN;
00090   }
00091   else
00092   {
00093     TIM4->CR1 &= (uint8_t)(~TIM4_CR1_CEN);
00094   }
00095 }
00096 
00097 /**
00098   * @brief  Enables or disables the specified TIM4 interrupts.
00099   * @param   NewState new state of the TIM4 peripheral.
00100   * This parameter can be: ENABLE or DISABLE.
00101   * @param   TIM4_IT specifies the TIM4 interrupts sources to be enabled or disabled.
00102   * This parameter can be any combination of the following values:
00103   * - TIM4_IT_UPDATE: TIM4 update Interrupt source
00104   * @param   NewState new state of the TIM4 peripheral.
00105   * @retval None
00106   */
00107 void TIM4_ITConfig(TIM4_IT_TypeDef TIM4_IT, FunctionalState NewState)
00108 {
00109   /* Check the parameters */
00110   assert_param(IS_TIM4_IT_OK(TIM4_IT));
00111   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00112   
00113   if (NewState != DISABLE)
00114   {
00115     /* Enable the Interrupt sources */
00116     TIM4->IER |= (uint8_t)TIM4_IT;
00117   }
00118   else
00119   {
00120     /* Disable the Interrupt sources */
00121     TIM4->IER &= (uint8_t)(~TIM4_IT);
00122   }
00123 }
00124 
00125 /**
00126   * @brief  Enables or Disables the TIM4 Update event.
00127   * @param   NewState new state of the TIM4 peripheral Preload register. This parameter can
00128   * be ENABLE or DISABLE.
00129   * @retval None
00130   */
00131 void TIM4_UpdateDisableConfig(FunctionalState NewState)
00132 {
00133   /* Check the parameters */
00134   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00135   
00136   /* Set or Reset the UDIS Bit */
00137   if (NewState != DISABLE)
00138   {
00139     TIM4->CR1 |= TIM4_CR1_UDIS;
00140   }
00141   else
00142   {
00143     TIM4->CR1 &= (uint8_t)(~TIM4_CR1_UDIS);
00144   }
00145 }
00146 
00147 /**
00148   * @brief  Selects the TIM4 Update Request Interrupt source.
00149   * @param   TIM4_UpdateSource specifies the Update source.
00150   * This parameter can be one of the following values
00151   *                       - TIM4_UPDATESOURCE_REGULAR
00152   *                       - TIM4_UPDATESOURCE_GLOBAL
00153   * @retval None
00154   */
00155 void TIM4_UpdateRequestConfig(TIM4_UpdateSource_TypeDef TIM4_UpdateSource)
00156 {
00157   /* Check the parameters */
00158   assert_param(IS_TIM4_UPDATE_SOURCE_OK(TIM4_UpdateSource));
00159   
00160   /* Set or Reset the URS Bit */
00161   if (TIM4_UpdateSource != TIM4_UPDATESOURCE_GLOBAL)
00162   {
00163     TIM4->CR1 |= TIM4_CR1_URS;
00164   }
00165   else
00166   {
00167     TIM4->CR1 &= (uint8_t)(~TIM4_CR1_URS);
00168   }
00169 }
00170 
00171 /**
00172   * @brief  Selects the TIM4�s One Pulse Mode.
00173   * @param   TIM4_OPMode specifies the OPM Mode to be used.
00174   * This parameter can be one of the following values
00175   *                    - TIM4_OPMODE_SINGLE
00176   *                    - TIM4_OPMODE_REPETITIVE
00177   * @retval None
00178   */
00179 void TIM4_SelectOnePulseMode(TIM4_OPMode_TypeDef TIM4_OPMode)
00180 {
00181   /* Check the parameters */
00182   assert_param(IS_TIM4_OPM_MODE_OK(TIM4_OPMode));
00183   
00184   /* Set or Reset the OPM Bit */
00185   if (TIM4_OPMode != TIM4_OPMODE_REPETITIVE)
00186   {
00187     TIM4->CR1 |= TIM4_CR1_OPM;
00188   }
00189   else
00190   {
00191     TIM4->CR1 &= (uint8_t)(~TIM4_CR1_OPM);
00192   }
00193 }
00194 
00195 /**
00196   * @brief  Configures the TIM4 Prescaler.
00197   * @param   Prescaler specifies the Prescaler Register value
00198   * This parameter can be one of the following values
00199   *                       -  TIM4_PRESCALER_1
00200   *                       -  TIM4_PRESCALER_2
00201   *                       -  TIM4_PRESCALER_4
00202   *                       -  TIM4_PRESCALER_8
00203   *                       -  TIM4_PRESCALER_16
00204   *                       -  TIM4_PRESCALER_32
00205   *                       -  TIM4_PRESCALER_64
00206   *                       -  TIM4_PRESCALER_128
00207   * @param   TIM4_PSCReloadMode specifies the TIM4 Prescaler Reload mode.
00208   * This parameter can be one of the following values
00209   *                       - TIM4_PSCRELOADMODE_IMMEDIATE: The Prescaler is loaded
00210   *                         immediately.
00211   *                       - TIM4_PSCRELOADMODE_UPDATE: The Prescaler is loaded at
00212   *                         the update event.
00213   * @retval None
00214   */
00215 void TIM4_PrescalerConfig(TIM4_Prescaler_TypeDef Prescaler, TIM4_PSCReloadMode_TypeDef TIM4_PSCReloadMode)
00216 {
00217   /* Check the parameters */
00218   assert_param(IS_TIM4_PRESCALER_RELOAD_OK(TIM4_PSCReloadMode));
00219   assert_param(IS_TIM4_PRESCALER_OK(Prescaler));
00220   
00221   /* Set the Prescaler value */
00222   TIM4->PSCR = (uint8_t)Prescaler;
00223   
00224   /* Set or reset the UG Bit */
00225   TIM4->EGR = (uint8_t)TIM4_PSCReloadMode;
00226 }
00227 
00228 /**
00229   * @brief  Enables or disables TIM4 peripheral Preload register on ARR.
00230   * @param   NewState new state of the TIM4 peripheral Preload register.
00231   * This parameter can be ENABLE or DISABLE.
00232   * @retval None
00233   */
00234 void TIM4_ARRPreloadConfig(FunctionalState NewState)
00235 {
00236   /* Check the parameters */
00237   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00238   
00239   /* Set or Reset the ARPE Bit */
00240   if (NewState != DISABLE)
00241   {
00242     TIM4->CR1 |= TIM4_CR1_ARPE;
00243   }
00244   else
00245   {
00246     TIM4->CR1 &= (uint8_t)(~TIM4_CR1_ARPE);
00247   }
00248 }
00249 
00250 /**
00251   * @brief  Configures the TIM4 event to be generated by software.
00252   * @param   TIM4_EventSource specifies the event source.
00253   * This parameter can be one of the following values:
00254   *                       - TIM4_EVENTSOURCE_UPDATE: TIM4 update Event source
00255   * @retval None
00256   */
00257 void TIM4_GenerateEvent(TIM4_EventSource_TypeDef TIM4_EventSource)
00258 {
00259   /* Check the parameters */
00260   assert_param(IS_TIM4_EVENT_SOURCE_OK(TIM4_EventSource));
00261   
00262   /* Set the event sources */
00263   TIM4->EGR = (uint8_t)(TIM4_EventSource);
00264 }
00265 
00266 /**
00267   * @brief  Sets the TIM4 Counter Register value.
00268   * @param   Counter specifies the Counter register new value.
00269   * This parameter is between 0x00 and 0xFF.
00270   * @retval None
00271   */
00272 void TIM4_SetCounter(uint8_t Counter)
00273 {
00274   /* Set the Counter Register value */
00275   TIM4->CNTR = (uint8_t)(Counter);
00276 }
00277 
00278 /**
00279   * @brief  Sets the TIM4 Autoreload Register value.
00280   * @param   Autoreload specifies the Autoreload register new value.
00281   * This parameter is between 0x00 and 0xFF.
00282   * @retval None
00283   */
00284 void TIM4_SetAutoreload(uint8_t Autoreload)
00285 {
00286   /* Set the Autoreload Register value */
00287   TIM4->ARR = (uint8_t)(Autoreload);
00288 }
00289 
00290 /**
00291   * @brief  Gets the TIM4 Counter value.
00292   * @param  None
00293   * @retval Counter Register value.
00294   */
00295 uint8_t TIM4_GetCounter(void)
00296 {
00297   /* Get the Counter Register value */
00298   return (uint8_t)(TIM4->CNTR);
00299 }
00300 
00301 /**
00302   * @brief  Gets the TIM4 Prescaler value.
00303   * @param  None
00304   * @retval Prescaler Register configuration value.
00305   */
00306 TIM4_Prescaler_TypeDef TIM4_GetPrescaler(void)
00307 {
00308   /* Get the Prescaler Register value */
00309   return (TIM4_Prescaler_TypeDef)(TIM4->PSCR);
00310 }
00311 
00312 /**
00313   * @brief  Checks whether the specified TIM4 flag is set or not.
00314   * @param   TIM4_FLAG specifies the flag to check.
00315   * This parameter can be one of the following values:
00316   *                       - TIM4_FLAG_UPDATE: TIM4 update Flag
00317   * @retval FlagStatus The new state of TIM4_FLAG (SET or RESET).
00318   */
00319 FlagStatus TIM4_GetFlagStatus(TIM4_FLAG_TypeDef TIM4_FLAG)
00320 {
00321   FlagStatus bitstatus = RESET;
00322   
00323   /* Check the parameters */
00324   assert_param(IS_TIM4_GET_FLAG_OK(TIM4_FLAG));
00325   
00326   if ((TIM4->SR1 & (uint8_t)TIM4_FLAG)  != 0)
00327   {
00328     bitstatus = SET;
00329   }
00330   else
00331   {
00332     bitstatus = RESET;
00333   }
00334   return ((FlagStatus)bitstatus);
00335 }
00336 
00337 /**
00338   * @brief  Clears the TIM4�s pending flags.
00339   * @param   TIM4_FLAG specifies the flag to clear.
00340   * This parameter can be one of the following values:
00341   *                       - TIM4_FLAG_UPDATE: TIM4 update Flag
00342   * @retval None.
00343   */
00344 void TIM4_ClearFlag(TIM4_FLAG_TypeDef TIM4_FLAG)
00345 {
00346   /* Check the parameters */
00347   assert_param(IS_TIM4_GET_FLAG_OK(TIM4_FLAG));
00348   
00349   /* Clear the flags (rc_w0) clear this bit by writing 0. Writing �1� has no effect*/
00350   TIM4->SR1 = (uint8_t)(~TIM4_FLAG);
00351 }
00352 
00353 /**
00354   * @brief  Checks whether the TIM4 interrupt has occurred or not.
00355   * @param  TIM4_IT specifies the TIM4 interrupt source to check.
00356   * This parameter can be one of the following values:
00357   *                       - TIM4_IT_UPDATE: TIM4 update Interrupt source
00358   * @retval ITStatus The new state of the TIM4_IT (SET or RESET).
00359   */
00360 ITStatus TIM4_GetITStatus(TIM4_IT_TypeDef TIM4_IT)
00361 {
00362   ITStatus bitstatus = RESET;
00363   
00364   uint8_t itstatus = 0x0, itenable = 0x0;
00365   
00366   /* Check the parameters */
00367   assert_param(IS_TIM4_IT_OK(TIM4_IT));
00368   
00369   itstatus = (uint8_t)(TIM4->SR1 & (uint8_t)TIM4_IT);
00370   
00371   itenable = (uint8_t)(TIM4->IER & (uint8_t)TIM4_IT);
00372   
00373   if ((itstatus != (uint8_t)RESET ) && (itenable != (uint8_t)RESET ))
00374   {
00375     bitstatus = (ITStatus)SET;
00376   }
00377   else
00378   {
00379     bitstatus = (ITStatus)RESET;
00380   }
00381   return ((ITStatus)bitstatus);
00382 }
00383 
00384 /**
00385   * @brief Clears the TIM4's interrupt pending bits.
00386   * @param TIM4_IT specifies the pending bit to clear.
00387   * This parameter can be one of the following values:
00388   *                       - TIM4_IT_UPDATE: TIM4 update Interrupt source
00389   * @retval None.
00390   */
00391 void TIM4_ClearITPendingBit(TIM4_IT_TypeDef TIM4_IT)
00392 {
00393   /* Check the parameters */
00394   assert_param(IS_TIM4_IT_OK(TIM4_IT));
00395   
00396   /* Clear the IT pending Bit */
00397   TIM4->SR1 = (uint8_t)(~TIM4_IT);
00398 }
00399 
00400 /**
00401   * @}
00402   */
00403   
00404   /**
00405   * @}
00406   */
00407   
00408 
00409 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com