/taxi_v1.2/Project/Examples/TIM/OnePulse/main.c
C | 186 lines | 57 code | 35 blank | 94 comment | 2 complexity | ff659a41c5e305f3cbff88fbb8327987 MD5 | raw file
- /**
- ******************************************************************************
- * @file TIM/OnePulse/main.c
- * @author MCD Application Team
- * @version V3.0.0
- * @date 04/06/2009
- * @brief Main program body
- ******************************************************************************
- * @copy
- *
- * 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 2009 STMicroelectronics</center></h2>
- */
-
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
-
- /** @addtogroup StdPeriph_Examples
- * @{
- */
-
- /** @addtogroup TIM_OnePulse
- * @{
- */
-
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
-
- /* Private functions ---------------------------------------------------------*/
-
- /**
- * @brief Main program
- * @param None
- * @retval : None
- */
- int main(void)
- {
- /* System Clocks Configuration */
- RCC_Configuration();
-
- /* Configure the GPIO ports */
- GPIO_Configuration();
-
- /* TIM2 configuration: One Pulse mode ------------------------
- The external signal is connected to TIM2_CH2 pin (PA.01),
- The Rising edge is used as active edge,
- The One Pulse signal is output on TIM2_CH1 pin (PA.00)
- The TIM_Pulse defines the delay value
- The (TIM_Period - TIM_Pulse) defines the One Pulse value.
- The TIM2CLK is fixed to 72 MHz, the Prescaler is 1, so the
- TIM2 counter clock is 36 MHz.
- The Autoreload value is 65535 (TIM2->ARR), so the maximum
- frequency value to trigger the TIM2 input is 500 Hz.
- The TIM_Pulse defines the delay value, the delay value is fixed
- to 455.08 us:
- delay = CCR1/TIM2 counter clock = 455.08 us.
- The (TIM_Period - TIM_Pulse) defines the One Pulse value,
- the pulse value is fixed to 1.365ms:
- One Pulse value = (TIM_Period - TIM_Pulse)/TIM2 counter clock = 1.365 ms.
- ------------------------------------------------------------ */
-
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 65535;
- TIM_TimeBaseStructure.TIM_Prescaler = 1;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
-
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
-
- /* TIM2 PWM2 Mode configuration: Channel1 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 16383;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
-
- TIM_OC1Init(TIM2, &TIM_OCInitStructure);
-
- /* TIM2 configuration in Input Capture Mode */
-
- TIM_ICStructInit(&TIM_ICInitStructure);
-
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
- TIM_ICInitStructure.TIM_ICFilter = 0;
-
- TIM_ICInit(TIM2, &TIM_ICInitStructure);
-
- /* One Pulse Mode selection */
- TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
-
- /* Input Trigger selection */
- TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
-
- /* Slave Mode selection: Trigger Mode */
- TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);
-
- while (1)
- {}
- }
-
- /**
- * @brief Configures the different system clocks.
- * @param None
- * @retval : None
- */
- void RCC_Configuration(void)
- {
- /* Setup the microcontroller system. Initialize the Embedded Flash Interface,
- initialize the PLL and update the SystemFrequency variable. */
- SystemInit();
-
- /* TIM2 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- /* GPIOA clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- }
-
- /**
- * @brief Configure the GPIOD Pins.
- * @param None
- * @retval : None
- */
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* TIM2_CH1 pin (PA.00) configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* TIM2_CH2 pin (PA.01) configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
-
- #ifdef USE_FULL_ASSERT
-
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
-
- while (1)
- {}
- }
- #endif
-
- /**
- * @}
- */
-
- /**
- * @}
- */
-
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/