流行科技 发表于 2022-8-13 21:18

【N32L43x评测】DAC各种波形输出测试

<p ><b>一般DAC输出很多都是用在模拟量信号控制地方,可以控制运放输出电压。我们本次主要就是简单玩下其输出噪声,正弦波,三角波这些。</b></p>

<pre>
<code>#include "dac.h"





void TIM4_TrgoInit(void);



void dac_channel_one_init()

{

      

       GPIO_InitType GPIO_InitStructure;

       DAC_InitType DAC_InitStructure;





       /* DAC Periph clock enable */

    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);

      

    GPIO_InitStruct(&amp;GPIO_InitStructure);

    /* GPIOA Periph clock enable */

    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);

    /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically

       connected to the DAC converter. In order to avoid parasitic consumption,

       the GPIO pin should be configured in analog */

    GPIO_InitStructure.Pin       = GPIO_PIN_4;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;

    GPIO_InitStructure.GPIO_Pull = GPIO_No_Pull;

    GPIO_InitPeripheral(GPIOA, &amp;GPIO_InitStructure);

      

    /* DAC Channel Configuration */

    DAC_InitStructure.Trigger      = DAC_TRG_NONE;

    DAC_InitStructure.WaveGen      = DAC_WAVEGEN_NONE;

//    DAC_InitStructure.LfsrUnMaskTriAmp = DAC_TRIAMP_2047;

    DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_DISABLE;

    DAC_Init(&amp;DAC_InitStructure);



 

       DAC_Enable(ENABLE);

      

//    DAC_SetChData(DAC_ALIGN_R_12BIT,0x100);



//    TIM4_TrgoInit();

//    /* TIM4 enable counter */

//    TIM_Enable(TIM4, ENABLE);

}



/**

 * @brief  TIM4 Init.

 */

void TIM4_TrgoInit(void)

{

    TIM_TimeBaseInitType TIM_TimeBaseStructure;



    /* TIM4 Periph clock enable */

    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE); // RCC_APB2_PERIPH_TIM8

    /* TIM4 Configuration */

    /* Time base configuration */

    TIM_InitTimBaseStruct(&amp;TIM_TimeBaseStructure);

    TIM_TimeBaseStructure.Period    = 0x08;

    TIM_TimeBaseStructure.Prescaler = 0x03;

    TIM_TimeBaseStructure.ClkDiv    = 0x0;

    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM4, &amp;TIM_TimeBaseStructure);



    /* TIM4 TRGO selection */

    TIM_SelectOutputTrig(TIM4, TIM_TRGO_SRC_UPDATE);

}</code></pre>

<p ><b>这么一段初始化代码,都是我们后面各种测试能用上的,先看下上面的是正弦波配置,我看给的例程是DMA+定时器4触发产生的,这里我们用他直接输出电压加正弦函数表来实现,这个有缺点,就是while中只能有这个循环,多一个任务都不行。</b></p>

<pre>
<code>uint16_t SinData={2048, 2098, 2149, 2199, 2250, 2300, 2350, 2399, 2449, 2498, 2548, 2596, 2645, 2693, 2741, 2788, 2835, 2881, 2927, 2972, 3017, 3061, 3105, 3148, 3190, 3231, 3272, 3312, 3351, 3390, 3428, 3465, 3501, 3536, 3570, 3603, 3636, 3667, 3697, 3727, 3755, 3783, 3809, 3834, 3858, 3881, 3903, 3924, 3944, 3962, 3980, 3996, 4011, 4025, 4037, 4049, 4059, 4068, 4075, 4082, 4087, 4091, 4094, 4095, 4095, 4095, 4093, 4089, 4085, 4079, 4072, 4063, 4054, 4043, 4031, 4018, 4003, 3988, 3971, 3953, 3934, 3914, 3892, 3870, 3846, 3822, 3796, 3769, 3741, 3712, 3682, 3651, 3620, 3587, 3553, 3518, 3483, 3446, 3409, 3371, 3332, 3292, 3252, 3211, 3169, 3126, 3083, 3039, 2995, 2950, 2904, 2858, 2811, 2764, 2717, 2669, 2621, 2572, 2523, 2474, 2424,2375, 2325, 2275, 2224, 2174, 2124, 2073, 2023, 1972, 1922, 1872, 1821, 1771, 1721, 1672, 1622, 1573, 1524, 1475, 1427, 1379, 1332, 1285, 1238, 1192, 1146, 1101, 1057, 1013, 970, 927, 885, 844, 804, 764, 725, 687, 650, 613, 578, 543, 509, 476, 445, 414, 384, 355, 327, 300, 274, 250, 226, 204, 182, 162, 143, 125, 108, 93, 78, 65, 53, 42, 33, 24, 17, 11, 7, 3, 1, 0, 0, 2, 5, 9, 14, 21, 28, 37, 47, 59, 71, 85, 100, 116, 134, 152, 172, 193, 215, 238, 262, 287, 313, 341, 369, 399, 429, 460, 493, 526, 560, 595, 631, 668, 706, 745, 784, 824, 865, 906, 948, 991, 1035, 1079, 1124, 1169, 1215, 1261, 1308, 1355, 1403, 1451, 1500, 1548, 1598, 1647, 1697, 1746, 1796, 1846, 1897, 1947, 1998, 2048};

while(1)

{

              for(int x=0;x&lt;sizeof(SinData)/sizeof(SinData);x++){

                     DAC_SetChData(DAC_ALIGN_R_12BIT,SinData);

              }

}</code></pre>

<p ><b>就是如上代码,测试效果如下:</b></p>

<p > &nbsp;</p>

<p > &nbsp;</p>

<p ><b>三角波就是把红框里的都使能,然后有系统自己产生即可,使用定时器4。</b></p>

<p ><b>修改如下,测试效果如下:</b></p>

<pre>
<code>    DAC_InitStructure.Trigger      = DAC_TRG_T4_TRGO;

    DAC_InitStructure.WaveGen      = DAC_WAVEGEN_TRIANGLE;

       DAC_InitStructure.LfsrUnMaskTriAmp = DAC_TRIAMP_2047;</code></pre>

<p > &nbsp;</p>

<p >同样噪声输出也是一样,修改如下,效果如下:</p>

<pre>
<code>    DAC_InitStructure.Trigger      = DAC_TRG_T4_TRGO;

    DAC_InitStructure.WaveGen      = DAC_WAVEGEN_NOISE;

       DAC_InitStructure.LfsrUnMaskTriAmp = DAC_TRIAMP_2047;</code></pre>

<p > &nbsp;</p>

<p >其中我也测试了其稳定性,目前还是觉得波动不是特别大的,后期使用还需要考虑内部温升看有没有影响了。</p>

<div id="qb-sougou-search" style="display: none; opacity: 0;">
<p>搜索</p>

<p class="last-btn">复制</p>
<iframe src=""></iframe></div>

rgjinxuan 发表于 2022-8-15 19:30

<p>感谢分享</p>
页: [1]
查看完整版本: 【N32L43x评测】DAC各种波形输出测试