STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_OCToggle/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_OCToggle/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_OCToggle
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 __IO uint16_t CCR1_Val = 40961;
00044 __IO uint16_t CCR2_Val = 20480;
00045 __IO uint16_t CCR3_Val = 10240;
00046 __IO uint16_t CCR4_Val = 5120;
00047 uint16_t PrescalerValue = 0;
00048 
00049 /* Private function prototypes -----------------------------------------------*/
00050 static void TIM_Config(void);
00051 /* Private functions ---------------------------------------------------------*/
00052 
00053 /**
00054   * @brief  Main program.
00055   * @param  None
00056   * @retval None
00057   */
00058 int main(void)
00059 {
00060   /*!< At this stage the microcontroller clock setting is already configured, 
00061        this is done through SystemInit() function which is called from startup
00062        file (startup_stm32f0xx.s) before to branch to application main.
00063        To reconfigure the default setting of SystemInit() function, refer to
00064        system_stm32f0xx.c file
00065      */ 
00066 
00067   /* TIM3 Configuration */
00068   TIM_Config();
00069 
00070   /* Infinite loop */
00071   while (1)
00072   {
00073   }
00074 }
00075 
00076 /**
00077   * @brief  Configure the TIM3 Pins.
00078   * @param  None
00079   * @retval None
00080   */
00081 static void TIM_Config(void)
00082 {
00083   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00084   TIM_OCInitTypeDef  TIM_OCInitStructure;
00085   GPIO_InitTypeDef GPIO_InitStructure;
00086   NVIC_InitTypeDef NVIC_InitStructure;
00087   
00088   /* TIM3 clock enable */
00089   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00090 
00091   /* GPIOA and GPIOB clock enable */
00092   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
00093 
00094   /* GPIOA Configuration: TIM3 CH1 (PA6) and TIM3 CH2 (PA7) */
00095   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
00096   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00097   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00098   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00099   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
00100   GPIO_Init(GPIOA, &GPIO_InitStructure); 
00101 
00102   /* GPIOB Configuration: TIM3 CH2 (PB0) and TIM3 CH4 (PB1) */
00103   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00104   GPIO_Init(GPIOB, &GPIO_InitStructure);
00105 
00106   /* Connect TIM Channels to AF1 */
00107   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
00108   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1); 
00109   GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
00110   GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
00111 
00112   /* Enable the TIM3 global Interrupt */
00113   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
00114   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00115   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00116   NVIC_Init(&NVIC_InitStructure);
00117 
00118   /* ---------------------------------------------------------------------------
00119     TIM3 Configuration: Output Compare Toggle Mode:
00120     
00121     In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1).
00122       => TIM3CLK = PCLK1 = 48 MHz
00123                                               
00124      CC1 update rate = TIM3 counter clock / CCR1_Val = 1171.8 Hz
00125            ==> So the TIM3 Channel 1 generates a periodic signal with a 
00126                frequency equal to 585.9 Hz.
00127 
00128      CC2 update rate = TIM3 counter clock / CCR2_Val = 2343.75 Hz
00129            ==> So the TIM3 Channel 2 generates a periodic signal with a 
00130                frequency equal to 1171.8 Hz.
00131 
00132      CC3 update rate = TIM3 counter clock / CCR3_Val = 4687.5 Hz
00133            ==> So the TIM3 Channel 3 generates a periodic signal with a 
00134                frequency equal to 2343.75 Hz.
00135 
00136      CC4 update rate = TIM3 counter clock / CCR4_Val = 9375 Hz
00137            ==> So the TIM3 Channel 4 generates a periodic signal with a 
00138                frequency equal to 4687.5 Hz. 
00139   --------------------------------------------------------------------------- */   
00140 
00141   /* Time base configuration */
00142   TIM_TimeBaseStructure.TIM_Period = 65535;
00143   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00144   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00145   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00146 
00147   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00148 
00149   /* Output Compare Toggle Mode configuration: Channel1 */
00150   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
00151   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00152   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00153   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00154   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00155 
00156   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
00157 
00158   /* Output Compare Toggle Mode configuration: Channel2 */
00159   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00160   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00161 
00162   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00163 
00164   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
00165 
00166   /* Output Compare Toggle Mode configuration: Channel3 */
00167   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00168   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00169 
00170   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00171 
00172   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
00173 
00174   /* Output Compare Toggle Mode configuration: Channel4 */
00175   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00176   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00177 
00178   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00179 
00180   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
00181 
00182   /* TIM enable counter */
00183   TIM_Cmd(TIM3, ENABLE);
00184 
00185   /* TIM IT enable */
00186   TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
00187 }
00188 
00189 #ifdef  USE_FULL_ASSERT
00190 
00191 /**
00192   * @brief  Reports the name of the source file and the source line number
00193   *         where the assert_param error has occurred.
00194   * @param  file: pointer to the source file name
00195   * @param  line: assert_param error line source number
00196   * @retval None
00197   */
00198 void assert_failed(uint8_t* file, uint32_t line)
00199 { 
00200   /* User can add his own implementation to report the file name and line number,
00201      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00202 
00203   /* Infinite loop */
00204   while (1)
00205   {
00206   }
00207 }
00208 #endif
00209 
00210 /**
00211   * @}
00212   */
00213 
00214 /**
00215   * @}
00216   */
00217 
00218 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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