STM32 using Dma+dac+timer output sine wave

Source: Internet
Author: User
Tags sin



has been briefly discussed before, according to my personal Novice's understanding and understanding, the previous knowledge of the review:



DMA: My understanding is a channel, or a bridge. A channel of communication between static memory and static memory, or peripherals to static memory. The advantage of establishing this channel is that it can be thrown away from the CPU, without consuming CPU resources, and using the contents of this memory directly will speed up.



There are two dacs in the dac:stm32f103 that can be used simultaneously. The function of a DAC is to convert the amount of digital to analog (voltage), which is not explained much.



Timer: Timer. No explanation.



So for the principle or process of using dma+dac+timer to generate sine waves, I have such a simple understanding:



A data table that can generate a sine wave is saved in static memory, then a channel is built using DMA in the middle of the DAC and this memory, and after the above steps, the DAC module can take the data that can generate sine wave in the static memory through the dam channel, fetch the data, and then pass the number-mode quasi-exchange. The sine wave can be obtained by the output of the pin. Then of course, this speed is very fast, if not a certain delay, then the resulting estimate is a rapid change in the analog. So it's time to use a timer. When the DAC is initialized, it can be set to use a timer trigger, which means that when the timer overflows, the DAC is triggered. In this way, the period of the sine wave can be changed by changing the timer's timing time.



Above is a simple understanding of me, there should be a lot of not rigorous incorrect place, after all, is a novice rookie, the above views are also convenient for their own study, I will also be based on continuous learning to supplement nutrition. An example is shown below for analysis:



1. Initialize waveform table and output pin


/ ******** Sine wave output meter *********** /
void SineWave_Data (u16 cycle, u16 * D)
{
u16 i;
for (i = 0; i <cycle; i ++)
{
D [i] = (u16) ((Um * sin ((1.0 * i / (cycle-1)) * 2 * PI) + Um) * 4095 / 3.3);
}
}


/ ****************** Sinusoidal waveform table *********************** /
#ifdef Sine_WaveOutput_Enable
     u16 SineWave_Value [256]; // Wrap with function
#endif


/ ****** DAC register address statement ******* /
#define DAC_DHR12R1 (u32) & (DAC-> DHR12R1) // DAC channel 1 output address
#define DAC_DHR12R2 (u32) & (DAC-> DHR12R2) // DAC channel 2 output address


/ **************** Initialization pin ****************** /
void SineWave_GPIO_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE); // Open the clock
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push-pull output mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Output speed
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // Select pin
GPIO_SetBits (GPIOA, GPIO_Pin_5); // Pull up the output
GPIO_Init (GPIOA, & GPIO_InitStructure); // Init
}


2. Initialize the DAC


/ ****************** DAC initializationˉ ************************* /
void SineWave_DAC_Config (void)
{
     DAC_InitTypeDef DAC_InitStructure;
     RCC_APB1PeriphClockCmd (RCC_APB1Periph_DAC, ENABLE); // Open the DAC clock
Ranch
   / ************** DAC structure initialization ******************* /
     DAC_StructInit (& DAC_InitStructure);
     DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; // No waveform is generated
     DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; // Disable output buffer
     DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; // DAC trigger is Timer 2 trigger
     DAC_Init (DAC_Channel_1, & DAC_InitStructure); // Initialization
     DAC_Cmd (DAC_Channel_1, ENABLE); // Enable DAC channel 1
     DAC_DMACmd (DAC_Channel_1, ENABLE); // Enable DMA for DAC channel 1
}


3. Timer configuration


/ ********* Timer initialization ************ /
void SineWave_TIM_Config (u32 Wave1_Fre)
{
     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
     RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE); // Open the clock
     TIM_TimeBaseStructInit (& TIM_TimeBaseStructure);
     TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
     TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; // No frequency division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Count up
     TIM_TimeBaseStructure.TIM_Period = Wave1_Fre; // Set the output frequency
     TIM_TimeBaseInit (TIM2, & TIM_TimeBaseStructure);
     TIM_SelectOutputTrigger (TIM2, TIM_TRGOSource_Update); // Set the TIME output trigger to update mode
}


4. DMA Configuration


/ ********* DMA configuration *********** /
void SineWave_DMA_Config (void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA2, ENABLE); // Enable DMA2 clock
Ranch
DMA_StructInit (& DMA_InitStructure); // DMA structure initialization
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // Read data from the register
DMA_InitStructure.DMA_BufferSize = 256; // Register size
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // The peripheral address is not incremented
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // Increment memory address
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // width is halfword
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // width is halfword
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; / Priority is very high
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // Close memory to memory mode
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // cyclic transmission mode


DMA_INITSTRUCTURE.DMA_PERIPHERALBASEADDR = dac_dhr12r1;//Peripheral address is the address of DAC Channel 1
Dma_initstructure.dma_memorybaseaddr = (uint32_t) sinewave_value; //Waveform data table memory address



Dma_init (Dma2_channel3, &dma_initstructure);//initialization
Dma_cmd (Dma2_channel3, ENABLE); Enable DMA Channel 3



}



5. Sine Wave Initialization





void SineWave_Init (u16 Wave1_Fre)
{
U16 f1 = (u16) (72000000 / sizeof (SineWave_Value) * 2 / Wave1_Fre); // Calculate frequency
   SineWave_Data (256, SineWave_Value); // Generate waveform table of output sine wave
SineWave_GPIO_Config (); // Initialize io
SineWave_TIM_Config (f1); // Initialize the timer
SineWave_DAC_Config (); // Configure DAC
SineWave_DMA_Config (); // Configure DMA
TIM_Cmd (TIM2, ENABLE); // Enable timer
} 


After the above simple configuration, you can make the 32 plate output sin waveform.



STM32 using Dma+dac+timer output sine wave


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.