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