AnsweredAssumed Answered

ADC to turn on LED HELP!

Question asked by stone.jorge on May 11, 2015
Latest reply on May 18, 2015 by Clive One
Hello, I want to read data from ADC so that a LED turns on when I connect 5V to the input pin of the ADC and turns OFF when I connect 0V to it.

The problem is that the LED turns OFF when I connect 5V instead of 0V. Help please

This is the code:
#include "stm32f4_discovery.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
 
 
int ConvertedValue = 0;
 
/**************************************************************************************/
 
void RCC_Configuration(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
}
 
/**************************************************************************************/
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* ADC Channel 11 -> PC1 */
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}
 
 
void inicialitzaLED(void)
{
    GPIO_InitTypeDef initValues;
 
    initValues.GPIO_Pin = GPIO_Pin_12;
    initValues.GPIO_Mode = GPIO_Mode_OUT;
    initValues.GPIO_OType = GPIO_OType_PP;
    initValues.GPIO_PuPd = GPIO_PuPd_NOPULL;
    initValues.GPIO_Speed = GPIO_Speed_2MHz;
 
    GPIO_Init(GPIOD, &initValues);
}
 
/**************************************************************************************/
 
void ADC_Configuration(void)
{
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  ADC_InitTypeDef ADC_InitStructure;
 
  // ADC structure configuration
  ADC_DeInit(); // Reset all parameters to their default values
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // Input voltage is converted into a 12-bit number whose maximum value is 4095
  ADC_InitStructure.ADC_ScanConvMode = ENABLE; // No scan (only one channel)
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // the conversion is continuous (periodic)
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // no external trigger for conversion
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; // TRGO o CC1 (channel 1 capture/compare)
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // converted data will be shifted to the right
  ADC_InitStructure.ADC_NbrOfConversion = 1; // Number of used ADC channels
  ADC_Init(ADC1, &ADC_InitStructure);
  // ADC common structure configuration
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; // independent mode
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; // f(ADC3)=84/4=48MHz
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; // disable DMA_MODE
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // there are 5 clock cycles between 2 samplings
  ADC_CommonInit(&ADC_CommonInitStructure);
 
  /* ADC1 regular channel 11 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_144Cycles); // PC1 es el sample time (cicles que estara llegint dades?)
 
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
}
 
int adc_convert(){
 ADC_SoftwareStartConv(ADC1);//Start the conversion
 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
 return ADC_GetConversionValue(ADC1); //Return the converted data
}
 
/**************************************************************************************/
 
int main(void)
{
 
  RCC_Configuration();
 
  GPIO_Configuration();
 
  inicialitzaLED();
 
  ADC_Configuration();
 
  while(1) // Don't want to exit
  {
  ConvertedValue = adc_convert();
  if(ConvertedValue<3000)
  {
  GPIOD->BSRRL = GPIO_Pin_12;
  }
  else if(ConvertedValue>3000)
  {
  GPIOD->BSRRH = GPIO_Pin_12;
  }
  }
}

Outcomes