[Wiltshire] Random C coding question...

Lashley, Malcolm mlashley at sonusnet.com
Tue Apr 22 17:02:26 BST 2008


Hey Robert,

First - you might checkout www.arduino.cc if you're new to AVR coding -
their open source hardware/software is a good learning ground for this
stuff. (And hides some of the bare-metal concepts from the beginner,
whilst allowing them full hardware access should they choose to.)

Secondly - you guessed right with the bitmask.

Lemme now breakdown the code bit-by-bit (pun intended) you posted for
the benefit of anyone reading the archive.

#define LED1OFF PORTB|=(1<<PB1)

The right hand side of the above:  PORTB|=(1<<PB1) 
Would expand to: PORTB = PORTB | (1<<PB1)
| is bitwise OR, << is bitwise shift left.
PORTB is an 8-bit register controlling 8 digital inputs/outputs
(configurable)

Assume your LED is connected to an output pin corresponding to the least
significant bit (LSB) of PORTB (thus PB1=0)

1<<0  == 0000 0001b (b indicates binary representation)
Similarly (if PB1 were 1 (which I guess it is in your case))
1<<1  == 0000 0010b

So - starting with all outputs off (I'm making the assumption they are
all outputs)

PORTB= 0000 0000b

OR that value with 1<<1 

Gives

0000 0010b

And your LED turns on. 

Repeat for PB5 and you would have

PORTB = 0010 0010b

Turning off - the reverse uses the same bitshift, a negation (~ invert
all bits) and then logical and (&)

So starting with out PORTB as above.

1<<PB1 == 1<<1 == 0000 0010b

~ is negate

1111 1101b

And then and that with our old PORTB
0010 0010b (old PORTB)
1111 1101b  ~(1<<1)
==========
0010 0000   Result - your led on PB1 is turned off, the led you might
have had on PB5 retains it's previous state.

Other useful links:

http://www.arduino.cc/en/Reference/PortManipulation
http://www.arduino.cc/en/Reference/HomePage

Cheers,
Malc.




-----Original Message-----
From: wiltshire-bounces at mailman.lug.org.uk
[mailto:wiltshire-bounces at mailman.lug.org.uk] On Behalf Of Robert
Sent: 22 April 2008 13:51
To: wiltshire at mailman.lug.org.uk
Subject: [Wiltshire] Random C coding question...

Hi all,

Fairly random question, kind of related to linux, I'm hoping someone on
here might be able to answer as it is C-related (and lots of linux
things
are C) and I can't think how to google for the answer or where else to
ask. (putting "1<<" into google isn't helpful!)

Can someone explain to me the significance of 1<<PB1 in the following
extract?

     // gerneral purpose LED
     /* set output to Vcc, red LED off */
     #define LED1OFF PORTB|=(1<<PB1)
     /* set output to GND, red LED on */
     #define LED1ON PORTB&=~(1<<PB1)

What is it doing?  Why "1" and what does << do / mean / what is going
on.

A bit of background may be required I guess, I'm not a C programmer, but
I
do recall from a C++ book I once read things like 'cout << "some text"',
so I'm guessing it's some kind of redirect or pipe type thing.

The code extract is from some AVR code I'm playing with and I know what
the end result of the code is - the LED connected to pin 1 of port B on
the atmega168 chip turns on or off.  And I can modify the code to turn
stuff on and off on different ports by changing (eg) PB1 to PB2 and
PORTB
to PORTD, etc, etc and it works.  I just don't understand how it works!

My initial thoughts were some kind of bit mask, but the "1" just doesn't
make sense to me.  I'm guessing that PORTB is an 8-bit int with each bit
representing a pin, and that or-ing / and-ing (1<<PB1) somehow toggles
the
bit for pin1.  But I could be waaay off with that guess.

Anyway, I could go on all day about the things I don't know about C, so
I'll leave it there..

Robert.






_______________________________________________
Wiltshire mailing list
Wiltshire at mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/wiltshire



More information about the Wiltshire mailing list