cancel
Showing results for 
Search instead for 
Did you mean: 

Generating a sine wave using STM32G4

JMend.4
Associate

I'm a beginner with STM32 and I'm trying to generate a sine waveform using a STM32G4 through the NUCLEO-G431KB board to be exact. I've been looking through tutorials on how to generate a waveform using a STM32G4 board, particularly this tutorial but nothing has worked — most of the guides I've taken a look at are not for STM32G4 boards which is why I reckon they can't be applicable to the board I'm using. I've also taken a look at this reference manual but I don't know where to start.

Does anyone know of any resources that would be helpful for me in generating a waveform using a STM32G4 in particular? Or maybe, how different would programming a STM32G4 be in comparison to the STM32H7 tutorial that I've linked?

12 REPLIES 12

STM32G4 is relatively new and not used by as many.

No particular reason why method on other STM32 would be materially different in implementation to G4

Several TIM examples here

STM32Cube_FW_G4_V1.4.0\Projects\NUCLEO-G431RB\Examples\TIM

If not TIM PWM, how about DAC ?

STM32Cube_FW_G4_V1.4.0\Projects\NUCLEO-G431RB\Examples\DAC\DAC_SignalsGeneration2

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gregstm
Senior III

In the past I would create a quarter/half of a sine wave cycle on a spreadsheet and use that as downloaded data to generate a sine wave via the DAC. But your chip has one of those Trig accelerator things (CORDIC accelerator). I would use that to generate a full cycle sine wave kept in RAM for pumping out to the DAC.

Greg

I'm actually working on a similar G431 and/or G474 project as part of learing to use these for power electronics control.

In the short term I'd like to find a sample project that imported a spread sheet and used the DAC to generate a custom waveform that I build up in excel.  This would not be a sinusoidal waveform, it would be to simulate the input from a current sensor so it would have a brief pulse of voltage to the maximum value, then quickly drop to a lower value, then the slope would rise linearly for a certain percentage of the duty cycle, then it would drop to zero for the remainder of the switch period and then start over with the pulse.

After I can do that with a simple excel table, I'll likely make it a programming project to calculate the values in real time in response to a few digital inputs to toggle up and down various parameters like the slope, duty cycle, and average curent level.

Any references you could point me to that would get me started on that would be very helpful.Thanks

Dave

Perhaps have it output a .CSV file and paste the records into an int16_t C array?

int16_t PatternFile[] = { 1,2,3,4,5,6 };

Have a TIM pace the DAC, have that trigger the DMA pattern fetch, one-shot or circular.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@decook1110 , this thread is two years old.

 

The G4 Cube MCU package includes an example project called "DAC_SignalsGeneration2" which implements a signal generator using DAC and DMA. 

- If someone's post helped resolve your issue, please thank them by clicking "Accept as Solution".
- Please post an update with details once you've solved your issue. Your experience may help others.

When I generate table data, I tend to use an assembly language routine, not really for speed, just because I find it neater/easier to layout the data (I only use the routine to initialise an array at startup anyway). I just cut and paste it from the spread sheet program (or generate the data with a program). Here's a snippet of my routine (although I suspect using assembly language is frowned upon these days - maybe)  - 

;   s16 SINE512 (int index)

    and     r0, r0, #0x07f
                  ;note: sine_table label is in data space, so no
                  ;      problems with "interworking" bit
    ldr     r1, =sine512_table
    ldrsh   r0, [r1, r0, lsl #1]

                      ;return R0
    bx      lr      ;;return

    AREA    XXY, DATA, READONLY
    
sine512_table
        ;Table has 1/4 cycle (2*Pi=360degrees)using 127 words 
        
  DCW 0x0000;    0,      0
  DCW 0x0192;    1,    402
  DCW 0x0324;    2,    804
  DCW 0x04B6;    3,   1206
  DCW 0x0648;    4,   1608
  DCW 0x07D9;    5,   2009
  DCW 0x096A;    6,   2410
  DCW 0x0AFB;    7,   2811
  DCW 0x0C8C;    8,   3212
  DCW 0x0E1C;    9,   3612
   ....

 


@gregstm wrote:

When I generate table data, I tend to use an assembly language routine, not really for speed, just because I find it neater/easier to layout the data 


Really?

As @Tesla DeLorean , a C array seems easier & neater to me:

 

int16_t sine512_table[] =
{
        0,
      402,
      804,
     1206,
     1608,
     2009,
     2410,
     2811,
     3212,
     3612,
     etc ...

 

 You can do it directly in the spreadsheet - just Save As text:

AndrewNeil_1-1721988650343.png

 

EDIT: commas!

 

 

decook1110
Associate III

I wanted to Thank all the responders to my question, all the different responses gave me a lot to think about.

I will look for the "DAC_SignalsGeneration2" sample and the suggestion to save to a text file to insert the array data seems like the easiest way to get the data into the code.

Then I'll try adding some code to read an analog input from a potentiometer and move the non-zero part of the curve up and down

After that I'll try to write a routine that builds the curves in real time based on several parameters.

Thanks again, I'm sure I'll have further questions

Dave

BarryWhit
Lead II

 

- The STM32G4 has a single-precision floating point hardware unit (and anyway, you could turn on FP software emulation in options).

- The C standard library provides a floating point `sin()` function (whose values you can then cast to an integer value with appropriate scaling, for the DAC).

- So, just pre-compute a sine lookup table at startup.  

 

Or, at the very least, write a host program (in C, python, befunge, whatever) that takes in freq and phase as arguments, and generates a .h file containing an array of values. Then, include that file in your STM32 project code.

 

Why futz around with spreadsheets and copy-paste every time you need to tweak something?!? scandalous.

- If someone's post helped resolve your issue, please thank them by clicking "Accept as Solution".
- Please post an update with details once you've solved your issue. Your experience may help others.