Is PB0 or PD2 necessarily defined as 0 or 2?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am currently making my first steps on avr, and I have seen in a tutorial someone shifting bits using a number and not using the macro of a pin.

 

This is the example I saw:

int main() {
DDRB |= (1<<0);

while(1) {    
//Set to HIGH
PORTB |= (1<<0);

_delay_ms(500);

//Set to LOW
PORTB &= ~(1<<0);

_delay_ms(500);
}

return 0;
}

 

while all other examples I saw are as follow:

int main() {
DDRB |= (1<<PB0);

while(1) {    
PORTB |= (1<<PB0);

_delay_ms(500);

PORTB &= ~(1<<PB0);

_delay_ms(500);
}

return 0;
}

 

So my question is, does writing PBX(or for that matter PCX or PDX) the same as writing X?

Or in other words are PBX, PCX, PDX defined as X?

 

Thanks for help!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oren_daniel wrote:

does writing PBX(or for that matter PCX or PDX) the same as writing X?

Or in other words are PBX, PCX, PDX defined as X?

 

Yes, it's in a header file for the toolchain, which one, will depend on the toolchain your using.

The PXn version may be easier for some to read then the pure number version, as it is somewhat self documenting what the programmer is doing.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much for the answer.

I like defining macro so I find

TURN_ON(PORTB, 0) more readable then

TURN_ON(PORTB, PB0)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TURN_ON(PORTB

 

is turn on port B really that good?  Isn't the port always turned on?  That sounds somewhat ambiguous.  it could be high or low.

 

How about: set_pin_hi(portb, pb0)  ...now no possible confusion

                  set_pin_lo(portb, pb5)  

 

or perhaps: set_pin_pb_hi(pb5)

 

note i was too lazy to use caps...i'm not the shifty type.

    

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

TURN_ON(PORTB

 

How about: set_pin_hi(portb, pb0)  ...now no possible confusion

                  set_pin_lo(portb, pb5)  

 

or perhaps: set_pin_pb_hi(pb5)

 

 

I guess that you are right.

In my language it doesn't make sense to say that a pin is high or low, so I always forget that in English the proper words are high/low and not on/off.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR-libc has predefined macros that may do what you want. Try

 

https://www.nongnu.org/avr-libc/...

 

In particular, check _BV(bit) in <avr/sfr_defs.h>: Special function registers 

https://www.nongnu.org/avr-libc/...

 

FYI - avv-libc is included in the avr gcc installation in Studio7. Just #include <avr/sfr_defs.h> to access this particular macro.

 

Jim

 

 

 

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Generally, you were just fine.  If you told 100 people to "turn the pin on", 99 would probably just set it high.  However, if you asked 100 people "is the pin on", some might respond "yes",  whether is was outputting either a hi or a low.

 

When I was headed for the chili parlor, my wife told me she was hungry & they were offering "2 foot long coney dogs for a dollar special deal".

When I arrived, I ordered my chili & the 2 foot long special to go. As I ate my chili, the server came by and dumped two packages on the table.  I asked what was up & she said these are the two coneys you ordered.  I insisted I only ordered one of the specials.  She seriously glared at me and took one away.  Later I realized these were not 24 inch coneys for a dollar (which suddenly seemed a bit ridiculous) but TWO one-foot long coneys for a dollar.  I came home with chili on my face.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

AVR-libc has predefined macros that may do what you want. Try

 

https://www.nongnu.org/avr-libc/...

 

In particular, check _BV(bit) in <avr/sfr_defs.h>: Special function registers 

https://www.nongnu.org/avr-libc/...

 

FYI - avv-libc is included in the avr gcc installation in Studio7. Just #include <avr/sfr_defs.h> to access this particular macro.

 

Jim

 

 

 

 

 

I don't use Atmel studio, I do everything from the Unix command line, but I will have a look at it, thank you!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's a non-sequitur, the stuff Jim (ka7ehk) was pointing out has nothing to do with the operating system or IDE/build system you use. He was pointing out parts of the C lib which is the same whether you use Windows, Linux, OSX or something else.
.
The fact that it is universal is a good reason to standardize on using it.