Pages: [1]   Go Down
Author Topic: How do I know if address is in hex or dec?  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm learning how to read and write data using the wire interface at the moment, but as my question says I'm unsure if the code is interpretting the address as a hex value or decimal, for example:

Code:
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
 
  writeTo(DEVICE, 0x16, 8);     
}  // end of setup

Where writeTO is:

Code:
void writeTo(int device, byte address, byte val)
  {
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        // send register address
  Wire.write(val);        // send value to write
  Wire.endTransmission(); //end transmission
  } // end of writeTo

So, i'm using writeTo(DEVICE, 0x16, smiley-cool, how does it know that the 0x16 is hex and not decimal?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The '0x' part tells the compiler that you are using a hex constant.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

0x16 tells the code it is hex (the 0x part).
0x00 is a byte (8 bits) as in
byte flag1 = 0x01;

0x0000 is a int or word (16 bits), as in
int analogValue = analogRead (A0);

0x01234567 is a long (32 bits), as in
unsigned long currentTime = millis();

B11110000, B indicates binary.

'Nothing' preceding a number indicates it is decimal.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
B11110000, B indicates binary.
This is something the Arduino IDE does. There is also 0b11110000.

A leading 0 indicates an octal constant, I believe.
« Last Edit: March 26, 2012, 03:10:21 pm by dxw00d » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 237
Posts: 24278
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
'Nothing' preceding a number indicates it is decimal.
...and a leading zero indicates it is octal
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, it seems obvious now. If I had the register map below:



and I wanted to write FS_SEL=3  on register 0x16 then am I correct in saying:
FS_SEL =3 makes the line equal in binary:

00011000

which is equal to 24 in decimal, so i'd want:
writeTo(DEVICE, 0x16, 24); ?


Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or,
writeTo(DEVICE, 0x16, B00011000);
Or
writeTo(DEVICE, 0x16, 0x18);

Makes it a lot more straightforward which bits are being manipulated.
24, you can't tell where the bits are without doing the conversions.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 314
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could also write:
writeTo(DEVICE, 0x16, (3<<3));

That is the value 3 shifted 3 bit to the left.

I assume that you know that you will also be writing a 0 to DLPF_CFG when you do this.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8430
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
which is equal to 24 in decimal, so i'd want:
writeTo(DEVICE, 0x16, 24); ?
While technically this is correct it's unreadable, a day later you'll be wondering WTF the value means.

So I would use the B00011000 or (3<<3) options mentioned. Even better define a value to shift by

#define FS_SEL 3

(3<<FS_SEL)

You will notice that is how most AVR direct port manipulation is written. Straight away you know that the FS_SEL field is being set to 3.

If the DLPF_CFG field is <> 0 then

(3<<FS_SEL) | (dlpf_cfg_val << DLPF_CFG)

or

(3<<FS_SEL) | dlpf_cfg_val

because it's bit offset is 0 anyway.
______
Rob
« Last Edit: March 27, 2012, 07:42:19 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Graynomad, that approach makes a lot of sense smiley I'll be using that way in new code I write.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 3979
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are changing different registers then you might want to make a function to read the register and only change the bits you want changed. Perhaps you will need args for what to & and what to | with the original data before writing back.

Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: