6947 views|36 replies

224

Posts

0

Resources
The OP
 

【ACM32G103RCT6】7-DAC two methods to output sine waveform [Copy link]

 
 
This post was last edited by qiao--- on 2024-1-22 22:59

In this review, I will complete the use of DAC to output a positive waveform mentioned in my review plan. Please see the second floor for the DAC waveform output effect. Here I summarize two methods of outputting a sine waveform, the table lookup method and the formula method .

Preparation

First, let's look at the datasheet to see what the DAC pins are.

After reading this, we need to initialize the pins in the msp.c file. The msp.c file generally enables the corresponding hardware and initializes the pins.

The following code mainly initializes the dac and the pins of the two dac channels, and also enables the dma interrupt.

void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
{
    if(hdac->Instance==DAC)
    { 
        /* Enable DAC clock */
        __HAL_RCC_DAC1_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();
        
        GPIO_InitTypeDef GPIO_InitStructure;   	
        /* Initialization GPIO */
        /**DAC1 GPIO Configuration    
        PA4  ------> DAC_OUT1 
        PA5  ------> DAC_OUT2 
        */
        
        GPIO_InitStructure.Pin = GPIO_PIN_4|GPIO_PIN_5;	
        GPIO_InitStructure.Pull=GPIO_NOPULL;
        GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        printfS("DAC Channel-1:PA4\r\n");
        printfS("DAC Channel-2:PA5\r\n");
        
        /* Enable the DAC DMA underrun interrupt */
        hdac->Instance->CR |= DAC_CR_DMAUDIE1 | DAC_CR_DMAUDIE2;
        NVIC_ClearPendingIRQ(DAC_IRQn);
        NVIC_SetPriority(DAC_IRQn, 5);
        NVIC_EnableIRQ(DAC_IRQn);
    }
}

After completing this, we need to create a new dac.c file to store our dac-related codes. We know that dac is a digital-to-analog converter, which converts 12-bit or other digits into corresponding voltage values. The following is a dac conversion logic diagram to help you understand dac.

1. Table Lookup

The table lookup method is to store the sine wave data in an array, and then traverse the array in turn to store it in the DAC converter. The disadvantage of this method is that the CPU occupancy rate is high, which is not conducive to multi-tasking.

We first define an array to store the values of our DAC. Then we can directly write these numbers into the DAC conversion register to achieve the conversion.

Next we need to initialize the DAC with hardware. The initialization code is as follows:

Here I plan to use a timer to define the conversion time between each two values, define a variable volatile uint32_t gtimer_Update_Flag flag to determine whether the conversion can be performed, and set it to 1 in the timer interrupt. Here I use TIM7, the timer initialization code is as follows

We set this flag to 1 in the update interrupt, as shown below

At this point we can write the application code for the DAC to output a sine wave. We only need to use the function HAL_DACEx_DualSetValue to write the value of the array into the register when the flag is 1, as shown in the figure below.

Here we have completed all the code writing, and I will create a task to run it.

2. Formula method

In short, the formula method is to use mathematical formulas for calculations. I designed this step in the timer interrupt, which can reduce the CPU occupancy rate and facilitate the operation of multiple tasks.

Compared with the table lookup method above, our method only needs to modify the application code in the function DAC_OutPut_Sinx. It only needs to be initialized.

The highlight of this method is in the timer interrupt, where we perform calculations.

When you see the formula above, can you tell how big the amplitude of my positive selection wave is?

My amplitude A=(1240/4096)*3.3 is approximately equal to 1V. Those who are interested can look at my timer and calculate the frequency of my sine wave.

The code for creating a task is the same as the table lookup method and does not need to be changed.

Summary: In this test, I used two methods to output sine waves, namely the table lookup method and the formula method. However, I recommend the formula method because it is completed in the timer interrupt, which reduces the CPU occupancy rate and facilitates multi-tasking. I have implemented both methods in my project, and you can verify them.

06dac.7z (1.3 MB, downloads: 1)

06dac.7z

1.3 MB, downloads: 1

DAC两种方法输出正弦波形

