STM32F10x_StdPeriph_Examples/TIM/OnePulse/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/OnePulse/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup TIM_OnePulse
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Private macro -------------------------------------------------------------*/
00035 /* Private variables ---------------------------------------------------------*/
00036 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00037 TIM_ICInitTypeDef  TIM_ICInitStructure;
00038 TIM_OCInitTypeDef  TIM_OCInitStructure;
00039 uint16_t PrescalerValue = 0;
00040 
00041 /* Private function prototypes -----------------------------------------------*/
00042 void RCC_Configuration(void);
00043 void GPIO_Configuration(void);
00044 
00045 /* Private functions ---------------------------------------------------------*/
00046 
00047 /**
00048   * @brief   Main program
00049   * @param  None
00050   * @retval None
00051   */
00052 int main(void)
00053 {
00054   /*!< At this stage the microcontroller clock setting is already configured, 
00055        this is done through SystemInit() function which is called from startup
00056        file (startup_stm32f10x_xx.s) before to branch to application main.
00057        To reconfigure the default setting of SystemInit() function, refer to
00058        system_stm32f10x.c file
00059      */     
00060        
00061   /* System Clocks Configuration */
00062   RCC_Configuration();
00063 
00064   /* Configure the GPIO ports */
00065   GPIO_Configuration();
00066 
00067   /* TIM4 configuration: One Pulse mode ------------------------
00068      The external signal is connected to TIM4_CH2 pin (PB.07), 
00069      The Rising edge is used as active edge,
00070      The One Pulse signal is output on TIM4_CH1 pin (PB.06)
00071      The TIM_Pulse defines the delay value 
00072      The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
00073      TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz:
00074      - Prescaler = (TIM2CLK / TIM2 counter clock) - 1
00075      The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value 
00076      to trigger the TIM4 input is 24000000/65535 = 300 Hz.
00077 
00078      The TIM_Pulse defines the delay value, the delay value is fixed 
00079      to 682.6 us:
00080      delay =  CCR1/TIM4 counter clock = 682.6 us. 
00081      The (TIM_Period - TIM_Pulse) defines the One Pulse value, 
00082      the pulse value is fixed to 2.048 ms:
00083      One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = 2.048 ms.
00084 
00085   * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
00086     and Connectivity line devices and to 24 MHz for Value line devices
00087   ------------------------------------------------------------ */
00088 
00089   /* Compute the prescaler value */
00090   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
00091   /* Time base configuration */
00092   TIM_TimeBaseStructure.TIM_Period = 65535;
00093   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00094   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00095   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00096 
00097   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
00098 
00099   /* TIM4 PWM2 Mode configuration: Channel1 */
00100   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
00101   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00102   TIM_OCInitStructure.TIM_Pulse = 16383;
00103   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00104 
00105   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
00106 
00107   /* TIM4 configuration in Input Capture Mode */
00108 
00109   TIM_ICStructInit(&TIM_ICInitStructure);
00110 
00111   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
00112   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
00113   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
00114   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
00115   TIM_ICInitStructure.TIM_ICFilter = 0;
00116 
00117   TIM_ICInit(TIM4, &TIM_ICInitStructure);
00118 
00119   /* One Pulse Mode selection */
00120   TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);
00121 
00122   /* Input Trigger selection */
00123   TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
00124 
00125   /* Slave Mode selection: Trigger Mode */
00126   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);
00127 
00128   while (1)
00129   {}
00130 }
00131 
00132 /**
00133   * @brief  Configures the different system clocks.
00134   * @param  None
00135   * @retval None
00136   */
00137 void RCC_Configuration(void)
00138 {
00139   /* TIM4 clock enable */
00140   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
00141 
00142   /* GPIOB clock enable */
00143   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
00144 }
00145 
00146 /**
00147   * @brief  Configure the GPIOD Pins.
00148   * @param  None
00149   * @retval None
00150   */
00151 void GPIO_Configuration(void)
00152 {
00153   GPIO_InitTypeDef GPIO_InitStructure;
00154 
00155   /* TIM4_CH1 pin (PB.06) configuration */
00156   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00157   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00158   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00159 
00160   GPIO_Init(GPIOB, &GPIO_InitStructure);
00161 
00162   /* TIM4_CH2 pin (PB.07) configuration */
00163   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
00164   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00165 
00166   GPIO_Init(GPIOB, &GPIO_InitStructure);
00167 }
00168 
00169 #ifdef  USE_FULL_ASSERT
00170 
00171 /**
00172   * @brief  Reports the name of the source file and the source line number
00173   *         where the assert_param error has occurred.
00174   * @param  file: pointer to the source file name
00175   * @param  line: assert_param error line source number
00176   * @retval None
00177   */
00178 void assert_failed(uint8_t* file, uint32_t line)
00179 {
00180   /* User can add his own implementation to report the file name and line number,
00181      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00182 
00183   while (1)
00184   {}
00185 }
00186 #endif
00187 
00188 /**
00189   * @}
00190   */ 
00191 
00192 /**
00193   * @}
00194   */ 
00195 
00196 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32