cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery Analogue Input Counting

stevostephenson
Associate II
Posted on March 19, 2015 at 23:31

Hi Im new to programming and therefore new to the ST Board.

I am trying to hold open an output while  a sensor counts a passing objects. Once the objects have passed 10 times I want to close the output. 

My problem comes when I'm trying to distinguish the input impulses from the sensor. When the sensor is giving an input the program is seeing that it is above the 2000 value and is counting 10 instantly and then closing the output.

How do I get it to only recognise the rising edge of the input and not count again until there is a consecutive rising edge from the next passing input?

Your help will be much appreciated.

int main(void)

{

int sensor_count = 0;

int ConvertedValue = 0; //Converted value read from ADC

PA0_button_setup();

GPIO_ResetBits(GPIOB, GPIO_Pin_1 );

adc_configure(); //Start configuration

PB1_Pin_Setup();

while (1)

{

if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))

{

sensor_count = 0;

GPIO_SetBits(GPIOB, GPIO_Pin_1);

while (! (sensor_count >= 10))

{

ConvertedValue = adc_convert();//Read the ADC converted value

if (ConvertedValue > 2000)

{

sensor_count++;

}

if (sensor_count == 10)

{

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

}

}

}

else

{

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

}

}

}

#stm32f4 #discovery
6 REPLIES 6
Posted on March 19, 2015 at 23:44

Then may be instead of comparing absolutes, you compare a difference, and then only increment the count when you see a +1500 (or whatever) transition.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stevostephenson
Associate II
Posted on March 20, 2015 at 14:30

Could you elaborate further for me please?

What I'm thinking is:

if ( 2000 < ADC && ADC < 2002

Cheers

Posted on March 20, 2015 at 15:42

int A, B;
A = adc_convert(); // last
while(1)
{
B = adc_convert(); // current
if ((B - A) > 1500) count++; // the difference between the two is a positive rise of 1500
A = B; // last = current
}

You understand what a difference is?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stevostephenson
Associate II
Posted on March 21, 2015 at 17:15

Hi Clive I understand what your suggesting. If I understand correctly you would have to hold the first ADC value to be able to compare it as the cycle passes over. 

I have been attempting other methods and have found a way of doing it. Now the count holds once the count has incremented until the ADC goes lower and then it is able to increment once the ADC goes high again. 

Its worth noting when reading that with no volts on the ADC it read 4095 and when it had 5v on it was around 3066 figure so the difference will be around 1029.

It currently looks a bit messy so I will attempt at tidying it up.

Thank you for your help.

The code is.

int main(void)

{

int sensor_count = 0;

int cycle;

int A, B, C, D;

PA0_button_setup();

LED_Setup();

GPIO_ResetBits(GPIOB, GPIO_Pin_1 );

adc_configure(); //Start configuration

PB1_Pin_Setup();

ConvertedValue = adc_convert(); //Converted value read from ADC

while (1)

{

if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) && cycle == 0)

{

cycle = 1;

GPIO_SetBits(GPIOD, GPIO_Pin_15 |GPIO_Pin_14);

GPIO_SetBits(GPIOB, GPIO_Pin_1);

if (sensor_count >=3)

{

GPIO_ResetBits(GPIOD, GPIO_Pin_15 |GPIO_Pin_14);

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

}

while (!(sensor_count >= 3))

{

ConvertedValue = adc_convert();

A = ConvertedValue;

B = 4095;

C = (B - A);

cycle = 1;

switch (cycle)

{

case 1:

if (C > 850 && ! D == 0)

{

cycle = 2;

D = 0;

}

case 2:

if ((cycle == 2))

{

cycle = 3;

sensor_count++;

D = 0;

}

else

{

cycle = 1;

D = 0;

}

case 3:

if ( C < 850)

{

D = 1;

}

break;

default:

{

GPIO_ResetBits(GPIOD, GPIO_Pin_15 |GPIO_Pin_14);

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

cycle = 0;

sensor_count = 0;

}

}

}

GPIO_ResetBits(GPIOD, GPIO_Pin_15 |GPIO_Pin_14);

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

cycle = 0;

sensor_count = 0;

}

else

{

GPIO_ResetBits(GPIOD, GPIO_Pin_15 |GPIO_Pin_14);

GPIO_ResetBits(GPIOB, GPIO_Pin_1);

cycle = 0;

sensor_count = 0;

}

}

}

Posted on March 21, 2015 at 17:32

The analogue input is NOT rated for 5V, on an STM32F4-DISCO the board has an +VREF of 3.0V

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stevostephenson
Associate II
Posted on March 28, 2015 at 14:50

Hi Clive,

Looking on the stm32f407 data sheet, it says that I/O's are 5v tolerant. It does not distinguish between analogue or otherwise being different.