Skip to main content
Associate III
September 6, 2024
Question

The signal result becomes a straight line on the DAC

  • September 6, 2024
  • 3 replies
  • 1233 views

 

"I am using your program and made some changes, but when I use it in the function

 

 

HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)value, 89, DAC_ALIGN_12B_R); 
HAL_TIM_Base_Start(&htim4);

 

the signal result on the oscilloscope is not continuous. What I mean is, when I use a very long array, the result does not match what I expected. To help you understand, I am attaching two photos comparing the result I want and the result from the program.

WhatsApp Image 2024-09-06 at 16.06.24 (1).jpegWhatsApp Image 2024-09-06 at 16.06.24.jpeg

 

I have a signal result like the one on the left, but I want it to be like the one on the right. After investigating, it turns out that the length value in HAL_DAC_Start_DMA was manually changed. If it can be done automatically, please give suggestions so I don't have to set it manually.

program to check

 

uint16_t hold_samples = 50;
uint16_t firstHalf[64] =
{
2100, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355,
3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076,
4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730,
3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500,
2400, 2300, 2199, 2098

}; // Nilai pada bagian pertama
uint16_t secondHalf[71] =
{
1997, 1896, 1795, 1695, 1595, 1497,1305, 1212, 1120, 1031, 944, 860, 779, 701, 627,
556, 488, 424, 365, 309, 258, 211, 168, 130, 97, 69, 45, 26, 13,4, 0, 1, 8, 19, 35, 56, 82,
113, 149, 200, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258,
1353, 1449, 1546, 1645, 1745, 1845, 1947, 1965, 1990, 2010, 2022, 2047
}; // Nilai pada bagian kedua
uint16_t totalSize = (sizeof(firstHalf) / sizeof(uint16_t)) + hold_samples + (sizeof(secondHalf) / sizeof(uint16_t));
 uint16_t index = 0;
 uint16_t n_firstHalf = (sizeof(firstHalf) / sizeof(uint16_t));

 for (int i = 0; i < n_firstHalf; i++) {
 value[index] = firstHalf[i];
 index++;
 }

 for (int i = 0; i < hold_samples; i++) {
 value[index] = 2048;
 index++;
 }

 int n_secondHalf = (sizeof(secondHalf) / sizeof(uint16_t));
 for (int i = 0; i < n_secondHalf; i++) {
 value[index] = secondHalf[i];
 index++;
 }

HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)value, 89, DAC_ALIGN_12B_R);
HAL_TIM_Base_Start(&htim4);

 

3 replies

LCE
Principal II
September 6, 2024

1) Is the DMA in CIRCULAR mode?

2) you probably need to add the hold value to the end of the array

 

And please post source code with the "</>" button to make it more readable. And you can still edit your original post.

 

PS / edit: and the DAC can't draw stuff, so it's not "a straight line", it's a DC voltage. ;) 

macam1Author
Associate III
September 7, 2024

@LCE 

 

1. yes, DMA in circular mode

2. Can you give an example of what you mean? I'm a beginner and can't quite picture it. I just want to learn. Thank you for your feedback, I have already changed the output display.

TDK
Super User
September 6, 2024

> HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)value, 89, DAC_ALIGN_12B_R);

You're sending more than 89 values. Shouldn't this be

HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)value, index, DAC_ALIGN_12B_R);

 

Is the value array of sufficient size? Needs to be 64 + 50 + 71 samples long, of type uint16_t. Your code does not include its definition.

"If you feel a post has answered your question, please click ""Accept as Solution""."
macam1Author
Associate III
September 7, 2024

Yes, I have tried using

HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)value, index, DAC_ALIGN_12B_R);

but there is no result. That's why I tried using a fixed number. Is there something wrong with my code? Because when I add a watch for index, it shows 'not evaluated'. I'm a bit confused, please help.

@TDK 

TDK
Super User
September 7, 2024

Show the full code. Attach it as a 7zip file if needed. No reason why index should not work. Make sure you're using circular DMA.

"If you feel a post has answered your question, please click ""Accept as Solution""."
LCE
Principal II
September 9, 2024

The way you fill the array right now is:

firstHalf, hold_samples, secondHalf

From what you have shown what you want you might want to add hold_samples to the end.

But first you have to make the DMA run continously.