STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/DAC/DAC_ADC/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DAC/DAC_ADC/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 DAC_ADC
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private function prototypes -----------------------------------------------*/
00044 static void ADC_Config(void);
00045 static void DAC_Config(void);
00046 
00047 /* Private functions ---------------------------------------------------------*/
00048 
00049 /**
00050   * @brief  Main program.
00051   * @param  None
00052   * @retval None
00053   */
00054 int main(void)
00055 {
00056   /*!< At this stage the microcontroller clock setting is already configured, 
00057        this is done through SystemInit() function which is called from startup
00058        file (startup_stm32f0xx.s) before to branch to application main.
00059        To reconfigure the default setting of SystemInit() function, refer to
00060        system_stm32f0xx.c file
00061      */ 
00062 
00063   /* DAC configuration ---------------------------------------------*/
00064   DAC_Config();
00065 
00066   /* ADC configuration ---------------------------------------------*/
00067   ADC_Config();
00068 
00069   /* Infinite loop */
00070   while (1)
00071   {
00072   }
00073 }
00074 
00075 /**
00076   * @brief  Configures the DAC channel 1 with output buffer enabled.
00077   * @param  None
00078   * @retval None
00079   */
00080 static void DAC_Config(void)
00081 {
00082   DAC_InitTypeDef    DAC_InitStructure;
00083   GPIO_InitTypeDef   GPIO_InitStructure;
00084 
00085   /* Enable GPIOA clock */
00086   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00087   
00088   /* Configure PA.04 (DAC_OUT1) in analog mode -------------------------*/
00089   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
00090   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
00091   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
00092   GPIO_Init(GPIOA, &GPIO_InitStructure);
00093 
00094   /* Enable DAC clock */
00095   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
00096   
00097   /* DAC channel1 Configuration */
00098   DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
00099   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
00100   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
00101   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
00102 
00103   /* DAC Channel1 Init */
00104   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00105   
00106   /* Enable DAC Channel1 */
00107   DAC_Cmd(DAC_Channel_1, ENABLE);
00108 }
00109 
00110 /**
00111   * @brief  ADC1 channel configuration
00112   * @param  None
00113   * @retval None
00114   */
00115 static void ADC_Config(void)
00116 {  
00117   ADC_InitTypeDef    ADC_InitStructure;
00118   GPIO_InitTypeDef   GPIO_InitStructure;
00119   NVIC_InitTypeDef   NVIC_InitStructure;
00120 
00121   /* GPIOC Periph clock enable */
00122   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
00123   
00124   /* ADC1 Periph clock enable */
00125   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
00126   
00127   /* Configure ADC Channel11 as analog input */
00128 #ifdef USE_STM320518_EVAL
00129   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
00130 #else
00131   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;/* Configure ADC Channel10 as analog input */
00132 #endif /* USE_STM320518_EVAL */
00133   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00134   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
00135   GPIO_Init(GPIOC, &GPIO_InitStructure);
00136   
00137   /* ADC1 DeInit */  
00138   ADC_DeInit(ADC1);
00139   ADC_StructInit(&ADC_InitStructure);
00140   
00141   /* Configure the ADC1 in continous mode withe a resolution equal to 12 bits  */
00142   ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
00143   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
00144   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
00145   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00146   ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
00147   ADC_Init(ADC1, &ADC_InitStructure);
00148   
00149   /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ 
00150 #ifdef USE_STM320518_EVAL
00151   ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
00152 #else
00153   ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles);
00154 #endif /* USE_STM320518_EVAL */
00155  
00156   /* Enable End Of Conversion interupt */
00157   ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
00158   
00159   /* Configure and enable ADC1 interrupt */
00160   NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn;
00161   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00162   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00163   NVIC_Init(&NVIC_InitStructure);
00164   
00165   /* Enable the ADC peripheral */
00166   ADC_Cmd(ADC1, ENABLE);     
00167   
00168   /* Wait the ADRDY flag */
00169   while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));    
00170 
00171   /* ADC1 Start Conversion */ 
00172   ADC_StartOfConversion(ADC1);
00173 }
00174 
00175 #ifdef  USE_FULL_ASSERT
00176 
00177 /**
00178   * @brief  Reports the name of the source file and the source line number
00179   *         where the assert_param error has occurred.
00180   * @param  file: pointer to the source file name
00181   * @param  line: assert_param error line source number
00182   * @retval None
00183   */
00184 void assert_failed(uint8_t* file, uint32_t line)
00185 { 
00186   /* User can add his own implementation to report the file name and line number,
00187      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00188 
00189   /* Infinite loop */
00190   while (1)
00191   {
00192   }
00193 }
00194 #endif
00195 
00196 /**
00197   * @}
00198   */
00199 
00200 /**
00201   * @}
00202   */
00203 
00204 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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