Designing embedded systems with PIC microcontroller

583 519 0
Designing embedded systems with PIC microcontroller

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Designing Embedded Systems with PIC Microcontrollers Principles and applications This page intentionally left blank Designing Embedded Systems with PIC Microcontrollers Principles and applications Tim Wilmshurst AMSTERDAM • BOSTON • HEIDELBERG • LONDON • NEW YORK • OXFORD PARIS • SAN DIEGO • SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier Newnes is an imprint of Elsevier Linacre House, Jordan Hill, Oxford OX2 8DP, UK The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, UK 84 Theobald’s Road, London WC1X 8RR, UK Radarweg 29, PO Box 211, 1000 AE Amsterdam, The Netherlands 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA 525 B Street, Suite 1900, San Diego, CA 92101-4495, USA First edition 2007 Copyright © 2007, Timothy Wilmshurst Published by Elsevier Ltd All rights reserved The right of Timothy Wilmshurst to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988 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 the prior written permission of the publisher Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone (+44) (0) 1865 843830; fax (+44) (0) 1865 853333; email: permissions@elsevier.com Alternatively you can submit your request online by visiting the Elsevier web site at http://elsevier.com/locate/permissions, and selecting Obtaining permission to use Elsevier material Notice No responsibility is assumed by the publisher for any injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions or ideas contained in the material herein Because of rapid advances in the medical sciences, in particular, independent verification of diagnoses and drug dosages should be made British Library Cataloguing in Publication Data Wilmshurst, Tim Designing embedded systems with PIC microcontrollers: principles and applications Embedded computer systems – Design and construction Microprocessors – Design and construction I Title 004.1’6 Library of Congress Control Number: 2006933361 ISBN-13: 978-0-7506-6755-5 ISBN-10: 0-7506-6755-9 For information on all Newnes publications visit our website at www.books.elsevier.com Printed and bound in Great Britain 07 08 09 10 11 11 10 Contents Introduction Acknowledgements Section 1 Getting Started with Embedded Systems Tiny computers, hidden control 1.1 The main idea – embedded systems in today’s world 1.1.1 What is an embedded system? 1.2 Some example embedded systems 1.2.1 The domestic refrigerator 1.2.2 A car door mechanism 1.2.3 The electronic ‘ping-pong’ 1.2.4 The Derbot Autonomous Guided Vehicle 1.3 Some computer essentials 1.3.1 Elements of a computer 1.3.2 Instruction sets – CISC and RISC 1.3.3 Memory types 1.3.4 Organising memory 1.4 Microprocessors and microcontrollers 1.4.1 Microprocessors 1.4.2 Microcontrollers 1.4.3 Microcontroller families 1.4.4 Microcontroller packaging and appearance 1.5 Microchip and the PIC microcontroller 1.5.1 Background 1.5.2 PIC microcontrollers today 1.6 An introduction to PIC microcontrollers using the 12 Series 1.6.1 The 12F508 architecture 1.7 What others – a Freescale microcontroller Summary References Section 2 Minimum Systems and the PIC® 16F84A xxi xxv 3 4 9 10 10 11 11 12 13 14 15 15 15 17 18 20 22 22 23 Introducing the PIC® 16 Series and the 16F84A 25 2.1 25 25 The main idea – the PIC 16 Series family 2.1.1 A family overview vi Contents 2.1.2 The 16F84A 2.1.3 A caution on upgrades 2.2 An architecture overview of the 16F84A 2.2.1 The Status register 2.3 A review of memory technologies 2.3.1 Static RAM (SRAM) 2.3.2 EPROM (Erasable Programmable Read-Only Memory) 2.3.3 EEPROM (Electrically Erasable Programmable Read-Only Memory) 2.3.4 Flash 2.4 The 16F84A memory 2.4.1 The 16F84A program memory 2.4.2 The 16F84A data and Special Function Register memory (‘RAM’) 2.4.3 The Configuration Word 2.4.4 EEPROM 2.5 Some issues of timing 2.5.1 Clock oscillator and instruction cycle 2.5.2 Pipelining 2.6 Power-up and Reset 2.7 What others – the Atmel AT89C2051 2.8 Taking things further – the 16F84A on-chip reset circuit Summary References 27 27 27 29 29 30 31 31 31 32 32 33 35 35 37 37 38 38 40 41 44 44 Parallel ports, power supply and the clock oscillator 45 3.1 3.2 46 46 46 49 49 52 52 53 55 55 55 56 59 59 60 60 61 61 62 63 3.3 3.4 3.5 3.6 3.7 The main idea – parallel input/output The technical challenge of parallel input/output 3.2.1 Building a parallel interface 3.2.2 Port electrical characteristics 3.2.3 Some special cases Connecting to the parallel port 3.3.1 Switches 3.3.2 Light-emitting diodes The PIC 16F84A parallel ports 3.4.1 The 16F84A Port B 3.4.2 The 16F84A Port A 3.4.3 Port output characteristics The clock oscillator 3.5.1 Clock oscillator types 3.5.2 Practical oscillator considerations 3.5.3 The 16F84A clock oscillator Power supply 3.6.1 The need for power, and its sources 3.6.2 16F84A operating conditions The hardware design of the electronic ping-pong Contents vii Summary References 64 64 Starting to program – an introduction to Assembler 65 4.1 The main idea – what programs and how we develop them 4.1.1 The problem of programming and the Assembler compromise 4.1.2 The process of writing in Assembler 4.1.3 The program development process 4.2 The PIC 16 Series instruction set, with a little more on the ALU 4.2.1 More on the PIC 16 Series ALU 4.2.2 The PIC 16 Series instruction set – an introduction 4.3 Assemblers and Assembler format 4.3.1 Introducing Assemblers and the Microchip MPASMTM Assembler 4.3.2 Assembler format 4.3.3 Assembler directives 4.3.4 Number representation 4.4 Creating simple programs 4.4.1 A simple data transfer program 4.5 Adopting a development environment 4.5.1 Introducing MPLAB 4.5.2 The elements of MPLAB 4.5.3 The MPLAB file structure 4.6 An introductory MPLAB tutorial 4.6.1 Creating a project 4.6.2 Entering source code 4.6.3 Assembling the project 4.7 An introduction to simulation 4.7.1 Getting started 4.7.2 Generating port inputs 4.7.3 Viewing microcontroller features 4.7.4 Resetting and running the program 4.8 Downloading the program to a microcontroller 4.9 What others – a brief comparison of CISC and RISC instruction sets 4.10 Taking things further – the 16 Series instruction set format Summary References 66 66 67 68 69 69 70 71 71 71 72 72 73 73 76 76 76 77 77 77 79 80 81 81 81 82 82 83 86 87 88 88 Building Assembler programs 89 5.1 89 89 91 92 92 94 5.2 The main idea – building structured programs 5.1.1 Flow diagrams 5.1.2 State diagrams Flow control – branching and subroutines 5.2.1 Conditional branching and working with bits 5.2.2 Subroutines and the Stack viii Contents 5.3 5.4 Generating time delays and intervals Dealing with data 5.4.1 Indirect addressing and the File Select Register 5.4.2 Look-up tables 5.4.3 Example program with delays and look-up table 5.5 Introducing logical instructions 5.6 Introducing arithmetic instructions and the Carry flag 5.6.1 Using add instructions 5.6.2 Using subtract instructions 5.6.3 An arithmetic program example 5.6.4 Using indirect addressing to save the Fibonacci series 5.7 Taming Assembler complexity 5.7.1 Include Files 5.7.2 Macros 5.7.3 MPLAB special instructions 5.8 More use of the MPLAB simulator 5.8.1 Breakpoints 5.8.2 Stopwatch 5.8.3 Trace 5.9 The ping-pong program 5.9.1 A structure for the ping-pong program 5.9.2 Exploring the ping-pong program code 5.10 Simulating the ping-pong program – tutorial 5.10.1 Setting up input stimulus 5.10.2 Setting up the Watch window 5.10.3 Single stepping 5.10.4 Animate 5.10.5 Run 5.10.6 Breakpoints 5.10.7 Stopwatch 5.10.8 Trace 5.10.9 Debugging the full program 5.11 What others – graphical simulators Summary References 95 97 97 98 99 101 102 102 102 102 104 106 106 107 108 109 109 110 110 112 112 115 116 116 116 116 117 117 117 117 117 118 118 119 119 Working with time: interrupts, counters and timers 120 6.1 121 121 122 124 125 125 126 6.2 The main idea – interrupts 6.1.1 Interrupt structures 6.1.2 The 16F84A interrupt structure 6.1.3 The CPU response to an interrupt Working with interrupts 6.2.1 Programming with a single interrupt 6.2.2 Moving to multiple interrupts – identifying the source Contents Stopping interrupts from wrecking your program – context saving 6.2.4 Stopping interrupts from wrecking your program – critical regions and masking 6.3 The main idea – counters and timers 6.3.1 The digital counter reviewed 6.3.2 The counter as timer 6.3.3 The 16F84A Timer module 6.4 Applying the 16F84A Timer 0, with examples using the electronic ping-pong 6.4.1 Object or event counting 6.4.2 Hardware-generated delays 6.5 The Watchdog Timer 6.6 Sleep mode 6.7 What others 6.8 Taking things further – interrupt latency Summary ix 6.2.3 Section Larger Systems and the PIC® 16F873A 127 130 131 131 132 134 136 136 137 138 139 140 141 142 143 Larger systems and the PIC® 16F873A 145 7.1 7.2 146 146 146 147 150 150 150 152 154 155 155 156 158 158 159 161 161 161 161 161 163 164 164 7.3 7.4 7.5 7.6 7.7 The main idea – the PIC 16F87XA The 16F873A block diagram and CPU 7.2.1 Overview of CPU and core 7.2.2 Overview of memory 7.2.3 Overview of peripherals 16F873A memory and memory maps 7.3.1 The 16F873A program memory 7.3.2 The 16F873A data memory and Special Function Registers 7.3.3 The Configuration Word ‘Special’ memory operations 7.4.1 Accessing EEPROM and program memory 7.4.2 In-Circuit Serial Programming (ICSPTM ) The 16F873A interrupts 7.5.1 The interrupt structure 7.5.2 The interrupt registers 7.5.3 Interrupt identification and context saving The 16F873A oscillator, reset and power supply 7.6.1 The clock oscillator 7.6.2 Reset and power supply The 16F873A parallel ports 7.7.1 The 16F873A Port A 7.7.2 The 16F873A Port B 7.7.3 The 16F873A Port C Table A5.1 Continued PIC“ 18 Series instruction set (non-extended) Symbol a b d f fd fs k n s Table A5.2 Summary of Opcode Operand Symbols Description (MPLAB Assembler default value underlined) RAM Access bit a = 0: Memory location is in Access RAM a = 1: RAM Bank specified by Bank Select Register (BSR) bit number in byte Destination select bit d = 0: result stored in W Register d = 1: result stored in file register f (i.e data memory location) 8-bit data memory address 12-bit data memory address, destination address in a data move 12-bit data memory address, source address in a data move Literal value, constant data or label (8, 12 or 20-bit) relative address (2’s complement) for relative branch instructions, OR direct address for Call and Return instructions Fast Call/Return mode select bit s = 0: not update into or from shadow registers s = 1: update W, Status and BSR registers into or from shadow registers 543 Appendix Essentials of C This appendix provides summary information on key aspects of the C programming language, as a set of tables Example usage and further explanation of most of these features appears in Chapters 14–19 of this book Word char const double enum float int long short Word break case continue default else Table A6.1 C keywords associated with data type and structure definition Summary meaning Word Summary meaning A single character, usually 8-bit signed A qualifier applied to char or int (default for char and int is signed) Data that will not be modified sizeof Returns the size in bytes of a specified item, which may be variable, expression or array A ‘double precision’ floating-point number struct Allows definition of a data structure Defines variables that can only take certain typedef Creates new name for existing data type integer values A ‘single precision’ floating-point number union A memory block shared by two or more variables, of any data type An integer value unsigned A qualifier applied to char or int (default for char and int is signed) An extended integer value; if used alone, void No value or type integer is implied A short integer value; if used alone, integer volatile A variable which can be changed by factors is implied other than the program code Table A6.2 C keywords associated with program flow Summary meaning Word Summary meaning Causes exit from a loop for Defines a repeated loop – loop is executed as long as condition associated with for remains true Identifies options for selection within a goto Program execution moves to labelled switch expression statement Allows a program to skip to the end of a if Starts conditional statement; if condition is for, while or statement true, associated statement is executed Identifies default option in a switch return Returns program execution to calling expression, if no matches found routine, causing also return of any data value specified by function Used with while to create loop, in which switch Used with case to allow selection of a statement following is repeated as long number of alternatives; switch has an as while condition is true associated expression which is tested against a number of case options Used with if, and precedes alternative while Defines a repeated loop – loop is executed statement used if if condition is not true as long as condition associated with while remains true Essentials of C Word auto extern Table A6.3 C keywords associated with data storage class Summary meaning Word Summary meaning Variable exists only within block within register Variable to be stored in a CPU register; thus, which it is defined This is the default class address operator (&) has no effect Declares data defined elsewhere static Declares variable which exists throughout program execution; the location of its declaration affects in what part of the program it can be referenced Table A6.4 C data types, as implemented by the MPLAB Data type Description Length (bytes) char Character signed char Character unsigned char Character int Integer unsigned int Integer short Integer unsigned short Integer short long Integer unsigned short long Integer long Integer unsigned long Integer float Floating point double 545 Floating point, double precision C18 C compiler Range −128 to +127 −128 to +127 to 255 −32 768 to +32 767 to 65 535 −32 768 to +32 767 to 65 535 −8 388 608 to 388 607 to 16 777 215 −2 147 483 648 to 147 483 647 to 294 967 295 From 1.17549 × 10−38 to 6.80565 × 10+38 From 1.17549 × 10−38 to 6.80565 × 10+38 546 Essentials of C Prec and order Operation Parethenses and array access operators 1, L to R Function calls 1, L to R Subscript Arithmetic operators 4, L to R Add 4, L to R Subtract 2, R to L Unary plus 2, R to L Unary minus Relational operators 6, L to R Greater than 6, L to R Greater than or equal to 6, L to R Less than Logical operators 11, L to R AND (1 if both X and Y are not 0) 12, L to R OR (1 if either X or Y are not 0) Bitwise operators 8, L to R Bitwise AND Table A6.5 C operators Symbol Prec and order Operation Symbol () [] 1, L to R 1, L to R Point at member Select member X−>Y X.Y X+Y X−Y +X −X 3, L to R 3, L to R 3, L to R Multiply Divide Modulus X∗ Y X/Y % X>Y X>=Y X[...]... 20.2.1 The IrDA and the PIC microcontroller 20.3 Radio connectivity 20.3.1 Bluetooth 20.3.2 Zigbee 20.3.3 Zigbee and the PIC microcontroller 20.4 Controller Area Network (CAN) and Local Interconnect Network (LIN) 20.4.1 Controller Area Network (CAN) 20.4.2 CAN and the PIC microcontroller 20.4.3 Local Interconnect Network (LIN) 20.4.4 LIN and the PIC microcontroller 20.5 Embedded systems and the Internet... page intentionally left blank Section 1 Getting Started with Embedded Systems This introductory chapter introduces embedded systems and the microcontroller, leading to a survey of the Microchip range of PIC microcontrollers This page intentionally left blank 1 Tiny computers, hidden control We are living in the age of information revolution, with computers of astonishing power available for our use... product is called an embedded system, and it is what this book is about Those little computers we generally call microcontrollers; it is one extended family of these that the book studies In this chapter you will learn about: • • • • • The meaning of the term embedded system’ The microcontroller which lies at the heart of the embedded system The Microchip PIC family A first PIC microcontroller, the... the 12F508 An alternative microcontroller structure from Freescale 1.1 The main idea – embedded systems in today’s world 1.1.1 What is an embedded system? The basic idea of an embedded system is a simple one If we take any engineering product that needs control, and if a computer is incorporated within that product to undertake the control, then we have an embedded system An embedded system can be defined... pleasure of designing and building embedded systems Tim Wilmshurst University of Derby, UK This page intentionally left blank Acknowledgements Certain materials contained herein are reprinted with permission of the copyright holder, Microchip Technology Incorporated All rights reserved No further reprints or reproductions may be made without Microchip Technology Inc.’s prior written consent PIC , PICSTART®... Introduction This is a book about embedded systems, introduced primarily through the application of three PIC microcontrollers Starting from an introductory level, the book aims to make the reader into a competent and independent practitioner in the field of embedded systems, to a level whereby he or she has the skills necessary to gain entry to professional practice in the embedded world The book achieves... controlled by a computer embedded within it These days embedded systems are everywhere, appearing in the home, office, factory, car or hospital Table 1.1 lists some example products that are likely to be embedded systems, all chosen for their familiarity While many of these examples seem very different from each other, they all draw on the same principles as far as their characteristics as an embedded system... primarily by their interface capabilities It is this difference that gives the Derbot microcontroller its aura of far greater power 1.3 Some computer essentials When designing embedded systems we usually need to understand in some detail the features of the embedded computer that we are working with This is quite unlike working with a desktop computer used for word processing or computer-aided design, where... 11.8.4 Other measurement systems 11.9 Configuring the Derbot AGV as a light-seeking robot Summary References 304 305 306 308 308 309 310 311 312 312 313 317 318 319 319 319 319 320 321 321 322 323 324 324 326 326 327 327 329 329 329 331 331 331 332 332 332 xiv Contents Section 4 12 Smarter Systems and the PIC 18FXX2 333 Smarter systems and the PIC 18FXX2 335 12.1 The main idea – the PIC 18 Series and the... 1.2 Some example embedded systems Let’s take a look at some example embedded systems, first from everyday life and then from the projects used to illustrate this book 1.2.1 The domestic refrigerator A simple domestic refrigerator is shown in Figure 1.1 It needs to maintain a moderately stable, low temperature within it It does this by sensing its internal temperature and comparing that with the temperature .. .Designing Embedded Systems with PIC Microcontrollers Principles and applications This page intentionally left blank Designing Embedded Systems with PIC Microcontrollers Principles... the PIC microcontroller 20.4.3 Local Interconnect Network (LIN) 20.4.4 LIN and the PIC microcontroller 20.5 Embedded systems and the Internet 20.5.1 Connecting to the Internet with the PIC microcontroller. .. Getting Started with Embedded Systems Tiny computers, hidden control 1.1 The main idea – embedded systems in today’s world 1.1.1 What is an embedded system? 1.2 Some example embedded systems 1.2.1

Ngày đăng: 08/03/2016, 11:22

Từ khóa liên quan

Mục lục

  • Cover

  • Contents

  • 1 Tiny computers, hidden control

  • 2 Introducing the PIC® 16 Series and the 16F84A

  • 3 Parallel ports, power supply and the clock oscillator

  • 4 Starting to program – an introduction to Assembler

  • 5 Building Assembler programs

  • 6 Working with time: interrupts, counters and timers

  • 7 Larger systems and the PIC® 16F873A

  • 8 The human and physical interfaces

  • 9 Taking timing further

  • 10 Starting with serial

  • 11 Data acquisition and manipulation

  • 12 Smarter systems and the PIC® 18FXX2

  • 13 The PIC® 18FXX2 peripherals

  • 14 Introducing C

  • 15 C and the embedded environment

  • 16 Acquiring and using data with C

  • 17 More C and the wider C environment

  • 18 Multi-tasking and the Real Time Operating System

Tài liệu cùng người dùng

Tài liệu liên quan