在RA6M4处理器的ADC里,有两路ADC可以分别采样当前的CPU内部温度和参考电压值。
首先,在FSP配置中选择 Stacks ,点击 "New Stack" -> "Analog" -> "ADC(r_adc)" 添加 ADC 协议栈。
接下来设置 ADC 的相关配置,并重新生成代码。
然后在 "Module g_adc ADC(r_adc)" -> "Input" -> "Channel Scan Mask(channel availability varies by MCU)" 下选择 "Temperature Sensor" 和 "Voltage Sensor"两个通道,并生成代码。
创建ADC操作相关的头文件 src/bsp_adc.h 如下:
#ifndef BSP_ADC_H_
#define BSP_ADC_H_
/* Macro to provide delay in read adc data*/
#define ADC_READ_DELAY 0x01
fsp_err_t adc_init(void);
void adc_deinit(void);
fsp_err_t adc_read_cpu_hwinfo(void);
#endif /* BSP_ADC_H_ */
创建ADC操作相关的C文件 src/bsp_adc.c 如下:
#include
#include "bsp_api.h"
#include "hal_data.h"
#include "bsp_adc.h"
/* Flag to notify that adc scan is started, so start reading adc */
volatile bool scan_complete_flag = false;
fsp_err_t adc_init(void)
{
fsp_err_t err = FSP_SUCCESS;
/* Open/Initialize ADC module */
err = R_ADC_Open (&g_adc0_ctrl, &g_adc0_cfg);
if (FSP_SUCCESS != err)
{
printf("** R_ADC_Open API failed **
");
return err;
}
/* Configures the ADC scan parameters */
err = R_ADC_ScanCfg (&g_adc0_ctrl, &g_adc0_channel_cfg);
if (FSP_SUCCESS != err)
{
printf("** R_ADC_ScanCfg API failed **
");
return err;
}
return FSP_SUCCESS;
}
void adc_deinit(void)
{
fsp_err_t err = FSP_SUCCESS;
/* close the ADC driver */
err = R_ADC_Close (&g_adc0_ctrl);
if (FSP_SUCCESS != err)
{
printf("** R_ADC_Close API failed **
");
}
}
#define ADCTEMP_AS_C(a) ((((float)a) * 0.196551f) - 277.439f)
#define ADCTEMP_AS_F(a) ((((float)a) * 0.353793f) - 467.39f)
#define ADCTEMP_AS_V(a) (float)((((float)a) * 3.3) / 4096.0)
fsp_err_t adc_read_cpu_hwinfo(void)
{
fsp_err_t err = FSP_SUCCESS; // Error status
uint16_t adc_data;
float mcu_temp = 0.0;
float mcu_vol = 0.0;
scan_complete_flag = false;
/* Start the ADC scan*/
err = R_ADC_ScanStart (&g_adc0_ctrl);
if (FSP_SUCCESS != err)
{
/* ADC Failure message */
printf("** R_ADC_ScanStart API failed **
");
return err;
}
while (!scan_complete_flag)
{
/* Wait for callback to set flag. */
}
/* Read the internal temperature sample result */
err = R_ADC_Read (&g_adc0_ctrl, ADC_CHANNEL_TEMPERATURE, &adc_data);
if (FSP_SUCCESS != err)
{
/* ADC Failure message */
printf("** R_ADC_Read API failed **
");
return err;
}
mcu_temp = ADCTEMP_AS_C(adc_data); /* Conversion ADC temperature in celsius */
printf("
MCU Internal Temperature ADC Data: %d Celsius: %.02f'C
", adc_data, mcu_temp);
/* Read the internal reference voltage sample result */
err = R_ADC_Read (&g_adc0_ctrl, ADC_CHANNEL_VOLT, &adc_data);
if (FSP_SUCCESS != err)
{
/* ADC Failure message */
printf("** R_ADC_Read API failed **
");
return err;
}
mcu_vol = ADCTEMP_AS_V(adc_data); /* Conversion ADC to voltage */
printf("MCU Internal Reference Voltage ADC Data: %d Voltage: %.02fV
", adc_data, mcu_vol);
/* 1 Seconds Wait time between successive readings */
R_BSP_SoftwareDelay (ADC_READ_DELAY, BSP_DELAY_UNITS_SECONDS);
return err;
}
/* Callback function */
void adc0_callback(adc_callback_args_t *p_args)
{
FSP_PARAMETER_NOT_USED(p_args);
scan_complete_flag = true;
}
修改 src/hal_entry.c 源文件,在里面添加按键初始化代码并修改蓝色Led灯为系统运行的心跳灯状态。
... ...
#include "bsp_adc.h"
... ...
void hal_entry(void)
{
... ...
adc_init();
while (1)
{
... ...
adc_read_cpu_hwinfo();
}
}
代码修改完成后,在开发板上编译运行,此时调试串口上将会实时打印当前CPU的内核温度和参考电压值。
全部0条评论
快来发表一下你的评论吧 !