[MCU] 【FM33LG0系列开发板测评】03.ADC、DAC

xld0932   2021-12-4 21:25 楼主

1、简介

FM33LG0带有最高采样率达到2Msps的12bit SAR-ADC,支持单端输入和差分输入,可以自由选择VREFP或者VDDA作为REF+参考电压,方便灵活;带有一个最高输出率为1Msps,12位的电压输出型数模转换器,需要注意的是DAC使用VREF+作为参考基准,所以在开发板上需要将J40的VP与V用跳帽进行短接!

 

2、实现功能

使用板载的ADC和DAC扩展接口进行功能验证,ADC使用单通道采集的方式,依次对CH15、CH16、CH17进行采样,计算并打印出电压值;DAC输出通道使用软件写入数据的方式实现正弦波形的输出;最后通过接线的方式,将CH15接VCC、CH16接DAC输出、CH17接GND,来测试在相邻通道的电压值快速变化的情况下,是否会对相邻通道的采样值产生影响。

 

3、代码实现

/*******************************************************************************
 * @file    ADC.c
 * @author  King
 * [url=home.php?mod=space&uid=252314]@version[/url] V1.00
 * [url=home.php?mod=space&uid=311857]@date[/url] 27-Nov-2021
 * [url=home.php?mod=space&uid=159083]@brief[/url] ......
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#define __ADC_C__


/* Includes ------------------------------------------------------------------*/
#include "ADC.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void ADC_Configure(void)
{
    FL_GPIO_InitTypeDef      GPIO_InitStruct;
    FL_ADC_CommonInitTypeDef ADC_CommonInitStruct;
    FL_ADC_InitTypeDef       ADC_InitStruct ;

    FL_GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.pin        = FL_GPIO_PIN_10 | FL_GPIO_PIN_11 | FL_GPIO_PIN_12;
    GPIO_InitStruct.mode       = FL_GPIO_MODE_ANALOG;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    FL_ADC_CommonStructInit(&ADC_CommonInitStruct);
    ADC_CommonInitStruct.operationSource   = FL_ADC_CLK_SOURCE_ADCCLK;
    ADC_CommonInitStruct.clockSource       = FL_CMU_ADC_CLK_SOURCE_RCHF;
    ADC_CommonInitStruct.clockPrescaler    = FL_CMU_ADC_PSC_DIV8;
    ADC_CommonInitStruct.APBClockPrescaler = FL_ADC_APBCLK_PSC_DIV1;
    ADC_CommonInitStruct.referenceSource   = FL_ADC_REF_SOURCE_VDDA;
    ADC_CommonInitStruct.bitWidth          = FL_ADC_BIT_WIDTH_12B;
    FL_ADC_CommonInit(&ADC_CommonInitStruct);

    FL_ADC_StructInit(&ADC_InitStruct);
    ADC_InitStruct.conversionMode        = FL_ADC_CONV_MODE_SINGLE;
    ADC_InitStruct.autoMode              = FL_ADC_SINGLE_CONV_MODE_AUTO;
    ADC_InitStruct.waitMode              = FL_ENABLE;
    ADC_InitStruct.overrunMode           = FL_ENABLE;
    ADC_InitStruct.scanDirection         = FL_ADC_SEQ_SCAN_DIR_FORWARD;
    ADC_InitStruct.externalTrigConv      = FL_ADC_TRIGGER_EDGE_NONE;
    ADC_InitStruct.triggerSource         = FL_ADC_TRGI_LUT0;
    ADC_InitStruct.fastChannelTime       = FL_ADC_FAST_CH_SAMPLING_TIME_2_ADCCLK;
    ADC_InitStruct.lowChannelTime        = FL_ADC_SLOW_CH_SAMPLING_TIME_192_ADCCLK;
    ADC_InitStruct.oversamplingMode      = FL_ENABLE;
    ADC_InitStruct.overSampingMultiplier = FL_ADC_OVERSAMPLING_MUL_8X;
    ADC_InitStruct.oversamplingShift     = FL_ADC_OVERSAMPLING_SHIFT_3B;
    FL_ADC_Init(ADC, &ADC_InitStruct);

    TASK_Append(TASK_ID_ADC, ADC_Handler, 500);
}


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * @attention   
*******************************************************************************/
uint32_t ADC_GetSingleChannelSample(uint32_t Channel)
{
    uint16_t Data = 0;

    FL_CMU_SetADCPrescaler(FL_CMU_ADC_PSC_DIV1);
    FL_ADC_EnableSequencerChannel(ADC, Channel);

    FL_ADC_ClearFlag_EndOfConversion(ADC);
    FL_ADC_Enable(ADC);

    FL_ADC_EnableSWConversion(ADC);
    while(FL_ADC_IsActiveFlag_EndOfConversion(ADC) == FL_RESET);

    FL_ADC_ClearFlag_EndOfConversion(ADC);
    Data = FL_ADC_ReadConversionData(ADC);

    FL_ADC_Disable(ADC);
    FL_ADC_DisableSequencerChannel(ADC, Channel);

    return Data;
}


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * @attention   
*******************************************************************************/
void ADC_Handler(void)
{
    uint32_t CH15_Data = 0, CH16_Data = 0, CH17_Data = 0;

    CH15_Data = ADC_GetSingleChannelSample(FL_ADC_EXTERNAL_CH15);
    CH16_Data = ADC_GetSingleChannelSample(FL_ADC_EXTERNAL_CH16);
    CH17_Data = ADC_GetSingleChannelSample(FL_ADC_EXTERNAL_CH17);

    printf("\r\nCH15 : %0.1fV, CH16 : %0.1fV, CH17 : %0.1fV",
            (float)CH15_Data / 4095 * 3.3,
            (float)CH16_Data / 4095 * 3.3,
            (float)CH17_Data / 4095 * 3.3);
}