This post is from Domestic Chip Exchange

Latest reply

That's still a good deal. I remember I bought a DS1054 in 2017 for more than 2,000 yuan.   Details Published on 2024-1-22 20:57
 
 

224

Posts

0

Resources
From 2
 

After waiting for three days, the portable oscilloscope I bought arrived. After debugging it, it successfully displayed the waveform.

The waveform of the first table lookup method is as follows:

The waveform of the second formula method is as follows:

From the second formula method, we can see that the peak-to-peak value is about 2V, which means the amplitude is 1V, the DAC output is normal, and the waveform is very stable.

This post is from Domestic Chip Exchange
 
 
 

6818

Posts

11

Resources
3
 
I output the voltage value directly, but when I use ADC to test it, the voltage is unstable. What should I do if I want to output the voltage value continuously?
This post is from Domestic Chip Exchange

Comments

Do you mean using the DAC to output voltage and then using the ADC to measure the DAC output voltage?  Details Published on 2024-1-20 12:39
 
 
 

6818

Posts

11

Resources
4
 

The highlight of this method is in the timer interrupt, where we perform calculations.

To correct the original poster's statement, it should be done in the timer "callback function".

I am also very careless and often make mistakes.

This post is from Domestic Chip Exchange

Comments

That's right, thanks for the correction  Details Published on 2024-1-20 12:37
 
 
 

6818

Posts

11

Resources
5
 

Let me demonstrate this to you using an oscilloscope.

This post is from Domestic Chip Exchange

Comments

How is it? Is there a waveform? If not, I will change the code  Details Published on 2024-1-20 12:38
 
 
 

623

Posts

0

Resources
6
 

You can also try the combination of DMA + DAC + Timer, that is, DMA is used to transmit the output value of DAC, and Timer is used to periodically trigger DMA to start transmission.

This post is from Domestic Chip Exchange

Comments

Indeed, this can also reduce the CPU usage, directly using DMA to transfer the numbers in the table. Thanks for your valuable advice.  Details Published on 2024-1-20 12:41
 
 
 

6818

Posts

11

Resources
7
 

I measured it and the waveform is as follows. Do you think it is correct?

This post is from Domestic Chip Exchange

Comments

Almost. How did you get this? Wasn't it displayed at first?  Details Published on 2024-1-20 19:30

赞赏

1

查看全部赞赏

 
 
 

224

Posts

0

Resources
8
 
lugl4313820 posted on 2024-1-20 09:02 The highlight of this method is in the timer interrupt, where we perform the calculation. To correct the original poster's statement, it should be in the timer...

That's right, thanks for the correction


This post is from Domestic Chip Exchange
 
 
 

224

Posts

0

Resources
9
 
lugl4313820 posted on 2024-1-20 09:05 I will now demonstrate it to you using an oscilloscope.

How is it? Is there a waveform? If not, I will change the code



This post is from Domestic Chip Exchange

Comments

It seems that there is no waveform, and what is measured is the interference waveform of the power supply. If you use the interrupt method, you need to add a delay, otherwise the LED task will not run.  Details Published on 2024-1-20 12:45
 
 
 

224

Posts

0

Resources
10
 
lugl4313820 published on 2024-1-20 08:59 I output the voltage value directly, but when I use ADC to test, the voltage is unstable. What should I do if I want to output the voltage value all the time?

Do you mean using the DAC to output voltage and then using the ADC to measure the DAC output voltage?


This post is from Domestic Chip Exchange
 
 
 

224

Posts

0

Resources
11
 
jobszheng5 posted on 2024-1-20 09:24 Original poster, you can also try the combination of DMA + DAC + Timer, that is, DMA is used to transmit the output value of DAC, and Timer is used to trigger DMA periodically...

Indeed, this can also reduce the CPU usage, directly using DMA to transfer the numbers in the table. Thank you for your valuable advice.


This post is from Domestic Chip Exchange
 
 
 

9702

Posts

24

Resources
12
 

Doing such calculations in a timer interrupt should take a long time before exiting the interrupt, right?

