电子说
2.
#ifndef __RA4_ADC0_H__
#define __RA4_ADC0_H__
#include "hal_data.h"
#include "stdio.h"
/*adc始化 */
void adc0_Init(void);
void read_adc(void);
#endif
#include "bsp_adc0.h"
/*
初始化 */
void adc0_Init(void)
{
fsp_err_t err = FSP_SUCCESS;
/* 初始化adc */
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
/* Handle any errors. This function should be defined by the user. */
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);
}
/*
adc中断回调 */
volatile bool scan_complete_flag = false;
void adc0_callback(adc_callback_args_t * p_args)
{
//宏将告知编译器回调函数不使用参数 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)
{
/* Wait for callback to set flag. */
}
/*
读取adc的值
*/
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);
}
审核编辑 黄昊宇
全部0条评论
快来发表一下你的评论吧 !