2015-05-11 03:46 AM
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;
}
}
}
#stm32f4-adc
2015-05-11 04:12 AM
1. STM32 MCUs are not 5V chips, you can burn your chip. The MCU can convert voltages up to the VDDA voltage (typically 3.3V), so don't connect 5V to your MCU's pins.
2. You don't need ADC for this simple task, use a digital input 3. Given your ADC init code is correct and you want to use ADC for some reason and not digital input, then you should remeber that you don't get the millivolts in the converted value, you should convert it to millivolts before comparing it to millivolts.2015-05-18 04:47 AM
Thank you for reply. Sorry, I wanted to say 3V not 5V. And yes, I'm trying a simple example with ADC to do something more complex later. You are right, I have to do a conversion to mV. I've changed the main to:
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
LED_Configuration();
ADC_Configuration();
while(1) // Don't want to exit
{
ConvertedValue = adc_convert();
valueinmV=ConvertedValue*3300/4095;
if(valueinmV<
3100
&& valueinmV>2900)
{
GPIOD->BSRRH = GPIO_Pin_12;
}
else if(valueinmV>3100 && valueinmV<
2900
)
{
GPIOD->BSRRL = GPIO_Pin_12;
}
}
}
It should turn on the LED when I connect 3V to the pin but it doesn't. What else am I doing wrong?
2015-05-18 06:03 AM
valueinmV=ConvertedValue*3300/4095;
That would scale for 3.3V for 3.0V, like on the DISCO use
valueinmV=ConvertedValue*3000/4095;
Use a debugger and explain the value being seen at various set points.