This post is from Domestic Chip Exchange

Comments

Interrupts should be fast in and fast out. My calculation is very fast. I saw it written like this in a book before.  Details Published on 2024-1-20 12:51
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

6818

Posts

11

Resources
13
 
qiao--- posted on 2024-1-20 12:38 How is it? Is there a waveform? If not, I will modify the code

It seems that there is no waveform, and what is measured is the interference waveform of the power supply. If you use the interrupt method, you need to add a delay, otherwise the LED task will not run.

This post is from Domestic Chip Exchange

Comments

I understand. The first method requires changing the timer callback function. You need to set it to 1. Have you changed it? If not, it may be because of this that there is no waveform. The second method has a waveform but the LED task does not run normally. Is this correct?  Details Published on 2024-1-20 12:49
 
 
 

6818

Posts

11

Resources
14
 

I also used DAC before, but it seems that the output voltage is not stable.

This post is from Domestic Chip Exchange

Comments

Unstable, DAC seems to have a correction function, you can try it and see how it works before use  Details Published on 2024-1-20 12:50
 
 
 

224

Posts

0

Resources
15
 
lugl4313820 posted on 2024-1-20 12:45 It seems that there is no waveform, and the measured waveform is the interference waveform of the power supply. There is also the interrupt method. You need to add a delay, otherwise the LED task will not run. ...

I understand. The first method requires changing the timer callback function. You need to set it to 1. Have you changed it? If not, it may be because of this that there is no waveform. The second method has a waveform but the LED task does not run normally. Is this correct?


This post is from Domestic Chip Exchange

Comments

The second one doesn’t seem to exist either, and the same goes for the first one.  Details Published on 2024-1-20 13:10
 
 
 

224

Posts

0

Resources
16
 
lugl4313820 posted on 2024-1-20 12:46 I also used DAC before, but it seems that the output voltage is not stable.

Unstable, DAC seems to have a correction function, you can try it and see how it works before use


This post is from Domestic Chip Exchange

Comments

In fact, the CPU frequency is very high, and it is no problem for the CPU to use your multitasking system. You can use DMA with interrupts to achieve this.  Details Published on 2024-1-20 13:12
 
 
 

224

Posts

0

Resources
17
 
littleshrimp posted on 2024-1-20 12:43 It should take a long time to exit the interrupt if such calculation is performed in the timer interrupt, right?

Interrupts should be fast in and fast out. My calculation is very fast. I saw it written like this in a book before.


This post is from Domestic Chip Exchange
 
 
 

6818

Posts

11

Resources
18
 
qiao--- posted on 2024-1-20 12:49 I see. To use the first method, you need to change the timer callback function and set it to 1. Have you changed this? If not, it may be because of this...

The second one doesn’t seem to exist either, and the same goes for the first one.

This post is from Domestic Chip Exchange

Comments

OK, thanks to the moderator, I'll check what the problem is.  Details Published on 2024-1-20 13:13
OK, thanks to the moderator, I'll check what the problem is.  Details Published on 2024-1-20 13:11
 
 
 

224

Posts

0

Resources
19
 
lugl4313820 posted on 2024-1-20 13:10 The second type doesn't seem to exist either, and the first type is the same.

OK, thanks to the moderator, I'll check what the problem is.


This post is from Domestic Chip Exchange
 
 
 

6818

Posts

11

Resources
20
 
qiao--- posted on 2024-1-20 12:50 Unstable, DAC seems to have a correction function, you can try it, and try to correct it before use

In fact, the CPU frequency is very high, and it is no problem for the CPU to use your multitasking system. You can use DMA with interrupts to achieve this.

This post is from Domestic Chip Exchange
 
 
 

224

Posts

0

Resources
21
 
lugl4313820 posted on 2024-1-20 13:10 The second type doesn't seem to exist either, and the first type is the same.

It's inconvenient without an oscilloscope.


This post is from Domestic Chip Exchange

Comments

I have it, but I am not very skilled in using it. But I must learn how to use it.  Details Published on 2024-1-20 21:28
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list