STM32F10x_StdPeriph_Examples/TIM/TIM9_OCToggle/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM9_OCToggle/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 TIM9_OCToggle
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Private macro -------------------------------------------------------------*/
00035 /* Private variables ---------------------------------------------------------*/
00036 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00037 TIM_OCInitTypeDef  TIM_OCInitStructure;
00038 __IO uint16_t CCR1Val = 32768;
00039 __IO uint16_t CCR2Val = 16384;
00040 uint16_t PrescalerValue = 0;
00041 
00042 /* Private function prototypes -----------------------------------------------*/
00043 void GPIO_Configuration(void);
00044 void NVIC_Configuration(void);
00045 
00046 /* Private functions ---------------------------------------------------------*/
00047 
00048 /**
00049   * @brief  Main program
00050   * @param  None
00051   * @retval None
00052   */
00053 int main(void)
00054 {
00055   /*!< At this stage the microcontroller clock setting is already configured, 
00056        this is done through SystemInit() function which is called from startup
00057        file (startup_stm32f10x_xx.s) before to branch to application main.
00058        To reconfigure the default setting of SystemInit() function, refer to
00059        system_stm32f10x.c file
00060      */     
00061        
00062   /* Configure TIM9 pins */
00063   GPIO_Configuration();
00064 
00065   /* NVIC Configuration */
00066   NVIC_Configuration();
00067 
00068   /* ---------------------------------------------------------------------------
00069     TIM9 Configuration: Output Compare Toggle Mode:
00070     TIM9CLK = SystemCoreClock (72MHz),
00071     The objective is to get TIM9 counter clock at 24 MHz:
00072      - Prescaler = (TIM9CLK / TIM9 counter clock) - 1
00073     CC1 update rate = TIM9 counter clock / CCR1Val = 732.4 Hz
00074     CC2 update rate = TIM9 counter clock / CCR2Val = 1464.8 Hz
00075   ----------------------------------------------------------------------------*/
00076   /* Compute the prescaler value */
00077   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
00078 
00079   /* Time base configuration */
00080   TIM_TimeBaseStructure.TIM_Period = 65535;
00081   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00082   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00083   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00084 
00085   TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
00086 
00087   /* Output Compare Toggle Mode configuration: Channel1 */
00088   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
00089   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00090   TIM_OCInitStructure.TIM_Pulse = CCR1Val;
00091   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00092   TIM_OC1Init(TIM9, &TIM_OCInitStructure);
00093 
00094   TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Disable);
00095 
00096   /* Output Compare Toggle Mode configuration: Channel2 */
00097   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00098   TIM_OCInitStructure.TIM_Pulse = CCR2Val;
00099 
00100   TIM_OC2Init(TIM9, &TIM_OCInitStructure);
00101 
00102   TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Disable);
00103 
00104   /* TIM enable counter */
00105   TIM_Cmd(TIM9, ENABLE);
00106 
00107   /* TIM IT enable */
00108   TIM_ITConfig(TIM9, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
00109 
00110   while (1)
00111   {
00112   }
00113 }
00114 
00115 /**
00116   * @brief  Configure TIM9 pins.
00117   * @param  None
00118   * @retval None
00119   */
00120 void GPIO_Configuration(void)
00121 {
00122   GPIO_InitTypeDef GPIO_InitStructure;
00123 
00124   /* Enable TIM9 and GPIOA clock  */
00125   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9 | RCC_APB2Periph_GPIOA, ENABLE);
00126   
00127   /* GPIOA Configuration:TIM9 Channel1 and 2 as alternate function push-pull */
00128   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
00129   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00130   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00131   
00132   GPIO_Init(GPIOA, &GPIO_InitStructure);
00133 }
00134 
00135 /**
00136   * @brief  Configure the nested vectored interrupt controller.
00137   * @param  None
00138   * @retval None
00139   */
00140 void NVIC_Configuration(void)
00141 {
00142   NVIC_InitTypeDef NVIC_InitStructure;
00143 
00144   /* Enable the TIM9 global Interrupt */
00145   NVIC_InitStructure.NVIC_IRQChannel =  TIM1_BRK_TIM9_IRQn;
00146   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00147   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00148   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00149   NVIC_Init(&NVIC_InitStructure);
00150 }
00151 
00152 #ifdef  USE_FULL_ASSERT
00153 
00154 /**
00155   * @brief  Reports the name of the source file and the source line number
00156   *         where the assert_param error has occurred.
00157   * @param  file: pointer to the source file name
00158   * @param  line: assert_param error line source number
00159   * @retval None
00160   */
00161 void assert_failed(uint8_t* file, uint32_t line)
00162 {
00163   /* User can add his own implementation to report the file name and line number,
00164      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00165 
00166   while (1)
00167   {}
00168 }
00169 #endif
00170 /**
00171   * @}
00172   */ 
00173 
00174 /**
00175   * @}
00176   */ 
00177 
00178 /******************* (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