getting input from pushbutton(lpc1768)

hi, im using lpc1768 development board
i have written a code for push button
when button pressed led should turn on
but the led is not responding to button, it is on always
even i checked in keil peripherals(ports)it is going inside the if condition without the pin high
here is my code

#include "lpc17xx.h"
void delay(uint32_t c)
{ while(c>0)
c--;
} int main(void)
{ SystemInit();
LPC_GPIO0->FIODIR |=(1<<4);
LPC_GPIO2->FIODIR &= ~(1<<10);
while(1)
{ if(((LPC_GPIO2->FIOPIN >> 10) & 1) == 1)
{ LPC_GPIO0->FIOSET=0x00000010;
delay(10000);
} else
{ LPC_GPIO0->FIOCLR=0x00000010;
delay(10000);
} }
}

my project is on hold because of this
please help me out

thank you

Parents
  • 1) You don't think it would have helped if you had read the posting instructions for posting source code? It's available directly above the message input box.

    2) Why use magic numbers? Why using two different forms of magic numbers when you initialize the port compared to when you use the port? In one situation you use a shift value. In another situation you use an already shifted bit pattern.

    Why not

    #define GPIO2_BUTTON1  (1<<10)
    
    ...
    
    LPC_GPIO2->FIODIR &= ~GPIO2_BUTTON1;       // Clear direction bits for input.
    
    ...
    
    if (LPC_GPIO2->FIOPIN & GPIO2_BUTTON1) {
        ...
    } else {
        ...
    }
    

    It feels easier to read.
    And easier to maintain.
    And if you move the button to a different port then the symbol name changes which means you get compilation errors that informs you that you must also modify the initialization and test code to switch to the correct GPIO port.

    3) Your project is currently on hold? Why? Because you expect someone else to solve your problems? Aren't you busy trying to figure out the problem yourself? How do you know you have the correct pin on the correct port? Have you measured the actual voltage level of the pin when you expect it to be high? And when you expect it to be low? Have you verified the actual contents of your registers? Any external or internal pull-up? Any alternative mode configured? Any other GPIO2 port pin that can correctly detect both low and high levels?

Reply
  • 1) You don't think it would have helped if you had read the posting instructions for posting source code? It's available directly above the message input box.

    2) Why use magic numbers? Why using two different forms of magic numbers when you initialize the port compared to when you use the port? In one situation you use a shift value. In another situation you use an already shifted bit pattern.

    Why not

    #define GPIO2_BUTTON1  (1<<10)
    
    ...
    
    LPC_GPIO2->FIODIR &= ~GPIO2_BUTTON1;       // Clear direction bits for input.
    
    ...
    
    if (LPC_GPIO2->FIOPIN & GPIO2_BUTTON1) {
        ...
    } else {
        ...
    }
    

    It feels easier to read.
    And easier to maintain.
    And if you move the button to a different port then the symbol name changes which means you get compilation errors that informs you that you must also modify the initialization and test code to switch to the correct GPIO port.

    3) Your project is currently on hold? Why? Because you expect someone else to solve your problems? Aren't you busy trying to figure out the problem yourself? How do you know you have the correct pin on the correct port? Have you measured the actual voltage level of the pin when you expect it to be high? And when you expect it to be low? Have you verified the actual contents of your registers? Any external or internal pull-up? Any alternative mode configured? Any other GPIO2 port pin that can correctly detect both low and high levels?

Children
More questions in this forum