Jump to content
43oh

Couldn't read GPIO pin of stellaris (push buttons)


Recommended Posts

Hi, I am simply trying to read the GPIO pin F0 (the push buttons in stellaris) but don't know why I couldn't read it.

 

I activated the clock for portf, enabled pull up for pins 0 and pins 4 and enabled the pins as digital and enabled pins 1,2,4 as O/P.

 

I can write to the pin 1, 2, 3 (R G B leds) and those are working fine.

But cannot read from pin 0 but can read from pin4.

 

Here is my code:

#include "inc/lm4f120h5qr.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"

int main(void)
{
volatile unsigned long ulLoop;
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;
ulLoop = SYSCTL_RCGC2_R;
GPIO_PORTF_PUR_R = (1<<0)|(1<<4); //PULL UP ENABLE
GPIO_PORTF_DIR_R = (1<<1)|(1<<2)|(1<<3); //O/P ENABLE
GPIO_PORTF_DEN_R = (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4); //DIGITAL ENABLE for 3 LEDs(1,2,3) and 2 Buttons (0,4)
while(1) {
 if(GPIO_PORTF_DATA_R & 1) //reading from F0
 GPIO_PORTF_DATA_R = 1<<1; //RED LED
 else
 GPIO_PORTF_DATA_R = 1<<2; //bue LED	
}
}

 

Always the blue LED is ON even if the switch is ON

 

BUT I CAN READ PIN4 (1<<4).

Now I checked the voltage of PORTF PIN0 (1<<0) and it is 1.64V. I enabled and disabled Pullup but in both case it is 1.64v and if I press the button the voltage becomes 0. In case of pin4 (1<<4) the voltage is around 3.3 and I can read the push button status on P4.

 

So I don't know what is wrong with the P0.... Any additional functionality enabled by default?...

Link to post
Share on other sites

PF0 is on this device shared with an NMI signal. In order to not accidentally trigger NMI Stellaris "locks" this GPIO pin from being changed by default.

 

Below is the code from StellarisWare\boards\ek-lm4f120xl\drivers\buttons.c which unlocks PF0 so that it can become an input and be read. You may also need to enable the internal weak pullups.

 

   //
   // Unlock PF0 so we can change it to a GPIO input
   // Once we have enabled (unlocked) the commit register then re-lock it
   // to prevent further changes.  PF0 is muxed with NMI thus a special case.
   //
   HWREG(BUTTONS_GPIO_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
   HWREG(BUTTONS_GPIO_BASE + GPIO_O_CR) |= 0x01;
   HWREG(BUTTONS_GPIO_BASE + GPIO_O_LOCK) = 0;

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...