Searching \ for '[PIC] Quick C question' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=c
Search entire site for: 'Quick C question'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Quick C question'
2007\03\06@182845 by Mauricio Jancic

flavicon
face
Hi,
       I modifying a C code for an ATMEL chip, and I found this statement:

if(!(PINB & (1<<PB1)))

       Whats that? Specially the (1<<PB1) that I cant seem to figure out...

Regards,

Mauricio

2007\03\06@184215 by Mauricio Jancic

flavicon
face
Im a fool... Sorry to bother.

Mauricio

{Quote hidden}

> -

2007\03\06@184323 by David VanHorn

picon face
On 3/6/07, Mauricio Jancic <infospamKILLspamjanso.com.ar> wrote:
>
> Hi,
>        I modifying a C code for an ATMEL chip, and I found this statement:
>
> if(!(PINB & (1<<PB1)))
>
>        Whats that? Specially the (1<<PB1) that I cant seem to figure
> out...


That's a bit shift, a common way to address individual pins.
They are masking the read of PINB against a byte with the PB1 bit set.
So, the statement is conditional on gettting a zero on the PB1 bit of PINB.

2007\03\06@184903 by Bob Blick

face picon face

--- Mauricio Jancic <.....infoKILLspamspam.....janso.com.ar> wrote:
>
> if(!(PINB & (1<<PB1)))
>
>        Whats that? Specially the (1<<PB1)

Hi Mauricio,

1<<PB1 is 1 left-shifted by the amount of PB1, the
same as 2**PB1

The full statement is testing if a particular bit in
PINB is low - PB1 being from 0 to 7.

Cheers,

Bob



2007\03\06@185040 by Richard Prosser

picon face
I'm guessing the PB1 has been defined somewhere as a constant  -
probably 1 since it's PB1),

Then 1<<PB1 means "shift 1 left PB1 (i.e. 1) times. [ - The result is 2]

So the statement tests if the input of port B, pin 1 is high. (or
whichever pin PB1 actually points to).

RP


On 07/03/07, Mauricio Jancic <EraseMEinfospam_OUTspamTakeThisOuTjanso.com.ar> wrote:
{Quote hidden}

> -

2007\03\06@190041 by Mauricio Jancic

flavicon
face
Yes, I knew it was a bit shift, but I didn't understand de PB1 and why was
shifting PB1 1 time to the left (here you can see my mistake)...

The thing is PB1 is a constant indicating pin number, and it was shifting
the 1 PB1 times to the left and then ANDing it to PINB.

Just a missreading

THANKS!!

Mauricio



> {Original Message removed}

2007\03\06@212211 by Rikard Bosnjakovic

picon face
On 3/7/07, Mauricio Jancic <infospamspam_OUTjanso.com.ar> wrote:

> if(!(PINB & (1<<PB1)))
>
>         Whats that? Specially the (1<<PB1) that I cant seem to figure out...

The statement "x << y" means "take the number x and rotate its bits y
times left". If PB1 in your example is 5, then "1<<PB1" would mean "1
<< 5", which in binary is "00000001 << 5", which finally gives the
result "00010000".

Next step (going from the innermost parenthesis and out) is "PINB &
(the above)", which is a logical AND-function. AND returns 1 at those
bitpositions where both are 1s. "111000 & 000111" is zero, while
"111000 & 001000" is 8 (1000 binary).  For the sake of the argument,
if the value of PINB has a "1" at the same position as the result from
above will be the same as (1 << PB1).

And finally, the "!" inverts the whole parenthesis.

What the if does is checking if the particular bit in PINB is set or
not. If it's not set, the code will proceed into the if's then-clause.


--
- Rikard.

2007\03\07@004300 by William Chops Westfield

face picon face

> Whats that? Specially the (1<<PB1) that I cant seem to figure out.

Nobody has been wrong yet, I think.  But the way I'd put it:

   (1<<BITNUM) is a common C construct for converting a bit NUMBER
   to a bit MASK.  It's especially common in microcontroller projects
   because of the existence of machine instructions that take the
   bit number directly (and you really don't want to have separate
   definitions for the mask for a bit and the bit number for a bit.)
   It works by shifting a single 1 bit ("1") left one position for
   each bit number (for systems that number their bits from the LSB.)

Your confusion demonstrates the usual problem with short symbol names
(especially) in global include files.  But no one wants to type
       1<<PORTB_BITNUM_B0

BillW

2007\03\07@022852 by Ruben Jönsson

flavicon
face
>
> The thing is PB1 is a constant indicating pin number, and it was shifting
> the 1 PB1 times to the left and then ANDing it to PINB.
>
> Just a missreading
>
> THANKS!!
>

Which proves the fact that in order to write maintainable code it is very
important to choose a meaningful name for variables and constants.

The original author actually did manage half way since he in fact provided a
way to easily change both the port and pin for a function in one place - the
header file.

I would have used macros, though.

/Ruben
==============================
Ruben Jönsson
AB Liros Electronic
Box 9124, 200 39 Malmö, Sweden
TEL INT +46 40142078
FAX INT +46 40947388
@spam@rubenKILLspamspampp.sbbs.se
==============================

2007\03\07@050116 by Chris McSweeny

picon face
Though the code author's not really done anything wrong, since PINB and PB1
are both defined in the standard header file provided by Atmel, and are
standard constants. Arguably he should have used PINB1 instead of PB1 for
better clarity.

On 3/7/07, Ruben Jönsson <KILLspamrubenKILLspamspampp.sbbs.se> wrote:
{Quote hidden}

>

2007\03\07@054057 by Gerhard Fiedler

picon face
William ChopsWestfield wrote:

> Your confusion demonstrates the usual problem with short symbol names
> (especially) in global include files.  But no one wants to type
>        1<<PORTB_BITNUM_B0

That's why one should have comfortable development tools that scan the
header files for symbols and help with completion of long names.

Gerhard

2007\03\07@062326 by Byron A Jeff

face picon face
On Tue, Mar 06, 2007 at 08:28:36PM -0300, Mauricio Jancic wrote:
> Hi,
>        I modifying a C code for an ATMEL chip, and I found this statement:
>
> if(!(PINB & (1<<PB1)))
>
>        Whats that? Specially the (1<<PB1) that I cant seem to figure out...

It shifts a bit over PB1 positions. Generally used to create a bit mask.

BAJ

2007\03\07@063139 by Ruben Jönsson

flavicon
face
He should have used a describing name for the input and bit, like ALARM_PORT
and ALARM_BIT and defined these in a header file to PINB1 and PINB.

This way he could have changed the definition for ALARM_PORT and ALARM_BIT at
one place only if he wanted to use another input pin. The source code would
also directly tell what he wanted to do.

I don't know the AVR but I take it that as he wrote it, is a general way to
check for a bit in a port using the predefined names for ports and bits. If
this is the case I got it wrong from the beginning since he not only didn't
provide a way to change the port pin for a specific use in one place but also
didn't use an understandable name that easily can be redefined.

/Ruben

{Quote hidden}

> > --

More... (looser matching)
- Last day of these posts
- In 2007 , 2008 only
- Today
- New search...