STM32F10x_StdPeriph_Examples/ADC/3ADCs_DMA/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/3ADCs_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_3ADCs_DMA
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #define ADC1_DR_Address    ((uint32_t)0x4001244C)
00035 #define ADC3_DR_Address    ((uint32_t)0x40013C4C)
00036 
00037 /* Private macro -------------------------------------------------------------*/
00038 /* Private variables ---------------------------------------------------------*/
00039 ADC_InitTypeDef ADC_InitStructure;
00040 DMA_InitTypeDef DMA_InitStructure;
00041 __IO uint16_t ADC1ConvertedValue = 0, ADC3ConvertedValue = 0;
00042 
00043 /* Private function prototypes -----------------------------------------------*/
00044 void RCC_Configuration(void);
00045 void GPIO_Configuration(void);
00046 void NVIC_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   /* NVIC configuration ------------------------------------------------------*/
00068   NVIC_Configuration();
00069 
00070   /* GPIO configuration ------------------------------------------------------*/
00071   GPIO_Configuration();
00072 
00073   /* DMA1 channel1 configuration ----------------------------------------------*/
00074   DMA_DeInit(DMA1_Channel1);
00075   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
00076   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC1ConvertedValue;
00077   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00078   DMA_InitStructure.DMA_BufferSize = 1;
00079   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00080   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
00081   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00082   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00083   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00084   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00085   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00086   DMA_Init(DMA1_Channel1, &DMA_InitStructure);  
00087   /* Enable DMA1 channel1 */
00088   DMA_Cmd(DMA1_Channel1, ENABLE);
00089 
00090   /* DMA2 channel5 configuration ----------------------------------------------*/
00091   DMA_DeInit(DMA2_Channel5);
00092   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC3_DR_Address;
00093   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC3ConvertedValue;
00094   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00095   DMA_InitStructure.DMA_BufferSize = 1;
00096   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00097   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
00098   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00099   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00100   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00101   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00102   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00103   DMA_Init(DMA2_Channel5, &DMA_InitStructure);  
00104   /* Enable DMA2 channel5 */
00105   DMA_Cmd(DMA2_Channel5, ENABLE);
00106 
00107   /* ADC1 configuration ------------------------------------------------------*/
00108   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00109   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
00110   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
00111   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
00112   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00113   ADC_InitStructure.ADC_NbrOfChannel = 1;
00114   ADC_Init(ADC1, &ADC_InitStructure);
00115   /* ADC1 regular channels configuration */ 
00116   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);    
00117   /* Enable ADC1 DMA */
00118   ADC_DMACmd(ADC1, ENABLE);
00119 
00120   /* ADC2 configuration ------------------------------------------------------*/
00121   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00122   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
00123   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
00124   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
00125   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00126   ADC_InitStructure.ADC_NbrOfChannel = 1;
00127   ADC_Init(ADC2, &ADC_InitStructure);
00128   /* ADC2 regular channels configuration */ 
00129   ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 1, ADC_SampleTime_28Cycles5);
00130   /* Enable ADC2 EOC interupt */
00131   ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
00132 
00133   /* ADC3 configuration ------------------------------------------------------*/
00134   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00135   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
00136   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
00137   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
00138   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00139   ADC_InitStructure.ADC_NbrOfChannel = 1;
00140   ADC_Init(ADC3, &ADC_InitStructure);
00141   /* ADC3 regular channel14 configuration */ 
00142   ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_28Cycles5);
00143   /* Enable ADC3 DMA */
00144   ADC_DMACmd(ADC3, ENABLE);
00145 
00146   /* Enable ADC1 */
00147   ADC_Cmd(ADC1, ENABLE);
00148 
00149   /* Enable ADC1 reset calibaration register */   
00150   ADC_ResetCalibration(ADC1);
00151   /* Check the end of ADC1 reset calibration register */
00152   while(ADC_GetResetCalibrationStatus(ADC1));
00153 
00154   /* Start ADC1 calibaration */
00155   ADC_StartCalibration(ADC1);
00156   /* Check the end of ADC1 calibration */
00157   while(ADC_GetCalibrationStatus(ADC1));
00158 
00159   /* Enable ADC2 */
00160   ADC_Cmd(ADC2, ENABLE);
00161 
00162   /* Enable ADC2 reset calibaration register */   
00163   ADC_ResetCalibration(ADC2);
00164   /* Check the end of ADC2 reset calibration register */
00165   while(ADC_GetResetCalibrationStatus(ADC2));
00166 
00167   /* Start ADC2 calibaration */
00168   ADC_StartCalibration(ADC2);
00169   /* Check the end of ADC2 calibration */
00170   while(ADC_GetCalibrationStatus(ADC2));
00171 
00172   /* Enable ADC3 */
00173   ADC_Cmd(ADC3, ENABLE);
00174 
00175   /* Enable ADC3 reset calibaration register */   
00176   ADC_ResetCalibration(ADC3);
00177   /* Check the end of ADC3 reset calibration register */
00178   while(ADC_GetResetCalibrationStatus(ADC3));
00179 
00180   /* Start ADC3 calibaration */
00181   ADC_StartCalibration(ADC3);
00182   /* Check the end of ADC3 calibration */
00183   while(ADC_GetCalibrationStatus(ADC3));
00184 
00185   /* Start ADC1 Software Conversion */ 
00186   ADC_SoftwareStartConvCmd(ADC1, ENABLE);
00187   /* Start ADC2 Software Conversion */ 
00188   ADC_SoftwareStartConvCmd(ADC2, ENABLE);
00189   /* Start ADC3 Software Conversion */ 
00190   ADC_SoftwareStartConvCmd(ADC3, ENABLE);
00191 
00192   while (1)
00193   {
00194   }
00195 }
00196 
00197 /**
00198   * @brief  Configures the different system clocks.
00199   * @param  None
00200   * @retval None
00201   */
00202 void RCC_Configuration(void)
00203 {
00204   /* ADCCLK = PCLK2/4 */
00205   RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
00206     
00207   /* Enable peripheral clocks ------------------------------------------------*/
00208   /* Enable DMA1 and DMA2 clocks */
00209   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2, ENABLE);
00210 
00211   /* Enable ADC1, ADC2, ADC3 and GPIOC clocks */
00212   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 |
00213                          RCC_APB2Periph_ADC3 | RCC_APB2Periph_GPIOC, ENABLE);
00214 }
00215 
00216 /**
00217   * @brief  Configures the different GPIO ports.
00218   * @param  None
00219   * @retval None
00220   */
00221 void GPIO_Configuration(void)
00222 {
00223   GPIO_InitTypeDef GPIO_InitStructure;
00224 
00225   /* Configure PC.02, PC.03 and PC.04 (ADC Channel12, ADC Channel13 and 
00226      ADC Channel14) as analog inputs */
00227   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
00228   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00229   GPIO_Init(GPIOC, &GPIO_InitStructure);
00230 }
00231 
00232 /**
00233   * @brief  Configures Vector Table base location.
00234   * @param  None
00235   * @retval None
00236   */
00237 void NVIC_Configuration(void)
00238 {
00239   NVIC_InitTypeDef NVIC_InitStructure;
00240 
00241   /* Configure and enable ADC interrupt */
00242   NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
00243   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00244   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00245   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00246   NVIC_Init(&NVIC_InitStructure);
00247 }
00248 
00249 #ifdef  USE_FULL_ASSERT
00250 
00251 /**
00252   * @brief  Reports the name of the source file and the source line number
00253   *         where the assert_param error has occurred.
00254   * @param  file: pointer to the source file name
00255   * @param  line: assert_param error line source number
00256   * @retval None
00257   */
00258 void assert_failed(uint8_t* file, uint32_t line)
00259 { 
00260   /* User can add his own implementation to report the file name and line number,
00261      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00262 
00263   /* Infinite loop */
00264   while (1)
00265   {
00266   }
00267 }
00268 #endif
00269 
00270 /**
00271   * @}
00272   */ 
00273 
00274 /**
00275   * @}
00276   */ 
00277 
00278 /******************* (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