image.png

read_vref_raw_avg(n) 평균 샘플링 추가

  1. 목적
static uint32_t read_vref_raw_avg(uint8_t n) {
  uint32_t sum = 0;
  if (n == 0) return 0;

  for (uint8_t i = 0; i < n; i++) {
    HAL_ADC_Start(&hadc1);
    HAL_ADC_PollForConversion(&hadc1, 10);
    sum += HAL_ADC_GetValue(&hadc1);
    HAL_ADC_Stop(&hadc1);
  }

  return (sum + (n/2)) / n; // 반올림
}
  1. n==0이면 0 리턴(방어 코드)
  2. 루프 n번 돌면서
  3. 최종 평균 계산

⇒ raw의 순간 튐이 줄음 ⇒ vdda도 안정화

통계 기능 : stats_reset, stats_update, stats 명령

  1. 목적
static uint32_t st_min_raw = 0xFFFFFFFFu;
static uint32_t st_max_raw = 0;
static uint64_t st_sum_raw = 0;
static uint32_t st_cnt_raw = 0;

static uint32_t st_min_vdda = 0xFFFFFFFFu;
static uint32_t st_max_vdda = 0;
static uint64_t st_sum_vdda = 0;
static uint32_t st_cnt_vdda = 0;

static void stats_reset(void) {
    st_min_raw = 0xFFFFFFFFu; st_max_raw = 0; st_sum_raw = 0; st_cnt_raw = 0;
    st_min_vdda = 0xFFFFFFFFu; st_max_vdda = 0; st_sum_vdda = 0; st_cnt_vdda = 0;
}

통계 초기화