bigbat 发表于 2025-1-19 17:28

【FRDM-MCXN947测评】DAC数模转换测试

<p><strong>1、测试介绍</strong></p>

<p>MCXN947VDF MCU有两组DAC通道,12bit 2个,14bit 1个本次使用测试的是DAC0通道,测试通过中断进行设置操作,通过串口进行修改测试电压值的操作。</p>

<p>硬件:FRDM-MCXN947开发板,万用表。</p>

<p>软件:GCC ARM Embedded 13.2.1编译器。不知道这个编译器版本是绑定的,在VScode中无法更换。</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Putty串口终端软件。</p>

<p>&nbsp;通过J1接口的4_2pin输出。&nbsp;</p>

<p> &nbsp;</p>

<p><strong>2、测试过程</strong></p>

<p>程序项目参考dac_1_buffer_interrupt样例</p>

<p>使用vscode打开项目</p>

<p>&nbsp; 使用Putty软件连接串口。</p>

<p> &nbsp;</p>

<p>通过putty终端进行修改电压值</p>

<p>&nbsp; 电压最低值:0.0095,最高值:2.484,输出基本上稳定。</p>

<p><strong>3、软件设计</strong></p>

<p>软件步骤:</p>

<p>1、初始化DAC设备</p>

<p>2、启动DAC设备</p>

<p>3、启动DAC中断</p>

<pre>
<code class="language-cpp">/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_dac.h"

/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_DAC_BASEADDR         DAC0
#define DEMO_DAC_IRQ_ID         DAC0_IRQn
#define DEMO_DAC_IRQ_HANDLER_FUNC DAC0_IRQHandler
#define DEMO_DAC_VREF             kDAC_ReferenceVoltageSourceAlt1
#define DEMO_DAC_VALUE_ARRAY_SIZE 32U

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/
volatile uint32_t g_DacInputIndex    = 0U;
volatile uint32_t g_DacOutputIndex   = 0U;
volatile uint32_t g_DacInterruptDone = false;
/* User-defined wave for DAC output. */
const uint32_t g_DacValues = {
    0U,   100,200,300,400,500,600,700,800,900,1000, 1100, 1200, 1300, 1400, 1500,
    1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100};

/*******************************************************************************
* Code
******************************************************************************/

/*!
* @brief Main function
*/
int main(void)
{
    dac_config_t dacConfigStruct;

    /* attach FRO 12M to FLEXCOMM4 (debug console) */

    CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
    CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    /* attach FRO HF to DAC0 */
    CLOCK_SetClkDiv(kCLOCK_DivDac0Clk, 1u);
    CLOCK_AttachClk(kFRO_HF_to_DAC0);

    /* enable DAC0 and VREF */
    SPC0-&gt;ACTIVE_CFG1 |= 0x11;

    BOARD_InitPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    EnableIRQ(DEMO_DAC_IRQ_ID); /* Enable interrupt in NVIC. */

    PRINTF("\r\nDAC buffer interrupt Example.\r\n");

    /* Configure the DAC. */
    DAC_GetDefaultConfig(&amp;dacConfigStruct);
    dacConfigStruct.referenceVoltageSource = DEMO_DAC_VREF;
    dacConfigStruct.fifoTriggerMode      = kDAC_FIFOTriggerBySoftwareMode; /* Software trigger. */
    dacConfigStruct.fifoWorkMode         = kDAC_FIFOWorkAsNormalMode;      /* Normal FIFO mode. */
    dacConfigStruct.fifoWatermarkLevel   = 4U;                           /* Watermark. */
    DAC_Init(DEMO_DAC_BASEADDR, &amp;dacConfigStruct);
    DAC_Enable(DEMO_DAC_BASEADDR, true); /* Enable output. */

    PRINTF("Press any key to trigger the DAC...\r\n");
    /* Enable DAC interrupts. */
    DAC_EnableInterrupts(DEMO_DAC_BASEADDR, kDAC_FIFOEmptyInterruptEnable);

    while (1)
    {
      /* Wait*/
      while (!g_DacInterruptDone)
      {
      }
      g_DacInterruptDone = false;

      /* Trigger the buffer and move the pointer. */
      GETCHAR();
      DAC_DoSoftwareTriggerFIFO(DEMO_DAC_BASEADDR);
      PRINTF("DAC next output: %d\r\n", g_DacValues);
      if (g_DacOutputIndex &gt;= DEMO_DAC_VALUE_ARRAY_SIZE - 1U)
      {
            g_DacOutputIndex = 0U;
      }
      else
      {
            g_DacOutputIndex++;
      }
    }
}

/*!
* @brief IRQ function for DAC buffer interrupt
*/
void DEMO_DAC_IRQ_HANDLER_FUNC(void)
{
    uint32_t flags = DAC_GetStatusFlags(DEMO_DAC_BASEADDR);

    if (0U != (kDAC_FIFOEmptyFlag &amp; flags))
    {
      DAC_SetData(DEMO_DAC_BASEADDR, g_DacValues);
      if (g_DacInputIndex &gt;= (DEMO_DAC_VALUE_ARRAY_SIZE - 1U))
      {
            g_DacInputIndex = 0U;
      }
      else
      {
            g_DacInputIndex++;
      }
    }
    g_DacInterruptDone = true;
    SDK_ISR_EXIT_BARRIER;
}
</code></pre>

<p>4、刷新DAC_DoSoftwareTriggerFIFO(DEMO_DAC_BASEADDR);输入输出堆列。</p>

<p>中断中修改value</p>

<pre>
<code class="language-cpp">/*!
* @brief IRQ function for DAC buffer interrupt
*/
void DEMO_DAC_IRQ_HANDLER_FUNC(void)
{
    uint32_t flags = DAC_GetStatusFlags(DEMO_DAC_BASEADDR);

    if (0U != (kDAC_FIFOEmptyFlag &amp; flags))
    {
      DAC_SetData(DEMO_DAC_BASEADDR, g_DacValues);
      if (g_DacInputIndex &gt;= (DEMO_DAC_VALUE_ARRAY_SIZE - 1U))
      {
            g_DacInputIndex = 0U;
      }
      else
      {
            g_DacInputIndex++;
      }
    }
    g_DacInterruptDone = true;
    SDK_ISR_EXIT_BARRIER;
}
</code></pre>

<p>通过&nbsp;DAC_SetData(DEMO_DAC_BASEADDR, g_DacValues);数组修改DAC的输出值。</p>

<p><strong>4、总结</strong></p>

<p>本次测试通过中断修改DAC的输出数值,当DAC完成输出后即使通过FIFO队列输出数据。NXP的DAC中断设计相当的完善,可以防止DAC输出出现间断的可能。</p>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【FRDM-MCXN947测评】DAC数模转换测试