之前写完了点灯,按键,串口,RTC等等,现在来写写ADC操作。

根据数据手册,可知,RA4M2 & SENSOR内部有一个温度传感器,叫做TSN,是一个特殊的ADC通道。论坛里的人都不知道这个东西,除了我和RA的版主,所以这篇帖子就是来给这些人扫盲的。


TSN的计算算法入下:
39.3.1
Preparation for Using the Temperature Sensor
The ambient temperature (T) is proportional to the temperature sensor voltage output (Vs), so ambient temperature is
calculated with the following formula:
T = (Vs - V1) / slope + T1
● T: Ambient temperature of MCU as calculation result (°C)
● Vs: Voltage output by the temperature sensor on temperature measurement (V)
● T1: Temperature experimentally measured at one point (°C)
● V1: Voltage output by the temperature sensor on measurement of T1 (V)
● T2: Temperature experimentally measured at a second point (°C)
● V2: Voltage output by the temperature sensor on measurement of T2 (V)
● Slope: Temperature gradient of the temperature sensor (V / °C), slope = (V2 - V1) / (T2 -T1)
Characteristics vary between sensors, so Renesas recommends measuring two different sample temperatures as follows:
-
Use the 12-bit A/D converter to measure the voltage V1 output by the temperature sensor at temperature T1.
-
Again use the 12-bit A/D converter to measure the voltage V2 output by the temperature sensor at a different
temperature T2.
-
Obtain the temperature gradient (slope = (V2 - V1) / (T2 - T1)) from these results.
-
Subsequently, obtain temperatures by substituting the slope into the formula for the temperature characteristic (T = (Vs -
V1) / slope + T1).
If you are using the temperature gradient given in section 47, Electrical Characteristics, use the A/D converter to measure
the voltage V1 output by the temperature sensor at temperature T1, then calculate the temperature characteristic using the
following formula:
T = (Vs - V1) / slope + T1
Note:
This method produces less accurate temperatures than measurement at two points.
In this MCU, the TSCDR register stores the temperature value (CAL127) of the temperature sensor measured under the
condition Ta = Tj = 127°C and AVCC0 = VREFH0 = 3.3 V. If you use this value as the sample measurement result at the
first point, you can omit the preparation before using the temperature sensor.
V1 is calculated from CAL127:
V1 = 3.3 × CAL127 / 4096 [V] (In case of 12 bit accuracy)
Using this value, the measured temperature can be calculated according to the following formula:
T = (Vs - V1) / slope + 127 [°C]
● T: Ambient temperature of MCU as calculation result (°C)
● Vs: Voltage output by the temperature sensor when the temperature is measured (V)
● V1: Voltage output by the temperature sensor when Ta = Tj = 127°C and AVCC0 = VREFH0 = 3.3 V (V)

只要认真读懂了这套算法,就能准备计算出TSN的温度值!!!
你们能不能实现就看你们的理解能力了,当然还有英语能力!!!!
反正我是读懂了。
1。打开瑞萨的RA smart配置软件
配置TSN的ADC




2。生成代码,打开KEIL工程
添加核心代码,计算TSN的温度
#include "hal_data.h"
#include "LED.h"
#include "Systick.h"
#include "usart0.h"
#include "usart9.h"
#include "shell.h"
//#include "ebtn_app.h"
//#include "coremark.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
void ADC_TSN(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
assert(FSP_SUCCESS == err);
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
assert(FSP_SUCCESS == err);
}
volatile bool scan_complete_flag = false;
void adc_callback (adc_callback_args_t * p_args)
{
//宏将告知编译器回调函数不使用参数 p_args,从而避免编译器发出警告,
FSP_PARAMETER_NOT_USED(p_args);
scan_complete_flag = true;
}
double a0,a5;
uint16_t adc_data1=0;
uint16_t adc_data2=0;
/*******************************************************************************************************************//**
-
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)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t time = 0;
uint8_t voltage_1 = 0; //电压整数
uint8_t voltage_2 = 0; //电压小数
static adc_info_t adc_info ; //adc information
static double die_temperature = 0 ;
static double V1 = 0;
static double Vs = 0;
uint8_t *dataPtr = NULL;
/* TODO: add your own code here */
hal_systick_init();
UART0_Init();UART9_Init();
//ebtn_APP_Key_INIT();
//shell_init();
ADC_TSN();
/**** Set the time variable of the calendar ****/
Calendar_TimeVariable_Set(2025,8,31,19,33,00);
while(1)
{
//shell_process();
//printf("现在时间�? %d�?d�?d�?d�?d�?d秒\r\n",ptime.year,ptime.month,ptime.day,ptime.hour,ptime.min,ptime.second);
//printf("现在时间: %d年%d月%02d日%02d时%02分%02d秒\r\n",datetime.year,datetime.month,datetime.day,datetime.hour,datetime.min,datetime.second);
/* Enable scan triggering from ELC events. */
(void) R_ADC_ScanStart(&g_adc0_ctrl);
scan_complete_flag = false;
while (!scan_complete_flag)
{
}
err = R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_TEMPERATURE, &adc_data1);
assert(FSP_SUCCESS == err);
//Get ADC info
err = R_ADC_InfoGet(&g_adc0_ctrl, &adc_info);
assert(FSP_SUCCESS == err);
V1 = ( 3300 * (int) adc_info.calibration_data ) / 4096 ;
Vs = ( 3300 * (int) adc_data1 ) / 4096 ;
die_temperature = CONSTANT * ( Vs - V1 ) / ( adc_info.slope_microvolts ) + 127.0;
static char string_die_temp[10] = {0};
sprintf( string_die_temp, "%.2f", die_temperature );
printf("RA4M2 Sensor开发板芯片当前温度是: %s 度\r\n", string_die_temp);
HAL_Delay(700);
}
#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
R_FACI_LP->DFLCTL = 1U;
#endif
}
if (BSP_WARM_START_POST_C == event)
{
R_IOPORT_Open(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
#if BSP_CFG_SDRAM_ENABLED
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

编译工程,烧录代码到板子

打开串口助手

RA4M2 Sensor开发板芯片当前温度是: 36.97 度
可以看到板子温度为36。97度
非常的准确。
附件是我编译的TSN温度hex文件,你们可以自己下载烧录测试,只需使用USB线的默认串口即可
*附件:RA4M2_TSN.zip