cancel
Showing results for 
Search instead for 
Did you mean: 

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

Ateam
Associate

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
KDJEM.1
ST Employee

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

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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

 

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

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.