Skip to main content
Visitor II
June 1, 2022
Question

Generating a sine wave using STM32G4

  • June 1, 2022
  • 4 replies
  • 6488 views

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?

    This topic has been closed for replies.

    4 replies

    Graduate II
    June 1, 2022

    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

    Graduate
    June 2, 2022

    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.

    Graduate
    July 25, 2024

    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

    Graduate II
    July 25, 2024

    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.

    Graduate
    July 26, 2024

    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

    Graduate II
    July 27, 2024

     

    - 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.

    Super User
    July 29, 2024

    @BarryWhit wrote:

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


    Creating a spreadsheet is just another form of programming - just as easy to set up a spreadsheet as to write something in C, Python, whatever.

    And there's no need for copy & paste - just 'Save As" straight from the spreadsheet.

    Graduate II
    July 29, 2024

    phooey