00 - 255
There are three
ways to enter numbers into a program. Binary, Decimal and Hexadecimal.
When you are working at the lowest level (called Machine Code), you are writing
instructions the microcontroller can understand.
The most convenient number systems at this level are BINARY and HEXADECIMAL.
Once you know how they work, you will find they match with the 8 bits in each
file and corresponds to the input and output lines.
You don't have to know very much about the systems. You don't even need to know
how to add two hex numbers together (this is very
difficult). You only need to know how to count to fifteen.
It all starts with the bits in a file. There are 8 bits. If they are presented
as 8 empty boxes:
each box can be
filled with "0" or "1."
The easiest way to represent this is: 0000 0000 or 1111 1111
Any combination of 0's or 1's can be used.
As with normal numbers, the lowest number is to the right: 0000
0001 and since the only values allowed in the "boxes are 0 or
1, the numbers increment: 0000 0010 0000
0011 0000 0100
0000 0101 0000 0110 0000
0111 0000 1000 0000
1001 0000 1010 0000
0000 1100 0000 1101 0000
1110 0000 1111 etc.
This is called BINARY and is exactly the way the value in a file increments.
When all the "boxes" are filled, (1111 1111) the file will contain
But don't worry about the 255 at the moment. Simply look at the numbers
such as 1010 1001 or 1100 1110 and
give an instant answer to the value of each. It's very difficult. Our mind does
not work in 1's and 0's.
To make it easy, clever programmers have devised a simple way to read a group
of four binary values. They have given it a single digit from 0 to 9 and then
the letters A, B, C, D, E and F.
This produces two digits to cover all the values from 00 to 255.
There are only two more points we have to cover and you know all about Binary
The first is the
Binary to Hex table:
other thing is the value of each "bit" or "line."
Each bit can have a LED connected to it and to be able to illuminate an
individual LED, its value must be known. Similarly, if you want to illuminate
two or three LEDs or globes, or relays at the same time, you need to know how
to combine (add up) the value of each line to provide a number for the
instruction in a program.
The value of each LED is shown on the PIC LAB-1 PC board as:
From the table
above you can see the lowest LED has the value 01, the next LED has the value
02, the next has the value 04, then 08. The next set of four LEDs have the same
values but since they represent the left-hand digit, the values are 10, 20, 40
This allows two or more values to be combined into a 2-digit answer.
Take the four lower LEDs for example: Go to the table to see the value of the
two lowest LEDs. The answer is 0000 0011 = 03 All the four lower
LEDs = 0000 1111 = 0F The three top LEDs =
1110 0000 = E0. The four middle LEDs = 0011 1100 = 3C.
This is how you write a value in the program to turn on a particular set of
Any of the 255 combinations of LEDs can be presented in this way.
HOW DOES IT WORK?
1. Putting a value in a file:
You can put any number from 00 to 255 into a file. Numbers (values) are used
for a number of purposes. It may be to output onto a display or for
decrementing a file.
You can think of numbers such as 50, 100, 200 etc but in computer terms you generally
think in terms of a full file or half-filled file etc.
This brings you around to 0FFh for a full file or 80h for a half-filled file.
If you want 75 for a loop sub-routine, go to the converter above and get
4Bh as the value for loading into a file (via W).
2. Outputting to a display:
If you are creating a display on a set of LEDs or 7-segment display, you will
need to know the LEDs to be illuminated and the value of each LED.
Here are some examples:
Under each LED, write 0 for not illuminated and 1 for illuminated. Go to
the table above and write down the value for the first 4 LEDs. Do the same for
the other 4 LEDs. This is the Hex value for the output.
The same applies for a 7-segment display:
When 5Bh is output to the 7-segment display, the figure "2"
appears on it.
The 7-segment display is also capable of producing almost all the letters of
the alphabet as well as many other characters and effects. See Expt
4c for letters on the display.
Here are the values to produce the numbers 0 to 9:
When writing a program, Hex numbers are loaded into W and moved
from W to a file. In the comments section, the binary value is shown so that
you can see how the Hex number is generated.
For instance, in the instruction below, the value 0011 1111 is loaded into W to
turn on segments of the 7-segment display to create the number
"0." This is the value 3Fh. The programmer produces the binary
number first in the comments section then converts the value to hex for the
;Load W with 0011 1111
;Output "0" on 7-segment display
ADDING HEX NUMBERS
Adding Hex numbers is very difficult. The easiest and quickest way to add
Hex numbers is to convert to decimal by using the converter above, add the
numbers conventionally, then convert to a Hex value.
If you need to increment a hex value or add a small number to a hex value
(such as for an increment function) you can go to the Hex table above and
increment down the table.
1. What is the hex number to turn ON the lowest LED on the display?
2. What is the hex number to turn
ON the 4 lowest LEDs?
3. What is the hex number to turn ON the top LED?
4. What is the hex number to turn
ON all the LEDs?
5. What is the hex number to turn ON the highest and lowest LEDs?
6. What LEDs will turn ON: 0011 1100
7. What LEDs will turn ON: 1100 0011 and what is the Hex
8. What LEDs will turn ON: 1010 1010 and what is the Hex
9. What LEDs will turn ON: 0000 0000 and what is the Hex
10. What LEDs will turn ON: 1111 1111 and what is the Hex