We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
How can this expression be true?
if((LPC_GPIO2->FIOPIN & GPIO2_BUTTON1)==1)
The left side of the compare can not extract a value 1, since GPIO2_BUTTON1 isn't the value 1. Just because a bit operation extracts the value of a single bit, it need not give a result that is 0 or 1 - the actual value will depend on the bit position of the extracted bit. And comparing with a value isn't needed since C performs an implicit "!= 0" in conditional statements.
So your conclusion that it always matches the "if" and never reaches the "else" can't be true, when you have a left part of the expression that can never take the value 1.
So is your LED really connected to the pin you think?
And have you really verified the value of all the registers?
And why didn't you modify the code with a #define for your LED too, instead of still leaving magic values in your code? It isn't a good thing to have the same value duplicated to three different locations of the code, which means you must modify three locations to switch to a different LED.
Another thing - what is the purpose of your delay? To keep the LED still lit a while after you release the button? Or you think it's some kind of debounce to avoid flicker from switch bounces? That's not a very long delay. And the compiler can even decide to totally ignore it.
#include <avr/io.h> #include <util/delay.h> void main(void) { DDRB |= (1<<PB0); DDRC &= ~(1<<PC0); while(1) { while(PINC & (1<<PC0) ==1) //button is pressed { PORTB |= 1<<PINB0; // led on } PORTB &= ~(1<<PINB0); // led off } }
sir, this is my AVR code for push button which is working absolutely fine when button is pressed led getting on else off.
i just want my led to get on when button is pressed(when i supply VCC to that pin).
#include "lpc17xx.h" #define GPIO2_BUTTON1 (1<<10) #define GPIO0_LED (1<<4) int main(void) { SystemInit(); LPC_GPIO0->FIODIR |= GPIO0_LED; LPC_GPIO2->FIODIR &= ~GPIO2_BUTTON1; while(1) { if ((LPC_GPIO2->FIOPIN & GPIO2_BUTTON1)) { LPC_GPIO0->FIOSET=GPIO0_LED; } else { LPC_GPIO0->FIOCLR=GPIO0_LED; } } }
#include "lpc17xx.h" #define GPIO2_BUTTON1 (1<<10) #define GPIO0_LED (1<<4) uint32_t status; int main(void) { SystemInit(); LPC_GPIO0->FIODIR |=GPIO0_LED; LPC_GPIO2->FIODIR &= ~GPIO2_BUTTON1; while(1) { status=(LPC_GPIO2->FIOPIN & (GPIO0_LED) >>10); LPC_GPIO0->FIOSET=(status<<4); } }
without pressing the button the led is getting on(without supplying VCC) for both the codes even in keil peripherals same thing happening.
can you please explain me where am i going wrong thank you
"the led is getting on(without supplying VCC)"
Don't you think you should think some more about this? The LED turns on without any VCC supplied? Where does the LED get any current from? So start solving that before you worry about your source code.
If you have broken, or incorrectly connected, hardware then it isn't meaningful to sit and stare at the source code.
Right now, we haven't even seen any indication that you are even able to correctly download and start any program in the processor.
sir i mean i dint give logic high to the pin where button is connected forget about the connections same thing happening in keil first im checking in keil then im uploading to my development board in keil only it is giving trouble for me
please help me with the code
In Keil? You mean in simulator? The simulator can't simulate potential issues on the outside of the processor.
Have you tried a trivial program that only does set the pin high and then hang in a loop? Corresponding program that only sets the pin low and then hang in a loop? You haven't given any indication that you have actually verified that the LED is on the exact pin you claim. And that the LED is correctly connected. No report of what a multimeter or oscilloscope measures when your program tries to hold the pin high. Or when you try to hold the pin low. And you haven't given any indication how you have verified that the push button is on the pin you claim. Or that you have tried to verify the full 32-bit content of the port when the button is pressed or when it isn't pressed, to see if any other bit toggles. When you don't give any feedback what tests you do, then people can't help you.
Debugging is the processor splitting problems into smaller sub-problems until each sub-problem is small enough that you can deduce if it works or not - and can figure out what to do to fix. You don't seem to have been able to deduce if your issue is to detect the button. Or if the problem is to drive the LED. Or if the problem is to download and run an application in the board. Step one is to prove that you can download and run a trivial program. Then you can start to verify individual subsystems. Right now we don't know if the LED is always lit. Or if you get any difference on any port pin if you keep the reset button pressed or not.
sir, led is connected to port0.4 button is connected to port2.10 connection is proper i checked it
In keil, debug-> start debug session and im running the program step by step
"Connection is proper i checked it". But you don't seem to have indicated any voltage levels measured anywhere. And despite me mentioning it, you haven't given any feedback if the voltages measured differs if you keep the reset button pressed or not.
And how are you debugging? On the real hardware? Using what interface? A JTAG adapter? Have you verified that your project doesn't just start the simulator, in which case you can single-step your program as much as you like without a single instruction being sent to - or run on - the hardware where your "properly connected" button and LED are.
And the simulator will not know anything about button presses on the real hardware. And it will not be able to turn on or off any LED on the real hardware.
I have mentioned it multiple times now, but you haven't given one single indication that you are actually able to run any code on real hardware. So right now, the progress is just about zero.