STM32F10x_StdPeriph_Examples/ADC/TIMTrigger_AutoInjection/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    ADC/TIMTrigger_AutoInjection/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_TIMTrigger_AutoInjection
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 TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
00041 TIM_OCInitTypeDef         TIM_OCInitStructure;
00042 __IO uint16_t ADC_RegularConvertedValueTab[32], ADC_InjectedConvertedValueTab[32];
00043 
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_Configuration(void);
00047 void NVIC_Configuration(void);
00048   
00049 /* Private functions ---------------------------------------------------------*/
00050 
00051 /**
00052   * @brief   Main program
00053   * @param  None
00054   * @retval None
00055   */
00056 int main(void)
00057 {
00058   /*!< At this stage the microcontroller clock setting is already configured, 
00059        this is done through SystemInit() function which is called from startup
00060        file (startup_stm32f10x_xx.s) before to branch to application main.
00061        To reconfigure the default setting of SystemInit() function, refer to
00062        system_stm32f10x.c file
00063      */     
00064        
00065   /* System clocks configuration ---------------------------------------------*/
00066   RCC_Configuration();
00067 
00068   /* NVIC configuration ------------------------------------------------------*/
00069   NVIC_Configuration();
00070 
00071   /* GPIO configuration ------------------------------------------------------*/
00072   GPIO_Configuration();
00073 
00074   /* TIM1 configuration ------------------------------------------------------*/ 
00075   /* Time Base configuration */
00076   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
00077   TIM_TimeBaseStructure.TIM_Period = 0xFF;          
00078   TIM_TimeBaseStructure.TIM_Prescaler = 0x4;       
00079   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
00080   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
00081   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00082   /* TIM1 channel1 configuration in PWM mode */
00083   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
00084   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                
00085   TIM_OCInitStructure.TIM_Pulse = 0x7F; 
00086   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;         
00087   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00088 
00089   /* DMA1 Channel1 Configuration ----------------------------------------------*/
00090   DMA_DeInit(DMA1_Channel1);
00091   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
00092   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab;
00093   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00094   DMA_InitStructure.DMA_BufferSize = 32;
00095   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00096   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00097   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00098   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00099   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
00100   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00101   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00102   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
00103   
00104   /* Enable DMA1 channel1 */
00105   DMA_Cmd(DMA1_Channel1, ENABLE);
00106 
00107   /* ADC1 configuration ------------------------------------------------------*/
00108   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00109   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
00110   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
00111   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
00112   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00113   ADC_InitStructure.ADC_NbrOfChannel = 1;
00114   ADC_Init(ADC1, &ADC_InitStructure);
00115 
00116   /* ADC1 regular channel14 configuration */ 
00117   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5);
00118 
00119   /* Set injected sequencer length */
00120   ADC_InjectedSequencerLengthConfig(ADC1, 1);
00121   /* ADC1 injected channel Configuration */ 
00122   ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5);
00123   /* ADC1 injected external trigger configuration */
00124   ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
00125 
00126   /* Enable automatic injected conversion start after regular one */
00127   ADC_AutoInjectedConvCmd(ADC1, ENABLE);
00128 
00129   /* Enable ADC1 DMA */
00130   ADC_DMACmd(ADC1, ENABLE);
00131 
00132   /* Enable ADC1 external trigger */ 
00133   ADC_ExternalTrigConvCmd(ADC1, ENABLE);
00134 
00135   /* Enable JEOC interupt */
00136   ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
00137 
00138   /* Enable ADC1 */
00139   ADC_Cmd(ADC1, ENABLE);
00140 
00141   /* Enable ADC1 reset calibaration register */   
00142   ADC_ResetCalibration(ADC1);
00143   /* Check the end of ADC1 reset calibration register */
00144   while(ADC_GetResetCalibrationStatus(ADC1));
00145 
00146   /* Start ADC1 calibaration */
00147   ADC_StartCalibration(ADC1);
00148   /* Check the end of ADC1 calibration */
00149   while(ADC_GetCalibrationStatus(ADC1));
00150 
00151   /* TIM1 counter enable */
00152   TIM_Cmd(TIM1, ENABLE);
00153   /* TIM1 main Output Enable */
00154   TIM_CtrlPWMOutputs(TIM1, ENABLE);  
00155 
00156   /* Test on channel1 transfer complete flag */
00157   while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
00158   /* Clear channel1 transfer complete flag */
00159   DMA_ClearFlag(DMA1_FLAG_TC1); 
00160 
00161   /* TIM1 counter disable */
00162   TIM_Cmd(TIM1, DISABLE);
00163 
00164   while (1)
00165   {
00166   }
00167 }
00168 
00169 /**
00170   * @brief  Configures the different system clocks.
00171   * @param  None
00172   * @retval None
00173   */
00174 void RCC_Configuration(void)
00175 {
00176 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00177   /* ADCCLK = PCLK2/2 */
00178   RCC_ADCCLKConfig(RCC_PCLK2_Div2);
00179 #else
00180   /* ADCCLK = PCLK2/4 */
00181   RCC_ADCCLKConfig(RCC_PCLK2_Div4);
00182 #endif
00183   /* Enable peripheral clocks ------------------------------------------------*/
00184   /* Enable DMA1 clock */
00185   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00186 
00187   /* Enable GPIOA, GPIOC, ADC1 and TIM1 clock */
00188   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC |
00189                          RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1, ENABLE);
00190 }
00191 
00192 /**
00193   * @brief  Configures the different GPIO ports.
00194   * @param  None
00195   * @retval None
00196   */
00197 void GPIO_Configuration(void)
00198 {
00199   GPIO_InitTypeDef GPIO_InitStructure;
00200 
00201   /* Configure TIM1_CH1 (PA8) as alternate function push-pull */
00202   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
00203   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00204   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00205   GPIO_Init(GPIOA, &GPIO_InitStructure);
00206 
00207   /* Configure PC.06 as output push-pull */
00208   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00209   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
00210   GPIO_Init(GPIOC, &GPIO_InitStructure);
00211 
00212   /* Configure PC.01 and PC.04 (ADC Channel11 and Channel14) as analog input */
00213   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4;
00214   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00215   GPIO_Init(GPIOC, &GPIO_InitStructure);
00216 }
00217 
00218 /**
00219   * @brief  Configures NVIC and Vector Table base location.
00220   * @param  None
00221   * @retval None
00222   */
00223 void NVIC_Configuration(void)
00224 {
00225   NVIC_InitTypeDef NVIC_InitStructure;
00226 
00227     /* Configure and enable ADC interrupt */
00228 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00229   NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
00230 #else
00231   NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
00232 #endif
00233   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00234   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00235   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00236   NVIC_Init(&NVIC_InitStructure);
00237 }
00238 
00239 #ifdef  USE_FULL_ASSERT
00240 
00241 /**
00242   * @brief  Reports the name of the source file and the source line number
00243   *         where the assert_param error has occurred.
00244   * @param  file: pointer to the source file name
00245   * @param  line: assert_param error line source number
00246   * @retval None
00247   */
00248 void assert_failed(uint8_t* file, uint32_t line)
00249 { 
00250   /* User can add his own implementation to report the file name and line number,
00251      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00252 
00253   /* Infinite loop */
00254   while (1)
00255   {
00256   }
00257 }
00258 #endif
00259 
00260 /**
00261   * @}
00262   */ 
00263 
00264 /**
00265   * @}
00266   */ 
00267 
00268 /******************* (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