cancel
Showing results for 
Search instead for 
Did you mean: 

ADC3 in Nuceo f767

mohsen
Associate II
Posted on November 02, 2016 at 21:44

I wrote the following program, it works with ADC1 and ADC2. But it read zero in ADC3.

Also, when I use all ADC at same program it remain on ADC3 reading line!

In my project I need to use all ADC at same program.

I use Atollic TrueStudio

======================

&sharpinclude ''stm32f7xx_hal.h''

static GPIO_InitTypeDef GPIO_InitStruct;

uint32_t g_ADCValue;

int g_MeasurementNumber;

uint16_t size=1;

uint16_t temp;

uint8_t rdt;

ADC_HandleTypeDef hadc3;

UART_HandleTypeDef huart3;

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_ADC3_Init(void);

int main(void)

{

HAL_Init();

SystemClock_Config();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOD_CLK_ENABLE();

MX_ADC3_Init();

// Init UART 3

huart3.Instance = USART3;

huart3.Init.BaudRate = 115200;

huart3.Init.WordLength = UART_WORDLENGTH_8B;

huart3.Init.StopBits = UART_STOPBITS_1;

huart3.Init.Parity = UART_PARITY_NONE;

huart3.Init.Mode = UART_MODE_TX_RX;

huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart3.Init.OverSampling = UART_OVERSAMPLING_16;

huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart3) != HAL_OK)

{

Error_Handler();

}

// Configure IO in output push-pull mode to drive external LEDs

GPIO_InitStruct.Pin = GPIO_PIN_0;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1);

GPIO_InitStruct.Pin = GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, 1);

HAL_ADC_Start(&hadc3);

while (1)

{

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);

HAL_Delay(100); // delay 1000 ms

if (HAL_ADC_PollForConversion(&hadc3, 1000000) == HAL_OK)

{

g_ADCValue = HAL_ADC_GetValue(&hadc3);

g_MeasurementNumber++;

rdt= (uint8_t) g_ADCValue;

HAL_UART_Transmit(&huart3, &rdt, size, 0xFFFF);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, 1);

}

else

{

//HAL_UART_Transmit(&huart3, &rdt, size, 0xFFFF);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, 0);

}

}

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 16;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLM = 16;

RCC_OscInitStruct.PLL.PLLN = 192;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 2;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

if (HAL_PWREx_EnableOverDrive() != HAL_OK)

{

Error_Handler();

}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)

{

Error_Handler();

}

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;

PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

{

Error_Handler();

}

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* ADC3 init function */

static void MX_ADC3_Init(void)

{

ADC_ChannelConfTypeDef sConfig;

/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

*/

hadc3.Instance = ADC3;

hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

hadc3.Init.Resolution = ADC_RESOLUTION_8B;

hadc3.Init.ScanConvMode = DISABLE;

hadc3.Init.ContinuousConvMode = ENABLE;

hadc3.Init.DiscontinuousConvMode = DISABLE;

hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc3.Init.NbrOfConversion = 1;

hadc3.Init.DMAContinuousRequests = DISABLE;

hadc3.Init.EOCSelection = DISABLE; //ADC_EOC_SINGLE_CONV;

if (HAL_ADC_Init(&hadc3) != HAL_OK)

{

Error_Handler();

}

/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

*/

sConfig.Channel = ADC_CHANNEL_9;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)

{

Error_Handler();

}

}

void Error_Handler(void)

{

while(1)

{

}

}

&sharpifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

}

&sharpendif

#stm32f767zi-nucleo
4 REPLIES 4
Posted on November 02, 2016 at 21:51

You enable clock?

  __HAL_RCC_ADC3_CLK_ENABLE();

ADC3 Example

  STM32Cube_FW_F7_V1.3.0\Projects\STM32746G-Discovery\Examples\ADC\ADC_RegularConversion_DMA

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mohsen
Associate II
Posted on November 02, 2016 at 22:06

STM32CubeMX add that line in the other file. however, I add it to min file, did not change any thing.

I use same code for ADC1 and ADC2 and works.

only ADC3 does not work

Posted on November 02, 2016 at 23:53

Try porting the cited example.

I have no visibility to code you don't post. Could you be specific about the pin you are using?

Is the pin/channel association correct? Is the pin tied to ground, does it conflict with other hardware on the board?

Have you tried other pins/channels?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on November 03, 2016 at 12:31

Hi Mohsen, 

If the application is already programmed in Dual or Triple mode , ii will be a problem when trying to work with only ADC3 (independent mode) because it was configured as a slave to ADC1.

Maks sure that ''MULTI'' bit fileds in ADC_CCR register is at it reset value 0 ( ALL ADC independent case) . You can reconfigure it using  HAL_ADCEx_MultiModeConfigChannel()

For CubeMX using, in this case  you should reprogram the tool to deselect ADC1 and ADC2 and only select ADC3. 

In case you will work with the trip ADCs , you select them all and you select triple mode as the MULTI mode.

-Hannibal-