Lecture: | 1 |
Next lecture | Download and install MPLab X with XC8
|
Objective | Embedded development process, numbering systems
|
Overview
Embedded systems is about the study of embedding computers into dedicated
applications. The goal this semester is to get you to a point where you
can design these systems.
A quick look around will reveal a tremendous number of
embedded processors in your environment. The computational requirements
vary from the very simple to the very sophisticated.
Consequently there are a range of processors available; their computational
capability is often characterized by the word-size of the processor. The
word-size is the maximum number of bits a processor operation can complete
in a single instruction. Generally, 8-bit processors are reserved for
simple tasks, 16-bit for signal processing applications, and 32-bit
reserved for general purpose and high performance applications. The
PIC processor has a word-size of 8-bit.
We will be working with the Microchops PIC 18F26K22 8-bit microcontroller.
You will write assembly and C code on a PC using MPLab X and download the
object code to the processor which resides on your development board. I have
decided to forego a textbook in favor of the orginal source technical
documents for the PIC 18F26K22 Data Sheet and the XC8 compiler User's
Guide. The links for both documents are posted on the class main page.
Do not bother to print these documents out as they consist of over a
1000 pages, most of which you will not need. However, please download
and store a copy on your local drive as you may need to consult them
during the term when the network is unavailable.
Codes
Microcontrollers encode all sorts of information using bits.
It's important to realize that
bits have no meaning in themselves,
we interpret them according to some set of rules. Throughout the
term, we will primarily use the following codes.
- Decimal
- Unsigned binary
- Signed binary
- Hexadecimal
- ASCII - http://www.asciitable.com/
To gain a better understanding of these codes, let's complete the
following exercise.
Conversions
Perform the following conversions assuming a 4-bit word size.
Binary | hex | decimal | representation
|
0b1010 |   |   | unsigned
|
0b1010 |   |   | signed
|
  | 0xC |   | unsigned
|
  | 0xC |   | signed
|
  |   | -3 | signed
|
  |   | 6 | unsigned
|
The American Standard Code for Information Interchange (ASCII)
represents character codes needed to present text on terminal
devices like computer monitors. There is no expectation that
you will memorize the ASCII table, so find one you like, like
that in the URL posted in the list above, to perform the following
conversion.
ASCII | hex | decimal
|
'3' |   |  
|
  | 0x20 |  
|
  |   | 88
|
Data Types
The C programming language allows you to represent values using
data types with different numbers of bits. A guiding principle
in Embedded Programming is to
use the smallest data type
sufficient to meet the needs of the problem being solved.
Representation | Width | Min | Max | Alias
|
unsigned | 8-bit | 0 | 255 | uint8_t
|
signed | 8-bit | -128 | 127 | int8_t
|
unsigned | 16-bit | 0 | 216-1 | uint16_t
|
signed | 16-bit | -215 | 215-1 | int16_t
|
unsigned | 32-bit | 0 | 232-1 | uint32_t
|
signed | 32-bit | -231 | 231-1 | int32_t
|
Take a look at Table 5.3 and 5.4 in the XC8 Compiler Manual for the
official C designations for these data types. We will stick with the
alias' in the right-most columns.
You will notice that we did not include floating
point values in the above list.
You are forbidden to use
floating point types in this class. Every lab is structured
so that there is no need for floating point.
Radix
The term "radix" is synonymous with "base" and denotes the number of
symbols used to represent quantities. We will primarily work with
three different radix in our course: binary, decimal and hexadecimal.
The C programming language provides prefixes that allows you
to describe constant values in different bases. The following three
lines of C-code describe three different variable declarations in
these three different radix.
uint8_t binVar = 0b10101100;
uint8_t decVar = 172;
uint8_t hexVar = 0xAC;
You must realize that the radix is only a convenience for humans; the
PIC will store all three of binVar, decVar and hexVar in binary
inside the machine's memory using identical binary representations
because all three represent the same quantity.
While not strictly a radix, you can initialize variable using ASCII
equivalent characters surrounded by single quotes. For example, the
following is a legal variable declaration.
char asciiVar = 'X';
The result of this declaration would be to initialize asciiVar with
the ascii code for the letter 'X' which is decimal 88. I could have
legally made the declaration
uint8_t asciiVar = 'X';, but this
declaration would not have conveyed the intention of the
declaration, that asciiVar is being used to store character information not
a numerical value.
Test your understanding
You can find the solutions embedded in the "source code" for this
web page by right mouse clicking on this web page and selecting
"view source". The solutions are in HTML comments.
- Complete the following table assuming an 8-bit word size.
Binary | Hex | Decimal | Representation
|
0b10010011 |   |   | unsigned
|
0b10010011 |   |   | signed
|
  | 0x85 |   | unsigned
|
  | 0x85 |   | signed
|
  |   | 102 | unsigned
|
  |   | -102 | signed
|
- Determine the ASCII codes for each letter in "Blaster".
Represent the ASCII code in decimal.
- For each of the following, determine the best C data type for
a variable which needs to describe an element of the Domain. For
example, to describe the age of a person, you would first need to
determine the range of ages for people. A quick search reveals the
oldest person was 122 years-old. So the smallest data type
which can accommodate this range is "unsigned char", which can
represent values in the range 0-255. We only use signed types when
we need to represent negative values.
Variable domain | Data type
|
Age of a person (years) | uint8_t
|
Numbered day of the year |  
|
Weight of a human (pounds) |  
|
Temperature in Golden Co (Celsius) |  
|
Number of gerbils in a pet store |  
|
Elevation of a location in Continental US (feet) |  
|
-
Interpret the following 4-bit values for A as integers (unsigned) and 4-bit
2's complement values (signed).
A | unsigned | signed
|
0011 |   |  
|
1010 |   |  
|
1111 |   |  
|
0110 |   |  
|
1100 |   |  
|
0010 |   |  
|
1010 |   |  
|
1001 |   |  
|
0100 |   |  
|
0111 |   |  
|
0000 |   |  
|
1000 |   |  
|
0101 |   |  
|
1101 |   |  
|
1011 |   |  
|
0001 |   |  
|
1110 |   |  
|
- Decode the following secret message by interperting the
8-bit binary values as ascii characters.
0b01000100
0b01110010
0b01101001
0b01101110
0b01101011
0b00100000
0b01111001
0b01101111
0b01110101
0b01110010
0b00100000
0b01001111
0b01110110
0b01100001
0b01101100
0b01110100
0b01101001
0b01101110
0b01100101
0b00100001