STM32F10x_StdPeriph_Examples/TIM/OCActive/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/OCActive/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_OCActive
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 uint16_t CCR1_Val = 1000;
00039 uint16_t CCR2_Val = 500;
00040 uint16_t CCR3_Val = 250;
00041 uint16_t CCR4_Val = 125;
00042 uint16_t PrescalerValue = 0;
00043 
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_Configuration(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_stm32f10x_xx.s) before to branch to application main.
00060        To reconfigure the default setting of SystemInit() function, refer to
00061        system_stm32f10x.c file
00062      */     
00063        
00064  /* System Clocks Configuration */
00065   RCC_Configuration();
00066 
00067   /* Configure the GPIO ports */
00068   GPIO_Configuration();
00069 
00070   /* ---------------------------------------------------------------
00071     TIM3 Configuration: 
00072     TIM3CLK = SystemCoreClock / 2,
00073     The objective is to get TIM3 counter clock at 1 KHz:
00074      - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00075     And generate 4 signals with 4 different delays:
00076     TIM3_CH1 delay = CCR1_Val/TIM3 counter clock = 1000 ms
00077     TIM3_CH2 delay = CCR2_Val/TIM3 counter clock = 500 ms
00078     TIM3_CH3 delay = CCR3_Val/TIM3 counter clock = 250 ms
00079     TIM3_CH4 delay = CCR4_Val/TIM3 counter clock = 125 ms
00080 
00081   * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
00082     and Connectivity line devices and to 24 MHz for Low-Density Value line and
00083     Medium-Density Value line devices
00084   --------------------------------------------------------------- */
00085   /*Compute the prescaler value */
00086   PrescalerValue = (uint16_t) (SystemCoreClock / 2000) - 1;
00087   /* Time base configuration */
00088   TIM_TimeBaseStructure.TIM_Period = 65535;
00089   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00090   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00091   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00092 
00093   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00094 
00095   /* Output Compare Active Mode configuration: Channel1 */
00096   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
00097   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00098   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00099   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00100   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00101 
00102   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
00103 
00104   /* Output Compare Active Mode configuration: Channel2 */
00105   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00106   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00107 
00108   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00109 
00110   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
00111 
00112   /* Output Compare Active Mode configuration: Channel3 */
00113   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00114   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00115 
00116   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00117 
00118   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
00119 
00120   /* Output Compare Active Mode configuration: Channel4 */
00121   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00122   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00123 
00124   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00125 
00126   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
00127 
00128   TIM_ARRPreloadConfig(TIM3, ENABLE);
00129 
00130 #ifdef STM32F10X_CL
00131   /* Set PD.07 pin */
00132   GPIO_SetBits(GPIOD, GPIO_Pin_7);
00133 #else
00134   /* Set PC.06 pin */
00135   GPIO_SetBits(GPIOC, GPIO_Pin_6);
00136 #endif
00137 
00138   /* TIM3 enable counter */
00139   TIM_Cmd(TIM3, ENABLE);
00140 
00141   while (1)
00142   {}
00143 }
00144 
00145 /**
00146   * @brief  Configures the different system clocks.
00147   * @param  None
00148   * @retval None
00149   */
00150 void RCC_Configuration(void)
00151 {
00152   /* PCLK1 = HCLK/4 */
00153   RCC_PCLK1Config(RCC_HCLK_Div4);
00154 
00155   /* TIM3 clock enable */
00156   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00157 
00158   /* GPIOA and GPIOC clock enable */
00159   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB |
00160                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
00161 }
00162 
00163 /**
00164   * @brief  Configure the TIM3 and the GPIOE Pins.
00165   * @param  None
00166   * @retval None
00167   */
00168 void GPIO_Configuration(void)
00169 {
00170   GPIO_InitTypeDef GPIO_InitStructure;
00171 
00172 #ifdef STM32F10X_CL
00173   /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
00174   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
00175   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00176   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00177 
00178   GPIO_Init(GPIOC, &GPIO_InitStructure);
00179 
00180   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00181 
00182  /* GPIOD Configuration: Pin7 an Output push-pull */
00183   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
00184   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
00185 
00186   GPIO_Init(GPIOD, &GPIO_InitStructure);
00187 #else
00188   /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
00189   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
00190   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00191   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00192 
00193   GPIO_Init(GPIOA, &GPIO_InitStructure);
00194 
00195   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00196   GPIO_Init(GPIOB, &GPIO_InitStructure);
00197 
00198   /* GPIOC Configuration: Pin6 an Output push-pull */
00199   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00200   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
00201 
00202   GPIO_Init(GPIOC, &GPIO_InitStructure);
00203 #endif
00204 }
00205 
00206 #ifdef  USE_FULL_ASSERT
00207 
00208 /**
00209   * @brief  Reports the name of the source file and the source line number
00210   *         where the assert_param error has occurred.
00211   * @param  file: pointer to the source file name
00212   * @param  line: assert_param error line source number
00213   * @retval None
00214   */
00215 void assert_failed(uint8_t* file, uint32_t line)
00216 {
00217   /* User can add his own implementation to report the file name and line number,
00218      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00219 
00220   while (1)
00221   {}
00222 }
00223 #endif
00224 
00225 /**
00226   * @}
00227   */ 
00228 
00229 /**
00230   * @}
00231   */ 
00232 
00233 /******************* (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