You are on page 1of 21

Introduction to Binary Numbers

1. Since microcontrollers only use binary numbers, it is important to know how these numbers work
when writing programs for your microcontroller.

Let’s begin with the number system that you are used to. It is called decimal because we count in
base 10. Base 10 means that we have ten different digits that we can use to count:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

So when we count, we start off counting from 0 to 9. However, after 9, we have run out of digits.
When this happens, we put a 1 in the tens place and a 0 in the ones place to get the number 10.
Then, we keep the 1 in the tens place and change the number in the ones place to count from 10 to
19, and the pattern keeps repeating.

2. Binary numbers count in the same pattern, except there are only two digits that we can use to
count: 0 and 1. Since there are only two digits that we can use, we say that binary is base 2.

If we wanted to count up to decimal 5 in binary, we would start by counting from 0 to 1. Then,


since we run out of digits, we put a 1 in front and start the other digit at 0 again to give us 10,
which is equal to 2 in decimal. Then, we count up to 11 (3 in decimal). Next, we run out of
digits to use, so we put another 1 in front and make the rest of the digits: 100 (4 in decimal).
Finally, to get the decimal number 5, we count up one more number to get 101.

Binary Number Decimal Equivalent


0 0
1 1
10 2
11 3
100 4
101 5

3. There are a couple of different way that you can indicate that a number is in binary instead of
decimal:

a) Use a suffix of B: 1010B


b) Use a suffix of b: 1010b
c) Use a suffix of 2: 10102

4. Each of these are equally valid ways of denoting a binary number. “B” and “b” stand for binary,
and the 2 stands for base 2. However, in this course, we will be using the first option.

5. From now on, if we do not use any suffix, we (and CCS) will always interpret a number to be
decimal.

Copyright © 2012-2015 Page 1 of 21


Valparaiso University
6. The following table shows how to count to decimal 16 in binary. By taking a careful look at the
binary column, you may begin to grow more comfortable with the counting pattern that the binary
numbers follow:

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000

