Skip to content

Generate a sawtooth with a DAC

Introduction

In order to control the converter in current mode without the subhamonical oscillations, we need to be able to generate a sawtooth signal. For that purpose, this tutorial aims to teach you how to use a DAC to generate a sawtooth signal with STM32CubeMX and Simulink.

For the current mode control on the V1.1.2, we need to generate 2 sawtooth (one for each arm) and a 1.024V reference to center the signal representing the voltages.

Warning: the DAC configuration presented here isn't exactly the one used to run the converter in current mode, but is made for the user to watch the DAC signals.

Prerequisites

Having installed and configured as presented in Getting started the following software:

  • STM32CubeMX
  • STM32CubeIDE
  • STM32MatTarget
  • Matlab

Hardware

This tutorial was made on the version V1.1.2 of the Owntech converter. Hence it may need a few adjustments if you want to follow it with different hardware.

STM32CubeMX settings

General settings

The folowing steps are necessary to work on the V.1.1.2 Owntech converter whatever the control mode used.

  • In System core>RCC set High speed clock(HSE) to Crystal/Ceramic resonator

00

  • In System core>SYS: set Debug to Trace asynchronous Sw, set VREFBUFF Mode to Internal voltage reference, set Timebase source to TIM1.

01

DAC settings

In Analog>DAC1:

  • Set OUT1 mode to Connected to external pin and to on-chip peripherals.
  • Set Trigger to HRTIM RST Out1 event and Trigger2 to HRTIM STEP Out1 event (so that the DAC sawtooth is triggered by HRTIM1 TIM_A)
  • Set Wave generation mode to Sawtooth wave generation and verify that Polarity is set to Sawtooth wave decrement.
  • Set reset data to 4000 and step data to 200 (These values are arbitrary in order to observe the sawtooth well).

002

In Analog>DAC2:

  • Set OUT1 mode to Connected to external pin only.

003

In Analog>DAC4:

  • Tick the box that says Out1 connected to on-chip peripherals only.
  • Apply the same settings as DAC1 except for Trigger and Trigger2 whiches must be HRTIM RST Out2 and HRTIM STEP Out2 (to be driven by HRTIM1 TIM_B).

004

As you may have noticed, the DAC4 output isn't connected to any output pin. In order to measure it anyway, we need to connect it to a follower operational amplifier.

In Analog>OPAMP4:

  • Set Mode to Follower-DAC4_OUT1-INP.

005

HRTIM Settings

Since we want two sawtooth signals of the same frequency with a 180° phase shift, we need two timers with shifted by 180°.

In Timers>HRTIM1:

  • Tick the Master timer enable box.

  • Set Timer A to TA1 output active and Timer B to TA2 output active.

006

In the tab Master Timer:

  • Set Prescaler ratio to HRTIM Clock is multiplied by 16, and Period to 13600 so that the resulting PWM frequency is 200kHz.

  • Enable Compare Unit 1 and set Compare value to 6800 (50% of the timer's period).

007

In the tab Timer A:

  • Set Prescaler ratio to HRTIM Clock is multiplied by 16, and Period to 13600 so that the resulting PWM frequency is 200kHz.

  • Set Reset trigger sources Selection to 1 and 1st Trigger source selection to master timer period event.

008

  • Enable Compare Unit 1 and set Compare value to 6800 (50% of the timer's period).

  • Enable Compare Unit 2 and set Compare value to 500.

009

Still in Timer A tab, in Output 1 Configuration:

  • Set Set Source selection to 1, and 1st Set Source to Timer period event.

  • Set Reset Source Selection to 1, and 1st Reset Source to Timer Compare 1 event.

  • In Dual DAC Channel Configuration set Dual DAC Channel Trigger to Slope Compensation Edge-aligned PWM. This setting enables the timer to generate a trigger for the DAC.

010

In the tab Timer B:

  • Apply the same settings as Timer A except for Timing Unit>1st Reset Source Selection, set it to master timer Compare 1 event.

011

012

013

Simulink model

  • In your project folder, create a blank Simulink model.

Placing and setting blocks

  • In the Simulink Library, in Target Support package - STM32 Adapter>MCU Config Select the STM32 Config block and place it in your model. 014

  • In this block click on select STM32 configuration file and browse to the .ioc file you just configured.

015

  • From the Simulink library, place 3 DAC blocks.

016

  • Set one block for each DAC, (DAC1, DAC2, DAC4) such that only DAC2's block has an input corresponding to DAC2_OUT1.

017

  • In the Simulink library, in Commonly used, find a Constant block and place it in your model. Connect it to the DAC2 input, set its value to 2048 and its output Data Type to uint16.

018

In the Model Cofiguration parameters

In Hardware Implementation

  • Set Device vendor to custom processor.

020

  • Set Code Generation System target file to stm32.tlc.

021

In Code generation>STM32 Options

  • Copy paste your installation path to STM32CubeMX, STM32MAT (may not be the same as me) and the relative path to the .ioc file you just configured.

022

  • In the Apps tab, select Embedded Coder, go to the C Code tab that just opened, and click on Build.

023

024

STM32CubeIDE

  • Open STM32CubeIDE and select the folder that contains your project as your workplace.

  • Open your project with File> Open Projects from file system and click on directory in the new window to browse for your project.

  • Once open, expand it and right-click on the folder named Tuto_DAC (or whatever name you gave to your .slx file) go to properties, and in the new window, in C/C++ Debug, untick the box that says Exclude resource from build. Don't forget to apply the change before closing the window.

025

  • Now connect the debugger to the shield board on J7 and to your computer via an USB port and click on the play button to upload and launch the program on the micro controller.

026

Results

If you connect measure probes on TP4 (HRTIM1_TA1), TP6 (HRTIM1_TB1), pin 18 of the µC, and pin 34 of the µC, you should observe this kind of signal. We observe that each DAC output is synchronised with a PWM and the pairs DAC/PWM are shifted by 180° from each other.

You should also measure a voltage of 1.024V on JP3, if it it not exactly 1.024V, calibrate the DAC with the procedure explained here and then change the 2048 in your Simulink model with the right value.