STM32F10x_StdPeriph_Examples/ADC/ADC1_DMA/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/ADC1_DMA/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 ADC_ADC1_DMA
00029   * @{
00030   */ 
00031 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 #define ADC1_DR_Address    ((uint32_t)0x4001244C)
00036 
00037 /* Private macro -------------------------------------------------------------*/
00038 /* Private variables ---------------------------------------------------------*/
00039 ADC_InitTypeDef ADC_InitStructure;
00040 DMA_InitTypeDef DMA_InitStructure;
00041 __IO uint16_t ADCConvertedValue;
00042 
00043 /* Private function prototypes -----------------------------------------------*/
00044 void RCC_Configuration(void);
00045 void GPIO_Configuration(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_stm32f10x_xx.s) before to branch to application main.
00059        To reconfigure the default setting of SystemInit() function, refer to
00060        system_stm32f10x.c file
00061      */     
00062        
00063   /* System clocks configuration ---------------------------------------------*/
00064   RCC_Configuration();
00065 
00066   /* GPIO configuration ------------------------------------------------------*/
00067   GPIO_Configuration();
00068 
00069   /* DMA1 channel1 configuration ----------------------------------------------*/
00070   DMA_DeInit(DMA1_Channel1);
00071   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
00072   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
00073   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00074   DMA_InitStructure.DMA_BufferSize = 1;
00075   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00076   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
00077   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00078   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00079   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00080   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00081   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00082   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
00083   
00084   /* Enable DMA1 channel1 */
00085   DMA_Cmd(DMA1_Channel1, ENABLE);
00086   
00087   /* ADC1 configuration ------------------------------------------------------*/
00088   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00089   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
00090   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
00091   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
00092   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00093   ADC_InitStructure.ADC_NbrOfChannel = 1;
00094   ADC_Init(ADC1, &ADC_InitStructure);
00095 
00096   /* ADC1 regular channel14 configuration */ 
00097   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
00098 
00099   /* Enable ADC1 DMA */
00100   ADC_DMACmd(ADC1, ENABLE);
00101   
00102   /* Enable ADC1 */
00103   ADC_Cmd(ADC1, ENABLE);
00104 
00105   /* Enable ADC1 reset calibaration register */   
00106   ADC_ResetCalibration(ADC1);
00107   /* Check the end of ADC1 reset calibration register */
00108   while(ADC_GetResetCalibrationStatus(ADC1));
00109 
00110   /* Start ADC1 calibaration */
00111   ADC_StartCalibration(ADC1);
00112   /* Check the end of ADC1 calibration */
00113   while(ADC_GetCalibrationStatus(ADC1));
00114      
00115   /* Start ADC1 Software Conversion */ 
00116   ADC_SoftwareStartConvCmd(ADC1, ENABLE);
00117 
00118   while (1)
00119   {
00120   }
00121 }
00122 
00123 /**
00124   * @brief  Configures the different system clocks.
00125   * @param  None
00126   * @retval None
00127   */
00128 void RCC_Configuration(void)
00129 {
00130 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00131   /* ADCCLK = PCLK2/2 */
00132   RCC_ADCCLKConfig(RCC_PCLK2_Div2); 
00133 #else
00134   /* ADCCLK = PCLK2/4 */
00135   RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
00136 #endif
00137   /* Enable peripheral clocks ------------------------------------------------*/
00138   /* Enable DMA1 clock */
00139   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00140 
00141   /* Enable ADC1 and GPIOC clock */
00142   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
00143 }
00144 
00145 /**
00146   * @brief  Configures the different GPIO ports.
00147   * @param  None
00148   * @retval None
00149   */
00150 void GPIO_Configuration(void)
00151 {
00152   GPIO_InitTypeDef GPIO_InitStructure;
00153 
00154   /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
00155   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
00156   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00157   GPIO_Init(GPIOC, &GPIO_InitStructure);
00158 }
00159 
00160 #ifdef  USE_FULL_ASSERT
00161 
00162 /**
00163   * @brief  Reports the name of the source file and the source line number
00164   *         where the assert_param error has occurred.
00165   * @param  file: pointer to the source file name
00166   * @param  line: assert_param error line source number
00167   * @retval None
00168   */
00169 void assert_failed(uint8_t* file, uint32_t line)
00170 { 
00171   /* User can add his own implementation to report the file name and line number,
00172      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00173 
00174   /* Infinite loop */
00175   while (1)
00176   {
00177   }
00178 }
00179 #endif
00180 
00181 /**
00182   * @}
00183   */ 
00184 
00185 /**
00186   * @}
00187   */ 
00188 
00189 /******************* (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