STM32F10x_StdPeriph_Examples/DAC/TwoChannels_TriangleWave/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DAC/TwoChannels_TriangleWave/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_TwoChannels_TriangleWave
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Init Structure definition */
00035 DAC_InitTypeDef            DAC_InitStructure;
00036 TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
00037 
00038 /* Private macro -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 /* Private function prototypes -----------------------------------------------*/
00041 void RCC_Configuration(void);
00042 void GPIO_Configuration(void);
00043 void Delay(__IO uint32_t nCount);
00044 
00045 /* Private functions ---------------------------------------------------------*/
00046 
00047 /**
00048   * @brief   Main program.
00049   * @param  None
00050   * @retval None
00051   */
00052 int main(void)
00053 {
00054   /*!< At this stage the microcontroller clock setting is already configured, 
00055        this is done through SystemInit() function which is called from startup
00056        file (startup_stm32f10x_xx.s) before to branch to application main.
00057        To reconfigure the default setting of SystemInit() function, refer to
00058        system_stm32f10x.c file
00059      */     
00060        
00061   /* System Clocks Configuration */
00062   RCC_Configuration();   
00063 
00064   /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
00065      connected to the DAC converter. In order to avoid parasitic consumption, 
00066      the GPIO pin should be configured in analog */
00067   GPIO_Configuration();
00068 
00069   /* TIM2 Configuration */
00070   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
00071   TIM_TimeBaseStructure.TIM_Period = 0xF;          
00072   TIM_TimeBaseStructure.TIM_Prescaler = 0xF;       
00073   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
00074   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
00075   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00076 
00077   /* TIM2 TRGO selection */
00078   TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
00079 
00080   /* DAC channel1 Configuration */
00081   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
00082   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
00083   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
00084   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
00085   DAC_Init(DAC_Channel_1, &DAC_InitStructure);
00086 
00087   /* DAC channel2 Configuration */
00088   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023;
00089   DAC_Init(DAC_Channel_2, &DAC_InitStructure);
00090 
00091   /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is 
00092      automatically connected to the DAC converter. */
00093   DAC_Cmd(DAC_Channel_1, ENABLE);
00094 
00095   /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is 
00096      automatically connected to the DAC converter. */
00097   DAC_Cmd(DAC_Channel_2, ENABLE);
00098 
00099   /* Set DAC dual channel DHR12RD register */
00100   DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);
00101 
00102   /* TIM2 enable counter */
00103   TIM_Cmd(TIM2, ENABLE);
00104 
00105   while (1)
00106   {
00107   }
00108 }
00109 
00110 
00111 /**
00112   * @brief  Configures the different system clocks.
00113   * @param  None
00114   * @retval None
00115   */
00116 void RCC_Configuration(void)
00117 {   
00118   /* Enable peripheral clocks ------------------------------------------------*/
00119   /* GPIOA Periph clock enable */
00120   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
00121   /* DAC Periph clock enable */
00122   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
00123   /* TIM2 Periph clock enable */
00124   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00125 }
00126 
00127 /**
00128   * @brief  Configures the different GPIO ports.
00129   * @param  None
00130   * @retval None
00131   */
00132 void GPIO_Configuration(void)
00133 {
00134   GPIO_InitTypeDef GPIO_InitStructure;
00135 
00136   /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically 
00137      connected to the DAC converter. In order to avoid parasitic consumption, 
00138      the GPIO pin should be configured in analog */
00139   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5;
00140   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00141   GPIO_Init(GPIOA, &GPIO_InitStructure);
00142 }
00143 
00144 /**
00145   * @brief  Inserts a delay time.
00146   * @param  nCount: specifies the delay time length.
00147   * @retval None
00148   */
00149 void Delay(__IO uint32_t nCount)
00150 {
00151   for(; nCount != 0; nCount--);
00152 }
00153 
00154 #ifdef  USE_FULL_ASSERT
00155 
00156 /**
00157   * @brief  Reports the name of the source file and the source line number
00158   *         where the assert_param error has occurred.
00159   * @param  file: pointer to the source file name
00160   * @param  line: assert_param error line source number
00161   * @retval None
00162   */
00163 void assert_failed(uint8_t* file, uint32_t line)
00164 { 
00165   /* User can add his own implementation to report the file name and line number,
00166      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00167 
00168   /* Infinite loop */
00169   while (1)
00170   {
00171   }
00172 }
00173 #endif
00174 
00175 /**
00176   * @}
00177   */ 
00178 
00179 /**
00180   * @}
00181   */ 
00182 
00183 /******************* (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