Pages: [1]   Go Down
Author Topic: Need help with HEX DEC and the meaning of life  (Read 1374 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I just can't seem to get my head around Hex, Decimal and byte addressing on I2C (probably just got all those terms mixed up  :-/ )

Question 1/
I'm trying to adjust the gain on a ADJD-S311 color sensor which the capacitors  have a range of 00H to 0FH but don't know how to write that into 0x?? format. Is there a table to covert it?

Question 2/
Pretty much as above but i have to convert the range from C4H to 09H to 0x?? format for gain

Any help would be greatly apppreciated here as the sun is coming up and i've read so much i've got so confussed  :o It's probably going to be something simple that i'll just sit here and call myself a "D" head and slap myself in the back of the head

Code:
// Avago ADJD-S311-CR999 Color Sensor demo
// by Musashinodenpa (tf@musashinodenpa.com)

#include <Wire.h>
#define Led  3
#define I2C_ADDRESS 0x74       // 7bit

#define REG_CAP_RED       0x06
#define REG_CAP_GREEN     0x07
#define REG_CAP_BLUE      0x08
#define REG_CAP_CLEAR     0x09

#define REG_INT_RED_LO    0x0A
#define REG_INT_RED_HI    0x0B
#define REG_INT_GREEN_LO  0x0C
#define REG_INT_GREEN_HI  0x0D
#define REG_INT_BLUE_LO   0x0E
#define REG_INT_BLUE_HI   0x0F
#define REG_INT_CLEAR_LO  0x10
#define REG_INT_CLEAR_HI  0x11

#define REG_DATA_RED_LO   0x40
#define REG_DATA_RED_HI   0x41
#define REG_DATA_GREEN_LO 0x42
#define REG_DATA_GREEN_HI 0x43
#define REG_DATA_BLUE_LO  0x44
#define REG_DATA_BLUE_HI  0x45
#define REG_DATA_CLEAR_LO 0x46
#define REG_DATA_CLEAR_HI 0x47

void set_register(unsigned char r, unsigned char v)
{
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.send(r);
  Wire.send(v);
  Wire.endTransmission();
}

unsigned char read_register(unsigned char r)
{
  unsigned char v;
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.send(r);  // register to read
  Wire.endTransmission();

  Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
  while(!Wire.available()) {
    // waiting
  }
  v = Wire.receive();
  return v;
}

void setup(void)
{
  Wire.begin();
  Serial.begin(9600);

  pinMode (Led, OUTPUT);

  // sensor gain setting (Avago app note 5330)
  // CAPs are 4bit (higher value will result in lower output)
  set_register(REG_CAP_RED, 0x03);
  set_register(REG_CAP_GREEN, 0x03);
  set_register(REG_CAP_BLUE, 0x03);
  set_register(REG_CAP_CLEAR, 0x08);
  // INTs are 12bit (higher value will result in higher sensor value)
  set_register(REG_INT_RED_LO, 0xC4);
  set_register(REG_INT_RED_HI, 0x09);
  set_register(REG_INT_GREEN_LO, 0xC4);
  set_register(REG_INT_GREEN_HI, 0x09);
  set_register(REG_INT_BLUE_LO, 0xC4);
  set_register(REG_INT_BLUE_HI, 0x09);
  set_register(REG_INT_CLEAR_LO, 0xC4);
  set_register(REG_INT_CLEAR_HI, 0x09);
}

void loop()
{
   digitalWrite(Led,HIGH);

  int r, g, b, c;

  set_register(0x00,0x01); // start sensing

  while(read_register(0x00) != 0) {
    // waiting for a result
  }
  // 10bit results
  c = read_register(REG_DATA_CLEAR_LO) + (read_register(REG_DATA_CLEAR_HI)<<8);
  r = read_register(REG_DATA_RED_LO) + (read_register(REG_DATA_RED_HI)<<8);
  g = read_register(REG_DATA_GREEN_LO) + (read_register(REG_DATA_GREEN_HI)<<8);
  b = read_register(REG_DATA_BLUE_LO) + (read_register(REG_DATA_BLUE_HI)<<8);

  Serial.print("crgb=");
  Serial.print(c ,DEC);
  Serial.print(",");
  Serial.print(r ,DEC);
  Serial.print(",");
  Serial.print(g ,DEC);
  Serial.print(",");
  Serial.println(b ,DEC);

  delay(1000);
}

« Last Edit: April 10, 2010, 05:23:04 pm by Gee63 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I might be getting my head around this?

A range of C4H to 09H would be if it was 16bit..
C4H = 0xC4 = 196
C2H = 0xC2 = 194
C1H = 0xC1 = 193
C0H = 0xC0 = 192
BFH = 0xBF = 191 and so on all the way down to 09H (0x09) which would equal 9

OR at 12bit as i need...
C4H = 0xC4 = 148
C2H = 0xC2 = 146
C1H = 0xC1 = 145
C0H = 0xC0 = 144
B8H = 0xB8 = 143 and so on all the way down to 09H (0x09) which would equal 9


as per
hexadecimal  decimal
-----------  -------
    0         0   (zero)
    0x1         1   (one)
    0x2         2   (two)
    0x3         3   (three)
    0x4         4   (four)
    0x5         5   (five)
    0x6         6   (six)
    0x7         7   (seven)
    0x8         8   (eight)
    0x9         9   (nine)
    0xA        10   (ten)
    0xB        11   (eleven)
    0xC        12   (twelve)
    0xD        13   (thirteen)
    0xE        14   (fourteen)
    0xF        15   (fifteen)
   0x10        16   (sixteen)
   0x11        17   (seventeen)

Am i on the right track ????????
« Last Edit: April 10, 2010, 05:23:56 pm by Gee63 » Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gee63:
Quote
Am i on the right track ????????
Yes

Quote
I'm trying to adjust the gain on a ADJD-S311 color sensor which the capacitors  have a range of 00H to 0FH but don't know how to write that into 0x?? format. Is there a table to covert it?
There's nothing to convert.  The 'H' and the '0x' are just two different ways of expressing the fact that we are dealing with a hexadecimal number.  A third method is to use a leading '$'.  So 0x74, 74H, and $74 are three different ways to indicate that this particular number 74 is a hexadecimal number.

Why use hexadecimal?  Well you have to understand that you are dealing with a binary device that only understands '0's and '1's.  The actual number here is 01110100.  In dealing with with a bunch of binary numbers, particularly large ones, the system becomes unwieldy for us mere mortals.  The hexadecimal system is just a means to make those number smaller and (believe it or not) easier to deal with.  The binary number 01110100 is broken into two groups of 4 digits, 0111 and 0100, and each group is converted to it's hexadecimal equivalent.  So 0111 --> 7 and 0100 --> 4.  

The binary number 01110100 could also have been converted to it's decimal equivalent of 116 but, to a programmer this doesn't help things.  A programmer can look at the hex number 74 and mentally convert it back to it's binary equivalent 0111 0100 without resorting to a chart or a calculator.  A conversion from decimal back to binary is not so simple.

If you would like such a chart you can follow the link to "A handy  Decimal - Binary - Octal - Hex - ASCII Conversion Chart" at http://web.alfredstate.edu/weimandn.  Note that this chart only covers the decimal numbers 0 - 127 and takes up a full sheet of paper.

Don
« Last Edit: April 11, 2010, 09:44:00 am by floresta » Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Note that this chart only covers the decimal numbers 0 - 127 and takes up a full sheet of paper.

Also, many calculator apps that ship with modern operating systems have the ability to convert large numbers between decimal, hex and binary.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks TBAr and floresta, its greatly appreciated more than you'll ever realise

Yes i have to admit the "H" did confuse the heck out of me and i was looking for a more complicated meaning than it really is.

The other thing i probably stuffed up on was....

"A range of C4H to 09H would be if it was 16bit..
C4H = 0xC4 = 196
C2H = 0xC2 = 194
C1H = 0xC1 = 193
C0H = 0xC0 = 192
BFH = 0xBF = 191 and so on all the way down to 09H (0x09) which would equal 9

OR at 12bit as i need...
C4H = 0xC4 = 148
C2H = 0xC2 = 146
C1H = 0xC1 = 145
C0H = 0xC0 = 144
B8H = 0xB8 = 143 and so on all the way down to 09H (0x09) which would equal 9"

With the multiplication for different "bit's" i.e. multipy by the number of bits not just by 16 to the power of etc.

The way i understand it now is that HEX is always multipied by 16 to the power of and bit relates to te number of 0's and 1's in the binary string?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"A range of C4H to 09H would be if it was 16bit..
C4H = 0xC4 = 196
C2H = 0xC2 = 194
C1H = 0xC1 = 193
C0H = 0xC0 = 192
BFH = 0xBF = 191 and so on all the way down to 09H (0x09) which would equal 9

OR at 12bit as i need...
C4H = 0xC4 = 148
C2H = 0xC2 = 146

I don't understand what you're saying here.

0xC4 is 196 in decimal whether you express it in 12 bits (three hex digits) or in 16bits (four hex digits).
I don't see where you get the "148" from.

It's calculated as (C16 * 1610) + 410 = (1210 * 1610) + 410 = 192 + 4.

Twelve bits is sufficient to represent 4096 values.
« Last Edit: April 12, 2010, 01:59:56 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Groove

Thats why I opened that comment with "The other thing i probably stuffed up on was...."

In my first post in the string i thought thats how it worked, you multiplied by the number of bits but i know better now  smiley-wink
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6654
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any number that has two hex digits (ie all the numbers you've used in your examples so far) will fit in an 8 bit variable.

C4H = 0xC4 = C416 = 110001002

(One of the advantages of Hex is that you can see how many bits things must be right away.  0x123456 needs at most 6*4 bits (24 bits.  Actually only 21, since the most significant digit is just "1")    With a decimal number it is harder to tell.  123456 takes 17 bits, but it's likely to be a few years before you can tell just by looking at it.)
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are on the right track, except as noted by the others above. This makes not much sense:
[edit]OR at 12bit as i need...
C4H = 0xC4 = 148
C2H = 0xC2 = 146
C1H = 0xC1 = 145
C0H = 0xC0 = 144
B8H = 0xB8 = 143 and so on all the way down to 09H (0x09) which would equal 9[/edit]

I suspect you mean "base 12", although that is not correct either. There is no one symbol for 12 in base 12 (hex C). And I don't follow that last line with 0xB8. Maybe you ment 0xBF? Except in base-12, 144 would be 0x100 (base-12 in hex notation smiley-razz).

Besides I doubt base-12 is used (unless you have some special use for it). Base-8 is sometimes used (octal), but I've never really encountered that either. However all this is something else than "12 bit". 12 bit is 12 bit smiley As westfw noted, all you examples fit in 8 bits.

If this is what you ment, that is... lots of assumptions from my side here.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Besides I doubt base-12 is used (unless you have some special use for it).
It is used quite extensively in parts of North America, but, bizzarely it has two symbols for "10".  
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: