eew_cT3H5d 发表于 2024-8-5 23:12

【瑞萨RA8D1评测】第七篇:AD采样电压串口打印到电脑上

<div class='showpostmsg'> 本帖最后由 eew_cT3H5d 于 2024-8-8 08:13 编辑

<p>本部分采样滑动变阻器模块的电压,将电压数值打印到电脑上,显示当前采样的电压</p>

<p> &nbsp;&nbsp;&nbsp;</p>

<p>配置ADC使能</p>

<p> &nbsp;</p>

<p>添加配置ADC模块</p>

<p> &nbsp;</p>

<p>配置相关adc参数</p>

<p> &nbsp;</p>

<p>配置相关通道</p>

<p> &nbsp;</p>

<p>添加函数</p>

<p> &nbsp;</p>

<p>adc.h文件</p>

<pre>
<code>#ifndef _ADC_H
#define _ADC_H

#include "hal_data.h"

void ADC_Init(void);
double Read_ADC_Voltage_Value(void);

#endif
</code></pre>

<p>adc.c文件</p>

<pre>
<code>#include &lt;ADC/adc.h&gt;

//ADC转换完成标志位
volatile bool scan_complete_flag = false;

void adc_callback(adc_callback_args_t * p_args)
{
    FSP_PARAMETER_NOT_USED(p_args);
    scan_complete_flag = true;
}

void ADC_Init(void)
{
    fsp_err_t err;
    err = R_ADC_Open(&amp;g_adc0_ctrl, &amp;g_adc0_cfg);
    err = R_ADC_ScanCfg(&amp;g_adc0_ctrl, &amp;g_adc0_channel_cfg);
    assert(FSP_SUCCESS == err);
}

/* 进行ADC采集,读取ADC数据并转换结果 */
double Read_ADC_Voltage_Value(void)
{
    uint16_t adc_data;
    double a0;

    (void)R_ADC_ScanStart(&amp;g_adc0_ctrl);
    while (!scan_complete_flag) //等待转换完成标志
    {
      ;
    }
    scan_complete_flag = false; //重新清除标志位

    /* 读取通道0数据 */
    R_ADC_Read(&amp;g_adc0_ctrl, ADC_CHANNEL_0, &amp;adc_data);
    /* ADC原始数据转换为电压值(ADC参考电压为3.3V) */
    a0 = (double)(adc_data*3.3/4095);

    return a0;
}
</code></pre>

<p>hal_entry.c函数</p>

<pre>
<code>/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include &quot;hal_data.h&quot;

#include &quot;debug_uart3.h&quot;

#include &quot;ADC/adc.h&quot;
fsp_err_t err;

void R_BSP_WarmStart(bsp_warm_start_event_t event);

//extern bsp_leds_t g_bsp_leds;

/*******************************************************************************************************************//**
* <a href="home.php?mod=space&amp;uid=159083" target="_blank">@brief </a>Blinky example application
*
* Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
*
**********************************************************************************************************************/
void hal_entry (void)
{

    Debug_UART3_Init();
    ADC_Init();
    while (1)
    {
      printf(&quot;HELLO eeworld !\r\n&quot;);
      printf(&quot;a0 = %f\r\n&quot;, Read_ADC_Voltage_Value());
      R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
    }
}

/*******************************************************************************************************************//**
* This function is called at various points during the startup process.This implementation uses the event that is
* called right before main() to set up the pins.
*
* @paramevent    Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart (bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

      /* Enable reading from data flash. */
      R_FACI_LP-&gt;DFLCTL = 1U;

      /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
      /* C runtime environment and system clocks are setup. */

      /* Configure pins. */
      R_IOPORT_Open(&amp;IOPORT_CFG_CTRL, &amp;IOPORT_CFG_NAME);
    }
}
</code></pre>

<p>编译下载程序</p>

<p> &nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页: [1]
查看完整版本: 【瑞萨RA8D1评测】第七篇:AD采样电压串口打印到电脑上