/******************************************************************** * DAC1 initialization code template ********************************************************************/ void DAC1_Init() { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitStructure; /* Enable peripheral clocks ------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOA Periph clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // DAC Periph clock enable /*GPIO Configuretion -------------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /*DAC1 Configuretion ----------------------------------------*/ DAC_InitStructure.DAC_Trigger = $DAC1Tri$; //Trigger Source DAC_InitStructure.DAC_WaveGeneration = $DAC1WaveGene$; //Wave Chose #if(STRCMP($DAC1WaveGene$,DAC_WaveGeneration_Noise) == 1) DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = $DAC1LFSRUnma$; //LFSR Unmask #endif #if(STRCMP($DAC1WaveGene$,DAC_WaveGeneration_Triangle) == 1) DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = $DAC1TriAmpli$;//Triangle Edge #endif DAC_InitStructure.DAC_OutputBuffer = $DAC1OutBuff$; //Ouput Buffer Enable /*DAC1 Init ------------------------------------------------------*/ DAC_Init(DAC_Channel_1, &DAC_InitStructure); /*DAC1 Enable ----------------------------------------------------*/ DAC_Cmd(DAC_Channel_1, ENABLE); #if(STRCMP($DAC1Tri$,DAC_Trigger_Software) == 1) /*Software Trigger Enable -----------------------------------------*/ DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); #endif #if(STRCMP($DAC1WaveGene$,DAC_WaveGeneration_Noise) == 1) /*Noise Generation Enable ------------------------------------------*/ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise, ENABLE); #endif #if(STRCMP($DAC1WaveGene$,DAC_WaveGeneration_Triangle) == 1) /*Trigger Wave Generation Enable -----------------------------------*/ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Triangle, ENABLE); #endif #if(STRCMP($DMAEn$,ENABLE) == 1) /*DMA Enable--------------------------------------------------------*/ DAC_DMACmd(DAC_Channel_1, ENABLE); #endif #if(STRCMP($DAC1Int$,ENABLE) == 1) /*Enables DAC1 Interrupts -----------------------------------------*/ DAC_ITConfig(DAC_Channel_1, DAC_IT_DMAUDR, ENABLE); #endif #if(STRCMP($AlignSty$,DAC_Align_8b_R) == 1) /*8 bit Data Right Align Register Set--------------------------------*/ DAC_SetChannel1Data($AlignSty$, $EightB$); #endif #if(STRCMP($AlignSty$,DAC_Align_12b_L) == 1) /*12 bit Data Light Align Register Set--------------------------------*/ DAC_SetChannel1Data($AlignSty$, $ElevB$); #endif #if(STRCMP($AlignSty$,DAC_Align_12b_R) == 1) /*12 bit Data Right Align Register Set--------------------------------*/ DAC_SetChannel1Data($AlignSty$, $ElevB$); #endif }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Preconfiguration before using DAC----------------------------------------*/ DAC_PreConfig(); /* TIM2 Configuration ------------------------------------------------------*/ /* TIM2 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 TRGO selection */ TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); /* Configures Button GPIO and EXTI Line */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); while (1) { /* If the wave form is changed */ if (WaveChange == 1) { /* Switch the selected waves forms according the Button status */ if (SelectedWavesForm == 1) { /* The sine wave and the escalator wave has been selected */ /* Sine Wave generator ---------------------------------------------*/ DAC_DeInit(); /* DAC channel1 and channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel3, &DMA_InitStructure); /* Enable DMA1 Channel3 */ DMA_Cmd(DMA1_Channel3, ENABLE); /* DAC Channel2 Init */ DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* Escalator Wave generator ----------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DMA1 channel2 configuration */ DMA_DeInit(DMA1_Channel2); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit; DMA_InitStructure.DMA_BufferSize = 6; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_Init(DMA1_Channel2, &DMA_InitStructure); /* Enable DMA1 Channel2 */ DMA_Cmd(DMA1_Channel2, ENABLE); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } /* The triangle wave and the noise wave has been selected */ else { /* Noise Wave generator --------------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits8_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0); /* Enable DAC channel1 wave generator */ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE); /* Triangle Wave generator -----------------------------------------*/ DAC_DeInit(); /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC channel2 DHR12RD register */ DAC_SetChannel2Data(DAC_Align_12b_R, 0x100); } WaveChange = !WaveChange; } } }
/** * @brief Main program. * @param None * @retval None */ void DAC_SignalsGeneration(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* Preconfiguration before using DAC----------------------------------------*/ DAC_Config(); /* TIM2 configuration to trigger DAC */ TIM_Config(); /* Configures Key Button EXTI Line */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Infinite loop */ while (1) { /* If the wave form is changed */ if (WaveChange == 1) { /* Switch the selected waves forms according the Button status */ if (SelectedWavesForm == 1) { /* The sine wave and the escalator wave has been selected */ /* Sine Wave generator ---------------------------------------------*/ /* DAC channel2 Configuration */ DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; /* DAC Channel2 Init */ DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2 */ DAC_Cmd(DAC_Channel_2, ENABLE); /* DMA2 channel3 configuration */ DMA_DeInit(DMA2_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel3, &DMA_InitStructure); /* Enable DMA2 Channel3 */ DMA_Cmd(DMA2_Channel3, ENABLE); /* Enable DMA for DAC Channel2 */ DAC_DMACmd(DAC_Channel_2, ENABLE); /* Escalator Wave generator ----------------------------------------*/ /* DAC channel1 Configuration */ DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* DMA2 channel4 configuration */ DMA_DeInit(DMA2_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit; DMA_InitStructure.DMA_BufferSize = 6; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_Init(DMA2_Channel4, &DMA_InitStructure); /* Enable DMA2 Channel4 */ DMA_Cmd(DMA2_Channel4, ENABLE); /* Enable DAC1 Channel1: Once the DAC1 channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Enable DMA for DAC Channel1 */ DAC_DMACmd(DAC_Channel_1, ENABLE); } /* The triangle wave and the noise wave has been selected */ else { /* Noise Wave generator --------------------------------------------*/ /* DAC channel1 Configuration */ DAC_DeInit(); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_1, ENABLE); /* Set DAC Channel1 DHR12L register */ DAC_SetChannel1Data(DAC_Align_12b_L, 0x7FF0); /* Enable DAC channel1 wave generator */ DAC_WaveGenerationCmd(DAC_Channel_1, DAC_Wave_Noise , ENABLE); /* Triangle Wave generator -----------------------------------------*/ /* DAC channel2 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1023; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_2, &DAC_InitStructure); /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is automatically connected to the DAC converter. */ DAC_Cmd(DAC_Channel_2, ENABLE); /* Set DAC channel2 DHR12RD register */ DAC_SetChannel2Data(DAC_Align_12b_R, 0x100); } WaveChange = !WaveChange; } } }