Skip to main content
Nmo.1
Associate III
May 10, 2020
Question

ADC STM32f0xx Hi everyone, I want to turn on LEDs whith a Potentiometer an ADC in STM32f091RC nucleo Board. I wrote this code, but it doesn't work. I am a beginner and I do not know where I have mistakes. I would be grateful if someone please help m

  • May 10, 2020
  • 8 replies
  • 3399 views
#include <stdio.h>
#include "diag/Trace.h"
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "pin.h"
#include "adc.h"
 
int main(int argc, char* argv[])
{
 
	 //(#) ADC pins (pA1) configuration --------------------------------------------------------------
 
	GPIO_InitTypeDef GPIO_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
//leds (pB0... pB3) configuration--------------------------------------------------------------
 
 
	 GPIO_InitTypeDef GPIO_LEDs;
	 		 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 
	 		 GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	 		 GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
	 		 GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
	 		 GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
	 		 GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
	 		 GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
	 ADC_InitTypeDef ADC_InitStructure;
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
	 ADC_Init(ADC1, &ADC_InitStructure);
 
	 ADC_GetCalibrationFactor(ADC1);
 ADC_Cmd(ADC1, ENABLE);
 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN) == RESET);
 ADC_StartOfConversion(ADC1);
	 uint16_t valueADC = ADC_GetConversionValue(ADC1);
	 ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_1_5Cycles);
 
	 //dma--------------------------------------------------------------------------------------
	 DMA_InitTypeDef DMA_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
	 DMA_DeInit(DMA1_Channel1);
	 DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
	 DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
	 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	 DMA_InitStructure.DMA_BufferSize = 2;
	 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	 DMA_Init(DMA1_Channel1, &DMA_InitStructure);
 	 DMA_Cmd(DMA1_Channel1, ENABLE);
	 ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
	 ADC_DMACmd(ADC1, ENABLE);
 
 
		 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
 
		 return ADC_GetConversionValue(ADC1); //Return the converted data
		 ADC_StartOfConversion(ADC1);
 
	while (1)
		if(valueADC<2000){
		GPIO_SetBits(GPIOB, GPIO_Pin_0);
		}
		else if(valueADC>2000){
			GPIO_SetBits(GPIOB, GPIO_Pin_1);
			 }
}
 // Infinite loop, never return.
 
 

This topic has been closed for replies.

8 replies

TDK
May 11, 2020
		 return ADC_GetConversionValue(ADC1); //Return the converted data

The main() function will return when it hits this statement. It never gets to the main loop.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Nmo.1
Nmo.1Author
Associate III
May 11, 2020

should I delete this part completely?

bnymntrz
Associate III
May 11, 2020

It would be better if you create a function other than main and read the adc operation and perform the cycle inside that function and call it in the location in the main.

Nmo.1
Nmo.1Author
Associate III
May 11, 2020

yes you are right. but it is easier for me to write everything in the main.

bnymntrz
Associate III
May 11, 2020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 uint32_t valueADC (void){
 
 ADC_RegularChannelConfig(ADC1,ADC_Channel_9,1,ADC_SampleTime_56Cycles);
 ADC_SoftwareStartConv(ADC1);
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
	return ADC_GetConversionValue(ADC1);
 
 }
 
int main(int argc, char* argv[])
{
 
	 //(#) ADC pins (pA1) configuration --------------------------------------------------------------
 
	GPIO_InitTypeDef GPIO_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
//leds (pB0... pB3) configuration--------------------------------------------------------------
 
 
	 GPIO_InitTypeDef GPIO_LEDs;
	 		 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 
	 		 GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	 		 GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
	 		 GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
	 		 GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
	 		 GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
	 		 GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
	 ADC_InitTypeDef ADC_InitStructure;
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
	 ADC_Init(ADC1, &ADC_InitStructure);
 
	 ADC_GetCalibrationFactor(ADC1);
 ADC_Cmd(ADC1, ENABLE);
 
 
	 uint16_t valueADC = ADC_GetConversionValue(ADC1);
	 
 
	 //dma--------------------------------------------------------------------------------------
	 DMA_InitTypeDef DMA_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
	 DMA_DeInit(DMA1_Channel1);
	 DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
	 DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
	 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	 DMA_InitStructure.DMA_BufferSize = 2;
	 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	 DMA_Init(DMA1_Channel1, &DMA_InitStructure);
 	 DMA_Cmd(DMA1_Channel1, ENABLE);
	 ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
	 ADC_DMACmd(ADC1, ENABLE);
 
 
 
	while (1)
 
 uint16_t valueADC = ADC_GetConversionValue(ADC1);
 
		if(valueADC<2000){
		GPIO_SetBits(GPIOB, GPIO_Pin_0);
		}
		else if(valueADC>2000){
			GPIO_SetBits(GPIOB, GPIO_Pin_1);
			 }
}

