DAC Library

DAC (Digital to Analog Converter) module is available with a number of ARM MCU modules. DAC is an electronic circuit that converts discrete digital numbers to continuous signals. DAC Library provides you a comfortable work with the module.

Library Routines

DACx_Init

Prototype

void DACx_Init(unsigned int channel);

// for Kinetis devices

void DACx_Init();

Description

This routine initializes DAC module and sets used channel.

Parameters
  • channel: used DAC channel :
    Value Description
    _DAC_CH1_ENABLE Channel 1.
    _DAC_CH2_ENABLE Channel 2.
    _DAC_DUAL_MODE_ENABLE Channels 1 and 2.
Returns

Nothing.

Requires
  • MCU with built-in DAC module.
Example
DAC1_Init(_DAC_CH1_ENABLE);  // Initialize DAC1 module for STM32 device, channel 1
DAC1_Init();  // Initialize DAC1 module for Kinetis device
Notes
  • DAC library routines require you to specify the module you want to use. To select the desired DAC module, simply change the letter x in the routine prototype for a number from 1 to 2.
  • Number of DAC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

DACx_Init_Advanced

Prototype

void DACx_Init_Advanced(unsigned long config);

Description

This routine initializes DAC module according to custom configuration.

Parameters
  • config: used DAC configuration :
    Value Description
    _DAC_CFG_REFVOLT_SRC_VREF1 Select DACREF_1 as the reference voltage.
    _DAC_CFG_REFVOLT_SRC_VREF2 Select DACREF_2 as the reference voltage.
    _DAC_CFG_LOWPOWER_ENABLED Enable the low power mode.
Returns

Nothing.

Requires
  • MCU with built-in DAC module.
Example
DAC0_Init_Advanced(_DAC_CFG_REFVOLT_SRC_VREF1 || _DAC_CFG_LOWPOWER_ENABLED);
Notes
  • DAC library routines require you to specify the module you want to use. To select the desired DAC module, simply change the letter x in the routine prototype for a number from 1 to 2.
  • Number of DAC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • This routine is available only for Kinetis devices.

DACx_Advanced_Chy_Output

Prototype

void DACx_Advanced_Chy_Output(unsigned long value, unsigned long config);

Description

This routine outputs analog value from the DAC module with user defined settings.

Parameters
  • value: output voltage value. Depending on the used mode (8-bit or 12-bit) this value is in range of 0-255 or 0-4095.

    The output analog voltage is calculated according to the equation: DACoutput = VREF * value/mode,
    where VREF is the voltage reference value from the VREF pin and mode is 255 (8-bit mode) or 4095 (12-bit mode).

  • config: configuration of the DAC module. Following constants can be or-ed to form the desired configuration :
    Value Description
    _DAC_8_BIT Set DAC module in 8-bit mode.
    _DAC_12_BIT Set DAC module in 12-bit mode.
    _DAC_RIGHT_ALIGN Right aligned data (available only in 12-bit mode).
    _DAC_LEFT_ALIGN Left aligned data (available only in 12-bit mode).
    _DAC_NOISE_WAVE Enable Noise-wave generation.
    _DAC_TRIANGLE_WAVE Enable Triangle-wave generation.
    _DAC_WAVE_DISABLED Wave generation disabled.
    _DAC_MMAP_1 Unmask bit 0 of LFSR/ triangle amplitude equal to 1.
    _DAC_MMAP_3 Unmask bits[1:0] of LFSR/ triangle amplitude equal to 3.
    _DAC_MMAP_7 Unmask bits[2:0] of LFSR/ triangle amplitude equal to 7.
    _DAC_MMAP_15 Unmask bits[3:0] of LFSR/ triangle amplitude equal to 15.
    _DAC_MMAP_31 Unmask bits[4:0] of LFSR/ triangle amplitude equal to 31.
    _DAC_MMAP_63 Unmask bits[5:0] of LFSR/ triangle amplitude equal to 63.
    _DAC_MMAP_127 Unmask bits[6:0] of LFSR/ triangle amplitude equal to 127.
    _DAC_MMAP_255 Unmask bits[7:0] of LFSR/ triangle amplitude equal to 255.
    _DAC_MMAP_511 Unmask bits[8:0] of LFSR/ triangle amplitude equal to 511.
    _DAC_MMAP_1023 Unmask bits[9:0] of LFSR/ triangle amplitude equal to 1023.
    _DAC_MMAP_2047 Unmask bits[10:0] of LFSR/ triangle amplitude equal to 2047.
    _DAC_MMAP_4095 Unmask bits[11:0] of LFSR/ triangle amplitude equal to 4095.
    _DAC_TRIGGER_TIM6 Timer 6 used as DAC trigger selection.
    _DAC_TRIGGER_TIM3 Timer 3 used as DAC trigger selection.
    _DAC_TRIGGER_TIM7 Timer 7 used as DAC trigger selection.
    _DAC_TRIGGER_TIM5 Timer 5 used as DAC trigger selection.
    _DAC_TRIGGER_TIM18 Timer 18 used as DAC trigger selection.
    _DAC_TRIGGER_TIM2 Timer 2 used as DAC trigger selection.
    _DAC_TRIGGER_TIM4 Timer 4 used as DAC trigger selection.
    _DAC_TRIGGER_EXTI External interrupt used as DAC trigger selection.
    _DAC_TRIGGER_SW DAC triggered from the software.
