cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure an GPIO Input port: NoPull,PullUp,PullDown ??

mrupprath
Associate II
Posted on February 09, 2013 at 17:05

Hello;

i have problems to understand how to configure an GPIO input port correctly.

What i wan't:

Port PA5 shall be configured als input port.

A simple switch is conected between the +5V source and the input pin.

The desired behaviour:

Input PA5 is ''high'' when switch is closed

Input PA5 is ''low'' when switched is opened

i tried to configure the port as follows:

.....

GPIOA_InitSTructure.

GPIO_Pin

= GPIO_Pin_5;

GPIOA_InitSTructure.

GPIO_Mode

=

GPIO_Mode_IN

;

GPIOA_InitSTructure.

GPIO_OType

=

GPIO_OType_PP

;

GPIOA_InitSTructure.

GPIO_PuPd

=

GPIO_PuPd_DOWN

;

/*GPIOA_InitSTructure.GPIO_PuPd = GPIO_PuPd_NOPULL*/

;

GPIOA_InitSTructure.

GPIO_PuPd

=

GPIO_PuPd_UP

;

GPIOA_InitSTructure.

GPIO_Speed

=

GPIO_Speed_50MHz

;

GPIO_Init(GPIOA, &GPIOA_InitSTructure);

The problem:

The Port is always high, even when the switch is open or not connected to Port .I tried all 3 combinations: PullUp, Pull Down,NoPull

What wen't wrong and how to configure the ports correctly ?

I am using a Waveshare Open407Z Board with

STM32F407ZGT6 Processor

Thanks in advance.

Marc

7 REPLIES 7
Posted on February 09, 2013 at 18:00

Why 5V, wouldn't the 3-3.3V supply the part is running off be more appropriate.

Perhaps then problem is the code you're not showing, always helps to provide a complete context. If you don't enable the clock you won't be able to program the peripheral. Look at the register settings in a debugger.

void TestPA5(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStructure);
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) != RESET)
puts(''High'');
else
puts(''Low'');
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mrupprath
Associate II
Posted on February 09, 2013 at 20:42

Hello;

Yes of course: I have enabled the clock first, thats not the problem

I also used the 3.3 V Supply at the header but the behaviour is the same.

I think it depends on the hardware design of the Button Interface on the board.

I have noticed at least one pullup resitor in the schematic drawing.

I will try another input port and check again.

Tank you

Andrew Neil
Evangelist
Posted on February 09, 2013 at 22:33

GPIOA_InitSTructure.

GPIO_PuPd

=

GPIO_PuPd_DOWN

;

 

 

/*GPIOA_InitSTructure.GPIO_PuPd = GPIO_PuPd_NOPULL*/

;

 

GPIOA_InitSTructure.

GPIO_PuPd

=

GPIO_PuPd_UP

;

So you set it to pull-down, then immediately overwrite that with pull-UP - so of course it will always be high!

Posted on February 10, 2013 at 02:01

Yes of course: I have enabled the clock first, thats not the problem

If I got a dollar for everyone who didn't....

I can only work with the information presented.

I'd check for external drivers. Do you have a schematic you can attach or link too, nothing is dropping out with a quick search.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist
Posted on February 10, 2013 at 16:05

''can only work with the information presented.''

And, according to the information presented, he has the switch configured to switch to +V, and also has a pull-up  enabled - so the input

will

  always be high

irrespective

  of the state of the switch!

 

 

To put it another way, the only effect of the switch is to make a ''stronger'' pullup!
Posted on February 10, 2013 at 16:48

The Port is always high, even when the switch is open or not connected to Port .I tried all 3 combinations: PullUp, Pull Down,NoPull

Andrew, I'm assuming Marc has tried a number of things before getting here, the slap dash paste job might be a results of that, and why I pasted what I thought was a correct and complete configuration.

If that doesn't work, then looking at the attached circuitry is in order. Personally I would have switched to ground, and used a pull-up. The 50K internal, or whatever the poly gets you in this process, should suffice for the test here.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist
Posted on February 10, 2013 at 21:54

Well, as you say, all we have to go on is what's posted.

Time for the OP to clarify what's really going on...