测试开发板ADC功能。通过电位器外接3.3V电源电压,调整ADC的电压输入,通过串口打印输出ADC采集的电压值。
一、硬件部分
1.1、ADC测试引脚
测试使用P004引脚作为ADC的输入引脚,电位器分压后,输入到P004。

1.2、硬件连接

二、配置ADC
2.1、配置ADC引脚
配置P004为ADC输入引脚

2.2、配置ADC参数

三、程序部分
3.1、fun_adc.c
#include "hal_data.h"
volatile bool scan_complete_flag = false;
void adc0_callback(adc_callback_args_t * p_args)
{
FSP_PARAMETER_NOT_USED(p_args);
scan_complete_flag = true;
}
double Read_ADC_Voltage_Value(void)
{
uint16_t adc_data;
double a0;
(void)R_ADC_ScanStart(&g_adc0_ctrl);
while (!scan_complete_flag)
{
;
}
scan_complete_flag = false;
R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_4, &adc_data);
a0 = (double)(adc_data*3.3/4095);
return a0;
}
void init_adc(void)
{
fsp_err_t err;
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
assert(FSP_SUCCESS == err);
}
3.2、fun_adc.h
#ifndef FUN_ADC_H_
#define FUN_ADC_H_
void init_adc(void);
double Read_ADC_Voltage_Value(void);
#endif
3.3、hal_entry.c
#include "hal_data.h"
#include "led/fun_led.h"
#include "uart/fun_uart.h"
#include "adc/fun_adc.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
double adv=0.0;
/* TODO: add your own code here */
init_uart();
init_led();
init_adc();
while(1)
{
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
led1_on();
led2_off();
led3_on();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
led1_off();
led2_on();
led3_off();
adv = Read_ADC_Voltage_Value();
printf("adc = %0.1f \r\n",adv);
//printf("RA6M4 Board UART9 TEST!\r\n");
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
* 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.
*
* @param[in] event 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->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(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
#if BSP_CFG_SDRAM_ENABLED
/* Setup SDRAM and initialize it. Must configure pins first. */
R_BSP_SdramInit(true);
#endif
}
}
#if BSP_TZ_SECURE_BUILD
FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{
}
FSP_CPP_FOOTER
#endif
四、运行结果
下载程序后,调整电位器,串口输出采样的电压值

|