Teste semf
Kontakt

DAC – Digital to Analog Converter

General

The DigitalOut hardware abstraction in semf creates an analog signal from an integer value respectively an interger array. There are two approaches:

  • DigitalOut for outputting one value for an unlimited time.
  • DigitalOutDma for outputting an array of values in a configured frequency.

In the following example, we use the hardware abstraction of the Stm32F7 core.

Initialization of AnalogOut

Using Stm32CubeMX you can enable the timer by the following steps:

  • Open Analog configuration and enable OUTx.
  • Enable Output Buffer in Parameter Settings.
  • Set Trigger to Software trigger in Parameter Settings.
  • Wave generation mode is disabled.

Usage of AnalogOut

For use the AnalogOut, call the setValue and start methods. You can stop the AnalogOut by calling the stop function and update the value by calling setValue whenever you need.

#include <HardwareAbstraction/Stm32F7/stm32f7analogout.h>

extern DAC_HandleTypeDef hadc;
class Bsp
{
public:
	static Bsp& instance()
	{
		static Bsp bsp;
		return bsp;
	}
	
	void init()
	{
		m_analogOut.setValue(0x10);
		m_analogOut.start();
	}
	
	semf::AnalogOut& analogOut()
	{
		return m_analogOut;
	}

private:
	Bsp()
	: m_analogOut(hdac, 1, semf::Stm32F7AnalogOut::Alignment::ALIGN_8BIT_RIGHT)
	 {

	 }

	 semf::Stm32F7AnalogOut m_analogOut;
};

Initialitation of AnalogOutDma

Using Stm32CubeMX you can enable the timer by the following steps:

  • Open Analog configuration and enable OUTx.
  • Enable Output Buffer in Parameter Settings.
  • Set Trigger to Software trigger in Parameter Settings.
  • Wave generation mode is disabled.
  • Enable the interrupt in the NVIC Settings.
  • Add a DMA channel with Half Word Data Width.
  • For outputting the same data cyclic, enable the Circular Mode.

Usage of AnalogOutDma

For using the AnalogOutDma, call the setValues and start methods. You can stop the AnalogOutDma by calling the stop function and update the value by calling setValues whenever you need.

#include <HardwareAbstraction/Stm32F7/stm32f7analogoutdma.h>

extern DAC_HandleTypeDef hdac;

class Bsp
{
public:
	static Bsp&amp; instance()
	{
		static Bsp bsp;
		return bsp;
	}

	void init()
	{
		static uint16_t data[16];
		for (size_t i=0; i &lt; 16; i++)
		{
			 data[i] = 20 * i;
		}

		// Connect dataWritten signal with onAnalogOutDataWritten() slot for getting into
		// the slot after all data is written
		m_analogOutDma.dataWritten.connect(this, &amp;Bsp::onAnalogOutDataWritten);
		m_analogOutDma.setData(data, 16);
		m_analogOutDma.start();
	}

	semf::AnalogOutDma&amp; analogOut()
	{
		return m_analogOutDma;
	}

private:
	void onAnalogOutDataWritten()
	{
		// do something
	}

	Bsp()
	: m_analogOutDma(hdac, 1, semf::Stm32F7AnalogOut::Alignment::ALIGN_8BIT_RIGHT)
	{ }

	semf::Stm32F7AnalogOutDma m_analogOutDma;
};

zurück zur Dokumentation