cancel
Showing results for 
Search instead for 
Did you mean: 

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 me.

Nmo.1
Associate III
#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.
 
 

16 REPLIES 16
bnymntrz
Associate III

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

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

0693W000000XBgnQAG.png0693W000000XBgFQAW.png

bnymntrz
Associate III

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.

Thank you. I have now only this error. Because i use STM320XX.h i change the code.

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

that doesn't work yet :(

Nmo.1
Associate III

0693W000000XBqTQAW.png

bnymntrz
Associate III

ADC_RegularChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_55_5Cycles);

should be

Unfortunately, no. Does not work yet