STM32F10x_StdPeriph_Examples/ADC/ExtLinesTrigger/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/ExtLinesTrigger/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_ExtLinesTrigger
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #define ADC1_DR_Address    ((uint32_t)0x4001244C)
00035 
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 ADC_InitTypeDef   ADC_InitStructure;
00039 DMA_InitTypeDef   DMA_InitStructure;
00040 __IO uint16_t ADC_RegularConvertedValueTab[64], ADC_InjectedConvertedValueTab[32];
00041 
00042 /* Private function prototypes -----------------------------------------------*/
00043 void RCC_Configuration(void);
00044 void GPIO_Configuration(void);
00045 void NVIC_Configuration(void);
00046 void EXTI_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   /* EXTI configuration ------------------------------------------------------*/
00074   EXTI_Configuration();
00075 
00076   /* DMA1 channel1 configuration ----------------------------------------------*/
00077   DMA_DeInit(DMA1_Channel1);
00078   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
00079   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab;
00080   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00081   DMA_InitStructure.DMA_BufferSize = 64;
00082   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00083   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00084   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00085   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00086   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00087   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00088   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00089   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
00090   
00091   /* Enable DMA1 channel1 */
00092   DMA_Cmd(DMA1_Channel1, ENABLE);
00093   
00094   /* ADC1 configuration ------------------------------------------------------*/
00095   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00096   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
00097   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
00098   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_Ext_IT11_TIM8_TRGO;
00099   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00100   ADC_InitStructure.ADC_NbrOfChannel = 2;
00101   ADC_Init(ADC1, &ADC_InitStructure);
00102 
00103   /* ADC1 regular channels configuration */ 
00104   ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles5);
00105   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 2, ADC_SampleTime_28Cycles5);
00106 
00107   /* Regular discontinuous mode channel number configuration */
00108   ADC_DiscModeChannelCountConfig(ADC1, 1);
00109   /* Enable regular discontinuous mode */
00110   ADC_DiscModeCmd(ADC1, ENABLE);
00111 
00112   /* Enable ADC1 external trigger conversion */ 
00113   ADC_ExternalTrigConvCmd(ADC1, ENABLE);
00114 
00115   /* Set injected sequencer length */
00116   ADC_InjectedSequencerLengthConfig(ADC1, 2);
00117   /* ADC1 injected channel configuration */ 
00118   ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_28Cycles5);
00119   ADC_InjectedChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_28Cycles5);
00120   /* ADC1 injected external trigger configuration */
00121   ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_Ext_IT15_TIM8_CC4);
00122   /* Enable ADC1 injected external trigger conversion */
00123   ADC_ExternalTrigInjectedConvCmd(ADC1, ENABLE);
00124 
00125   /* Enable JEOC interupt */
00126   ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
00127 
00128   /* Enable ADC1 DMA */
00129   ADC_DMACmd(ADC1, ENABLE);
00130   
00131   /* Enable ADC1 */
00132   ADC_Cmd(ADC1, ENABLE);  
00133 
00134   /* Enable ADC1 reset calibaration register */   
00135   ADC_ResetCalibration(ADC1);
00136   /* Check the end of ADC1 reset calibration register */
00137   while(ADC_GetResetCalibrationStatus(ADC1));
00138 
00139   /* Start ADC1 calibaration */
00140   ADC_StartCalibration(ADC1);
00141   /* Check the end of ADC1 calibration */
00142   while(ADC_GetCalibrationStatus(ADC1));
00143 
00144   while (1)
00145   {
00146   }
00147 }
00148 
00149 /**
00150   * @brief  Configures the different system clocks.
00151   * @param  None
00152   * @retval None
00153   */
00154 void RCC_Configuration(void)
00155 {
00156 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00157   /* ADCCLK = PCLK2/2 */
00158   RCC_ADCCLKConfig(RCC_PCLK2_Div2);
00159 #else
00160   /* ADCCLK = PCLK2/4 */
00161   RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
00162 #endif
00163   /* Enable peripheral clocks ------------------------------------------------*/
00164   /* Enable DMA1 clock */
00165   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00166 
00167   /* Enable GPIOs and ADC1 clock */
00168   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | 
00169                          RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO |
00170                          RCC_APB2Periph_ADC1, ENABLE);
00171 }
00172 
00173 /**
00174   * @brief  Configures the different EXTI lines.
00175   * @param  None
00176   * @retval None
00177   */
00178 void EXTI_Configuration(void)
00179 {
00180   EXTI_InitTypeDef EXTI_InitStructure;
00181 #ifdef STM32F10X_HD_VL
00182   /* Select the EXTI Line11 the GPIO pin source */
00183   GPIO_EXTILineConfig(GPIO_PortSourceGPIOF, GPIO_PinSource11);
00184 #else
00185   GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource11);
00186 #endif
00187 
00188   /* EXTI line11 configuration -----------------------------------------------*/  
00189   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;
00190   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00191   EXTI_InitStructure.EXTI_Line = EXTI_Line11;
00192   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00193   EXTI_Init(&EXTI_InitStructure);
00194 
00195   /* Select the EXTI Line15 the GPIO pin source */
00196   GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource15);
00197   /* EXTI line15 configuration -----------------------------------------------*/  
00198   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;
00199   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00200   EXTI_InitStructure.EXTI_Line = EXTI_Line15;
00201   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00202   EXTI_Init(&EXTI_InitStructure);
00203 }
00204 
00205 /**
00206   * @brief  Configures the different GPIO ports.
00207   * @param  None
00208   * @retval None
00209   */
00210 void GPIO_Configuration(void)
00211 {
00212   GPIO_InitTypeDef GPIO_InitStructure;
00213 
00214   /* Configure PC.01, PC.02 and PC.04 (ADC Channel11, Channel12 and Channel14)
00215    as analog input -----------------------------------------------------------*/
00216   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_4;
00217   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00218   GPIO_Init(GPIOC, &GPIO_InitStructure);
00219 
00220   /* Configure PA.04 (ADC Channel4) as analog input --------------------------*/
00221   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
00222   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00223   GPIO_Init(GPIOA, &GPIO_InitStructure);
00224 
00225   /* Configure EXTI line11 ---------------------------------------------------*/
00226   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
00227   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00228 #ifdef STM32F10X_HD_VL 
00229   GPIO_Init(GPIOF, &GPIO_InitStructure);
00230 #else
00231   GPIO_Init(GPIOE, &GPIO_InitStructure);
00232 #endif
00233 
00234   /* Configure EXTI line15 ---------------------------------------------------*/
00235   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
00236   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00237   GPIO_Init(GPIOE, &GPIO_InitStructure);
00238 }
00239 
00240 /**
00241   * @brief  Configures NVIC and Vector Table base location.
00242   * @param  None
00243   * @retval None
00244   */
00245 void NVIC_Configuration(void)
00246 {
00247   NVIC_InitTypeDef NVIC_InitStructure;
00248 
00249     /* Configure and enable ADC interrupt */
00250 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00251   NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
00252 #else
00253   NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
00254 #endif
00255   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00256   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00257   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00258   NVIC_Init(&NVIC_InitStructure);
00259 }
00260 
00261 #ifdef  USE_FULL_ASSERT
00262 
00263 /**
00264   * @brief  Reports the name of the source file and the source line number
00265   *         where the assert_param error has occurred.
00266   * @param  file: pointer to the source file name
00267   * @param  line: assert_param error line source number
00268   * @retval None
00269   */
00270 void assert_failed(uint8_t* file, uint32_t line)
00271 { 
00272   /* User can add his own implementation to report the file name and line number,
00273      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00274 
00275   /* Infinite loop */
00276   while (1)
00277   {
00278   }
00279 }
00280 #endif
00281 
00282 /**
00283   * @}
00284   */ 
00285 
00286 /**
00287   * @}
00288   */ 
00289 
00290 /******************* (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