7. Now, let’s use Code Composer Studio (CCS) to watch as we count numbers in binary. First,
create a new project called Counting. (Instructions for creating projects can be found in the
Section 1 handout, Let’s Get Started.

Copy and paste the following program into the main.c file with the CCS Editor interface:

// Program to look at counting in binary and learn new features in CCS

#include <msp430.h> // Used to make code easier to read

#define DEVELOPMENT 0x5A80 // Used to disable watchdog timer for development

main()
{
WDTCTL = DEVELOPMENT; // Disable watchdog timer for development

long count = 0; // Create variable named count and set equal to 0

while(count<20) // Keep going until count is 20


{
count = count + 1; // Add 1 to variable count
}

while(1); // After counting, stay here forever

Copyright © 2012-2015 Page 2 of 21


Valparaiso University
8. At the beginning of this program we have a #include and a #define statement. As in the Let’s
Get Started handout, these will make our program easier to read.

9. Next, we enter the main() function of our program. This contains the actual instructions the
microcontroller will perform.

10. The first line inside of main() is used to disable one of the security features of the microcontroller,
the watchdog timer. We will use this line of code at the beginning of most of programs for the
first part of the course. We will go into more detail about the watchdog timer in a later section.

WDTCTL = DEVELOPMENT;

11. Next, we create a variable named count and set it equal to 0. This is the variable that we will use
to “count” in binary.

long count = 0;

12. After creating the variable count, we use it in a while loop. This loop makes sure that we keep
running the code inside of its curly braces over-and-over again until count is equal to 20 (decimal,
that is).

while(count<20)
{
count = count + 1; // Add 1 to variable count until it reaches 20
}

13. Finally, after we have incremented count 20 times, the program continues to the while(1);
instruction.

We will see shortly that this acts as an infinite loop to effectively halt the program after it
increments count to 20.

14. Over the past 25 years, we have found that it may occasionally be frustrating to some students to
use instructions like while that they have not learned a lot about yet. However, there is real value
to be learning about the microcontroller, the C programming language, CCS, and binary numbers
all at the same time. Please be patient, and know that Section 3 provides a much deeper
explanation of the various loops available in the C programming language. If you really want to
learn about them know, go ahead and take a look. We will be here when you get back – we
promise.

Copyright © 2012-2015 Page 3 of 21


Valparaiso University
15. Save and Build your new program. Once the project is done building, go ahead and launch the
CCS Debugger.

16. Before we start using the Debugger, we want to point out a new feature we will be using.

17. In the Let’s Get Started handout, we would start out program by pushing the Resume (or play)
button.

18. Instead of just letting the program run, we are going to step line-by-line through our program.
This will let us actually watch as the microcontroller increments the variable count.

The short-cut button for single-stepping (executing a single line of C code) is called Step Into
and is shown below.

Copyright © 2012-2015 Page 4 of 21


Valparaiso University
19. Take a look at the main.c window pane in the Debugger.

Notice how in the screen shot below, line 9 is highlighted and has a small blue arrow in front of
it? This indicates that this line of code will be the next instruction performed. (The #include
and #define statements are both taken care of before the program actually starts.)

Copyright © 2012-2015 Page 5 of 21


Valparaiso University
20. Also, take a look at the Variables window pane. The variable count is ready, but it does not
have the correct (0) value yet. If you do not see the variable, make sure you click the
Variables tab instead of Expressions or Registers tabs.

21. If you cannot see the Variables window pane at all, you can open it at any time from the View
menu.

Copyright © 2012-2015 Page 6 of 21


Valparaiso University
22. Click on the Step Into button one time to perform the first instruction.

23. The while(count<20) instruction should now be highlighted.

Yes, CCS did “skip” over the variable definition statement, but that is ok for now.

Copyright © 2012-2015 Page 7 of 21


Valparaiso University
24. Click on the Step Into button again. Now, the next line of code should be highlighted.

In addition, we can see that the variable count has been updated to show its correct value (0).

Copyright © 2012-2015 Page 8 of 21


Valparaiso University
25. Right click on the count value 0 and a pop-menu window will appear. Select Number Format
and then click on Binary.

26. Now, the value of count will be shown as a bunch (32 to be exact) of zeroes.

Copyright © 2012-2015 Page 9 of 21


Valparaiso University
27. Note, if the value of count cannot be display in the space available, you may see something like
this:

28. To increase the value cell width, point your mouse at the edge of the value cell. The cell width
tool will appear (see below). Left-click with this tool and you can make the cell wider or
narrower.

29. Ok, we are getting ready to click the Step Into button again. Remember, this will execute the
instruction that increments the count variable.

Copyright © 2012-2015 Page 10 of 21


Valparaiso University
30. Go ahead and click Step Into. The value of count has been incremented from 0 to 1.

Also note that the next instruction to be performed is incrementing count again. This makes
sense since our program is supposed to do this until count reaches 20.

31. Click the Step Into a couple more times. You will see the value of the count variable
continues to increase.

Copyright © 2012-2015 Page 11 of 21


Valparaiso University
32. When count is equal to 101, right click on the value, and select Number Format and Decimal
from the pop-up menus.

33. The value of count will now be display again in base 10. As expected, the binary value we saw
(101) has a decimal equivalent of 5.

34. If at any time you make a mistake and want to restart the program and the counting process, click
the Soft Reset button.

This will effectively start your program over.

Copyright © 2012-2015 Page 12 of 21


Valparaiso University
35. While count is 5, switch the display back to binary again by right clicking on the value and
selecting Number Format and Binary from the pop-up menus.

36. Try clicking Step Into several more times and notice how count continue to increment toward
a decimal value of 20.

For your convenience, we are repeating here the decimal-binary equivalent table for you to follow
as you go.

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000

Copyright © 2012-2015 Page 13 of 21


Valparaiso University
37. Click on the Step Into button until count has a binary value of 10011. (Note, be patient –
CCS will not allow you to click too quickly.)

Switch the display back to decimal to verify the decimal equivalent is 19.

38. If you click Step Into one more time, you will see that the value of count is incremented to 20.
This ends the while(count<20) loop, and the Debugger shows you that the program has moved
on to the next instruction in the program.

Copyright © 2012-2015 Page 14 of 21


Valparaiso University
39. If you continue clicking the Step Into button, you can see that the program stays at the
while(1); loop. If you would like to try this incrementing process again, remember at any time
you can click Soft Reset to restart your program.

40. When you are satisfied, click on Terminate to go back to the CCS Editor.

41. Now we know how to count in binary, but what if we want to know what a certain decimal
number is equal to in binary? We don’t want to have to count all the way up to that number
because that could get very tedious. Instead, we can use a relatively simple process to convert
between binary and decimal numbers.

42. To convert a decimal number into binary, use the Short Division by Two with Remainder method.
While other ways exist, this method relies only on division-by-two and is the simplest for
beginners to use.

43. Imagine that you wanted to convert the decimal number 117 to binary.

First, you write 117 as the dividend inside an upside-down "long division" symbol. Then write 2
as the divisor. Write the word Remainder above and to the right.

Copyright © 2012-2015 Page 15 of 21


Valparaiso University
44. 117  2 is 58 with a remainder of 1.

45. Next, we are going to divide 58 by 2.

46. 58  2 is 29 with a remainder of 0.

Copyright © 2012-2015 Page 16 of 21


Valparaiso University
47. Next, divide 29  2 is 14 with a remainder of 1.

48. Continue this process of dividing by 2 and noting the remainder.

14  2 is 7 with a remainder of 0.
7  2 is 3 with a remainder of 1.
3  2 is 1 with a remainder of 1.
1  2 is 0 with a remainder of 1.

Copyright © 2012-2015 Page 17 of 21


Valparaiso University
49. Ok, you are almost done. Right the remainders from the bottom to the top: 1110101.

Congratulations! The binary equivalent of 117 decimal is 1110101.

Copyright © 2012-2015 Page 18 of 21


Valparaiso University
50. Now, let us convert a number from binary to decimal.

To do this, we will use the Position Notation Method. This method is very powerful and is used
by most designers when working with binary numbers. It makes use of the powers of 2:

20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256…

51. Each digit (or bit) in a binary number has a decimal equivalent of a power of 2.

The least-significant bit (all the way on the right) has a value of 20 or 1.
The next bit (second on the right) has a value of 21 or 2.
The next bit (third on the right) has a value of 22 or 4…

52. Let’s take a look at the binary number 1B. It has a decimal equivalent of 20 or 1.

Copyright © 2012-2015 Page 19 of 21


Valparaiso University
53. Let’s take a look at the binary number 100B. It has a decimal equivalent of 22 or 4.

54. Now, we will move on to some larger binary numbers. 101B has a decimal equivalent of 22 + 20
or 5.

55. Finally, let us go back to our binary number from our original example: 1110101B. As you can
see, this has the decimal equivalent of 117.

Copyright © 2012-2015 Page 20 of 21


Valparaiso University
All tutorials and software examples included herewith are intended solely for
educational purposes. The material is provided in an “as is” condition. Any
express or implied warranties, including, but not limited to the implied warranties
of merchantability and fitness for particular purposes are disclaimed.

The software examples are self-contained low-level programs that typically


demonstrate a single peripheral function or device feature in a highly concise
manner. Therefore, the code may rely on the device's power-on default register
values and settings such as the clock configuration and care must be taken when
combining code from several examples to avoid potential side effects.
Additionally, the tutorials and software examples should not be considered for use
in life support devices or systems or mission critical devices or systems.

In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.

Copyright © 2012-2015 Page 21 of 21


Valparaiso University

You might also like