cancel
Showing results for 
Search instead for 
Did you mean: 

Why cant blink leds according to potentiometer adc read values?

YagciOnur
Associate II

Hi.Im trying to read potentiometers on the pins pa6 and pa7,according to the values blink the leds with if else conditions.Im simulating the project in proteus but i cannot get any output from the leds...I have  used internal oscilator with hclk 84mhz pll method.

My proteus simulation settings and simulation is here.My code is here as programmed in HAL LIBRARY

 

#include "main.h"

uint32_t adc_buffer[2];

uint32_t adc_value[2];

ADC_HandleTypeDef hadc1;

DMA_HandleTypeDef hdma_adc1;

 

 

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_DMA_Init(void);

static void MX_ADC1_Init(void);

 

int main(void)

{

 

HAL_Init();

 

 

SystemClock_Config();

 

MX_GPIO_Init();

MX_DMA_Init();

MX_ADC1_Init();

 

HAL_ADC_Start_DMA(&hadc1, adc_buffer, 2);

 

 

while (1)

{

 

}

 

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {

adc_value[0]=adc_buffer[0];

adc_value[1]=adc_buffer[1];

if(adc_value[0]>1000) {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);

}

else {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);

}

if(adc_value[1]>3000) {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);

}

else {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);

}

}

 

 

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 

 

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

 

 

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 84;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 4;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

 

/** Initializes the CPU, AHB and APB buses clocks

*/

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_2) != HAL_OK)

{

Error_Handler();

}

}

 

 

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.ScanConvMode = ENABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.NbrOfConversion = 2;

hadc1.Init.DMAContinuousRequests = ENABLE;

hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

if (HAL_ADC_Init(&hadc1) != HAL_OK)

{

Error_Handler();

}

 

 

 

sConfig.Channel = ADC_CHANNEL_6;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_7;

sConfig.Rank = 2;

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

{

Error_Handler();

}

/* USER CODE BEGIN ADC1_Init 2 */

 

/* USER CODE END ADC1_Init 2 */

 

}

 

/**

* Enable DMA controller clock

*/

static void MX_DMA_Init(void)

{

 

 

__HAL_RCC_DMA2_CLK_ENABLE();

 

 

HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);

 

}

 

 

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */

 

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

 

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);

 

/*Configure GPIO pins : PB0 PB1 */

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

 

}

 

 

void Error_Handler(void)

{

 

__disable_irq();

while (1)

{

}

 

}

 

#ifdef USE_FULL_ASSERT

 

void assert_failed(uint8_t *file, uint32_t line)

{

 

}

#endif /* USE_FULL_ASSERT */

 

Why the problem outcomes you think?

is there any error on proteus or code?

 

 

4 REPLIES 4
AScha.3
Chief II

Hi,

blink the leds with if else conditions

Where are these ? I see only this :

while (1)

{

}

= nothing.

So for your question:

Why cant blink leds according to potentiometer adc read values?

Because you do something wrong.

 

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

sorry for that,i forgot to put the conditions in while true but when i put them nothing changes.This is the main function.I think some of the functions place is wrong?

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_DMA_Init();

MX_ADC1_Init();

HAL_ADC_Start_DMA(&hadc1, adc_buffer, 2);

while (1)

{

 

adc_value[0]=adc_buffer[0];

adc_value[1]=adc_buffer[1];

if(adc_value[0]>1000) {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);

}

else {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);

}

if(adc_value[1]>3000) {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);

}

else {

HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);

}

 

}

 

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {

}

 

 

 

I don't see any code to blink them.

The ADC reads 16-bit values, not 32-bit

Print out the values you are actually using to threshold to see what it's actually comparing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I dont understand:

in callback, where you get data - you do nothing :

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {

}

-- so how >>  if(adc_value[0]>1000) { << should ever get a value ?

 

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