STM32F10x_StdPeriph_Examples/DAC/DualModeDMA_SineWave/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DAC/DualModeDMA_SineWave/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 DAC_DualModeDMA_SineWave
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #define DAC_DHR12RD_Address      0x40007420
00035 
00036 /* Init Structure definition */
00037 DAC_InitTypeDef            DAC_InitStructure;
00038 DMA_InitTypeDef            DMA_InitStructure;
00039 TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
00040 uint32_t Idx = 0;  
00041   
00042 /* Private macro -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 const uint16_t Sine12bit[32] = {
00045                       2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
00046                       3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, 
00047                       599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
00048 
00049 uint32_t DualSine12bit[32];
00050 
00051 /* Private function prototypes -----------------------------------------------*/
00052 void RCC_Configuration(void);
00053 void GPIO_Configuration(void);
00054 void Delay(__IO uint32_t nCount);
00055 
00056 /* Private functions ---------------------------------------------------------*/
00057 
00058 /**
00059   * @brief   Main program.
00060   * @param  None
00061   * @retval None
00062   */
00063 int main(void)
00064 {
00065   /*!< At this stage the microcontroller clock setting is already configured, 
00066        this is done through SystemInit() function which is called from startup
00067        file (startup_stm32f10x_xx.s) before to branch to application main.
00068        To reconfigure the default setting of SystemInit() function, refer to
00069        system_stm32f10x.c file
00070      */     
00071           
00072   
00073   /* System Clocks Configuration */
00074   RCC_Configuration();   
00075 
00076   /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
00077      connected to the DAC converter. In order to avoid parasitic consumption, 
00078      the GPIO pin should be configured in analog */
00079   GPIO_Configuration();
00080 
00081   /* TIM2 Configuration */
00082   /* Time base configuration */
00083   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
00084   TIM_TimeBaseStructure.TIM_Period = 0x19;          
00085   TIM_TimeBaseStructure.TIM_Prescaler = 0x0;       
00086   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
00087   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
00088   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00089 
00090   /* TIM2 TRGO selection */
00091   TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
00092 
00093   /* DAC channel1 Configuration */
00094   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00095   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
00096   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00097   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00098 
00099   /* DAC channel2 Configuration */
00100   DAC_Init(DAC_Channel_2, &DAC_InitStructure);
00101 
00102   /* Fill Sine32bit table */
00103   for (Idx = 0; Idx < 32; Idx++)
00104   {
00105     DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
00106   }
00107 
00108 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL
00109   /* DMA2 channel4 configuration */
00110   DMA_DeInit(DMA2_Channel4);
00111 #else
00112   /* DMA1 channel4 configuration */
00113   DMA_DeInit(DMA1_Channel4);
00114 #endif
00115   DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address;
00116   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DualSine12bit;
00117   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00118   DMA_InitStructure.DMA_BufferSize = 32;
00119   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00120   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00121   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
00122   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
00123   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00124   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00125   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00126 
00127 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL
00128   DMA_Init(DMA2_Channel4, &DMA_InitStructure);
00129   /* Enable DMA2 Channel4 */
00130   DMA_Cmd(DMA2_Channel4, ENABLE);
00131 #else
00132   DMA_Init(DMA1_Channel4, &DMA_InitStructure);
00133   /* Enable DMA1 Channel4 */
00134   DMA_Cmd(DMA1_Channel4, ENABLE);
00135 #endif
00136 
00137   /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
00138      automatically connected to the DAC converter. */
00139   DAC_Cmd(DAC_Channel_1, ENABLE);
00140   /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is 
00141      automatically connected to the DAC converter. */
00142   DAC_Cmd(DAC_Channel_2, ENABLE);
00143 
00144   /* Enable DMA for DAC Channel2 */
00145   DAC_DMACmd(DAC_Channel_2, ENABLE);
00146 
00147   /* TIM2 enable counter */
00148   TIM_Cmd(TIM2, ENABLE);
00149 
00150   while (1)
00151   {
00152   }
00153 }
00154 
00155 
00156 /**
00157   * @brief  Configures the different system clocks.
00158   * @param  None
00159   * @retval None
00160   */
00161 void RCC_Configuration(void)
00162 {   
00163   /* Enable peripheral clocks ------------------------------------------------*/
00164 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL
00165   /* DMA2 clock enable */
00166   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
00167 #else
00168   /* DMA1 clock enable */
00169   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00170 #endif
00171   /* GPIOA Periph clock enable */
00172   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
00173   /* DAC Periph clock enable */
00174   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
00175   /* TIM2 Periph clock enable */
00176   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00177 }
00178 
00179 
00180 /**
00181   * @brief  Configures the different GPIO ports.
00182   * @param  None
00183   * @retval None
00184   */
00185 void GPIO_Configuration(void)
00186 {
00187   GPIO_InitTypeDef GPIO_InitStructure;
00188 
00189   /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
00190      connected to the DAC converter. In order to avoid parasitic consumption, 
00191      the GPIO pin should be configured in analog */
00192   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
00193   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00194   GPIO_Init(GPIOA, &GPIO_InitStructure);
00195 }
00196 
00197 /**
00198   * @brief  Inserts a delay time.
00199   * @param  nCount: specifies the delay time length.
00200   * @retval None
00201   */
00202 void Delay(__IO uint32_t nCount)
00203 {
00204   for(; nCount != 0; nCount--);
00205 }
00206 
00207 #ifdef  USE_FULL_ASSERT
00208 
00209 /**
00210   * @brief  Reports the name of the source file and the source line number
00211   *         where the assert_param error has occurred.
00212   * @param  file: pointer to the source file name
00213   * @param  line: assert_param error line source number
00214   * @retval None
00215   */
00216 void assert_failed(uint8_t* file, uint32_t line)
00217 { 
00218   /* User can add his own implementation to report the file name and line number,
00219      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00220 
00221   /* Infinite loop */
00222   while (1)
00223   {
00224   }
00225 }
00226 #endif
00227 
00228 /**
00229   * @}
00230   */ 
00231 
00232 /**
00233   * @}
00234   */ 
00235 
00236 /******************* (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