BITS, BYTES, ETC
POSITIVE LOGIC Binary Number Voltage
0 0 volts (approximately) 1 5 volts (approximately)
in a 5 volt system
The exact voltage ranges which represent 0 and 1 vary depending on the logic supply voltage and the integrated circuit logic chip family used (TTL, CMOS, etc.).
The choice of using binary 0 to represent 0 volts is arbitrary. Positive logic is shown above. It can be done the opposite way which is called negative logic.
NIBBLE
A nibble consists of 4 bits and can represent 16 possible states. A nibble is typically the upper or lower half of a byte (most significant or least signi (leant nibble).
BYTE
A byte consists of 8 bits and is said to be 8 bits wide. 8-bit microcontrollers move bytes around on an 8-bit data buss (8 conductors wide).
BINARY
A binary' number with more than one bit can represent numbers larger than " I". How much larg
er depends on the number of bits. An 8-bit binary number (byte) can represent 256 possible numbers (0 to 255). A 16-bit binary number can represent numbers from 0 to 65,535. If we use a byte to transmit information, we can transmit 256 possible combinations, enough to represent
Binary numbers arc based on powers of 2. The value of bit 0 is 2° = 1 if it contains a 1, or 0 if it contains 0. The value of bit 1 is 21 = 2 if it contains a 1, or 0 if it contains 0. The value of bit 3 is 23 = 8 if it contains a 1, or 0 if it contains 0, and so on.
For a 16-bit binary number, bit 0 is the least significant bit, and bit 15 is the most significant bit.
The following table shows the value of each bit position if it contains a " I
Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Most Significant Least Significant
Bit Value
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384 15 32768
The value of a binary number contained in a 16-bit timer/counter would be determined by multiplying the contents of each bit by the value of each bit.
1 0 0 1 1 0 0 1 0 0 1 0 0 01 1 1 Bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
L
L37
Counting up in binary goes like this: count in a binary count up example program.
PIC on-board timer/counters count in binary.
A binary number may be used to represent byte-wide bit patterns sent to output ports.
HEXADECIMAL
Binary numbers which are two bytes long are difficult to recognize, remember and write without errors, so the hexadecimal numbering system is sometimes used instead. Think of hex as a kind of shorthand notation to make life easier rather than some kind of terrible math.
Hexadecimal Binary Decimal represent bytes of data. Using hex is not difficult. All you need is a little practice.
One byte requires two hex digits. Note that the bits representing a byte are sometimes shown in groups of four. Note that the most significant hex digit is on the left.
To summarize:
Nibbles
Bytes
Two Bytes
1 Binary
1 Hex
1 1 Decimal 1 i i
0000
1 t 0
t 1 0
0001 1 1
0101 5 5
1111 F 15
0000 0000 00 0
0000 0001 01 1
0000 1111 OF 15
0001 0000 10 16
1111 1111 FF 255
0000 0000 0000 0000 0000 0 0000 0000 0000 0001 0001 1 0000 0001 0000 0000 0100 256 1111 1111 1111 1111 FFFF 65535
Hexadecimal OxFFFF (the very top of the program memory address space in some microcon
trollers) is much easier to write or remember than either 1111 1111 1111 1111 or 65535.
39
CONSTANTS
A literal constant may be defined as part of a built-in function such as:
output_b (OxOf); //defines hex byte OxOf and sends bit pattern // to port B
A symbolic constant may be defined using the key word const which is a modifier that can be applied to any numeric declaration.
const int LEVEL = 1 0 ; // defines integer type constant named // level with a value of 10
After the symbolic constant is defined, it is referred to by name in the program.
The #define pre-processor directive may be used for defining constants.
#define MAX_SAMPLES 32
This method for defining constants is probably used more often than the key word const method.
A convention in C is to use upper case letters in all constant names.
Constants are stored in program memory and cannot be changed during program execution.
VARIABLES
Variables are memory locations used to store data. A variable must be defined prior to use in a program by using the assignment operator.
variable = data
'---assignment operator
Variables are named according to the data type that will be stored in them, ie. an integer variable holds integer data, etc. (see next chapter, Data). The variable definition process tells the
compiler how much memory space should be allocated for the variable being defined.
A variable, as the name implies, may be changed during program execution and will be stored in a general purpose file register (RAM location) in the PIC microcontroller.
Variables are either global or local.
• Global variables are defined outside a function and can be used by any function.
• Local variables are defined inside a function (after an opening brace) and used within that function (before the corresponding closing brace).
It is considered good practice to use local variables wherever possible so you can control access to them.
If a function needs to use another function's local variable, that variable can be passed to the function that needs it. Access to local variables is controlled, a good thing.
Passing variables (arguments) will be discussed in a later chapter on the subject.
Declaration of a variable will be demonstrated in the following chapter after data types have been explained. Naming of variables is explained in the chapter after that.
DATA
Basic data types used in this book are (see note below):
Character (ASCII)
A character is a single ASCII character which may be represented by 8 bits. There are 256 ASCII characters. The
characters most commonly used in microcontroller applications are contained in a table later in this chapter.
Apostrophes indicate a character 'A' 'a' ' 1 '6'
More than one character is called a string and is designated by quotation marks " " (see Strings chapter) Bit
One bit
Called inti, sometimes called short or boolean, can be called bit with use of typedef declarator (details follow)
Can represent one of two numbers, "0" or "1"
Byte
Can represent 0 - 65535 Integer
Whole numbers No decimal point
Called int - same as int8 by default Can represent 0 - 255
Float
Real numbers
May have a decimal point Not used in this book Void
Indicates no specific type
These are called data "types" and are used in type declarations to allocate the space required in memory for data storage.
Type qualifiers may be added to further delineate these basic number types, but we won't use them.
Variables with negative values will not be discussed.
Note: The definitions of C data types depends heavily on the source of the information. The degree of inconsistency is huge! My goal is to present what is needed to get going in a simpli
fied way while still giving you a representative look at the wrorid of C. Using the CCS names (inti, int8, inti6) exclusively would simplify things, but you would not be able to read code found elsewhere. You will have some difficulty anyway, but I hope to keep it to a minimum.
The following table lists the data types used in this book plus inti 6.