cancel
Showing results for 
Search instead for 
Did you mean: 

ADC Basic (no timer, dma, etx) ADC 1 & 2 work but not 3

RobG
Associate III

I have setup HAL, systems clocks, etc.  ADC 1 & 2 work PC4 (channel 14).  ADC3 with PF3 (channel 9) PF4 (channel 14) or PF5 (channel 15) don't work with the same code.  Port C & F enabled, ADC1-3 clocks enabled.

Thanks for your help!

 

{
GPIO_TypeDef *ThePort;
int ThePin, TheChannel;
ADC_TypeDef *TheADC;
ADC_HandleTypeDef hadc;

__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();

//PF3 Thermisor ADC3_CHANNEL_9
//PF4 Thermisor ADC3_CHANNEL_14
//PF5 Thermisor ADC3_CHANNEL_15
//PC4 Power ADC12_CHANNEL_14

/* works
ThePort = GPIOC;
ThePin = GPIO_PIN_4;
TheChannel  = ADC_CHANNEL_14;
TheADC = ADC1;  // or ADC2;
*/

ThePort = GPIOF;
ThePin = GPIO_PIN_4;
TheChannel  = ADC_CHANNEL_14;
TheADC = ADC3;

// Configure PF3 as Analog Input
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = ThePin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ThePort, &GPIO_InitStruct);


// Enable ADC3 Clock
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_ADC2_CLK_ENABLE();
__HAL_RCC_ADC3_CLK_ENABLE();

hadc.Instance = TheADC;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc.Init.Resolution = ADC_RESOLUTION_12B; // 0-4095
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE; // Single conversion
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONV_T1_CC1;//ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.EOCSelection = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DMAContinuousRequests = DISABLE;
HAL_ADC_Init(&hadc);

// Configure Channel
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = TheChannel; // PF3 is ADC3_IN9
sConfig.Offset = 0; // added
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc, &sConfig);

while (1)
{
usleep(100000);    // sleep(0.10 seconds)

HAL_ADC_Start(&hadc);
int adc_val;
// Wait for conversion to complete
if (HAL_ADC_PollForConversion(&hadc, 100) == HAL_OK)
{      // Read the value
adc_val = HAL_ADC_GetValue(&hadc);
}
// Stop ADC
HAL_ADC_Stop(&hadc);

printf((char *) "ADC:  %d\n", adc_val);
} // while(1)
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
RobG
Associate III

I believe the code posted works (slight changes as testing went on testing).  TDK had the right idea.  Is ADC3 broken? No it wasn't.  The circuit has been used in multiple designs.  However, the board was manufactured with some part my EE didn't recognize and didn't act like an op-amp.  Once that was replaced all is good.

 

Thanks again TDK.

Rob

View solution in original post

5 REPLIES 5
TDK
Super User

What isn't working about it? What does it do and what do you expect instead?

> ADC_SAMPLETIME_3CYCLES

For a thermistor, increase this to max.

If you feel a post has answered your question, please click "Accept as Solution".
RobG
Associate III

I get very low reading < 10; usually 0 to 2.  Increase to 480 cycles with the same results.

TDK
Super User

Well, 0 to 2 is a valid ADC reading. The code you show is fine.

Either the voltage on the pin isn't what you think it is, or ADC3 is damaged. It's too bad there isn't an internal channel you could try like on ADC1, but you could easily connect a pin to VDD and ensure it gets 4095.

If you feel a post has answered your question, please click "Accept as Solution".
RobG
Associate III

Yep, 0-2 are valid, but I think I should be closer to 1250.  I couldn't see why the code didn't work - that is why I am here.

Working on another way to validate the electronics - aren't new board fun so many places for issues.  .I am getting parts to try PA3 which can be used with all 3 ADC.  The parts will also allow me to replace the thermistor with a potentiometer for a little more control on expected readings.

Thanks for your advice on increasing the sampling time.  Could you explain a little more why that is recommended.

Thanks for your help TDK

RobG
Associate III

I believe the code posted works (slight changes as testing went on testing).  TDK had the right idea.  Is ADC3 broken? No it wasn't.  The circuit has been used in multiple designs.  However, the board was manufactured with some part my EE didn't recognize and didn't act like an op-amp.  Once that was replaced all is good.

 

Thanks again TDK.

Rob