I also recommend you to try it.

Nmo.1
Nmo.1Author
Associate III
May 11, 2020

I write with STM32f0, so I have to change your code a bit.

but that doesn't work yet.

ADC_ChannelConfig(ADC1,ADC_Channel_9,ADC_SampleTime_55_5Cycles);
 
ADC_StartOfConversion(ADC1);

bnymntrz
Associate III
May 11, 2020

Set the cycle for yourself, but it will work like this.

bnymntrz
Associate III
May 11, 2020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 uint32_t ReadValue;
 
 uint32_t valueADC (void){
 
 ADC_RegularChannelConfig(ADC1,ADC_Channel_9,1,ADC_SampleTime_56Cycles);
 ADC_SoftwareStartConv(ADC1);
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
	return ADC_GetConversionValue(ADC1);
 
 }
 
int main(int argc, char* argv[])
{
 
	 //(#) ADC pins (pA1) configuration --------------------------------------------------------------
 
	GPIO_InitTypeDef GPIO_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
//leds (pB0... pB3) configuration--------------------------------------------------------------
 
 
	 GPIO_InitTypeDef GPIO_LEDs;
	 		 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 
	 		 GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	 		 GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
	 		 GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
	 		 GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
	 		 GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
	 		 GPIO_Init(GPIOB, &GPIO_LEDs);
//ADC-------------------------------------------------------------------------------------------
	 ADC_InitTypeDef ADC_InitStructure;
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	 ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
	 ADC_Init(ADC1, &ADC_InitStructure);
 
	 ADC_GetCalibrationFactor(ADC1);
 ADC_Cmd(ADC1, ENABLE);
 
 
 
 
	 //dma--------------------------------------------------------------------------------------
	 DMA_InitTypeDef DMA_InitStructure;
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
	 DMA_DeInit(DMA1_Channel1);
	 DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
	 DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
	 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	 DMA_InitStructure.DMA_BufferSize = 2;
	 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	 DMA_Init(DMA1_Channel1, &DMA_InitStructure);
 	 DMA_Cmd(DMA1_Channel1, ENABLE);
	 ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
	 ADC_DMACmd(ADC1, ENABLE);
 
 
 
	while (1)
 
 ReadValue = valueADC();
 
		if(ReadValue<2000){
		GPIO_SetBits(GPIOB, GPIO_Pin_0);
		}
		else if(ReadValue>2000){
			GPIO_SetBits(GPIOB, GPIO_Pin_1);
			 }
}

Would you try again this way? Please

Nmo.1
Nmo.1Author
Associate III
May 11, 2020

in STM32f0xx we have not code "ADC_RegularChannelConfig..." we have ADC_ChannelConfig(ADC1,ADC_Channel_9,ADC_SampleTime_55_5Cycles);

and we have not "ADC_SoftwareStartConv(ADC1);.."

bnymntrz
Associate III
May 11, 2020

But they certainly have a counterpart in the ADC library of f0xx.

That's all I can think of.:smiling_face_with_smiling_eyes:

bnymntrz
Associate III
May 11, 2020

I made a correction in the code let's try deleting the unit16_t part in while

Uint 16 in loop should be Uint 32 or Uint should not be written.

bnymntrz
Associate III
May 11, 2020

ADC_RegularChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_55_5Cycles);

should be

Nmo.1
Nmo.1Author
Associate III
May 11, 2020

Unfortunately, no. Does not work yet