Jump to content
juniorlucival

GPIOPinRead

Recommended Posts

i'm make the code, where i press button 1 and turn on light, but my iar don't read button, please help

#include "inc/lm4f120h5qr.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
int main(void)
{
 int LED = 2;
 int data_in = 0x10;
      SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
      SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
      GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
data_in = 20;
      while(1)
      {
       
       //  data_in = (GPIOPinRead(GPIO_PORTF_BASE, (GPIO_PIN_0)));
       
     
          
    if(GPIOPinRead(GPIO_PORTF_BASE, 0x00000001) == 0x00000001)
    
        {
        
            //            Port Base Addr   Mask of pins to write  Value to write to the PORT
           GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, LED);
        }
        else
        {
       GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, LED);
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x00);
        }
    }
      }

here the video about found

Share this post


Link to post
Share on other sites

In addition to what bluehash said about setting the pin as input, if you are using Port F, Pin 0 you have to do some extra steps to unlock the pin before you can set it as input (because F0 is also the NMI)

 

See for example buttons.c in Stellarisware, (boards/ek-lm4f120xl/drivers/buttons.c)

Share this post


Link to post
Share on other sites
/*
 * main.c
 */


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


int main(void) {
long pinState;
int value = 2;


SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;


SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);


GPIOPinTypeGPIOInput(GPIO_PORTF_BASE , GPIO_PIN_0 | GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4 , GPIO_STRENGTH_2MA , GPIO_PIN_TYPE_STD_WPU);


while(1){
pinState = GPIOPinRead(GPIO_PORTF_BASE , GPIO_PIN_0 | GPIO_PIN_4);
if((pinState & 0x01) == 0x00){
while((pinState & 0x01) == 0x00){
pinState = GPIOPinRead(GPIO_PORTF_BASE , GPIO_PIN_0 | GPIO_PIN_4);
}
SysCtlDelay(100000);
if(value==2){
value = 0;
}else
{
value = 2;
}
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, value);
}
}
}

this is the kind of code you need.

Share this post


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...