STM32F10x_StdPeriph_Examples/DMA/ADC_TIM1/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DMA/ADC_TIM1/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 DMA_ADC_TIM1
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #define ADC1_DR_Address     0x4001244C
00035 #define TIM1_CCR1_Address   0x40012C34
00036 
00037 /* Private macro -------------------------------------------------------------*/
00038 /* Private variables ---------------------------------------------------------*/
00039 ADC_InitTypeDef           ADC_InitStructure;
00040 TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
00041 TIM_OCInitTypeDef         TIM_OCInitStructure;
00042 DMA_InitTypeDef           DMA_InitStructure;
00043   
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_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   /* Configure the GPIO ports */
00068   GPIO_Configuration();
00069 
00070   /* DMA1 Channel5 configuration ----------------------------------------------*/
00071   DMA_DeInit(DMA1_Channel5);
00072   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR1_Address;
00073   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_DR_Address;
00074   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00075   DMA_InitStructure.DMA_BufferSize = 1;
00076   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00077   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
00078   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00079   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00080   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00081   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00082   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00083   DMA_Init(DMA1_Channel5, &DMA_InitStructure);
00084   /* Enable DMA1 Channel5 */
00085   DMA_Cmd(DMA1_Channel5, ENABLE);
00086 
00087   /* ADC1 configuration ------------------------------------------------------*/
00088   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00089   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
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 RegularChannelConfig Test */ 
00097   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
00098 
00099   /* TIM1 configuration ------------------------------------------------------*/
00100   /* Time Base configuration */
00101   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
00102   TIM_TimeBaseStructure.TIM_Period = 0xFF0;
00103   TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
00104   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
00105   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00106   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00107   /* Channel1 Configuration in PWM mode */
00108   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
00109   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00110   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00111   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00112 
00113   /* Enable TIM1 */  
00114   TIM_Cmd(TIM1, ENABLE);
00115   /* Enable TIM1 outputs */
00116   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00117 
00118   /* Enable TIM1 DMA interface */
00119   TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
00120 
00121   /* Enable ADC1 */
00122   ADC_Cmd(ADC1, ENABLE);
00123 
00124   /* Enable ADC1 reset calibaration register */
00125   ADC_ResetCalibration(ADC1);
00126   /* Check the end of ADC1 reset calibration register */
00127   while(ADC_GetResetCalibrationStatus(ADC1));
00128 
00129   /* Start ADC1 calibaration */
00130   ADC_StartCalibration(ADC1);
00131   /* Check the end of ADC1 calibration */
00132   while(ADC_GetCalibrationStatus(ADC1));
00133 
00134   /* Start ADC1 conversion */ 
00135   ADC_SoftwareStartConvCmd(ADC1, ENABLE);
00136 
00137   while (1)
00138   {
00139   }
00140 }
00141 
00142 /**
00143   * @brief  Configures the different system clocks.
00144   * @param  None
00145   * @retval None
00146   */
00147 void RCC_Configuration(void)
00148 {
00149   /* ADCCLK = PCLK2/8 */
00150   RCC_ADCCLKConfig(RCC_PCLK2_Div8);   
00151 
00152   /* Enable peripheral clocks ------------------------------------------------*/
00153   /* Enable DMA1 clock */
00154   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00155   /* Enable GPIOA, GPIOC, ADC1 and TIM1 Periph clock */
00156   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC |
00157                          RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1, ENABLE);
00158 }
00159 
00160 /**
00161   * @brief  Configures the different GPIO ports.
00162   * @param  None
00163   * @retval None
00164   */
00165 void GPIO_Configuration(void)
00166 {
00167   GPIO_InitTypeDef GPIO_InitStructure;
00168 
00169   /* Configure TIM1 Channel1 output */
00170   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
00171   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00172   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00173   GPIO_Init(GPIOA, &GPIO_InitStructure);
00174 
00175   /* Configure ADC Channel14 as analog input */
00176   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4  ;
00177   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00178   GPIO_Init(GPIOC, &GPIO_InitStructure);
00179 }
00180 
00181 #ifdef  USE_FULL_ASSERT
00182 /**
00183   * @brief  Reports the name of the source file and the source line number
00184   *         where the assert_param error has occurred.
00185   * @param  file: pointer to the source file name
00186   * @param  line: assert_param error line source number
00187   * @retval None
00188   */
00189 void assert_failed(uint8_t* file, uint32_t line)
00190 { 
00191   /* User can add his own implementation to report the file name and line number,
00192      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00193 
00194   /* Infinite loop */
00195   while (1)
00196   {
00197   }
00198 }
00199 #endif
00200 
00201 /**
00202   * @}
00203   */ 
00204 
00205 /**
00206   * @}
00207   */ 
00208 
00209 /******************* (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