Returns

Nothing.

Requires
  • The MCU with built-in DAC module.
Example
DAC1_Advanced_Ch1_Output(2047, _DAC_12_BIT | DAC_RIGHT_ALIGN | _DAC_WAVE_DISABLED | _DAC_TRIGGER_SW);  // get output from the channel 1 of DAC1 module
Notes
  • DAC library routines require you to specify the module you want to use. To select the desired DAC module, simply change the letter x in the routine prototype for a number from 1 to 2.
  • Also, simply change the letter y in the routine prototype for a number from 1 to 2 for a desired channel.
  • Number of DAC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • This routine is available only for STM32 devices.

DACx_Chy_Output

Prototype

void DACx_Chy_Output(unsigned long value);

Description

This routine outputs value from the DAC module and channel with the default settings :

  • 12-bit mode,
  • data right aligned,
  • voltage reference taken from the Vref pin.

Parameters
  • value: output voltage value. This value is in range of 0-4095. The output analog voltage is calculated according to the equation: DACoutput = VREF * value/4095,
    where VREF is the voltage reference value from the VREF pin.

  • Returns

    Nothing.

    Requires
    • The MCU with built-in DAC module.
    Example
    DAC1_Ch2_Output(127);  // get output from the channel 2 of DAC1 module
    
    Notes
    • DAC library routines require you to specify the module you want to use. To select the desired DAC module, simply change the letter x in the routine prototype for a number from 1 to 2.
    • Also, simply change the letter y in the routine prototype for a number from 1 to 2 for a desired channel.
    • Number of DAC modules/channelrs per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
    • This routine is available only for STM32 devices.

    DAC1_Dual_Output

    Prototype

    void DAC1_Dual_Output(unsigned long value1, unsigned long value2, unsigned long config1, unsigned long config2);

    Description

    This routine outputs value from both channels of the DAC1 module with user defined settings.

    Parameters
    • value1, value2: output voltage value. Depending on the used mode (8-bit or 12-bit) this value is in range of 0-255 or 0-4095.

      The output analog voltage is calculated according to the equation: DACoutput = VREF * value/mode,
      where VREF is the voltage reference value from the VREF pin and mode is 255 (8-bit mode) or 4095 (12-bit mode).

    • config1, config2 : configuration of the DAC module. Following constants can be or-ed to form the desired configuration :
      Value Description
      _DAC_8_BIT Set DAC module in 8-bit mode.
      _DAC_12_BIT Set DAC module in 12-bit mode.
      _DAC_RIGHT_ALIGN Right aligned data (available only in 12-bit mode).
      _DAC_LEFT_ALIGN Left aligned data (available only in 12-bit mode).
      _DAC_NOISE_WAVE Enable Noise-wave generation.
      _DAC_TRIANGLE_WAVE Enable Triangle-wave generation.
      _DAC_WAVE_DISABLED Wave generation disabled.
      _DAC_MMAP_1 Unmask bit 0 of LFSR/ triangle amplitude equal to 1.
      _DAC_MMAP_3 Unmask bits[1:0] of LFSR/ triangle amplitude equal to 3.
      _DAC_MMAP_7 Unmask bits[2:0] of LFSR/ triangle amplitude equal to 7.
      _DAC_MMAP_15 Unmask bits[3:0] of LFSR/ triangle amplitude equal to 15.
      _DAC_MMAP_31 Unmask bits[4:0] of LFSR/ triangle amplitude equal to 31.
      _DAC_MMAP_63 Unmask bits[5:0] of LFSR/ triangle amplitude equal to 63.
      _DAC_MMAP_127 Unmask bits[6:0] of LFSR/ triangle amplitude equal to 127.
      _DAC_MMAP_255 Unmask bits[7:0] of LFSR/ triangle amplitude equal to 255.
      _DAC_MMAP_511 Unmask bits[8:0] of LFSR/ triangle amplitude equal to 511.
      _DAC_MMAP_1023 Unmask bits[9:0] of LFSR/ triangle amplitude equal to 1023.
      _DAC_MMAP_2047 Unmask bits[10:0] of LFSR/ triangle amplitude equal to 2047.
      _DAC_MMAP_4095 Unmask bits[11:0] of LFSR/ triangle amplitude equal to 4095.
      _DAC_TRIGGER_TIM6 Timer 6 used as DAC trigger selection.
      _DAC_TRIGGER_TIM3 Timer 3 used as DAC trigger selection.
      _DAC_TRIGGER_TIM7 Timer 7 used as DAC trigger selection.
      _DAC_TRIGGER_TIM5 Timer 5 used as DAC trigger selection.
      _DAC_TRIGGER_TIM18 Timer 18 used as DAC trigger selection.
      _DAC_TRIGGER_TIM2 Timer 2 used as DAC trigger selection.
      _DAC_TRIGGER_TIM4 Timer 4 used as DAC trigger selection.
      _DAC_TRIGGER_EXTI External interrupt used as DAC trigger selection.
      _DAC_TRIGGER_SW DAC triggered from the software.
    Returns

    Nothing.

    Requires
    • The MCU with built-in DAC module.
    Example
    DAC1_Dual_Output(2047, _DAC_12_BIT, 511, _DAC_8_BIT);  // get output from both channels of DAC1 module
    
    Notes
    • This routine is available only for STM32 devices.

    DACx_Output

    Prototype

    void DACx_Output(unsigned int value);

    Description

    This routine drives the DAC module to output DAC value.

    Parameters
    • value output voltage value. This value is in range of 0-4095.

      The output analog voltage is calculated according to the equation: DACoutput = VREF * value/4095,
      where VREF is the voltage reference value..

    Returns

    Nothing.

    Requires
    • The MCU with built-in DAC module.
    Example
    DAC1_Output(2047);  // get output from DAC1 module
    
    Notes
    • This routine is available only for Kinetis devices.

    DACx_Deinit

    Prototype

    void DACx_Deinit();

    Description

    This function deinitializes previously used DAC module

    Parameters

    None.

    Returns

    Nothing.

    Requires
    • The MCU with built-in DAC module.
    Example
    DAC1_Deinit(); // deinitialize DAC1 module
    
    Notes
    • DAC library routines require you to specify the module you want to use. To select the desired DAC module, simply change the letter x in the routine prototype for a number from 1 to 2.
    • Number of DAC modules/channelrs per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
    • This routine is available only for STM32 devices.
    Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
    What do you think about this topic ? Send us feedback!
    Want more examples and libraries? 
    Find them on LibStock - A place for the code