cancel
Showing results for 
Search instead for 
Did you mean: 

Basic Simple ADC use on STM32G0 to measure 0-3.3 thermistors ...

JD3
Associate II

STM32G0B1CBT6

 

#define ADC0_OBJ_COUNT 9

const uint8_t ch_remap[ADC0_OBJ_COUNT] = {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, ADC_CHANNEL_4, ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7, ADC_CHANNEL_10};

//const uint8_t ch_remap[ADC0_OBJ_COUNT] = {0 , 1, 2, 3, 4, 5, 6, 7, 10};

uint32_t adc1_value[ADC0_OBJ_COUNT];

 

 

static void MX_ADC1_Init(void) {

ADC_ChannelConfTypeDef sConfig = {0};

 

hadc1.Instance = ADC1;

hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

hadc1.Init.Resolution = ADC_RESOLUTION_12B;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;

hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;

hadc1.Init.LowPowerAutoWait = DISABLE;

hadc1.Init.LowPowerAutoPowerOff = DISABLE;

hadc1.Init.ContinuousConvMode = DISABLE;

hadc1.Init.NbrOfConversion = 1; // 1 konverze najednou

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.DMAContinuousRequests = DISABLE;

hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;

hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5;

hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;

hadc1.Init.OversamplingMode = DISABLE;

hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_LOW;

 

if (HAL_ADC_Init(&hadc1) != HAL_OK) {

Error_Handler();

}

 

// Inicializujte první kanál jako výchozí

sConfig.Channel = ADC_CHANNEL_0;

sConfig.Rank = ADC_REGULAR_RANK_1;

sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {

Error_Handler();

}

}

 

void adc1_update(void) {

ADC_ChannelConfTypeDef sConfig = {0};

 

for (uint8_t i = 0; i < ADC0_OBJ_COUNT; i++) {

/* Configure Regular Channel: add channel to sequence */

sConfig.Channel = ch_remap[i];

sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

//sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);

 

// Spustíme konverzi

HAL_ADC_Start(&hadc1);

HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

adc1_value[i] = HAL_ADC_GetValue(&hadc1);

adc_obj[i].curr = kalman_filter(&kalman_filters[i], adc1_value[i]);

 

/* Configure Regular Channel: remove channel from sequence */

sConfig.Channel = ch_remap[i];

sConfig.Rank = ADC_RANK_NONE;

HAL_ADC_ConfigChannel(&hadc1, &sConfig);

}

}

 

 

Where is mistake ? It measures only CH0 and copies to all buffer spaces 0-8. I would like to used DMA which works fine, but DMA can do 8 ch RANKS only  i need 16 channels in future slow measuring of resitors

2 REPLIES 2
Karl Yamashita
Principal

Use the </> icon when posting code so it's properly formatted and readable.

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.
CAN Jammer an open source CAN bus hacking tool
waclawek.jan
Super User

Cube/HAL is open source, so you can debug it as your own code. Read out and check/post ADC registers content before you start the individual conversions. 

JW