Files

67 lines
1.5 KiB
C
Raw Permalink Normal View History

2025-12-08 16:43:26 +01:00
/*
* adc.c
*
* Created on: Dec 8, 2025
* Author: user
*/
#include "adc.h"
/* Filled by ADC+DMA: latest raw value of each channel */
volatile uint16_t adc_dma_buf[ADC_NUM_CHANNELS];
/* Smoothed value (50ms average with settings above) */
volatile uint16_t adc_avg[ADC_NUM_CHANNELS];
/* Internal accumulators used only in the timer ISR */
static uint32_t adc_sum[ADC_NUM_CHANNELS];
static uint8_t adc_sum_count = 0;
/* Optional flag to tell main loop that a new averaged set is ready */
volatile uint8_t adc_avg_ready = 0;
uint16_t ch4 ;
uint16_t ch5 ;
uint16_t ch6 ;
uint16_t ch7 ;
uint16_t ch8 ;
void manageAdc(void){
if (adc_avg_ready)
{
adc_avg_ready = 0;
// Read averaged channels (example mapping)
ch4 = adc_avg[0];
ch5 = adc_avg[1];
ch6 = adc_avg[2];
ch7 = adc_avg[3];
ch8 = adc_avg[4];
// Use them (convert to volts, control, etc.)
// float v4 = (3.3f * ch4) / 4095.0f;
}
}
void readAdc(void){
for (uint8_t i = 0; i < ADC_NUM_CHANNELS; i++)
{
adc_sum[i] += adc_dma_buf[i];
}
adc_sum_count++;
/* When we collected ADC_AVG_WINDOW samples → compute average */
if (adc_sum_count >= ADC_AVG_WINDOW)
{
for (uint8_t i = 0; i < ADC_NUM_CHANNELS; i++)
{
adc_avg[i] = (uint16_t)(adc_sum[i] / ADC_AVG_WINDOW);
adc_sum[i] = 0;
}
adc_sum_count = 0;
adc_avg_ready = 1; // tell main loop new data is ready
}
}