You are on page 1of 11

T Y E A M F L

Team-Fly

Page i

C Programming for Embedded Systems


Kirk Zurell

Page ii

Disclaimer: This netLibrary eBook does not include the ancillary media that was packaged with the original printed version of the book. R&D Books CMP Media, Inc. 1601 W. 23rd Street, Suite 200 Lawrence, KS 66046 USA Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where R&D is aware of a trademark claim, the product name appears in initial capital letters, in all capital letters, or in accordance with the vendor's capitalization preference. Readers should contact the appropriate companies for more complete information on trademarks and trademark registrations. All trademarks and registered trademarks in this book are the property of their respective holders. Copyright 2000 by Byte Craft Limited. Licensed Material. All rights reserved. Published by R&D Books, CMP Media, Inc. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher; with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. The programs in this book are presented for instructional value. The programs have been carefully tested, but are not guaranteed for any particular purpose. The publisher does not offer any warranties and does not guarantee the accuracy, adequacy, or completeness of any information herein and is not responsible for any errors or omissions. The publisher assumes no liability for damages resulting from the use of the information in this book or for any infringement of the intellectual property rights of third parties that would result from the use of this information. Cover art created by Robert Ward. Distributed in the U.S. and Canada by: Publishers Group West 1700 Fourth Street Berkeley, CA 94710 ISBN 1-929629-04-4

Page iii

BYTE CRAFT LIMITED 421 King Street North Waterloo, Ontario Canada N2J 4E4 Telephone: (519) 888-6911 Fax: (519) 746-6751 E-mail: info@bytecraft.com http://www.bytecraft.com All example and program code is protected by copyright. Intel is a registered trademark of Intel Corporation. Microsoft and Windows are trademarks or registered trademarks of Microsoft Corporation. PC is a registered trademark of International Business Machines Corporation. Motorola is a registered trademark of Motorola Inc. COP8, MICROWIRE, and MICROWIRE/PLUS are trademarks or registered trademarks of National Semiconductor Corporation. PIC is a registered trademark of Microchip Technology Inc. in the USA Scenix is a trademark of Scenix Semiconductor, Inc. Cypress is a trademark of Cypress Semiconductor Corporation. I2C is a registered trademark of Philips. All other trademarks mentioned herein are property of their respective companies.

Page v

Acknowledgments
I would like to thank Walter Banks at Byte Craft Limited for dropping me head-first into the world of embedded programming. Walter and Andre have provided copious expertise in the very finest points of C programming and code generation. I would also like to thank my parents, who went out on a limb and purchased that Commodore 64 all those years ago. I hereby disclose publicly that I did not wash the dishes forever, as promised.

Page vii

Table of Contents
Acknowledgments v Chapter 1 Introduction 1 Role of This Book 1 Benefits of C in Embedded Systems 2 Outline of the Book 3 Typographical Conventions 3 Updates and Supplementary Information 4 Chapter 2 Problem Specification 5 Product Requirements 5 Hardware Engineering 6 Software Planning 8 Software Architecture 9 Pseudocode 10 Flowchart 11 State Diagram 12 Resource Management 13 Testing Regime 14

Page viii

Chapter 3 Microcontrollers In-depth 17 The Central Processing Unit (CPU) 19 Instruction Sets 20 The Stack 20 Memory Addressing and Types 21 RAM and ROM 22 ROM and Programming 22 von Neumann Versus Harvard Architectures 23 Timers 24 Watchdog Timer 25 Examples 26 26 Interrupt Circuitry 26 Vectored and Nonvectored Arbitration 27 Saving State during Interrupts 29 Executing Interrupt Handlers 30 Multiple Interrupts 31 RESET 31 I/O Ports 32 Analog-to-Digital Conversion 33 Serial Peripheral Buses 34 Development Tools for a Microcontroller 36 Chapter 4 Design Process 37

Product Functionality 37 Hardware Design 38 Software Design 39 Software Architecture 39 Flowchart 40 Resource Management 42 Scratch Pad 42 Interrupt Planning 42 Testing Choices 44 Design for Debugging 44 Code Inspection 44 Execution within a Simulator Environment 45 Execution within an Emulator Environment 45 Target System in a Test Harness 45

Page ix

Chapter 5 C for Embedded Systems 47 In-line Assembly Language 47 Device Knowledge 49 #pragma has 49 #pragma port 51 Endianness 52 Mechanical Knowledge 52 Libraries 54 First Look at an Embedded C Program 54 Chapter 6 Data Types and Variables 57 Identifier Declaration 59 Special Data Types and Data Access 59 Function Data Types 60 The Character Data Type 60 Integer Data Types 61 Byte Craft's Sized Integers 61 Bit Data Types 61 Real Numbers 63 Complex Data Types 63 Pointers 63 Arrays 64 Enumerated Types 65

Structures 66 Unions 68 typedef 69 Data Type Modifiers 70 Value Constancy Modifiers: const and volatile 70 Allowable Values Modifiers: signed and unsigned 71 Size Modifiers: short and long 72 Pointer Size Modifiers: near and far 72 Storage Class Modifiers 73 External Linkage 73 Internal Linkage 73 No Linkage 74 The extern Modifier 74 The static Modifier 75 The register Modifier 76 The auto Modifier 77

You might also like