cancel
Showing results for 
Search instead for 
Did you mean: 

Reading from GPIO PC0 ?

antonius
Senior
Posted on December 30, 2013 at 14:15

Guys,

How can I read a button from GPIO PC0 ?

I have created :

      RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOA, ENABLE);

//initialize PC0 and PC1 as button input    

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // this sets up input to be used with external push up/pull down resistors.

  GPIO_Init(GPIOC, &GPIO_InitStructure);

.

..

..

..

uint8_t button1;

     button1 = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);

         if (button1 == 0x00)

          {

             lcd_string(''Button 1 is pressed..'');

          }

any opinions ?

thanks
9 REPLIES 9
antonius
Senior
Posted on January 02, 2014 at 00:14

Why isn't response to my ''if'', the software keeps running without checking the button ??

any ideas guys ?

Posted on January 02, 2014 at 00:45

May be it would be helpful if you described the physical wiring of your button, and why the internal pull down?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
John F.
Senior
Posted on January 02, 2014 at 10:11

antonius
Senior
Posted on January 02, 2014 at 11:49

it's push button connected to ground into PC0....

trevor23
Associate III
Posted on January 02, 2014 at 13:55

If the push button is a connected between PC0 and ground then you need a pull up resistor, either internally or externally. You have configured an internal pull down resistor on PC0 so I suspect that you need to configure this as pull up (assuming no external pull up or down resistors connected to PC0 externally).

antonius
Senior
Posted on January 03, 2014 at 15:06

How can I wait for button input ? ( do nothing until button is pressed )

Is it like this ?

 while (button1 !=RESET)

        {    

         button1 = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);

        }  

             if (button1 == RESET)

               {

                 lcd_cmd(LCD_CLEAR);

                 lcd_string(Button 1 is pressed..);

                 vTaskDelay(2000);

              }

trevor23
Associate III
Posted on January 03, 2014 at 17:08

Like this (maybe). Note: this will keep writing to the LCD while the button is pressed but I have no way of knowing what else you might want it to do:

for (;;)
{ 
if (!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0))
{ 
lcd_cmd(LCD_CLEAR);
lcd_string(''Button 1 is pressed..'');
vTaskDelay(2000);
}
}

And this does not cater for any ''de-bouncing'' that might be required.
antonius
Senior
Posted on January 04, 2014 at 12:04

It doesn't give a response to a push button...any clues ?

trevor23
Associate III
Posted on January 04, 2014 at 13:24

You need to provide us with the ''clues'' as you are the only one that use a debugger with the actual hardware. E.g. what happens when you single step into GPIO_ReadInputDataBit ? Check the pin with a multimeter or scope to see that it goes low when switch is pressed and is high when the switch is not pressed. Check configuration registers against the data sheet and confirm with the debugger that they are getting set to what is expected. Make a small (small as possible) example and provide it all to us - otherwise we have to get our crystal ball out and mine is very dusty at the moment ;-).