taking input from io port

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

taking input from io port

1,358 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik C on Wed Jul 08 06:42:25 MST 2015
hi, I'm facing a problem with taking an input from push button
my code is like when you press the button led should get on
but with out pressing the button led is getting on
even i checked in keil peripherals it going inside the if condition without the pin high
please help me out with this
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);
}
}
}
标签 (1)
0 项奖励
回复
5 回复数

1,338 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik C on Fri Jul 10 00:21:27 MST 2015
#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
}
}
</pre>

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


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


sir can you please explain me where im going wrong
0 项奖励
回复

1,338 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu Jul 09 23:09:10 MST 2015

Quote: Karthik C
after uploading the code to development board the led turns on irrespective of button
even in keil peripherals(ports)it is going inside the if condition without the pin high
i checked the button state also in multimeter when i press it is giving 3.3v else 0v



Then check your hardware and your Pin setting...

Your code snippet is not showing any problems  :O
0 项奖励
回复

1,338 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik C on Thu Jul 09 22:43:05 MST 2015
#include "lpc17xx.h"
#define GPIO2_BUTTON1  (1<<10)
void delay(uint32_t c)
{
        while(c>0)
        c--;
}
int main(void)
{
        SystemInit();
        LPC_GPIO0->FIODIR |=(1<<4);
        LPC_GPIO2->FIODIR &= ~GPIO2_BUTTON1;
        while(1)
        {
                if (LPC_GPIO2->FIOPIN & GPIO2_BUTTON1)
                {
                        LPC_GPIO0->FIOSET=(1<<4);
                         delay(100);
                }
                else
                {
                        LPC_GPIO0->FIOCLR=(1<<4);
                        delay(100);
                }
        }
}

after uploading the code to development board the led turns on irrespective of button
even in keil peripherals(ports)it is going inside the if condition without the pin high
i checked the button state also in multimeter when i press it is giving 3.3v else 0v

if((LPC_GPIO2->FIOPIN & GPIO2_BUTTON1)==1)


i tried even with this condition it is not working for me
can u please help me out my project is on hold.

thank you
0 项奖励
回复

1,338 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Karthik C on Wed Jul 08 11:10:30 MST 2015
thanks for the reply
:)
im using lpc1768 development board
0 项奖励
回复

1,338 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nerd herd on Wed Jul 08 08:17:40 MST 2015
Hi Karthik,

Can you please give a bit more information? What MCU are you using? Are you using a development board?
0 项奖励
回复