STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/DAC/DAC_SignalsGeneration/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DAC/DAC_SignalsGeneration/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>&copy; 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 DAC_SignalsGeneration
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 #define DAC_DHR12R1_ADDRESS      0x40007408
00042 #define DAC_DHR8R2_ADDRESS       0x4000741C
00043 #define DAC_DHR8R1_ADDRESS       0x40007410
00044 
00045 /* Private macro -------------------------------------------------------------*/
00046 /* Private variables ---------------------------------------------------------*/
00047 DAC_InitTypeDef            DAC_InitStructure;
00048 DMA_InitTypeDef            DMA_InitStructure;
00049 const uint16_t Sine12bit[32] = {
00050                       2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
00051                       3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, 
00052                       599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};          
00053 const uint8_t Escalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};
00054 uint8_t Idx = 0;
00055 __IO uint8_t  SelectedWavesForm = 1;
00056 __IO uint8_t WaveChange = 1; 
00057 
00058 /* Private function prototypes -----------------------------------------------*/
00059 static void DAC_Config(void);
00060 static void TIM_Config(void);
00061 /* Private functions ---------------------------------------------------------*/
00062 
00063 /**
00064   * @brief  Main program.
00065   * @param  None
00066   * @retval None
00067   */
00068 int main(void)
00069 {
00070   /*!< At this stage the microcontroller clock setting is already configured, 
00071        this is done through SystemInit() function which is called from startup
00072        file (startup_stm32f0xx.s) before to branch to application main.
00073        To reconfigure the default setting of SystemInit() function, refer to
00074        system_stm32f0xx.c file
00075      */ 
00076 
00077   /* Preconfiguration before using DAC----------------------------------------*/
00078   DAC_Config();
00079   
00080   /* TIM2 configuration */
00081   TIM_Config();
00082   
00083   /* Configures Button GPIO and EXTI Line */
00084   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI);
00085 
00086   /* Infinite loop */
00087   while (1)
00088   {
00089     /* If the wave form is changed */
00090     if (WaveChange == 1)
00091     {  
00092       /* Switch the selected waves forms according the Button status */
00093       if (SelectedWavesForm == 1)
00094       {
00095 #if defined (STM32F072) || defined (STM32F091)  
00096           /* The sine wave has been selected */
00097           /* Sine Wave generator ---------------------------------------------*/
00098           DAC_DeInit(); 
00099           
00100           /* DAC channel1 Configuration */
00101           DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00102           DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
00103           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00104           DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
00105           
00106           /* DMA channel3 Configuration */
00107           DMA_DeInit(DMA1_Channel3); 
00108           DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS;
00109           DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
00110           DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00111           DMA_InitStructure.DMA_BufferSize = 32;
00112           DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00113           DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00114           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00115           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00116           DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00117           DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00118           DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00119           DMA_Init(DMA1_Channel3, &DMA_InitStructure);
00120 
00121           /* Enable DMA1 Channel3 */
00122           DMA_Cmd(DMA1_Channel3, ENABLE);
00123 
00124           /* DAC Channel1 Init */
00125           DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00126 
00127           /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
00128              automatically connected to the DAC converter. */
00129           DAC_Cmd(DAC_Channel_1, ENABLE);
00130 
00131           /* Enable DMA for DAC Channel1 */
00132           DAC_DMACmd(DAC_Channel_1, ENABLE);
00133           
00134         
00135           /* Escalator Wave generator ----------------------------------------*/
00136 
00137           /* DAC channel1 Configuration */
00138           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
00139           DAC_Init(DAC_Channel_2, &DAC_InitStructure);
00140   
00141           /* DMA1 channel4 configuration */
00142           DMA_DeInit(DMA1_Channel4);
00143           DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R2_ADDRESS;
00144           DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit;
00145           DMA_InitStructure.DMA_BufferSize = 6;
00146           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00147           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00148           DMA_Init(DMA1_Channel4, &DMA_InitStructure);
00149     
00150           /* Enable DMA1 Channel4 */
00151           DMA_Cmd(DMA1_Channel4, ENABLE);
00152     
00153           /* Enable DAC1 Channel1: Once the DAC1 channel2 is enabled, PA.05 is 
00154              automatically connected to the DAC converter. */
00155           DAC_Cmd(DAC_Channel_2, ENABLE);
00156 
00157           /* Enable DMA for DAC Channel1 */
00158           DAC_DMACmd(DAC_Channel_2, ENABLE);
00159 #else
00160           /* The sine wave has been selected */
00161           /* Sine Wave generator ---------------------------------------------*/
00162           DAC_DeInit(); 
00163           
00164           /* DAC channel1 Configuration */
00165           DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00166           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00167           
00168           /* DMA channel3 Configuration */
00169           DMA_DeInit(DMA1_Channel3); 
00170           DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS;
00171           DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
00172           DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00173           DMA_InitStructure.DMA_BufferSize = 32;
00174           DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00175           DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00176           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
00177           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
00178           DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
00179           DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00180           DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00181           DMA_Init(DMA1_Channel3, &DMA_InitStructure);
00182 
00183           /* Enable DMA1 Channel3 */
00184           DMA_Cmd(DMA1_Channel3, ENABLE);
00185 
00186           /* DAC Channel1 Init */
00187           DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00188 
00189           /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
00190              automatically connected to the DAC converter. */
00191           DAC_Cmd(DAC_Channel_1, ENABLE);
00192 
00193           /* Enable DMA for DAC Channel1 */
00194           DAC_DMACmd(DAC_Channel_1, ENABLE);          
00195 #endif /* STM32F072 || STM32F091*/
00196          
00197       }
00198       else
00199       {
00200 #if defined (STM32F072) || defined (STM32F091)         
00201           /* Noise Wave generator --------------------------------------------*/
00202           /* DAC channel1 Configuration */
00203           DAC_DeInit(); 
00204           DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00205           DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
00206           DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
00207           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
00208           DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00209 
00210           /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is 
00211              automatically connected to the DAC converter. */
00212           DAC_Cmd(DAC_Channel_1, ENABLE);
00213 
00214           /* Set DAC Channel1 DHR12L register */
00215           DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0);
00216     
00217           /* Enable DAC channel1 wave generator */
00218           DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE);
00219             
00220           /* Triangle Wave generator -----------------------------------------*/
00221           
00222           /* DAC channel2 Configuration */
00223           DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00224           DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
00225           DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
00226           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
00227           DAC_Init(DAC_Channel_2, &DAC_InitStructure);
00228 
00229           /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is 
00230              automatically connected to the DAC converter. */
00231           DAC_Cmd(DAC_Channel_2, ENABLE);
00232 
00233           /* Set DAC channel2 DHR12RD register */
00234           DAC_SetChannel2Data(DAC_Align_12b_R, 0x100);
00235 #else
00236         /* Escalator Wave generator -----------------------------------------*/
00237           DAC_DeInit();
00238           
00239           /* DAC channel1 Configuration */
00240           DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00241           DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00242           
00243   
00244           /* DMA1 channel2 configuration */
00245           DMA_DeInit(DMA1_Channel3);
00246 
00247           DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS;
00248           DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit;
00249           DMA_InitStructure.DMA_BufferSize = 6;
00250           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00251           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00252           DMA_Init(DMA1_Channel3, &DMA_InitStructure);
00253     
00254           /* Enable DMA1 Channel2 */
00255           DMA_Cmd(DMA1_Channel3, ENABLE);
00256     
00257           /* DAC channel1 Configuration */
00258           DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00259 
00260           /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
00261              automatically connected to the DAC converter. */
00262           DAC_Cmd(DAC_Channel_1, ENABLE);
00263 
00264           /* Enable DMA for DAC Channel1 */
00265           DAC_DMACmd(DAC_Channel_1, ENABLE);          
00266 #endif /* STM32F072 || STM32F091*/
00267       }
00268       WaveChange = !WaveChange;
00269     }
00270   }
00271 }
00272 
00273 /**
00274   * @brief  PrecConfiguration: configure PA4 in analog,
00275   *                           enable DAC clock, enable DMA1 clock
00276   * @param  None
00277   * @retval None
00278   */
00279 static void DAC_Config(void)
00280 {
00281   GPIO_InitTypeDef GPIO_InitStructure;
00282 
00283   /* DMA1 clock enable (to be used with DAC) */
00284   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00285 
00286   /* DAC Periph clock enable */
00287   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
00288 
00289   /* GPIOA clock enable */
00290   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00291   
00292   /* Configure PA.04 (DAC_OUT1) as analog */
00293   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
00294   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
00295   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00296   GPIO_Init(GPIOA, &GPIO_InitStructure);
00297   
00298 #if defined (STM32F072) || defined (STM32F091)    
00299   /* Configure PA.05 (DAC_OUT2) as analog */
00300   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;
00301   GPIO_Init(GPIOA, &GPIO_InitStructure);
00302 #endif
00303 }
00304 
00305 /**
00306   * @brief  TIM2 Configuration
00307   * @param  None
00308   * @retval None
00309   */
00310 static void TIM_Config(void)
00311 {
00312   TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
00313   
00314   /* TIM2 Periph clock enable */
00315   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00316   
00317   /* Time base configuration */
00318   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
00319   TIM_TimeBaseStructure.TIM_Period = 0xFF;          
00320   TIM_TimeBaseStructure.TIM_Prescaler = 0x0;       
00321   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
00322   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
00323   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00324 
00325   /* TIM2 TRGO selection */
00326   TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
00327   
00328   /* TIM2 enable counter */
00329   TIM_Cmd(TIM2, ENABLE);
00330 }
00331 
00332 #ifdef  USE_FULL_ASSERT
00333 
00334 /**
00335   * @brief  Reports the name of the source file and the source line number
00336   *         where the assert_param error has occurred.
00337   * @param  file: pointer to the source file name
00338   * @param  line: assert_param error line source number
00339   * @retval None
00340   */
00341 void assert_failed(uint8_t* file, uint32_t line)
00342 { 
00343   /* User can add his own implementation to report the file name and line number,
00344      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00345 
00346   /* Infinite loop */
00347   while (1)
00348   {
00349   }
00350 }
00351 #endif
00352 
00353 /**
00354   * @}
00355   */
00356 
00357 /**
00358   * @}
00359   */
00360 
00361 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32