cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f0 Adc Issue

d4ng3r09
Associate II
Posted on June 02, 2014 at 19:41

Hi every body,

I am using stm32f0 to control a water control systems by getting a value from a temperature sensor which is read by the ADC. I am trying to activate my system 15 secondes each 30 secondes, 4 times ,and after that wait 30 secondes. Since i wanted to test this system, my problem is that when i wire to the ground the pin which is supposed to be attached to the sensor's output while the system is working , it do the cycle again as if the adc is reading a value highly above zero. what i mean is that when the processor is executing the second loop and that i wire PA1 to ground, it executes the loop again. Can anybody help me please? Thank you for your attention . Here is my code:

#include ''stm32f0xx.h''
#include<stm32f0xx_gpio.h>
#include<stm32f0xx_rcc.h>
#include<stm32f0xx_pwr.h>
#include<stm32f0xx_adc.h>
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStruct ;
void
AdcConfiguration(){
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
/* Initialize the ADC_ContinuousConvMode member */
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
/* Initialize the ADC_ExternalTrigConvEdge member */
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
/* Initialize the ADC_ExternalTrigConv member */
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
/* Initialize the ADC_DataAlign member */
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
/* Initialize the ADC_ScanDirection member */
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_Init(ADC1,&ADC_InitStruct);
ADC_ChannelConfig( ADC1,ADC_Channel_1, ADC_SampleTime_239_5Cycles);
ADC_Cmd(ADC1, ENABLE);
ADC_StartOfConversion(ADC1);
}
void
InitPinX(uint32_t GPIO_Pin_X,GPIO_TypeDef* GPIOx)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_X ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOx, &GPIO_InitStructure);
}
void
delay()
{
long
j=0;
while
(j<50000000)
// delay 15 secondes
{
j++;
}
}
void
main(
void
)
{
int
arr=0;
uint16_t value=0;
float
temp=0;
AdcConfiguration();
InitPinX(GPIO_Pin_8,GPIOA);
GPIO_ResetBits( GPIOA,GPIO_Pin_8);
while
(1)
{ADC_StartOfConversion(ADC1);
value=ADC_GetConversionValue(ADC1);
temp=100*(value*3.3/4096+0.015);
if
(temp>20)
{ 
while
(arr !=4){
GPIO_SetBits( GPIOA,GPIO_Pin_8);
delay();
arr=arr+1;
GPIO_ResetBits( GPIOA,GPIO_Pin_8);
delay();
}
arr=0;
ADC_StopOfConversion(ADC1);
delay();
 delay();
value=0;
}
}
#ifdef USE_FULL_ASSERT
void
assert_failed(uint8_t* file, uint32_t line)
{
while
(1)
{
}
}
void
PWR_BackupAccessCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if
(NewState != DISABLE)
{
/* Enable the Backup Domain Access */
PWR->CR |= PWR_CR_DBP;
}
else
{
/* Disable the Backup Domain Access */
PWR->CR &= (uint32_t)~((uint32_t)PWR_CR_DBP);
}
}

2 REPLIES 2
Posted on June 03, 2014 at 16:17

Not sure the delay loop is effective.

When you start a conversion, you should perhaps wait for it to complete before reading the value.

The formula computing the value seems to have a lot of mixed types, careful use of casting and constants would allow the math to be simplified and done with floats.

Suggest you print out the actual measurement from the ADC, and the computed value, and provide a more clear table of data observations, and program behaviour.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
d4ng3r09
Associate II
Posted on June 04, 2014 at 18:17

Actually i fixed that  by taking 20 values given by the adc and calculating the average one .

Thanks anyway.