STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/COMP/COMP_PWMSignalControl/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    COMP/COMP_PWMSignalControl/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 COMP_PWMSignalControl
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private function prototypes -----------------------------------------------*/
00044 /* Private functions ---------------------------------------------------------*/
00045 static void COMP_Config(void);
00046 static void TIM_Config(void);
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_stm32f0xx.s) before to branch to application main.
00058        To reconfigure the default setting of SystemInit() function, refer to
00059        system_stm32f0xx.c file
00060      */ 
00061 
00062   /* TIM2 channels Configuration in PWM mode */
00063   TIM_Config();
00064 
00065   /* COMP2 Configuration */
00066   COMP_Config();
00067 
00068   /* Infinite loop */
00069   while (1)
00070   {
00071   }
00072 }
00073 
00074 /**
00075   * @brief  Configures TIM1: channels in PWM mode
00076   * @param  None
00077   * @retval None
00078   */
00079 static void TIM_Config(void)
00080 {
00081  
00082   TIM_BDTRInitTypeDef     TIM_BDTRInitStructure;
00083   TIM_OCInitTypeDef       TIM_OCInitStructure;
00084   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
00085   GPIO_InitTypeDef        GPIO_InitStructure;
00086 
00087   /* GPIOA clock enable */
00088   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00089 
00090   /* TIM1 channels pin configuration:
00091        TIM1_CH1 -> PA8
00092   */
00093   GPIO_StructInit(&GPIO_InitStructure);
00094   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
00095   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00096   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00097   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
00098   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
00099   GPIO_Init(GPIOA, &GPIO_InitStructure);
00100 
00101   /* Enable Alternate function on PA8 to be controlled by TIM1 */
00102   GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
00103 
00104   /* TIM1 clock enable */
00105   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
00106 
00107   /* Time Base configuration */
00108   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
00109   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00110   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00111   TIM_TimeBaseStructure.TIM_Period = 100;
00112   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00113   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
00114   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00115 
00116   /* Channel 1 Configuration in PWM mode */
00117   TIM_OCStructInit(&TIM_OCInitStructure);
00118   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
00119   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00120   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00121   TIM_OCInitStructure.TIM_Pulse = 50;
00122   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00123   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
00124   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00125   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
00126   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00127 
00128   /* Automatic Output enable, Break, dead time and lock configuration*/
00129   TIM_BDTRStructInit(&TIM_BDTRInitStructure);
00130   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
00131   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
00132   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
00133   TIM_BDTRInitStructure.TIM_DeadTime = 11;
00134   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
00135   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
00136   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
00137   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
00138 
00139   /* Main Output Enable */
00140   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00141 
00142   /* TIM1 counter enable */
00143   TIM_Cmd(TIM1, ENABLE);
00144 }
00145 
00146 /**
00147   * @brief  Configures COMP2: PA3 as COMP2 non inverting input
00148   *                           VREFINT as COMP2 inverting input
00149   *                           and COMP2 output to TIM2 BKIN.
00150   * @param  None
00151   * @retval None
00152   */
00153 static void COMP_Config(void)
00154 {
00155   
00156   COMP_InitTypeDef        COMP_InitStructure;
00157   GPIO_InitTypeDef        GPIO_InitStructure;
00158   /* GPIOA Peripheral clock enable */
00159   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00160 
00161   /* Configure PA3 in analog mode: PA3 is connected to COMP2 non inverting input */
00162   GPIO_StructInit(&GPIO_InitStructure);
00163   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
00164   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00165   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00166   GPIO_Init(GPIOA, &GPIO_InitStructure);
00167 
00168   /* COMP Peripheral clock enable */
00169   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
00170 
00171   /* COMP2 config */
00172   COMP_StructInit(&COMP_InitStructure);
00173   COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_VREFINT;
00174   COMP_InitStructure.COMP_Output = COMP_Output_TIM1BKIN;
00175   COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;
00176   COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower;
00177   COMP_InitStructure.COMP_OutputPol =  COMP_OutputPol_NonInverted;
00178   COMP_Init(COMP_Selection_COMP2, &COMP_InitStructure);
00179 
00180   /* Enable COMP2 */
00181   COMP_Cmd(COMP_Selection_COMP2, ENABLE);
00182 }
00183 
00184 
00185 #ifdef  USE_FULL_ASSERT
00186 
00187 /**
00188   * @brief  Reports the name of the source file and the source line number
00189   *         where the assert_param error has occurred.
00190   * @param  file: pointer to the source file name
00191   * @param  line: assert_param error line source number
00192   * @retval None
00193   */
00194 void assert_failed(uint8_t* file, uint32_t line)
00195 { 
00196   /* User can add his own implementation to report the file name and line number,
00197      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00198 
00199   /* Infinite loop */
00200   while (1)
00201   {
00202   }
00203 }
00204 #endif
00205 
00206 /**
00207   * @}
00208   */
00209 
00210 /**
00211   * @}
00212   */
00213 
00214 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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