MicroController Applications

Download Report

Transcript MicroController Applications

Computer Math
The Decimal System
How do we represent “One Hundred and
Twenty Five”?
 Simple: 125 !!!


100s
10s
1s
1
2
5
 Which digit is “More Significant”? Why?
 Answer: the left most digit

How Decimal System Works
This may look simple but please pay
attention!!!
 Decimal is based on ten digits from 0 ~ 9
 Every time there is an overrun from 0 to 9,
the digit to the left of the overrun is
increased by a value of 1.
 Very simple for most people.

The Binary System
Based on only 2 digits : ‘0’ and ‘1’
 Same idea as decimal.
 Since there are only two digits (symbols),
the overrun comes very quickly.
 E.g. zero is ‘0’, one is ‘1’. After ‘one’ is
‘two’ but you must write it as ‘1 0’ (see next
pg).

0
1
10
11
100
101
zero
one
two
three
four
five


How do we represent the number “twelve”
in binary?
8
4
2
1
1 1 0 0
 The number on top is the ‘weight’ of the
digit, so you can see that as you move to the
left, the ‘weight’ is multiplied by 2, thus
giving rise to the name bi-nary.
 We can see that to represent the number
‘twelve’ requires four binary digits.

Sometimes, instead of saying ‘digits’ we say
‘bits’ thus 1100 is a 4-bit binary number and
has a value of 12 (decimal).
 How many numbers can we represent if we
have 8 bits?
 How many bits do we need if we only want
to represent numbers ranging from 0 to 15?

Review Questions.
 Where does the word ‘binary’ come from?


How many symbols are used in a binary system?

Where does the word ‘decimal’ come from?

How many symbols are used in a decimal system?
Write the representation of two hundred thirty
three in binary.
Ans: 11101001


Adding and Subtracting Binary
Numbers
Adding the numbers is easy as long as we
have enough number of bits to represent the
number. What do we mean by that?
 E.g. For a 4-bit system, the biggest number
is 15 so as long as the result does not
exceed 15 the system works fine.

Lets add 5 to 3
0 1 0 1
0 0 1 1
1 0 0 0
0+0=0
0+1=1
1 + 1 = 0 (carry 1)

+
.
8
However if you add 8 to 8

For a 4-bit system you will have a problem.
1 0 0 0
1
1 0 0 0 +
0 0 0 0 !
There is a carry out from the left which
cannot be placed into the answer because
we only have 4 bits!

Of course the solution is to have more bits
but every number of bits has a limit to the
biggest number it can store.
2’s Complement
Take a binary number, change all ‘1’ to ‘0’
and all ‘0’ to ‘1’ and you have the 1’s
complement.
 Then add ‘1’ to the right most bit and you
get the 2’s complement.

Example : 01001001
0 1 0 0 1 0 0
1 0 1 1 0 1 1
+
1 0 1 1 0 1 1


Simple right?
1
0
1
1
original no.
1’s comp.
2’s comp.
Subtraction of Binary Numbers
This is a bit tricky and you need to know
how to represent signed numbers using 2’s
complement.
 Lets say we have a number ‘12’ in 8-bit
format.
 The binary representation is 00001100
 To say that this is +ve 12, we simply use the
left most bit (which is ‘0’) for that purpose.

0 0 0 0 1 1 0 0
12
sign
Which means that now, there are only seven
bits representing the number since one of
the bits is now used to indicate the sign.

To represent a negative 12, get the positive
12 first,
0 0 0 0 1 1 0 0
Perform 2’s complement on it and we get:
1 1 1 1 0 0 1 1
Add ‘1’ to it and we get
1 1 1 1 0 0 1 1
+
1
1 1 1 1 0 1 0 0
11110100 is (–12) in 2’s comp form.
The left most bit is a ‘1’ and is used to
indicate that the number is a negative
number.
 11110100 is (–12) in 2’s comp form.

Sign
Similarly, only 7 bits are now used to represent
the rest of the number.
There is another method using
the ‘true value’
Get the positive number 00001100
 Assign the sign bit
10001100
 That is what we call the ‘true value’ for –12
 Leave the sign alone and invert the rest
we get
11110011
 Add a ‘1’ to it
1
 We get the 2’s comp.
11110100

How to subtract?
We know that 24-12 = 12
 How do we prove this in binary?
 The binary of 24 is 00011000
 The binary of 12 is 00001100
 Simply take the binary (24 signed) and add
it to binary (-12 signed).

24 is
 +24 is still
 12 is
 -12 is

00011000
00011000
00001100
11110100
By adding them, we get:
carry
We get the answer 12!
1
00011000
11110100
00001100
Hexadecimal System
Now, lets come to the main reason for all
the previous notes.
 In MicroControllers, we work a lot with
machine codes or machine language, which
is basically binary codes in nature. But as
human beings, we are not really
comfortable working with binary.











To take an intermediate view, let’s look at how we
can use the hexa number system to help us.
Binary ----------------------Base 2
Decimal --------------------Base 10
Hexa ------------------------Base 16
So we start from 0 ~ 9,A,B,C,D,E,F
‘A’ has a value of 10
‘B’ has a value of 11
‘F’ has a value of 15
Altogether 0 ~ F there are 16 symbols.
Thus Base-16.
What’s the advantage?
Remember, how many bits do we need to
represent a number from 0 to 15 (slide 6)
 Yes, 4 bits.
 Microprocessors/Microcontrollers often
work in 8 bits, 16 bits, 32 bits which are
simply groups of 4 bits.

So, to make things simple
Instead of writing 11000110 which means
198 in decimal, we could split it up into
1100 0110 (2 sets of 4 bits)
And represent it using hexadecimal, which is
written as
C 6
Remember 1100 has a value of 12 whereas
0110 has a value of 6.


Hexadecimal number is usually written with
a subscript 16 e.g.
C616 to indicate base-16
Or sometimes with a H e.g.
C6H to indicate hexa
Or sometimes 0xC6
These are all different ways to represent
hexadecimal numbers.
Another look at 2’s Complement
What is the biggest hex number that can be
represented using 8 binary bits?
 Ans: FF i.e. 1 1 1 1 1 1 1 1
 If you have only 8 bits, what is the number
after FF?
 Ans: 00

0000 0000
1111 1111
1111 1110
1111 1101
00
FF
FE
FD
0000 0001
0000 0000
01
00
Which means that after FF, the whole cycle
repeats itself.
 If we consider decimal numbers and only
limited to 2 digits, we would also encounter
the same problem, i.e. from 0 ~ 99 then
back to 0 again.
 In dealing with signed numbers, what
number comes before zero?
 Ans: -1! i.e. negative one.

How do we make the systems
consistent?
So somehow if after FF comes 00, then FF
must have a value of –1 in signed
arithmetic!
 What happens if we treat FF as a 2’s
complement number?
 What number does it represent?

To do that,
FF is 1111 1111
 The leftmost ‘1’ is the sign indicator.
 To find the true value using the reverse
process, first keep the sign bit and reverse
all the rest of the bits.
 We first get 1000 0000
 Add one to the number


Ans:
10000000
1
10000001
This is the true value for ‘negative one’
That means FF is –1 if we consider signed
arithmetic. Does it make sense?
Pls do this: Prove that FE is –2.