Go Down

Topic: using integer type to define pins, why? (Read 3442 times) previous topic - next topic

retrolefty

#15
Feb 03, 2013, 06:52 am Last Edit: Feb 03, 2013, 06:54 am by retrolefty Reason: 1

Personally I use byte, because pin numbers are indeed unsigned, and don't go over 255.


But does for example analogRead(pin#) function cast it as a int size value inside the function? And as you said a pin number declared as a const anyway doesn't occupy any memory space on it's own, only by the thing that reference it?

Lefty

lloyddean

#16
Feb 03, 2013, 06:54 am Last Edit: Feb 03, 2013, 06:57 am by lloyddean Reason: 1
Actually pin numbers, as used with 'digitalWrite(12, HIGH)', are in fact specified as being of type 'uint8_t' so the compiler will down convert the 'int' to an 'uint8_t' for you.

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.

retrolefty


Actually pin numbers, as used with 'digitalWrite(12, HIGH)', are in fact specified as being of type 'uint8_t' so the compiler will down convert the 'int' to an 'uint8_t' for you.

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.


Ah got it, thank you.

Lefty

Nick Gammon


But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.


In other words, "const byte".
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty



But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.


In other words, "const byte".


But of course now I've learned that doesn't save any real space over using const int as applied to pin number use.  :D

Lefty

lloyddean

My problem with 'byte' is it doesn't tell me enough about it.

I believe it is a synonym for 'unsigned char' (because its different on different platforms), but I'm never certain and alway find myself doing the double-check. 

I much prefer 'uint8_t' as it's easier to know what you're getting and is the same everywhere I want to be.

Nick Gammon

That's fine, but the core type is "unsigned char" as shown here in stdint.h:

Code: [Select]
/** \ingroup avr_stdint
    8-bit unsigned type. */

typedef unsigned char uint8_t;


You just have to remember that "unsigned char" is an 8-bit unsigned type.

Using uint8_t is one typedef away from the core type.

This is obviously a style issue, but for me, I prefer to remember that. It's like saying:

"I want to call a dog a MammalWith2LegsThatBarks because that tells me more about it."

OK, but it's really a dog.

I don't want to start a "style war" here, hey next we'll be arguing about GOTO. ;)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


I much prefer 'uint8_t' as it's easier to know what you're getting and is the same everywhere I want to be.


If they have the typedef I quoted. If not, it's an unsigned char.

Hey, wait, I just shot myself in the foot here. I should be using "unsigned char" and not "byte". Oh well.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty


That's fine, but the core type is "unsigned char" as shown here in stdint.h:

Code: [Select]
/** \ingroup avr_stdint
    8-bit unsigned type. */

typedef unsigned char uint8_t;


You just have to remember that "unsigned char" is an 8-bit unsigned type.

Using uint8_t is one typedef away from the core type.

This is obviously a style issue, but for me, I prefer to remember that. It's like saying:

"I want to call a dog a MammalWith2LegsThatBarks because that tells me more about it."

OK, but it's really a dog.

I don't want to start a "style war" here, hey next we'll be arguing about GOTO. ;)


You own a two legged dog that barks? Post a video or it isn't true.   :D

Lefty

Nick Gammon

I was very emotional when I wrote that. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

lloyddean

Nick - I'm not sure what your getting at or if you're upset or not.

Just saying for me it's ambiguous as to if it's signed or unsigned (unless I look it up) where as 'uint8_t' it's clear that it's unsigned and is unsigned on all platforms where as 'byte' if expressed at all can be either.

Nick Gammon

I'm only upset that I thought a dog had two legs.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty


I'm only upset that I thought a dog had two legs.


Well they do have two legs, plus two more.  ;)


johncc

I think many people who know better, sometimes just use int instead, when trying to help someone to whom uint8_t or--even byte--would cause additional confusion.  One can't learn everything at once...


Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy