First Steps
Embedded
Systems
with
ox40;
gs&0x20)
table();
02A4 A
02A6 B
02A9 C
Byte Craft Limited
421 King Street North
Waterloo, Ontario
Canada
N2J 4E4
Tel: 519-888-6911
Fax: 519-746-6751
Do Byte Craft Limited compilers
support ANSI C?
All Byte Craft compilers are ANSI compatible within the
limitations of the target hardware.
How efficient is the optimizer
compared to hand-written
assembler code?
The compiler generates object code as tight and
efficient as most hand-written assembler code.
Can I combine C code and
assembler in my programs?
You can embed assembler code within your C program,
Systems are high-performance embedded
development packages designed for serious
developers. They generate small, fast, and
efficient code. They enable the professional
developer to produce stand-alone single-chip
microcontroller applications quickly. Developers
can easily port C language applications written
for other embedded platforms to the CDS.
Features
!
The Code Development Systems support
entire families of microcontrollers.
!
The optimizing C language cross-compilers are
ANSI-compatible within hardware limitations.
!
Tight, fast and efficient code optimization
generates clean, customized applications.
!
A built-in Macro Assembler allows inline
assembly language in C source.
!
CDS generate symbol and source reference
information for C source-level debugging with
popular emulators.
!
C language support for interrupt service
routines and direct access to ports.
!
Device files for individual parts precisely
BYTE CRAFT LIMITED
421 King Street North
Waterloo, Ontario
Canada N2J 4E4
Telephone:
(519) 888-6911
FAX:
(519) 746-6751
Email:
Copyright
2.3.10 Timer..................................................................................................................................17
2.3.11 Interrupt Circuitry............................................................................................................18
2.3.12 Buses ..................................................................................................................................19
2.4 Sample Microcontroller Configurations.................................................. 19
2.4.1 Motorola MC68HC705C8 ................................................................................................19
2.4.2 National Semiconductor COP8SAA7 ............................................................................20
2.4.3 Microchip PIC16C54.........................................................................................................20
2.4.4 Microchip PIC16C74.........................................................................................................21
Table of Contents
ii
3. The Embedded Environment 23
3.1 The Embedded Difference ......................................................................23
3.2 Fabrication Techniques ..........................................................................24
3.3 Memory Addressing and Types ..............................................................24
3.3.1 RAM..................................................................................................................................... 24
3.3.2 ROM .................................................................................................................................... 25
3.3.3 PROM ................................................................................................................................. 25
3.3.4 EPROM .............................................................................................................................. 25
3.3.5 EEPROM ........................................................................................................................... 26
3.3.6 Flash Memory..................................................................................................................... 27
3.3.7 Registers .............................................................................................................................. 27
3.3.8 Scratch Pad ......................................................................................................................... 28
3.4 Interrupts .................................................................................................29
3.4.1 Interrupt Handling ............................................................................................................ 30
3.4.2 Synchronous and Asynchronous Interrupt Acknowledgement................................. 30
3.4.3 Servicing Interrupts ........................................................................................................... 31
3.4.4 Interrupt Detection ........................................................................................................... 32
3.4.5 Executing Interrupt Handlers.......................................................................................... 33
3.4.6 Multiple Interrupts ............................................................................................................ 34
4.7 Instruction Sets........................................................................................ 47
4.8 The Development of Programming Languages ..................................... 48
4.9 Compilers ................................................................................................ 50
4.9.1 The Preprocessor ...............................................................................................................50
4.9.2 The Compiler......................................................................................................................50
4.9.3 The Linker ...........................................................................................................................50
4.10 Cross Development................................................................................ 51
4.10.1 Cross compiler..................................................................................................................51
4.10.2 Cross development tools ................................................................................................51
4.10.3 Embedded Development Cycle.....................................................................................52
5. First Look at a C Program 55
5.1 Program Comments................................................................................. 56
5.2 Preprocessor directives............................................................................ 56
5.3 C Functions ............................................................................................. 58
5.3.1 The main( ) function..........................................................................................................58
5.3.2 Calling a Function ..............................................................................................................59
Table of Contents
iv
5.4 The Function Body..................................................................................60
5.4.1 The Assignment Statement .............................................................................................. 60
5.4.2 Control statements ............................................................................................................ 60
5.4.3 Calling Functions ............................................................................................................... 62
5.5 The Embedded Difference......................................................................62
5.5.1 Device Knowledge ............................................................................................................ 63
5.5.2 Special Data Types and Data Access.............................................................................. 63
5.5.3 Program Flow..................................................................................................................... 63
5.5.4 Combining C and Assembly Language .......................................................................... 63
5.5.5 Mechanical Knowledge..................................................................................................... 64
6. C Program Structure 65
7.6 Integer Data Types.................................................................................. 78
7.6.1 Integer Sign Bit...................................................................................................................78
7.6.2 The short Data Type .........................................................................................................78
7.6.3 The long Data type ............................................................................................................79
7.6.4 Different Notations ...........................................................................................................79
7.7 Data Type Modifiers ............................................................................... 79
7.7.1 Signed and Unsigned .........................................................................................................80
7.7.2 Other Data Type Modifiers..............................................................................................80
7.8 Real Numbers ......................................................................................... 80
7.8.1 The float Data Type...........................................................................................................81
7.8.2 The double and long double Types.................................................................................81
7.8.3 Assigning an Integer to a float .........................................................................................81
8. Operators and Expressions 83
8.1 Operators ................................................................................................. 83
8.2 C Expressions.......................................................................................... 84
8.2.1 Binding.................................................................................................................................85
8.2.2 Unary Operators.................................................................................................................85
8.2.3 Binary Operators ................................................................................................................85
8.2.4 Trinary Operator ................................................................................................................86
8.2.5 Operator Precedence .........................................................................................................86
8.2.6 The = Operator..................................................................................................................87
8.3 Arithmetic Operators............................................................................... 88
8.3.1 Increment and Decrement Operators ............................................................................89
8.4 Assignment Operators............................................................................. 90
8.5 Comparison Operators ............................................................................ 91
8.5.1 Expressing True and False................................................................................................91
8.5.2 The Equality Operators.....................................................................................................92
8.5.3 Relational Operators..........................................................................................................92
8.5.4 Logical Operators...............................................................................................................93
Table of Contents
10.3 Function Prototype Declarations..........................................................113
10.3.1 Defining the Function Interface ................................................................................. 113
10.3.2 Calling Functions in Other Files ................................................................................. 113
10.3.3 Function Type, Name and Parameter List ................................................................114
10.3.4 Functions and void........................................................................................................ 115
Table of Contents
vii
10.4 Function Definitions.............................................................................116
10.4.1 Statement Block..............................................................................................................116
10.4.2 Variable Declarations in Function Definitions .........................................................116
10.5 Function Parameters.............................................................................117
10.5.1 Passing Data by Value...................................................................................................117
10.5.2 Passing Data by Reference ...........................................................................................117
10.5.3 Functions Without Parameters ....................................................................................118
11. Complex Data Types 121
11.1 Pointers..................................................................................................121
11.1.1 Declaring a Pointer ........................................................................................................121
11.1.2 Pointer Operators ..........................................................................................................122
11.1.3 Pointer Pitfalls ................................................................................................................123
11.2 Arrays ....................................................................................................124
11.2.1 Accessing Array Elements ............................................................................................124
11.2.2 Multidimensional Arrays...............................................................................................125
11.2.3 Array Operations and Pointer Arithmetic .................................................................125
11.2.4 Arrays of Pointers ..........................................................................................................126
11.3 User Defined Data Types .....................................................................127
11.3.1 Using typedef to Define New Data Types.................................................................127
11.3.2 Using types defined with typedef ................................................................................128
11.4 Enumerated Types................................................................................128
11.4.1 Enumerated Type Elements.........................................................................................129
12.2.2 Allowable Values Modifiers: signed and unsigned................................................... 146
12.2.3 Size Modifiers: short and long..................................................................................... 146
12.2.4 Pointer Size Modifiers: near and far........................................................................... 147
12.2.5 Using near and far pointers.......................................................................................... 148
12.2.6 Default pointer type ...................................................................................................... 148
13. The C Preprocessor 151
13.1 Preprocessor Directive Syntax ..............................................................151
13.2 White Space in the Preprocessor ......................................................... 152
13.3 File Inclusion ....................................................................................... 152
13.3.1 File Inclusion Searches .................................................................................................153
13.4 Defining Symbolic Constants .............................................................. 153
13.4.1 The #undef directive ....................................................................................................154
13.4.2 Defining “empty” symbols .......................................................................................... 155
13.5 Defining Macros .................................................................................. 155
13.5.1 Macro Expansion...........................................................................................................156
13.5.2 # and ## Operators..................................................................................................... 157
13.6 Conditional Source Code ..................................................................... 157
13.6.1 #if and #endif................................................................................................................ 157
13.6.2 The defined( ) Function................................................................................................ 158
Table of Contents
ix
13.6.3 The #else and #elif Directives ....................................................................................158
13.6.4 #ifdef and #ifndef.........................................................................................................159
13.7 Producing Error messages ...................................................................159
13.8 Defining Target Hardware ...................................................................160
13.9 In-line Assembly Language..................................................................160
13.9.1 The #asm and #endasm Directives............................................................................160
14. Libraries 161
14.1 Portable Device Driver Libraries ..........................................................161
xi
Table of Examples
Example 1: Defining ports with #pragma directives.............................................................17
Example 2: Using a union structure to create a scratch pad.................................................28
Example 3: Using globally allocated data space in a function ..............................................29
Example 4: A typical assembly language program for the COP8SAA................................49
Example 5: Program in Example 4 compiled for the 68HC705C8 .....................................49
Example 6: A typical microcontroller program ......................................................................55
Example 7: Syntax for the main( ) function ............................................................................59
Example 8: Using the C assignment statement.......................................................................60
Example 9: The if statement syntax..........................................................................................61
Example 10: Nesting if and while statements .........................................................................62
Example 11: Calling one function from another....................................................................62
Example 12: C functions containing inline assembly language ............................................64
Example 13: Common C keywords..........................................................................................66
Example 14: Using braces to delineate a block.......................................................................70
Example 15: The while loop......................................................................................................70
Example 16: Declaring variable types ......................................................................................72
Example 17: Assigning a character value.................................................................................76
Example 18: Octal, hex and binary notation...........................................................................79
Example 19: Data type modifiers..............................................................................................80
Example 20: Postfix and prefix unary operators ....................................................................85
Example 21: Sample binary operators......................................................................................85
Example 22: Trinary conditional operator...............................................................................86
Example 23: Combining operators in a statement .................................................................86
Example 24: Concatenating expressions with the comma operator....................................87
Example 25: Combining assignment operators in statements..............................................87
Example 53: Matching if and else statements ...................................................................... 102
Example 54: Using braces to clarify the combination of if and else................................. 102
Example 55: An alternate format for showing if else pairing ............................................ 103
Example 56: The switch..case structure ................................................................................ 103
Example 57: Using the fall-through effect with switch statements................................... 104
Example 58: Multiple case enhancement.............................................................................. 105
Example 59: Using the default case value............................................................................. 105
Example 60: The goto statement ........................................................................................... 106
Example 61: The while loop syntax....................................................................................... 107
Example 62: The do loop syntax............................................................................................ 107
Example 63: Comparing the while and for loops................................................................ 108
Example 64: Using the for loop ............................................................................................. 108
Example 65: Comparing function and variable declarations ............................................. 114
Example 66: The function statement block.......................................................................... 116
Example 67: Variable declarations inside functions............................................................ 116
Example 68: Passing data to a function by value................................................................. 117
Example 69: Passing a variable to a function by address (reference) ............................... 118
Example 70: Using the address of operator.......................................................................... 122
Example 71: Using the pointer dereference operator ......................................................... 123
Table of Examples
xiii
Example 72: Dereferencing a pointer set to NULL.............................................................123
Example 73: Initializing a pointer...........................................................................................124
Example 74: Array operations and pointer arithmetic.........................................................125
Example 75: The relationship between arrays and pointers ...............................................126
Example 76: Declaring and initializing an array of pointers ...............................................127
Example 77: Using typedef to define a new data type.........................................................127
Example 78: Defining a new enumerated type .....................................................................128
Example 79: Declaring multiple variables of the same enumerated type .........................129
xiv
Example 109: Nesting preprocessor directives .................................................................... 151
Example 110: Redefining a constant using #undef............................................................. 155
Example 111: Defining and calling a macro......................................................................... 156
Example 112: Using #if and #endif to conditionally compiler code............................... 157
Example 113: Using expressions in #if directives for conditional compilation ............. 158
Example 114: Using the defined() function for conditional compilation ........................ 158
Example 115: Using !defined() to test if a symbol has not been defined......................... 158
Example 116: Using #else and #elif to choose between compilation blocks................. 159
Example 117: Using #elif, #if and #endif for conditional compilation.......................... 159
Example 118: Using #ifdef and #ifndef............................................................................... 159
Example 119: Using the #error directive.............................................................................. 160
Example 120: Master function for PIC16C74 SPI communication ................................. 163
Example 121: Setting up the SPI on the Microchip PIC16C74 ........................................ 165
Example 122: Setting up SPI on the Motorola 68HC705C8 ............................................. 165
Example 123: Setting up SPI on the National COP8SAA7............................................... 166
Example 124: Initiating SPI send/receive on the Microchip PIC16C74......................... 167
Example 125: Initiating SPI send/receive on the Motorola 68HC705C8 ....................... 167
Example 126: Initiating SPI send/receive on the National COP8SAA7......................... 168
Example 127: Serial port connection example for the PIC16C74 .................................... 180
Example 128: Serial port connection example for the PC ................................................. 182 xv
Table of Figures
Figure 1: The microcontroller......................................................................................................7
Figure 2: Von Neumann memory map for the MC68705C8 .................................................9
Figure 3: Harvard memory map PIC16C74 ............................................................................10
Table 17: Modem Control Register ........................................................................................176
Table 18: Line Status Register..................................................................................................177
Table 19: Modem Status Register............................................................................................177
Table 20: Pin outs on the RS232 port ....................................................................................183
Table 21: Rules of operator precedence.................................................................................185
Table 22: ASCII characters ......................................................................................................187
xix
Acknowledgements
This book represents the hard work of many people at Byte Craft Limited. We
want to offer as much of our experience as possible to those entering the
Embedded Systems field. We are leveraging our experience in embedded
systems, in technical communication, and in publishing to bring about
informative publications that do just that.
Kirk Zurell edited this publication and designed the cover art.
1
1. Introduction
This book is intended to fill the need for an intermediate level overview of
programming microcontrollers using the C programming language. It is aimed
specifically at two groups of readers who have different, yet overlapping needs.
!
!!
!
The first group are familiar with C but require an examination of the general
ext
has an italicised
extension which indicates that the file can have any valid extension.
__
the double underscore contains a small space to display both characters.
Do not type the space in the double underscore character in your code.
#
is used within one section to refer to another section on a related topic.
N
OTE
An important note will appear in this way.
0x is used to denote a hexadecimal number. For example: 0xFFF
0b is used to denote a binary number. For example: 0b010101