explanation in lpc2148 programming program needed

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,663
Code:
#define LED1    (1 << 10)
#define LED2    (1 << 11)
#define BUTTON1 (1 << 15)
#define BUTTON2 (1 << 16)


int main(void)
{
  // Set buttons as input
  GPIO0_IODIR &= ~(BUTTON1 | BUTTON2);

  // Set LEDs as output
  GPIO0_IODIR |= (LED1 | LED2);

  // Turn both LEDS off (set them 'high')
  GPIO0_IOSET |= (LED1 | LED2);


please explain the actual working of codes inside the main function with an example.
 

The code you have given it is self explanatory.
but also

Code:
#define LED1    (1 << 10)               //LED1 poistion on port pin
#define LED2    (1 << 11)            //LED2 poistion on port pin
#define BUTTON1 (1 << 15)           //BUTTON1 poistion on port pin
#define BUTTON2 (1 << 16)            //BUTTON2 poistion on port pin


int main(void)
{
  // Set buttons as input
  GPIO0_IODIR &= ~(BUTTON1 | BUTTON2);

  // Set LEDs as output
  GPIO0_IODIR |= (LED1 | LED2);

  // Turn both LEDS off (set them 'high')
  GPIO0_IOSET |= (LED1 | LED2);
 
please explain this step..


GPIO0_IODIR &= ~(BUTTON1 | BUTTON2);
 

GPIO0_IODIR = GPIO0_IODIR && (~(button1 | button2) )

or operation is performed with the values of button1 and button2 and its inverse is done and with value in GPIO0_IODIR and assigned back to GPIO0_IODIR.
 
Buttons should be configured as input.

So, the code

GPIO0_IODIR &= ~(BUTTON1 | BUTTON2)

clears corresponding bits.
 
is it possible define in this manner for the above code?

Code:
#define LED1    (0 << 10)               //LED1 poistion on port pin
#define LED2    (0 << 11)            //LED2 poistion on port pin
#define BUTTON1 (0 << 15)           //BUTTON1 poistion on port pin
#define BUTTON2 (0 << 16)            //BUTTON2 poistion on port pin

main()
{
PIO0_IODIR &= (BUTTON1 | BUTTON2);
}

here i defined directly as 0 and removed the negation.is both are same ?
 

learn about bit wise operations in c , bit shifting what it does and what it is used for.
Then you can reply to your question so easily.

- - - Updated - - -

this might help you !
http://www.programiz.com/article/bitwise-operator-c-programming
 

You cant do it that way because

Code C - [expand]
1
(0 << 10)


is like shifting the zero for 10 times so still it is zero only so it does not have any meaning
what we need is

Code C - [expand]
1
2
3
4
5
6
7
8
~(1 << 10)
[syntax]
[code]
11111111 11111111 11111011 11111111
[/code]
to get the 10 th bit o and to AND with the DIR register. so that the 10th bit will be low and it will be configured as input
[syntax=c]
DIR &= ~(1 << 10);


Will configure the button one as input
 
IODIR is used to make the pin input or output. For input it should be 0 and for output 1.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…