Embedded C 8322 Prelims (I-Xvi) 25/2/02 3:04 Pm Page Ii 8322 Prelims (I-Xvi) 25/2/02 3:04 Pm Page Iii
Total Page:16
File Type:pdf, Size:1020Kb
8322 Prelims (i-xvi) 25/2/02 3:04 pm Page i Embedded C 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page ii 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page iii Embedded C Michael J. Pont An imprint of Pearson Education London • Boston • Indianapolis • New York • Mexico City • Toronto Sydney • Tokyo • Singapore • Hong Kong • Cape Town • New Delhi Madrid • Paris • Amsterdam • Munich • Milan • Stockholm 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page iv PEARSON EDUCATION LIMITED Head Office: London Office: Edinburgh Gate 128 Long Acre Harlow CM20 2JE London WC2E 9AN Tel: +44 (0)1279 623623 Tel: +44 (0)20 7447 2000 Fax: +44 (0)1279 431059 Fax: +44 (0)20 7240 5771 Websites: www.aw.com/cseng/ www.it-minds.com First published in Great Britain in 2002 © Pearson Education Limited 2002 The right of Michael J. Pont to be identified as Author of this Work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. ISBN 0 201 79523 X British Library Cataloguing in Publication Data A CIP catalogue record for this book can be obtained from the British Library Library of Congress-in-Publication Data Pont, Michael J. Embedded C/Michael J. Pont. p. cm. Includes bibliographical references and index. ISBN 0-201-79523-X (pbx. : alk. paper) 1.C (Computer program language) 2. Embedded computer systems--Design and construction. I. Title. QA76.73.C15 P65 2002 005.265--dc21 2001056731 All rights reserved; no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise without either the prior written permission of the Publishers or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1P 0LP. This book may not be lent, resold, hired out or otherwise disposed of by way of trade in any form of binding or cover other than that in which it is published, without the prior consent of the Publishers. The programs in this book have been included for their instructional value. The publisher does not offer any warranties or representations in respect of their fitness for a particular purpose, nor does the publisher accept any liability for any loss or damage arising from their use. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Pearson Education Limited has made every attempt to supply trademark information about manufacturers and their products mentioned in this book. The publishers wish to thank Infineon Technologies for permission to reproduce the material in Figure 1.4. 10 9 8 7 6 5 4 3 2 1 Designed by Claire Brodmann Book Designs, Lichfield, Staffs Typeset by Pantek Arts Ltd, Maidstone, Kent Printed and bound in Great Britain by Biddles Ltd of Guildford and King’s Lynn The Publisher’s policy is to use paper manufactured from sutainable forests. 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page v This book is dedicated to Sarah 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page vi About the author Michael J. Pont is an experienced software engineer who began his first embedded project in 1986. Since then he has lectured and carried out research at the University of Sheffield and the University of Leicester, and has provided consul- tancy and training services to a range of international companies. Michael is the author of two previous books Patterns for Time-Triggered Embedded Systems and Software Engineering with C++ and CASE tools. 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page vii Contents Preface xi 1 Programming embedded systems in C 1 1.1 Introduction 1 1.2 What is an embedded system? 1 1.3 Which processor should you use? 2 1.4 Which programming language should you use? 7 1.5 Which operating system should you use? 9 1.6 How do you develop embedded software? 12 1.7 Conclusions 15 2 Introducing the 8051 microcontroller family 17 2.1 Introduction 17 2.2 What’s in a name? 17 2.3 The external interface of the Standard 8051 18 2.4 Reset requirements 20 2.5 Clock frequency and performance 21 2.6 Memory issues 23 2.7 I/O pins 29 2.8 Timers 29 2.9 Interrupts 30 2.10 Serial interface 32 2.11 Power consumption 32 2.12 Conclusions 34 3 Hello, embedded world 35 3.1 Introduction 35 3.2 Installing the Keil software and loading the project 36 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page viii viii Contents 3.3 Configuring the simulator 37 3.4 Building the target 39 3.5 Running the simulation 39 3.6 Dissecting the program 43 3.7 Aside: Building the hardware 55 3.8 Conclusions 56 4 Reading switches 57 4.1 Introduction 57 4.2 Basic techniques for reading from port pins 58 4.3 Example: Reading and writing bytes 60 4.4 Example: Reading and writing bits (simple version) 61 4.5 Example: Reading and writing bits (generic version) 62 4.6 The need for pull-up resistors 67 4.7 Dealing with switch bounce 69 4.8 Example: Reading switch inputs (basic code) 70 4.9 Example: Counting goats 75 4.10 Conclusions 80 5 Adding structure to your code 81 5.1 Introduction 81 5.2 Object-oriented programming with C 82 5.3 The Project Header (MAIN.H) 88 5.4 The Port Header (PORT.H) 94 5.5 Example: Restructuring the ‘Hello Embedded World’ example 96 5.6 Example: Restructuring the goat-counting example 103 5.7 Further examples 111 5.8 Conclusions 111 6 Meeting real-time constraints 113 6.1 Introduction 113 6.2 Creating ‘hardware delays’ using Timer 0 and Timer 1 116 6.3 Example: Generating a precise 50 ms delay 120 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page ix Contents ix 6.4 Example: Creating a portable hardware delay 124 6.5 Why not use Timer 2? 129 6.6 The need for ‘timeout’ mechanisms 129 6.7 Creating loop timeouts 130 6.8 Example: Testing loop timeouts 133 6.9 Example: A more reliable switch interface 134 6.10 Creating hardware timeouts 136 6.11 Example: Testing a hardware timeout 140 6.12 Conclusions 142 7 Creating an embedded operating system 143 7.1 Introduction 143 7.2 The basis of a simple embedded OS 147 7.3 Introducing sEOS 152 7.4 Using Timer 0 or Timer 1 161 7.5 Is this approach portable? 166 7.6 Alternative system architectures 166 7.7 Important design considerations when using sEOS 172 7.8 Example: Milk pasteurization 174 7.9 Conclusions 187 8 Multi-state systems and function sequences 189 8.1 Introduction 189 8.2 Implementing a Multi-State (Timed) system 192 8.3 Example: Traffic light sequencing 192 8.4 Example: Animatronic dinosaur 198 8.5 Implementing a Multi-State (Input/Timed) system 204 8.6 Example: Controller for a washing machine 205 8.7 Conclusions 215 9 Using the serial interface 217 9.1 Introduction 217 9.2 What is RS-232? 217 9.3 Does RS-232 still matter? 218 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page x x Contents 9.4 The basic RS-232 protocol 218 9.5 Asynchronous data transmission and baud rates 219 9.6 Flow control 220 9.7 The software architecture 220 9.8 Using the on-chip UART for RS-232 communications 222 9.9 Memory requirements 224 9.10 Example: Displaying elapsed time on a PC 225 9.11 The Serial-Menu architecture 237 9.12 Example: Data acquisition 237 9.13 Example: Remote-control robot 252 9.14 Conclusions 253 10 Case study: Intruder alarm system 255 10.1 Introduction 255 10.2 The software architecture 257 10.3 Key software components used in this example 257 10.4 Running the program 258 10.5 The software 258 10.6 Conclusions 283 11 Where do we go from here 285 11.1 Introduction 285 11.2 Have we achieved our aims? 285 11.3 Suggestions for further study 286 11.4 Patterns for Time-Triggered Embedded Systems 288 11.5 Embedded Operating Systems 288 11.6 Conclusions 289 Index 291 Licensing Agreement 295 8322 Prelims (i-xvi) 25/2/02 3:04 pm Page xi Preface This book provides a ‘hardware-free’ introduction to embedded software for people who: G Already know how to write software for ‘desktop’ computer systems. G Are familiar with a C-based language (Java, C++ or C). G Want to learn how C is used in practical embedded systems. The remainder of this preface attempts to answer some questions which prospec- tive readers may have about the contents. I What is an embedded system? As far as this book is concerned: An embedded system is an application that contains at least one programmable computer (typically in the form of a microcontroller, a microprocessor or digital signal processor chip) and which is used by individuals who are, in the main, unaware that the system is computer-based. This type of embedded system is all around us. Use of embedded processors in pas- senger cars, mobile phones, medical equipment, aerospace systems and defence systems is widespread, and even everyday domestic appliances such as dishwash- ers, televisions, washing machines and video recorders now include at least one such device.