cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 DAC sinewave generate using formular

SEng.1
Associate II

I'm success create sinewave signal by using table:

uint32_t Wave_LUT[NS] = {
    2048, 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, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 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, 189,
    234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258,
    1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047
};
 
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, Wave_LUT, NS, DAC_ALIGN_12B_R);

but when i try create my own list from formula and got weird result:

#define NS	128
 
uint32_t IV[NS];
uint16_t value;
 
void calsine()
{
	for(int i = 0 ; i < NS ; i++)
	{
		value =  (uint16_t)rint((sinf(((2*M_PI)/NS)*i)+1)*2048);
		IV[i] = value < 4096 ? value : 4095;
	}
}
 
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t*)IV, NS, DAC_ALIGN_12B_R);

Any pointer where i got wrong?

10 REPLIES 10
TDK
Guru

> IV[i] = value < 4096 ? value : 4095;

This should be ">=" rather than "<".

If you feel a post has answered your question, please click "Accept as Solution".

> got weird result

What does it mean? What is in the array and how does the resulting waveform look like?

>> IV[i] = value < 4096 ? value : 4095;

> This should be ">=" rather than "<".

Why?

JW

Seems like the code was intended to clip signals to 12-but, as that’s what the DAC is.
If you feel a post has answered your question, please click "Accept as Solution".

So, if value is less than 4096, it should be used as is, and if not, 4095 should be used, right? That's what OP's code appears to do. Or am I overlooking something?

JW

No, you're right. I misread. It is backwards from how I'd write it, but I should have realized that. Sorry for the noise.

Agree with JW that a description of the result you're getting would be more useful than calling it "weird". You could also examine the values in the array in the debugger to verify your initialization is correct.

If you feel a post has answered your question, please click "Accept as Solution".

> It is backwards from how I'd write it

I understand that very well... But I am also curious, how exactly would you write it? I don't have formal programmer education so I am curious what is the "proper" (recommended) way of doing things.

Thanks and sorry for the OT.

JW

Hard to explain. Here, you want to see if it's outside the bounds and then clip it. Outside the bounds is the interesting thing. Inside the bounds, it's boring and you don't change the value.

I think the most readable way to write if statements is:

if (condition) then (interesting thing) else (boring thing)

In this case, you don't even need (boring thing) if you split it up. Here's how I would write it:

if (value > 4095) {
  value = 4095;
}
IV[i] = value;

Benefit here is you write the same value (4095) twice rather than 4096/4095. If you wanted a single line, this is my second preference:

IV[i] = (value > 4095) ? 4095 : value;

I don't know if this is taught one way or the other. Probably not.

I think Python did a better job of making this sort of statement readable:

IV[i] = value if value < 4095 else 4095

Sorry for the tangent, OP.

If you feel a post has answered your question, please click "Accept as Solution".

> Hard to explain.

But it makes perfect sense, it jumps out more clearly, indeed.

Thanks.

JW

SEng.1
Associate II

Hi @Community member​ :

I'm using onboard ADC and output to pc so that i can plot out the sinewave in python.

here is the graph with wave_LUT:

0693W00000DnbZmQAJ.png Here is the graph for calsine:

0693W00000DnbZrQAJ.pngAll the code exactly the same except one using lookup table and another is calculated.

I got this wave_lut table from here

As for the calsine, i follow this

I check the IV value, they all looks normal to me.

except slight difference compare to the wave_lut table.

0693W00000DnbZwQAJ.jpg0693W00000DnbaBQAR.jpg0693W00000Dnba6QAB.jpg0693W00000Dnba1QAB.jpg