배터리, 온도 SAADC 해상도 12bit 전환 및 float 연산 적용
- SAADC 해상도 10bit -> 12bit - int 연산 -> float 연산 -> BLE 전송 시 int
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user