Skip to main content
Visitor II
February 28, 2024
Solved

I want to use the Discovery kit with STM32H745XI MCU to generate sawtooth waves.

  • February 28, 2024
  • 3 replies
  • 1124 views

The Discovery kit with STM32H745XI MCU outputs a triangle wave, but how can I make it output a sawtooth wave instead of a triangle wave?
Please let me know the specific improvement and source code.
Looking forward to your answer.

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    Triangle wave generation is a DAC HW feature.

    Sawtooth you need to generate it by software by samples using a lookup table like done for the escalator wave generation.

    The escalator wave generation is the one you need:

     

    const uint8_t aEscalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};

     

     

     

    static void DAC_Ch1_EscalatorConfig(void)
    {
     /*##-1- Initialize the DAC peripheral ######################################*/
     if (HAL_DAC_Init(&DacHandle) != HAL_OK)
     {
     /* Initialization Error */
     Error_Handler();
     }
    
     /*##-1- DAC channel1 Configuration #########################################*/
     sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
     sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
    
     if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL) != HAL_OK)
     {
     /* Channel configuration Error */
     Error_Handler();
     }
    
     /*##-2- Enable DAC selected channel and associated DMA #############################*/
     if (HAL_DAC_Start_DMA(&DacHandle, DACx_CHANNEL, (uint32_t *)aEscalator8bit, 6, DAC_ALIGN_8B_R) != HAL_OK)
     {
     /* Start DMA Error */
     Error_Handler();
     }
    }

     

     

    Meanwhile, you need to increase the samples as much as possible with a short time between samples to be nearest as much as possible to a sawtooth wave.

    3 replies

    Technical Moderator
    February 28, 2024

    Hello @Ateam and welcome to the community :),

    You can use DAC to generate a sawtooth wave.

    For that, I advise you to take a look to this wiki page Getting started with DAC - stm32mcu.

    This wiki example used NUCLEO-L476RG board but it can be easily tailored to any other board.

    I hope this help you!

    Kaouthar

     

    Graduate II
    February 28, 2024

    The DAC will output any waveform you describe in the pattern buffer.

     

    mƎALLEmAnswer
    Technical Moderator
    February 28, 2024

    Hello,

    Triangle wave generation is a DAC HW feature.

    Sawtooth you need to generate it by software by samples using a lookup table like done for the escalator wave generation.

    The escalator wave generation is the one you need:

     

    const uint8_t aEscalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};

     

     

     

    static void DAC_Ch1_EscalatorConfig(void)
    {
     /*##-1- Initialize the DAC peripheral ######################################*/
     if (HAL_DAC_Init(&DacHandle) != HAL_OK)
     {
     /* Initialization Error */
     Error_Handler();
     }
    
     /*##-1- DAC channel1 Configuration #########################################*/
     sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
     sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
    
     if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL) != HAL_OK)
     {
     /* Channel configuration Error */
     Error_Handler();
     }
    
     /*##-2- Enable DAC selected channel and associated DMA #############################*/
     if (HAL_DAC_Start_DMA(&DacHandle, DACx_CHANNEL, (uint32_t *)aEscalator8bit, 6, DAC_ALIGN_8B_R) != HAL_OK)
     {
     /* Start DMA Error */
     Error_Handler();
     }
    }

     

     

    Meanwhile, you need to increase the samples as much as possible with a short time between samples to be nearest as much as possible to a sawtooth wave.