배터리, 온도 SAADC 해상도 12bit 전환 및 float 연산 적용

- SAADC 해상도 10bit -> 12bit
- int 연산 -> float 연산 -> BLE 전송 시 int
This commit is contained in:
jhChun
2026-03-27 16:12:31 +09:00
parent 7459fffc20
commit 153c4a027f
2 changed files with 22 additions and 20 deletions

View File

@@ -40,14 +40,14 @@
#include "main.h"
#include "debug_print.h"
/* SAADC 내부 기준전압 600mV */
#define BATTERY_REF_VOLTAGE_IN_MILLIVOLTS 600 /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
/* SAADC 내부 기준전압 600mV (부동소수점) */
#define BATTERY_REF_VOLTAGE_IN_MILLIVOLTS 600.0f /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
/* 1/3 프리스케일링 보상 계수 (입력 전압을 1/3로 분압하므로 x3, 추가 x2 = 총 x6) */
#define BATTERY_PRE_SCALING_COMPENSATION 6 /**< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
/* 1/3 프리스케일링 보상 계수 (입력 전압을 1/3로 분압하므로 x3, 추가 x2 = 총 x6) (부동소수점) */
#define BATTERY_PRE_SCALING_COMPENSATION 6.0f /**< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
/* 10비트 ADC 최대 디지털 값 */
#define BATTERY_ADC_RES_10BITS 1023 /**< Maximum digital value for 10-bit ADC conversion. */
/* 12비트 ADC 최대 디지털 값 (부동소수점) */
#define BATTERY_ADC_RES_12BITS 4095.0f /**< Maximum digital value for 12-bit ADC conversion. */
/**@brief Macro to convert the result of ADC conversion in millivolts.
*
@@ -56,7 +56,7 @@
* @retval Result converted to millivolts.
*/
#define BATTERY_RESULT_IN_MILLI_VOLTS(ADC_VALUE)\
((((ADC_VALUE) * BATTERY_REF_VOLTAGE_IN_MILLIVOLTS) / BATTERY_ADC_RES_10BITS) * BATTERY_PRE_SCALING_COMPENSATION)
((((ADC_VALUE) * BATTERY_REF_VOLTAGE_IN_MILLIVOLTS) / BATTERY_ADC_RES_12BITS) * BATTERY_PRE_SCALING_COMPENSATION)
/* 배터리 측정용 더블 버퍼 (SAADC가 비동기로 교대 사용) */
static nrf_saadc_value_t adc_bufs[2];
@@ -127,8 +127,8 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
nrf_saadc_value_t register_val = 0;
uint16_t batt_lvl_in_milli_volt_0 = 0; /* 보정 전 전압 */
uint16_t batt_lvl_in_milli_volt_1 = 0; /* 분압 보정 후 최종 전압 */
float batt_lvl_in_milli_volt_0 = 0; /* 보정 전 전압 (부동소수점) */
float batt_lvl_in_milli_volt_1 = 0; /* 분압 보정 후 최종 전압 (부동소수점) */
uint32_t err_code = 0;
/* ADC 변환 결과 읽기 */
@@ -149,7 +149,7 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
batt_lvl_in_milli_volt_0 = BATTERY_RESULT_IN_MILLI_VOLTS(register_val);
/* 분압 저항 보정 계수 1.42 적용 → 실제 배터리 전압 */
batt_lvl_in_milli_volt_1 = (batt_lvl_in_milli_volt_0) *1.42;
batt_lvl_in_milli_volt_1 = (batt_lvl_in_milli_volt_0) *1.42f;
/* === 저전압 체크 모드 (battery_loop 타이머에서 설정) === */
if(low_battery_check == true)
@@ -172,7 +172,7 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
{
/* 아직 10회 미만 — 카운터 증가 후 경고 출력 */
low_battery_cnt++;
DBG_PRINTF("WARNING!!! low_battery cnt = %d, Batt = %d(mV)\r\n", low_battery_cnt, batt_lvl_in_milli_volt_1);
DBG_PRINTF("WARNING!!! low_battery cnt = %d, Batt = %d(mV)\r\n", low_battery_cnt, (int)batt_lvl_in_milli_volt_1);
}
}
}
@@ -188,7 +188,7 @@ void battery_event_handler( nrf_drv_saadc_evt_t const * p_event )
{
if (cmd_type_t == CMD_UART)
{
DBG_PRINTF("Tn%d\r\n\r\n", batt_lvl_in_milli_volt_1);
DBG_PRINTF("Tn%d\r\n\r\n", (int)batt_lvl_in_milli_volt_1);
}
else if (cmd_type_t == CMD_BLE)
{
@@ -213,6 +213,7 @@ static void battery_configure(void)
{
/* SAADC 드라이버 초기화 (16x 오버샘플링으로 노이즈 저감) */
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
saadc_config.oversample = NRF_SAADC_OVERSAMPLE_4X;
ret_code_t err_code = nrf_drv_saadc_init(&saadc_config, battery_event_handler);
APP_ERROR_CHECK(err_code);

View File

@@ -43,8 +43,8 @@
#define TMP235_REF_VOLTAGE_IN_MILLIVOLTS 600.0f /**< Reference voltage (in milli volts) used by ADC while doing conversion. */
/* 1/3 프리스케일링 보상 계수 x6 (부동소수점) */
#define TMP235_PRE_SCALING_COMPENSATION 6.0f /**< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.*/
/* 10비트 ADC 최대값 1024 (부동소수점, 분해능 기준) */
#define TMP235_ADC_RES_10BITS 1024.0f /**< Maximum digital value for 10-bit ADC conversion. */
/* 12비트 ADC 최대값 4096 (부동소수점, 분해능 기준) */
#define TMP235_ADC_RES_12BITS 4096.0f /**< Maximum digital value for 12-bit ADC conversion. */
/**@brief Macro to convert the result of ADC conversion in millivolts.
*
@@ -52,9 +52,9 @@
*
* @retval Result converted to millivolts.
*/
/* ADC 원시값 → TMP235 출력전압(mV) 변환 매크로: ADC x (600/1024) x 6 */
/* ADC 원시값 → TMP235 출력전압(mV) 변환 매크로: ADC x (600/4096) x 6 */
#define TMP235_VOUT_IN_MILLI_VOLTS(ADC_VALUE)\
((((ADC_VALUE) * TMP235_REF_VOLTAGE_IN_MILLIVOLTS) / TMP235_ADC_RES_10BITS) * TMP235_PRE_SCALING_COMPENSATION)
((((ADC_VALUE) * TMP235_REF_VOLTAGE_IN_MILLIVOLTS) / TMP235_ADC_RES_12BITS) * TMP235_PRE_SCALING_COMPENSATION)
/* SAADC 변환 결과 저장 버퍼 (1채널) */
static nrf_saadc_value_t adc_buf;
@@ -94,7 +94,7 @@ volatile bool tmp235_saadc_done = false;
void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event) /* TMP325 Vout reading */
{
float led_temp; /* 계산된 온도 (°C, 부동소수점) */
uint16_t led_temp_16; /* BLE 전송용 온도 (°C x 100, 정수) */
float led_temp_16; /* BLE 전송용 온도 (°C x 100, 부동소수점) */
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
@@ -143,7 +143,7 @@ void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event) /* TMP325 Vout
/* info4 모드: 온도값을 정수(°C x 100)로 저장 (예: 36.50°C → 3650) */
if (info4 == true)
{
info_temp =(uint16_t)(led_temp*100);
info_temp = (uint16_t)(led_temp * 100);
}
/* UART 모드: 소수점 2자리까지 텍스트로 출력 */
else if(cmd_type_t == CMD_UART)
@@ -153,8 +153,8 @@ void tmp235_voltage_handler(nrf_drv_saadc_evt_t const * p_event) /* TMP325 Vout
/* BLE 모드: °C x 100 정수를 "rso:" 헤더로 바이너리 전송 */
else if(cmd_type_t == CMD_BLE)
{
led_temp_16 = (uint16_t)(led_temp*100);
single_format_data(ble_bin_buffer, "rso:", led_temp_16);
led_temp_16 = led_temp * 100;
single_format_data(ble_bin_buffer, "rso:", (uint16_t)led_temp_16);
dr_binary_tx_safe(ble_bin_buffer,3);
@@ -178,6 +178,7 @@ void tmp235_init(void)
{
/* SAADC 드라이버 초기화 (4x 오버샘플링으로 노이즈 저감) */
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;
saadc_config.oversample = NRF_SAADC_OVERSAMPLE_4X;
ret_code_t err_code = nrf_drv_saadc_init(&saadc_config, tmp235_voltage_handler);
APP_ERROR_CHECK(err_code);