Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[moved] fast gpio reading input value not working lpc1768

Status
Not open for further replies.

karthik chandra

Newbie level 5
Joined
Jul 10, 2015
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
69
led connected to port0.4
pushbutton(pulldown, when button is pressed 3.3v flows) to port0.5

Code:
#include"lpc17xx.h"
uint32_t value;
int main(void)
{
	LPC_GPIO0->FIODIR |=(1<<4);	   // port0.4 output 
	LPC_GPIO0->FIODIR &=~(1<<5);   //port0.5 input
	LPC_PINCON->PINSEL0 &= ~((1<<9)|(1<<8));  // 00 function gpio port0.4
	LPC_PINCON->PINSEL0 &= ~((1<<11)|(1<<10));	// 00 function gpio port0.5
	LPC_PINCON->PINMODE0 |= ((1<<11)|(1<<10)); // 11 function mode pulldown port0.5
	while(1)
	{
		value = (LPC_GPIO0->FIOPIN & (1 <<5)>>5);// reading the status of input port0.5
		LPC_GPIO0->FIOSET=(value<<4);	  //setting the port0.4 according to port0.5
	}
}



led is getting on without pressing the button
even i checked in keil peripherals(ports)it is making port0.4 high irrespective of port0.5


i tried with some codes too it is not reading the pin

Code:
#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);
                }
        }
}
(going inside the if condition without the pin high)
can anyone please suggest me where im going wrong


thankyou
 

Check priority in this line of your code:
Code:
value = (LPC_GPIO0->FIOPIN & (1 <<5)>>5);
you check lsb, not bit 5.
 

thanks for the reply sir
lsb means port0.0?
can you be little more clear
im new to this sorry:sad:
 

Yes lsb is the low significant bit (bit 0).

The code:
Code:
value = (LPC_GPIO0->FIOPIN & [B][FONT=Arial Black][COLOR="#FF0000"](1 <<5)>>5[/COLOR][/FONT][/B]);
is interpreted by the compiler as:
Code:
value = (LPC_GPIO0->FIOPIN &  [COLOR="#FF0000"][B][FONT=Arial Black]1[/FONT][/B][/COLOR]   );

You should write it as:
Code:
value = (LPC_GPIO0->FIOPIN & (1 <<5))>>5;
//but it'a a little unclear (not self commenting)


or

Code:
value = (LPC_GPIO0->FIOPIN & (1 <<5)) ? 1 : 0;
//witch IS self commenting,


or as I would prefer it to be:
Code:
value = LPC_GPIO0->FIOPIN & (1<<5);
//the above parenthesis are optional. 
//C priority states that shift is executed 
//before binary operator AND.
if(value)
           LPC_GPIO0->FIOSET &= ~(1u<<4);
else
           LPC_GPIO0->FIOSET |= (1<<4);
//storing to value in stead of check condition inside if, is optional)
 





when the bit is high also it is going to the if condition
 

I am not familiar with lpc1768.
I googled it a little and find some examples that write to FIOSET or FIOCLR.

Try this and tell me what happens

Code:
value = LPC_GPIO0->FIOPIN & (1<<5);
if(value)
           LPC_GPIO0->FIOSET = 1<<4;//turn led on
else
           LPC_GPIO0->FIOCLR = 1<<4;//turn led off
 

Code:
value = LPC_GPIO0->FIOPIN & (1<<5);
if(value)
           LPC_GPIO0->FIOSET = 1<<4;//turn led on
else
           LPC_GPIO0->FIOCLR = 1<<4;//turn led off

when button is not pressed (port0.5 is not set) it is going inside if condition
when button is pressed (port0.5 is set) again it is going inside if condition
 
Last edited by a moderator:

Code:
#include"lpc17xx.h"
uint32_t value;
int main(void)
{
	LPC_GPIO0->FIODIR |=(1<<4);			// port0.4 output 
	LPC_GPIO0->FIODIR &=~(1<<5);			//port0.5 input

	LPC_PINCON->PINSEL0 &= ~((1<<9)|(1<<8));	// 00 function gpio port0.4
	LPC_PINCON->PINSEL0 &= ~((1<<11)|(1<<10))	// 00 function gpio port0.5

	LPC_PINCON->PINMODE0 |= ((1<<11)|(1<<10));	//11 function mode pulldown port0.5

	while(1)
	{
		value = LPC_GPIO0->FIOPIN & (1<<5);
[U][COLOR="#FF0000"]//What is the value stored in 'LPC_GPIO0->FIOPIN' and 'value' he[/COLOR][/U]
		if(value)
		           LPC_GPIO0->FIOSET = 1<<4;	//turn port 0.4 on
		else
		           LPC_GPIO0->FIOCLR = 1<<4;	//turn port 0.4 off	
	}
}

Are you sure you have the above code?
Do you try it in simulation and have the problem? Are you sure that LPC_GPIO0->FIOPIN bit 5 (6th bit from right) is ZERO and program enters the if() block?
There is no error inside the while() block.
Check in the debugger the values of variable/register: value and LPC_GPIO0->FIOPIN before executing the if() command.
 

Are you sure you have the above code?


yes sir i do have the above code


Do you try it in simulation and have the problem? Are you sure that LPC_GPIO0->FIOPIN bit 5 (6th bit from right) is ZERO and program enters the if() block?

the pin state is always high
(pull down)im not able to set
may be for this reason the pin is always high

according to datasheet we should set pulldown for p0.5 like this
Code:
LPC_PINCON->PINMODE0 |= ((1<<11)|(1<<10));
is there any other way?
 

...the pin state is always high
(pull down)im not able to set
may be for this reason the pin is always high
obvioulsy this is your problem and that's why execution enters the if block.

1) Is it simulation?
2) Are you sure the simulator/debugger you use, handles correctly weak pull down flag?
3) Have you tested the code in real hardware?
4) If you externaly pull low the pin, does the bit changes in debugger?
If not, then the initialization of port function and/or direction does not work as expected.

according to datasheet we should set pulldown for p0.5 like this
Code:
LPC_PINCON->PINMODE0 |= ((1<<11)|(1<<10));
This is correct.
 

:)got my code working
Code:
#include "lpc17xx.h"
uint32_t buttonstate;
int main(void)
{
	SystemInit();
	LPC_GPIO0->FIODIR |= 0x00000010;  //output 
	LPC_GPIO0->FIODIR &= ~(0x00000100); // input
	LPC_PINCON->PINSEL0 &=~((1<<9)|(1<<8));	//led pin gpio function 
	LPC_PINCON->PINSEL0 &=~((1<<17)|(1<<16)); //button pin gpio function
	LPC_PINCON->PINMODE0 |= (1<<17); //button pin pulldown mode function
	LPC_PINCON->PINMODE0 |= (1<<16);
	while(1)
	{
		LPC_PINCON->PINSEL0 |= (1 << 17);
		buttonstate = ((LPC_GPIO0->FIOPIN & 0x00000100 )>> 8);
		if(buttonstate)
		{
			LPC_GPIO0->FIOSET |= 0x00000010;
		}
		else
		{
			LPC_GPIO0->FIOCLR |=0x0000010;
		}
	}
}


but this command is used to pulldown the pin 8 is
Code:
LPC_PINCON->PINSEL0 |= (1 << 17);
I got this from this link
https://anhnvnguyen.blogspot.in/2010/04/lpc17xx-gpio-basic_05.html


in lpc17xx manual it is written to pulldown the pin 8 we should use
Code:
LPC_PINCON->PINMODE0 |= (1<<17);
LPC_PINCON->PINMODE0 |= (1<<16);
but this isn't working can you please explain me:)

thank you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top