rasc 配置
1.配置adc引脚
2.配置stacks adc
2.
软件
bsp_adc0.h
#ifndef __RA4_ADC0_H__
#define __RA4_ADC0_H__
#include "hal_data.h"
#include "stdio.h"
void adc0_Init(void);
void read_adc(void);
#endif
bsp_adc0.c
#include "bsp_adc0.h"
void adc0_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
if(FSP_SUCCESS != err)
{
printf("ADC0 Open API failed\\r\\n");
}
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
assert(FSP_SUCCESS == err);
}
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;
}
void read_adc(void)
{
fsp_err_t err = FSP_SUCCESS;
double a0,a1,a2;
uint16_t adc_data=0;
(void) R_ADC_ScanStart(&g_adc0_ctrl);
scan_complete_flag = false;
while (!scan_complete_flag)
{
}
err = R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_0, &adc_data);
assert(FSP_SUCCESS == err);
a0=(double)(adc_data/4095.0)*3.3;
err = R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_1, &adc_data);
assert(FSP_SUCCESS == err);
a1=(double)(adc_data/4095.0)*3.3;
err = R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_2, &adc_data);
assert(FSP_SUCCESS == err);
a2=(double)(adc_data/4095.0)*3.3;
printf("ch0 %f ch2 %f ch3 %fv: %.02f \\r\\n",a0,a1,a2);
}
测试结果
|