Professional Documents
Culture Documents
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.
3. There are a couple of different way that you can indicate that a number is in binary instead of
decimal:
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.
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:
main()
{
WDTCTL = DEVELOPMENT; // Disable watchdog timer for development
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.
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.
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.)
21. If you cannot see the Variables window pane at all, you can open it at any time from the View
menu.
Yes, CCS did “skip” over the variable definition statement, but that is ok for now.
In addition, we can see that the variable count has been updated to show its correct value (0).
26. Now, the value of count will be shown as a bunch (32 to be exact) of zeroes.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.