Pages: [1]   Go Down
 Author Topic: Binary Tutorial?  (Read 572 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 12
 « on: June 15, 2013, 01:32:42 am » Bigger Smaller Reset

Hello everybody I'm new to Arduino and I'm getting used to C slowly. The bulk of my programming background is in programming PIC18's in machine assembly code.

My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment? Is it even possible to interface to C using raw binary values?

At the moment I've got a row of 10 LEDs and a potentiometer. One lab i would like to try is a binary counter and another is rotating the POT and having the LEDs show the binary value of the POT's wiper.
 Logged

SE USA
Offline
Karma: 39
Posts: 3781
@ssh0le
 « Reply #1 on: June 15, 2013, 02:35:55 am » Bigger Smaller Reset

byte = 0xff;
byte = b00000000;

for starters
 Logged

Valencia, Spain
Online
Karma: 106
Posts: 3612
 « Reply #2 on: June 15, 2013, 05:06:03 am » Bigger Smaller Reset

byte = 0xff;
byte = b00000000;

for starters

 Logged

No, I don't answer questions sent in private messages...

Global Moderator
UK
Offline
Brattain Member
Karma: 193
Posts: 22145
I don't think you connected the grounds, Dave.
 « Reply #3 on: June 15, 2013, 05:50:18 am » Bigger Smaller Reset

Quote
byte = b00000000;
Close, but no oral carcinogens.
 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
Newbie
Karma: 0
Posts: 12
 « Reply #4 on: June 18, 2013, 01:04:00 am » Bigger Smaller Reset

byte = 0xff;
byte = b00000000;

for starters

That's not very helpful to me. Could you use this in a sample of code to illustrate proper use?
 Logged

SF Bay Area (USA)
Offline
Karma: 93
Posts: 5937
Strongly opinionated, but not official!
 « Reply #5 on: June 18, 2013, 01:49:30 am » Bigger Smaller Reset

Quote
My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment?

In standard C, you can cause a number to be interpreted as a hex number by preceding it with a "0x" prefix.
In most versions of gcc (which is used by Arduino) you can cause a number to be interpreted as binary by preceding it with a "0b" prefix.
In the Arduino environment, there are a set of binary one-byte constants defined using "B" as a prefix.

So, 192, B11000000, 0b11000000, and 0xC0 are all the same number.
0xDeadBeef is a valid 32bit hex constant.  0b101001000100001000001 is a fine binary number (but rather unreadable.)

Don't put leading zeros on your decimal numbers, though.  That makes them be read as octal, so 010 would be the same as 8 ! (harkening back to the original PDP11 days, I guess.)

 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 193
Posts: 22145
I don't think you connected the grounds, Dave.
 « Reply #6 on: June 18, 2013, 01:56:37 am » Bigger Smaller Reset

Quote
harkening back to the original PDP11 PDP-8 (and possibly earlier) days

Octal is still relevant today in Linux systems, for setting access privileges.
 « Last Edit: June 18, 2013, 02:06:54 am by AWOL » 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.

SF Bay Area (USA)
Offline
Karma: 93
Posts: 5937
Strongly opinionated, but not official!
 « Reply #7 on: June 18, 2013, 02:16:42 am » Bigger Smaller Reset

Quote
harkening back to the original PDP11 PDP-8 (and possibly earlier) days
Well, C was originally developed for the PDP11, wasn't it?  Octal itself is certainly much older, but I hypothesize that its inclusion in C was directly because C was aimed at PDP11 "systems programming", and the PDP11 was defined/documented/etc in octal.

(The 8080 also had a fundamentally octal instruction set, and it always pissed me off that hex was so widely using for programming it.)
 Logged

Wahiawa, Hawaii
Offline
God Member
Karma: 27
Posts: 580
 « Reply #8 on: June 18, 2013, 04:34:18 am » Bigger Smaller Reset

Hehe, I still have my S-100 bus 8085/8088 system.  I remember doing everything in asm and hex.

 Logged

Offline
Newbie
Karma: 0
Posts: 12
 « Reply #9 on: June 18, 2013, 11:47:11 am » Bigger Smaller Reset

Quote
My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment?

In standard C, you can cause a number to be interpreted as a hex number by preceding it with a "0x" prefix.
In most versions of gcc (which is used by Arduino) you can cause a number to be interpreted as binary by preceding it with a "0b" prefix.
In the Arduino environment, there are a set of binary one-byte constants defined using "B" as a prefix.

So, 192, B11000000, 0b11000000, and 0xC0 are all the same number.
0xDeadBeef is a valid 32bit hex constant.  0b101001000100001000001 is a fine binary number (but rather unreadable.)

Don't put leading zeros on your decimal numbers, though.  That makes them be read as octal, so 010 would be the same as 8 ! (harkening back to the original PDP11 days, I guess.)

Ok that explains things more clearly thank you for that explanation!
 Logged

Ayer, Massachusetts, USA
Offline
Edison Member
Karma: 43
Posts: 1580
 « Reply #10 on: June 19, 2013, 07:57:02 am » Bigger Smaller Reset

Quote
harkening back to the original PDP11 PDP-8 (and possibly earlier) days
Well, C was originally developed for the PDP11, wasn't it?  Octal itself is certainly much older, but I hypothesize that its inclusion in C was directly because C was aimed at PDP11 "systems programming", and the PDP11 was defined/documented/etc in octal.
The PDP-11 had 8 registers and the instruction set tended to have registers at octal boundaries, so octal was a natural fit for the 11.  The left over bit in the 16-bit instruction word often times was used to indicate whether the instruction operated on 16-bit words or 8-bit characters.  I spent the summer of 1978 doing PDP-11 standalone/simulator code, and had to delve into the instruction set, including toggling in the boot code at the front panel with 3 finger octal.

Note, that before switching to first the PDP-7 and later the PDP-11, Ritchie and Thompson had been part of the Multics project which was on a 36-bit machine, where octal is more natural.  Bell Labs withdrew from the Multics project and Ritchie/Thompson and others started working on an underused PDP-7 and C/UNIX were begun.
 Logged

 Pages: [1]   Go Up