STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/DMA/DMA_ADCTIMTransfer/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file DMA/DMA_ADCTIMTransfer/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup DMA_ADCTIMTransfer 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 #define ADC1_DR_ADDRESS 0x40012440 00042 #define TIM3_CCR1_ADDRESS 0x40000434 00043 00044 /* Private macro -------------------------------------------------------------*/ 00045 /* Private variables ---------------------------------------------------------*/ 00046 /* Private function prototypes -----------------------------------------------*/ 00047 static void ADC_Config(void); 00048 static void DMA_Config(void); 00049 static void TIM_Config(void); 00050 00051 /* Private functions ---------------------------------------------------------*/ 00052 00053 /** 00054 * @brief Main program. 00055 * @param None 00056 * @retval None 00057 */ 00058 int main(void) 00059 { 00060 /*!< At this stage the microcontroller clock setting is already configured, 00061 this is done through SystemInit() function which is called from startup 00062 file (startup_stm32f0xx.s) before to branch to application main. 00063 To reconfigure the default setting of SystemInit() function, refer to 00064 system_stm32f0xx.c file 00065 */ 00066 00067 /* DMA1 channel3 configuration */ 00068 DMA_Config(); 00069 00070 /* TIM3 channel1 configuration */ 00071 TIM_Config(); 00072 00073 /* ADC channel11 configuration */ 00074 ADC_Config(); 00075 00076 while (1) 00077 { 00078 } 00079 } 00080 00081 /** 00082 * @brief Configures DMA1 channel3 to transfer data from 00083 * ADC1_DR_ADDRESS to TIM3_CCR1_ADDRESS 00084 * @param None 00085 * @retval None 00086 */ 00087 static void DMA_Config(void) 00088 { 00089 DMA_InitTypeDef DMA_InitStructure; 00090 00091 /* Enable DMA1 clock */ 00092 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 00093 00094 DMA_DeInit(DMA1_Channel3); 00095 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM3_CCR1_ADDRESS; 00096 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_DR_ADDRESS; 00097 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00098 DMA_InitStructure.DMA_BufferSize = 1; 00099 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 00100 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; 00101 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 00102 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 00103 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 00104 DMA_InitStructure.DMA_Priority = DMA_Priority_High; 00105 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 00106 DMA_Init(DMA1_Channel3, &DMA_InitStructure); 00107 00108 /* Enable DMA1 Channel3 */ 00109 DMA_Cmd(DMA1_Channel3, ENABLE); 00110 } 00111 00112 /** 00113 * @brief Configures the ADC1 channel11 in continuous mode. 00114 * @param None 00115 * @retval None 00116 */ 00117 static void ADC_Config(void) 00118 { 00119 ADC_InitTypeDef ADC_InitStructure; 00120 GPIO_InitTypeDef GPIO_InitStructure; 00121 /* Enable the HSI */ 00122 RCC_HSICmd(ENABLE); 00123 00124 /* Enable the GPIOC Clock */ 00125 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); 00126 00127 /* Configure ADC Channel11 (PC.01) as analog input */ 00128 #ifdef USE_STM320518_EVAL 00129 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; 00130 #else 00131 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;/* Configure ADC Channel10 as analog input */ 00132 #endif /* USE_STM320518_EVAL */ 00133 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 00134 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00135 GPIO_Init(GPIOC, &GPIO_InitStructure); 00136 00137 /* Check that HSI oscillator is ready */ 00138 while( RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); 00139 00140 ADC_StructInit(&ADC_InitStructure); 00141 00142 /* Enable ADC1 clock */ 00143 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 00144 00145 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 00146 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 00147 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; 00148 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 00149 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; 00150 ADC_Init(ADC1, &ADC_InitStructure); 00151 00152 /* ADC1 regular channel11 configuration */ 00153 #ifdef USE_STM320518_EVAL 00154 ADC_ChannelConfig(ADC1, ADC_Channel_11,ADC_SampleTime_28_5Cycles); 00155 #else 00156 /* ADC1 regular channel10 configuration */ 00157 ADC_ChannelConfig(ADC1, ADC_Channel_10,ADC_SampleTime_28_5Cycles); 00158 #endif /* USE_STM320518_EVAL */ 00159 00160 /* Enable the ADC peripheral */ 00161 ADC_Cmd(ADC1, ENABLE); 00162 00163 /* Wait the ADRDY flag */ 00164 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); 00165 00166 /* Enable the wait conversion mode */ 00167 ADC_WaitModeCmd(ADC1, ENABLE); 00168 00169 /* Start ADC1 Software Conversion */ 00170 ADC_StartOfConversion(ADC1); 00171 } 00172 00173 /** 00174 * @brief Configures the TIM3 channel1 in PWM mode 00175 * @param None 00176 * @retval None 00177 */ 00178 static void TIM_Config(void) 00179 { 00180 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00181 TIM_OCInitTypeDef TIM_OCInitStructure; 00182 GPIO_InitTypeDef GPIO_InitStructure; 00183 00184 /* Enable GPIOA clock */ 00185 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 00186 00187 /* GPIOA Configuration: PA6(TIM3 CH1) as alternate function push-pull */ 00188 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00189 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00190 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00191 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 00192 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 00193 GPIO_Init(GPIOA, &GPIO_InitStructure); 00194 GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1); 00195 00196 /* Enable TIM3 clock */ 00197 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 00198 00199 /* Time Base configuration */ 00200 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00201 TIM_TimeBaseStructure.TIM_Period = 0xFF0; 00202 TIM_TimeBaseStructure.TIM_Prescaler = 0x0; 00203 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 00204 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00205 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 00206 00207 /* Channel1 Configuration in PWM mode */ 00208 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 00209 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 00210 TIM_OCInitStructure.TIM_Pulse = 0xf0; 00211 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 00212 TIM_OC1Init(TIM3, &TIM_OCInitStructure); 00213 00214 /* Enable TIM3 DMA interface */ 00215 TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE); 00216 00217 /* Enable TIM3 */ 00218 TIM_Cmd(TIM3, ENABLE); 00219 } 00220 00221 #ifdef USE_FULL_ASSERT 00222 00223 /** 00224 * @brief Reports the name of the source file and the source line number 00225 * where the assert_param error has occurred. 00226 * @param file: pointer to the source file name 00227 * @param line: assert_param error line source number 00228 * @retval None 00229 */ 00230 void assert_failed(uint8_t* file, uint32_t line) 00231 { 00232 /* User can add his own implementation to report the file name and line number, 00233 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00234 00235 /* Infinite loop */ 00236 while (1) 00237 { 00238 } 00239 } 00240 #endif 00241 00242 /** 00243 * @} 00244 */ 00245 00246 /** 00247 * @} 00248 */ 00249 00250 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/