/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/

 

/*******************************************************************************
 * @file    DAC.c
 * @author  King
 * @version V1.00
 * @date    27-Nov-2021
 * @brief   ......
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#define __DAC_C__


/* Includes ------------------------------------------------------------------*/
#include "DAC.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * @attention   
*******************************************************************************/
void DAC_Configure(void)
{
    FL_GPIO_InitTypeDef GPIO_InitStruct;
    FL_DAC_InitTypeDef  DAC_InitStruct;

    FL_GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.pin        = FL_GPIO_PIN_5;
    GPIO_InitStruct.mode       = FL_GPIO_MODE_ANALOG;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    FL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    FL_DAC_StructInit(&DAC_InitStruct);
    DAC_InitStruct.sampleHoldMode = FL_DISABLE;
    DAC_InitStruct.sampleTime     = 0xFF;
    DAC_InitStruct.holdTime       = 0x00;
    DAC_InitStruct.triggerMode    = FL_DISABLE;
    DAC_InitStruct.triggerSource  = FL_DAC_TRGI_SOFTWARE;
    DAC_InitStruct.bufferMode     = FL_DISABLE;
    DAC_InitStruct.switchMode     = FL_ENABLE;
    FL_DAC_Init(DAC, &DAC_InitStruct);

    FL_DAC_Enable(DAC);
    FL_DAC_WriteData(DAC, 2047);

    TASK_Append(TASK_ID_DAC, DAC_Handler, 1);
}


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * @attention   
*******************************************************************************/
void DAC_Handler(void)
{
    uint16_t WaveData[1284] =
    {
        2047,2147,2248,2347,2446,2545,2641,2737,2831,2922,
        3012,3100,3185,3267,3346,3422,3495,3564,3630,3692,
        3749,3803,3853,3898,3939,3975,4006,4033,4055,4072,
        4085,4092,4095,4092,4085,4072,4055,4033,4006,3975,
        3939,3898,3853,3803,3749,3692,3630,3564,3495,3422,
        3346,3267,3185,3100,3012,2922,2831,2737,2641,2545,
        2446,2347,2248,2147,2047,1947,1846,1747,1648,1549,
        1453,1357,1263,1172,1082, 994, 909, 827, 748, 672,
         599, 530, 464, 402, 345, 291, 241, 196, 155, 119,
          88,  61,  39,  22,   9,   2,   0,   2,   9,  22,
          39,  61,  88, 119, 155, 196, 241, 291, 345, 402,
         464, 530, 599, 672, 748, 827, 909, 994,1082,1172,
        1263,1357,1453,1549,1648,1747,1846,1947
    };

    static uint8_t WaveIndex = 0;

    FL_DAC_WriteData(DAC, WaveData[WaveIndex]);

    WaveIndex = (WaveIndex + 1) % 128;
}


/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/

 

4、测试数据

DAC初始化完成后,设定输出VREF+一半的电压值:

1.jpg

软件方式实现正弦波输出:

2.jpg

各个通道采样稳定,没有受到相邻通道的影响:

3.png

5、工程源码

Project_ADC_DAC.zip (356.96 KB)
(下载次数: 22, 2021-12-4 21:23 上传)

 

We are a team and we work as a team !

回复评论 (3)

软件方式实现输出的正弦波还挺正确

想问一下将J40的VP与V用跳帽进行短接时,这个电压多少V

点赞  2021-12-5 20:40
引用: Jacktang 发表于 2021-12-5 20:40 软件方式实现输出的正弦波还挺正确 想问一下将J40的VP与V用跳帽进行短接时,这个电压多少V

理论上是3.3V,我这个板子用万用表实测是3.271V

We are a team and we work as a team !
点赞  2021-12-5 21:01

1Msps的12位DAC速度可以了。

虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2021-12-6 17:46
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复