STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_EncoderMode/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_EncoderMode/main.c  
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
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 "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup TIM_EncoderMode
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint16_t PrescalerValue = 0;
00044 
00045 /* Private function prototypes -----------------------------------------------*/
00046 static void TIM_Config(void);
00047 
00048 /* Private functions ---------------------------------------------------------*/
00049 
00050 /**
00051   * @brief  Main program.
00052   * @param  None
00053   * @retval None
00054   */
00055 int main(void)
00056 {
00057   /*!< At this stage the microcontroller clock setting is already configured, 
00058        this is done through SystemInit() function which is called from startup
00059        file (startup_stm32f0xx.s) before to branch to application main.
00060        To reconfigure the default setting of SystemInit() function, refer to
00061        system_stm32f0xx.c file
00062      */ 
00063 
00064   /* TIM Configuration */
00065   TIM_Config();
00066   
00067   /* Infinite loop */
00068   while (1)
00069   {
00070   }
00071 }
00072 
00073 /**
00074   * @brief  Configure the TIM pins and the TIM IRQ Handler.
00075   * @param  None
00076   * @retval None
00077   */
00078 static void TIM_Config(void)
00079 {
00080   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00081   NVIC_InitTypeDef NVIC_InitStructure;
00082   GPIO_InitTypeDef GPIO_InitStructure;
00083 
00084   /* TIM3 clock enable */
00085   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00086 
00087   /* GPIOA clock enable */
00088   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00089   
00090   /* TIM1 channel 2 pin (PE.11) configuration */
00091   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
00092   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00093   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00094   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00095   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00096   GPIO_Init(GPIOA, &GPIO_InitStructure);
00097 
00098   /* Connect TIM pins to AF1 */
00099   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
00100   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
00101   
00102   /* -----------------------------------------------------------------------
00103     TIM3 Configuration: Encoder mode1:
00104     
00105     In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1).
00106       TIM3CLK = PCLK1  
00107       PCLK1 = HCLK
00108       => TIM3CLK = HCLK = SystemCoreClock
00109           
00110     To get TIM3 counter clock at 6 MHz, the prescaler is computed as follows:
00111        Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00112        Prescaler = ((SystemCoreClock) /6 MHz) - 1
00113 
00114    TIM3 is configured to interface with an encoder:
00115    - The encoder mode is encoder mode1: Counter counts up/down on TI2 rising edge 
00116    depending on TI1 level 
00117 
00118    - The Autoreload value is set to 399, so the encoder round is 400 TIM counter clock.  
00119                                               
00120 
00121     Note: 
00122      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
00123      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
00124      function to update SystemCoreClock variable value. Otherwise, any configuration
00125      based on this variable will be incorrect.    
00126   ----------------------------------------------------------------------- */   
00127 
00128 
00129   /* Compute the prescaler value */
00130   PrescalerValue = (uint16_t) ((SystemCoreClock ) / 6000000) - 1;
00131 
00132   /* Time base configuration */
00133   TIM_TimeBaseStructure.TIM_Period = 399;
00134   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00135   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00136   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00137 
00138   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00139 
00140   /* Prescaler configuration */
00141   TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
00142 
00143   TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
00144    
00145   /* TIM Interrupts enable */
00146   TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
00147 
00148   /* TIM3 enable counter */
00149   TIM_Cmd(TIM3, ENABLE);
00150   
00151   /* Enable the TIM3 gloabal Interrupt */
00152   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
00153   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00154   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00155   NVIC_Init(&NVIC_InitStructure);
00156 }
00157 
00158 #ifdef  USE_FULL_ASSERT
00159 
00160 /**
00161   * @brief  Reports the name of the source file and the source line number
00162   *         where the assert_param error has occurred.
00163   * @param  file: pointer to the source file name
00164   * @param  line: assert_param error line source number
00165   * @retval None
00166   */
00167 void assert_failed(uint8_t* file, uint32_t line)
00168 { 
00169   /* User can add his own implementation to report the file name and line number,
00170      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00171 
00172   /* Infinite loop */
00173   while (1)
00174   {
00175   }
00176 }
00177 #endif
00178 
00179 /**
00180   * @}
00181   */
00182 
00183 /**
00184   * @}
00185   */
00186 
00187 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32