Nerd Club
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Binary and why you need it

Go down

Binary and why you need it Empty Binary and why you need it

Post  admin Thu Apr 02, 2009 11:01 am

Most PIC micros are 8-bit devices. There are 16-bit PICs and even 32-bit PICs but for now, we're going to keep things nice and simple and deal with 8-bits at a time. A bits are called a byte. (For the seriously nerdy, half a byte or four bits is often referred to as a nibble or nybble - no, honestly, it is: that's nerd-humour).

To be able to convert binary-to-decimal and back again is a useful skill, so that you can test and debug your microchip applications. If at any time your micro is misbehaving, you can get it to give the output of variable values in a whole number of ways - the obvious one is to spit data values out to an LCD. But if your micro is misbehaving, how do you know that the output values on the screen are correct? You can send data back to your PC via serial cable - but again, if the microchip isn't doing as it should, how do you know that you've received true data at the PC end, and not just some garbled nonsense?

The simplest way to output data (variable values) from your PIC micro, a byte at a time is to display the values as binary representations on the output pins.
If that sounds a bit scary, don't worry (even though it is). Let's say you want to know the value of the variable "x". To be sure that the value of "x" isn't going to get mangled as we send it to an LCD or to a PC via the serial cable, we just want to see what value "x" holds.
The easiest way to do this is to send the value to one of the output ports: PORTB=x

Now different pins on PORTB will be high/low depending on the value of X.
And this is where your leet binary skills come in. Using binary, you can decode the outputs to decipher which value has been sent to the output pins.
It's actually rather simple.

Let's imagine we're trying to convert the decimal value 78 into decimal.
From right-to-left, write the following numbers on a sheet of paper: 1, 2, 4, 8, 16, 32, 64, 128
Under the highest number (128) write a 1 or a 0 if this value is equal to or less than your number
(i.e. in this case, 128 is NOT less than our number 78, so we write zero).
Under the next number (64) write a 1 or a 0 if this value is equal to or less than your number
(i.e. in this case 64 IS less than our number, so we write a one).
Subtract 64 from your number (78-64=14)
Repeat for each number in the list.
So for example, under 32 we would write zero.
Under 16 we would write zero.
Under 8 we would write one (8 is less than 14) and subtract 8 from our number (leaving 6)
Under 4 we would write one (4 is less than 6) and subtract 4 from our number (leaving 2)
Under 2 we would write one (and after subtracting two from two we're left with zero but need to carry on)
Under 1 we would write zero (1 is NOT less than zero)

Now read the ones and zeros from left-to-right: 01001110

That's the binary representation of the number 78!

admin
Admin

Posts : 11
Join date : 2009-03-23
Location : Brighton, UK

http://www.nerdclub.co.uk

Back to top Go down

Binary and why you need it Empty Converting binary to decimal

Post  admin Thu Apr 02, 2009 11:05 am

Converting binary to decimal is the complete reverse of what we've just discussed.
First off, make sure that your binary number has 8 digits (i.e. is an 8-bit number).
If it has fewer than 8 bits, add some zeros to the beginning.

Starting from right-to-left, write the following numbers above each digit in the binary value: 1, 2, 4, 8, 16, 32, 64, 128
(it often helps to write your binary digits out r e a l l y w e l l s p a c e d o u t)

Add up all the numbers that have a one underneath them.
For example, binary 01010101 becomes:
64+16+4+1 = 85

That's all there is to it!

admin
Admin

Posts : 11
Join date : 2009-03-23
Location : Brighton, UK

http://www.nerdclub.co.uk

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum