STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/DMA/DMA_RAMDACTransfer/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file DMA/DMA_RAMDACTransfer/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_RAMDACTransfer 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 #define DAC_DHR12R_Address 0x40007408 00042 00043 /* Private macro -------------------------------------------------------------*/ 00044 /* Private variables ---------------------------------------------------------*/ 00045 uint16_t Sine12bit[32] = { 00046 2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056, 00047 3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, 00048 599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647}; 00049 00050 /* Private function prototypes -----------------------------------------------*/ 00051 static void TIM_Config(void); 00052 static void DAC_Config(void); 00053 static void DMA_Config(void); 00054 00055 /* Private functions ---------------------------------------------------------*/ 00056 00057 /** 00058 * @brief Main program. 00059 * @param None 00060 * @retval None 00061 */ 00062 int main(void) 00063 { 00064 /*!< At this stage the microcontroller clock setting is already configured, 00065 this is done through SystemInit() function which is called from startup 00066 file (startup_stm32f0xx.s) before to branch to application main. 00067 To reconfigure the default setting of SystemInit() function, refer to 00068 system_stm32f0xx.c file 00069 */ 00070 00071 /* DMA1 channel3 configuration: Sine12bit is used as memory base address */ 00072 DMA_Config(); 00073 00074 /* DAC configuration ------------------------------------------------------*/ 00075 DAC_Config(); 00076 00077 /* TIM2 configuration ------------------------------------------------------*/ 00078 TIM_Config(); 00079 00080 while (1) 00081 { 00082 } 00083 } 00084 00085 /** 00086 * @brief Configures the TIM2 00087 * @param None 00088 * @retval None 00089 */ 00090 static void TIM_Config(void) 00091 { 00092 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00093 00094 /* TIM2 Periph clock enable */ 00095 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 00096 00097 /* Time base configuration */ 00098 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 00099 TIM_TimeBaseStructure.TIM_Period = 48; 00100 TIM_TimeBaseStructure.TIM_Prescaler = 0x0; 00101 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 00102 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00103 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 00104 00105 /* TIM2 TRGO selection: update event is selected as trigger for DAC */ 00106 TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); 00107 00108 /* TIM2 enable counter */ 00109 TIM_Cmd(TIM2, ENABLE); 00110 } 00111 00112 /** 00113 * @brief Configures DAC channel 1 00114 * @param None 00115 * @retval None 00116 */ 00117 static void DAC_Config(void) 00118 { 00119 GPIO_InitTypeDef GPIO_InitStructure; 00120 DAC_InitTypeDef DAC_InitStructure; 00121 00122 /* Enable GPIOA Periph clock --------------------------------------*/ 00123 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 00124 00125 /* Configure PA.04 DAC_OUT as analog */ 00126 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ; 00127 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00128 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 00129 GPIO_Init(GPIOA, &GPIO_InitStructure); 00130 00131 /* DAC Periph clock enable */ 00132 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); 00133 00134 /* Fill DAC InitStructure */ 00135 DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; 00136 DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; 00137 DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; 00138 DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; 00139 00140 /* DAC channel1 Configuration */ 00141 DAC_Init(DAC_Channel_1, &DAC_InitStructure); 00142 00143 /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 00144 automatically connected to the DAC converter. */ 00145 DAC_Cmd(DAC_Channel_1, ENABLE); 00146 00147 /* Enable DMA for DAC Channel1 */ 00148 DAC_DMACmd(DAC_Channel_1, ENABLE); 00149 } 00150 00151 /** 00152 * @brief Configures DMA1 channel3 00153 * @param None 00154 * @retval None 00155 */ 00156 static void DMA_Config(void) 00157 { 00158 DMA_InitTypeDef DMA_InitStructure; 00159 00160 /* Enable DMA1 clock -------------------------------------------------------*/ 00161 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 00162 00163 DMA_DeInit(DMA1_Channel3); 00164 DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R_Address; 00165 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; 00166 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00167 DMA_InitStructure.DMA_BufferSize = 32; 00168 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 00169 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 00170 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 00171 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 00172 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 00173 DMA_InitStructure.DMA_Priority = DMA_Priority_High; 00174 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 00175 DMA_Init(DMA1_Channel3, &DMA_InitStructure); 00176 00177 /* Enable DMA1 Channel3 */ 00178 DMA_Cmd(DMA1_Channel3, ENABLE); 00179 } 00180 00181 #ifdef USE_FULL_ASSERT 00182 00183 /** 00184 * @brief Reports the name of the source file and the source line number 00185 * where the assert_param error has occurred. 00186 * @param file: pointer to the source file name 00187 * @param line: assert_param error line source number 00188 * @retval None 00189 */ 00190 void assert_failed(uint8_t* file, uint32_t line) 00191 { 00192 /* User can add his own implementation to report the file name and line number, 00193 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00194 00195 /* Infinite loop */ 00196 while (1) 00197 { 00198 } 00199 } 00200 #endif 00201 00202 /** 00203 * @} 00204 */ 00205 00206 /** 00207 * @} 00208 */ 00209 00210 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/