01~front_matter

15
Advanced PIC Microcontroller Projects in C

Upload: reema

Post on 10-Jul-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 01~Front_Matter

Advanced PIC MicrocontrollerProjects in C

Page 2: 01~Front_Matter
Page 3: 01~Front_Matter

Advanced PIC MicrocontrollerProjects in C

From USB to RTOS with the PIC18F Series

Dogan Ibrahim

Page 4: 01~Front_Matter

Newnes is an imprint of Elsevier

30 Corporate Drive, Suite 400, Burlington, MA 01803, USA

Linacre House, Jordan Hill, Oxford OX2 8DP, UK

Copyright # 2008, Elsevier Ltd. 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 the prior written permission of

the publisher.

Permissions may be sought directly from Elsevier s Science & Technology Rights Department in Oxford,

UK: phone: (þ44) 1865 843830, fax: (þ44) 1865 853333, E-mail: [email protected]. You may

also complete your request online via the Elsevier homepage (http://elsevier.com), by selecting “Support &

Contact” then “Copyright and Permission” and then “Obtaining Permissions.”

Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free

paper whenever possible.

Library of Congress Cataloging-in-Publication Data

Ibrahim, Dogan.

Advanced PIC microcontroller projects in C: from USB to RTOS with the PIC18F series/Dogan Ibrahim

p. cm.

Includes bibliographical references and index.

ISBN-13: 978-0-7506-8611-2 (pbk. : alk. paper) 1. Programmable controllers. 2. C (Computer program

language) I. Title.

TJ223.P76I268 2008

629.8095––dc222007050550

British Library Cataloguing-in-Publication Data

A catalogue record for this book is available from the British Library.

ISBN: 978-0-7506-8611-2

For information on all Newnes publicationsvisit our Web site at www.books.elsevier.com

Printed in the United States of America

08 09 10 11 12 13 9 8 7 6 5 4 3 2 1

Page 5: 01~Front_Matter

Contents

Preface............................................................................................. xiii

Acknowledgments ................................................................................ xv

Chapter 1: Microcomputer Systems.......................................................... 11.1 Introduction..................................................................................................11.2 Microcontroller Systems ...............................................................................1

1.2.1 RAM .................................................................................................51.2.2 ROM .................................................................................................51.2.3 PROM...............................................................................................51.2.4 EPROM.............................................................................................61.2.5 EEPROM ..........................................................................................61.2.6 Flash EEPROM .................................................................................6

1.3 Microcontroller Features...............................................................................61.3.1 Supply Voltage ..................................................................................71.3.2 The Clock..........................................................................................71.3.3 Timers ...............................................................................................71.3.4 Watchdog ..........................................................................................81.3.5 Reset Input ........................................................................................81.3.6 Interrupts ...........................................................................................81.3.7 Brown-out Detector ...........................................................................91.3.8 Analog-to-Digital Converter ...............................................................91.3.9 Serial Input-Output ............................................................................91.3.10 EEPROM Data Memory ..................................................................101.3.11 LCD Drivers....................................................................................101.3.12 Analog Comparator..........................................................................101.3.13 Real-time Clock...............................................................................111.3.14 Sleep Mode .....................................................................................111.3.15 Power-on Reset................................................................................11

www.newnespress.com

Page 6: 01~Front_Matter

1.3.16 Low-Power Operation ....................................................................111.3.17 Current Sink/Source Capability ......................................................111.3.18 USB Interface ................................................................................121.3.19 Motor Control Interface .................................................................121.3.20 CAN Interface ...............................................................................121.3.21 Ethernet Interface...........................................................................121.3.22 ZigBee Interface ............................................................................12

1.4 Microcontroller Architectures....................................................................121.4.1 RISC and CISC ...............................................................................13

1.5 Number Systems.......................................................................................131.5.1 Decimal Number System .................................................................141.5.2 Binary Number System....................................................................141.5.3 Octal Number System ......................................................................151.5.4 Hexadecimal Number System ..........................................................15

1.6 Converting Binary Numbers into Decimal.................................................161.7 Converting Decimal Numbers into Binary.................................................161.8 Converting Binary Numbers into Hexadecimal..........................................181.9 Converting Hexadecimal Numbers into Binary..........................................201.10 Converting Hexadecimal Numbers into Decimal .......................................211.11 Converting Decimal Numbers into Hexadecimal .......................................221.12 Converting Octal Numbers into Decimal...................................................231.13 Converting Decimal Numbers into Octal...................................................231.14 Converting Octal Numbers into Binary .....................................................241.15 Converting Binary Numbers into Octal .....................................................261.16 Negative Numbers ....................................................................................261.17 Adding Binary Numbers ...........................................................................271.18 Subtracting Binary Numbers .....................................................................291.19 Multiplication of Binary Numbers.............................................................291.20 Division of Binary Numbers .....................................................................311.21 Floating Point Numbers ............................................................................311.22 Converting a Floating Point Number into Decimal ....................................33

1.22.1 Normalizing Floating Point Numbers .............................................341.22.2 Converting a Decimal Number into Floating Point .........................341.22.3 Multiplication and Division of Floating Point Numbers ..................361.22.4 Addition and Subtraction of Floating Point Numbers ......................37

1.23 BCD Numbers ..........................................................................................381.24 Summary..................................................................................................401.25 Exercises ..................................................................................................40

Chapter 2: PIC18F Microcontroller Series .............................................. 432.1 PIC18FXX2 Architecture..........................................................................46

2.1.1 Program Memory Organization ........................................................50

www.newnespress.com

vi Contents

Page 7: 01~Front_Matter

2.1.2 Data Memory Organization ..............................................................512.1.3 The Configuration Registers.............................................................522.1.4 The Power Supply ...........................................................................572.1.5 The Reset ........................................................................................572.1.6 The Clock Sources...........................................................................602.1.7 Watchdog Timer ..............................................................................672.1.8 Parallel I/O Ports .............................................................................682.1.9 Timers .............................................................................................742.1.10 Capture/Compare/PWM Modules (CCP) ..........................................842.1.11 Analog-to-Digital Converter (A/D) Module ......................................932.1.12 Interrupts ....................................................................................... 101

2.2 Summary.................................................................................................. 1152.3 Exercises .................................................................................................. 115

Chapter 3: C Programming Language....................................................1193.1 Structure of a mikroC Program................................................................. 120

3.1.1 Comments ..................................................................................... 1213.1.2 Beginning and Ending of a Program .............................................. 1213.1.3 Terminating Program Statements.................................................... 1213.1.4 White Spaces ................................................................................. 1223.1.5 Case Sensitivity ............................................................................. 1223.1.6 Variable Names ............................................................................. 1233.1.7 Variable Types .............................................................................. 1233.1.8 Constants ....................................................................................... 1263.1.9 Escape Sequences .......................................................................... 1283.1.10 Static Variables.............................................................................. 1293.1.11 External Variables ......................................................................... 1293.1.12 Volatile Variables .......................................................................... 1303.1.13 Enumerated Variables .................................................................... 1303.1.14 Arrays ........................................................................................... 1313.1.15 Pointers ......................................................................................... 1333.1.16 Structures ...................................................................................... 1353.1.17 Unions........................................................................................... 1383.1.18 Operators in C ............................................................................... 1393.1.19 Modifying the Flow of Control ...................................................... 1483.1.20 Mixing mikroC with Assembly Language Statements ..................... 159

3.2 PIC Microcontroller Input-Output Port Programming ................................ 1603.3 Programming Examples ............................................................................ 1613.4 Summary.................................................................................................. 1653.5 Exercises .................................................................................................. 165

www.newnespress.com

viiContents

Page 8: 01~Front_Matter

Chapter 4: Functions and Libraries in mikroC.........................................1694.1 mikroC Functions ..................................................................................... 169

4.1.1 Function Prototypes ......................................................................... 1734.1.2 Passing Arrays to Functions............................................................. 1774.1.3 Passing Variables by Reference to Functions.................................... 1804.1.4 Variable Number of Arguments ....................................................... 1814.1.5 Function Reentrancy ........................................................................ 1844.1.6 Static Function Variables ................................................................. 184

4.2 mikroC Built-in Functions ........................................................................ 1844.3 mikroC Library Functions......................................................................... 188

4.3.1 EEPROM Library ............................................................................ 1894.3.2 LCD Library.................................................................................... 1924.3.3 Software UART Library .................................................................. 1994.3.4 Hardware USART Library ............................................................... 2044.3.5 Sound Library.................................................................................. 2064.3.6 ANSI C Library............................................................................... 2084.3.7 Miscellaneous Library...................................................................... 212

4.4 Summary.................................................................................................. 2184.5 Exercises .................................................................................................. 219

Chapter 5: PIC18 Development Tools ...................................................2215.1 Software Development Tools .................................................................... 222

5.1.1 Text Editors..................................................................................... 2225.1.2 Assemblers and Compilers............................................................... 2225.1.3 Simulators ....................................................................................... 2235.1.4 High-Level Language Simulators ..................................................... 2245.1.5 Integrated Development Environments (IDEs).................................. 224

5.2 Hardware Development Tools................................................................... 2245.2.1 Development Boards........................................................................ 2255.2.2 Device Programmers........................................................................ 2395.2.3 In-Circuit Debuggers ....................................................................... 2425.2.4 In-Circuit Emulators ........................................................................ 2455.2.5 Breadboards..................................................................................... 248

5.3 mikroC Integrated Development Environment (IDE) ................................. 2515.3.1 mikroC IDE Screen ......................................................................... 2515.3.2 Creating and Compiling a New File................................................. 2585.3.3 Using the Simulator ......................................................................... 2655.3.4 Using the mikroICD In-Circuit Debugger......................................... 2725.3.5 Using a Development Board ............................................................ 277

5.4 Summary.................................................................................................. 2855.5 Exercises .................................................................................................. 285

www.newnespress.com

viii Contents

Page 9: 01~Front_Matter

Chapter 6: Simple PIC18 Projects ........................................................2876.1 Program Description Language (PDL) ...................................................... 288

6.1.1 START-END .................................................................................. 2886.1.2 Sequencing...................................................................................... 2886.1.3 IF-THEN-ELSE-ENDIF .................................................................. 2886.1.4 DO-ENDDO ................................................................................... 2896.1.5 REPEAT-UNTIL............................................................................. 290

Project 6.1—Chasing LEDs ............................................................................ 290Project 6.2—LED Dice ................................................................................... 295Project 6.3—Two-Dice Project........................................................................ 301Project 6.4—Two-Dice Project Using Fewer I/O Pins ..................................... 303Project 6.5—7-Segment LED Counter............................................................. 313Project 6.6—Two-Digit Multiplexed 7-Segment LED...................................... 319Project 6.7—Two-Digit Multiplexed 7-Segment LED Counter

with Timer Interrupt...................................................................................... 326Project 6.8—Voltmeter with LCD Display ...................................................... 334Project 6.9—Calculator with Keypad and LCD ............................................... 341Project 6.10—Serial Communication–Based Calculator ................................... 352

Chapter 7: Advanced PIC18 Projects—SD Card Projects .........................3717.1 The SD Card ............................................................................................ 371

7.1.1 The SPI Bus.................................................................................... 3737.1.2 Operation of the SD Card in SPI Mode ........................................... 377

7.2 mikroC Language SD Card Library Functions .......................................... 384Project 7.1—Read CID Register and Display on a PC Screen ......................... 385Project 7.2—Read/Write to SD Card Sectors................................................... 392Project 7.3—Using the Card Filing System ..................................................... 392Project 7.4—Temperature Logger ................................................................... 397

Chapter 8: Advanced PIC18 Projects—USB Bus Projects .........................4098.1 Speed Identification on the Bus ................................................................ 4138.2 USB States ............................................................................................... 4138.3 USB Bus Communication......................................................................... 414

8.3.1 Packets............................................................................................ 4148.3.2 Data Flow Types............................................................................. 4168.3.3 Enumeration.................................................................................... 417

8.4 Descriptors ............................................................................................... 4188.4.1 Device Descriptors .......................................................................... 4188.4.2 Configuration Descriptors................................................................ 4218.4.3 Interface Descriptors ....................................................................... 4238.4.4 HID Descriptors .............................................................................. 4258.4.5 Endpoint Descriptors ....................................................................... 426

www.newnespress.com

ixContents

Page 10: 01~Front_Matter

8.5 PIC18 Microcontroller USB Bus Interface ................................................ 4278.6 mikroC Language USB Bus Library Functions ......................................... 429Project 8.1—USB-Based Microcontroller Output Port ..................................... 430Project 8.2—USB-Based Microcontroller Input/Output .................................... 456Project 8.3—USB-Based Ambient Pressure Display on the PC........................ 464

Chapter 9: Advanced PIC18 Projects—CAN Bus Projects ........................4759.1 Data Frame............................................................................................. 481

9.1.1 Start of Frame (SOF) .................................................................... 4829.1.2 Arbitration Field............................................................................ 4829.1.3 Control Field................................................................................. 4849.1.4 Data Field ..................................................................................... 4849.1.5 CRC Field..................................................................................... 4849.1.6 ACK Field .................................................................................... 485

9.2 Remote Frame ........................................................................................ 4859.3 Error Frame............................................................................................ 4859.4 Overload Frame...................................................................................... 4859.5 Bit Stuffing ............................................................................................ 4869.6 Types of Errors ...................................................................................... 4869.7 Nominal Bit Timing ............................................................................... 4869.8 PIC Microcontroller CAN Interface ........................................................ 4899.9 PIC18F258 Microcontroller..................................................................... 491

9.9.1 Configuration Mode ...................................................................... 4939.9.2 Disable Mode................................................................................ 4939.9.3 Normal Operation Mode................................................................ 4939.9.4 Listen-only Mode .......................................................................... 4939.9.5 Loop-Back Mode .......................................................................... 4949.9.6 Error Recognition Mode................................................................ 4949.9.7 CAN Message Transmission.......................................................... 4949.9.8 CAN Message Reception............................................................... 4949.9.9 Calculating the Timing Parameters ................................................ 496

9.10 mikroC CAN Functions .......................................................................... 4989.10.1 CANSetOperationMode ............................................................... 4999.10.2 CANGetOperationMode .............................................................. 5009.10.3 CANInitialize .............................................................................. 5009.10.4 CANSetBaudRate ........................................................................ 5019.10.5 CANSetMask .............................................................................. 5019.10.6 CANSetFilter .............................................................................. 5029.10.7 CANRead.................................................................................... 5029.10.8 CANWrite................................................................................... 503

9.11 CAN Bus Programming .......................................................................... 504Project 9.1—Temperature Sensor CAN Bus Project ........................................ 504

www.newnespress.com

x Contents

Page 11: 01~Front_Matter

Chapter 10: Multi-Tasking and Real-Time Operating Systems....................51510.1 State Machines ....................................................................................... 51610.2 The Real-Time Operating System (RTOS) .............................................. 518

10.2.1 The Scheduler.............................................................................. 51810.3 RTOS Services ....................................................................................... 52110.4 Synchronization and Messaging Tools .................................................... 52110.5 CCS PIC C Compiler RTOS................................................................... 522

10.5.1 Preparing for RTOS .................................................................... 52310.5.2 Declaring a Task ......................................................................... 524

Project 10.1—LEDs........................................................................................ 524Project 10.2—Random Number Generator....................................................... 528Project 10.3—Voltmeter with RS232 Serial Output ......................................... 532

Index...............................................................................................541

www.newnespress.com

xiContents

Page 12: 01~Front_Matter
Page 13: 01~Front_Matter

Preface

A microcontroller is a microprocessor system which contains data and program

memory, serial and parallel I/O, timers, and external and internal interrupts—all

integrated into a single chip that can be purchased for as little as two dollars. About 40

percent of all microcontroller applications are found in office equipment, such as PCs,

laser printers, fax machines, and intelligent telephones. About one third of all

microcontrollers are found in consumer electronic goods. Products like CD players,

hi-fi equipment, video games, washing machines, and cookers fall into this category.

The communications market, the automotive market, and the military share the rest of

the applications.

This book is written for advanced students, for practicing engineers, and for hobbyists

who want to learn more about the programming and applications of PIC18F-series

microcontrollers. The book assumes the reader has taken a course on digital logic

design and been exposed to writing programs using at least one high-level programming

language. Knowledge of the C programming language will be useful, and familiarity

with at least one member of the PIC16F series of microcontrollers will be an advantage.

Knowledge of assembly language programming is not required since all the projects in

the book are based on the C language.

Chapter 1 presents the basic features of microcontrollers, discusses the important

topic of numbering systems, and describes how to convert between number bases.

Chapter 2 reviews the PIC18F series of microcontrollers and describes various

features of these microcontrollers in detail.

Chapter 3 provides a short tutorial on the C language and then examines the features

of the mikroC compiler.

www.newnespress.com

Page 14: 01~Front_Matter

Chapter 4 covers advanced features of the mikroC language. Topics such as built-in

functions and libraries are discussed in this chapter with examples.

Chapter 5 explores the various software and hardware development tools for the

PIC18F series of microcontrollers. Various commercially available development kits

as well as development tools such as simulators, emulators, and in-circuit debuggers

are described with examples.

Chapter 6 provides some simple projects using the PIC18F series of microcontrollers

and the mikroC compiler. All the projects are based on the PIC18F452 micro-

controller, and all of them have been tested. This chapter should be useful for those

who are new to PIC microcontrollers as well as for those who want to extend their

knowledge of programming PIC18F microcontrollers using the mikroC language.

Chapter 7 covers the use of SD memory cards in PIC18F microcontroller projects.

The theory of these cards is given with real working examples.

Chapter 8 reviews the popular USB bus, discussing the basic theory of this bus

system with real working projects that illustrate how to design PIC18F-based projects

communicating with a PC over the USB bus.

The CAN bus is currently used in many automotive applications. Chapter 9 presents

a brief theory of this bus and also discusses the design of PIC18F microcontroller-

based projects with CAN bus interface.

Chapter 10 is about real-time operating systems (RTOS) and multi-tasking. The

basic theory of RTOS systems is described and simple multi-tasking applications are

given.

The CD-ROM that accompanies this book contains all the program source files and

HEX files for the projects described in the book. In addition, a 2K size limited version

of the mikroC compiler is included on the CD-ROM.

Dogan Ibrahim

London, 2007

www.newnespress.com

xiv Preface

Page 15: 01~Front_Matter

Acknowledgments

The following material is reproduced in this book with the kind permission of the

respective copyright holders and may not be reprinted, or reproduced in any other way,

without their prior consent.

Figures 2.1–2.10, 2.22–2.36, 2.37, 2.38, 2.41–2.55, 5.2–5.4, 5.17, 5.20, 8.8, and 9.13,

and Table 2.2 are taken from Microchip Technology Inc. data sheets PIC18FXX2

(DS39564C) and PIC18F2455/2550/4455/4550 (DS39632D).

Figure 5.5 is taken from the web site of BAJI Labs.

Figures 5.6–5.8 are taken from the web site of Shuan Shizu Ent. Co., Ltd.

Figures 5.9, 5.13, 5.18 are taken from the web site of Custom Computer Services Inc.

Figures 5.10, 5.19, and 6.43 are taken from the web site of mikroElektronika Ltd.

Figure 5.11 is taken from the web site of Futurlec.

Figure 5.21 is taken from the web site of Smart Communications Ltd.

Figure 5.22 is taken from the web site of RF Solutions.

Figure 5.23 is taken from the web site of Phyton.

Figures 5.1 and 5.14 are taken from the web site of microEngineering Labs Inc.

Figure 5.16 is taken from the web site of Kanda Systems.

Thanks is due to mikroElektronika Ltd. for their technical support and for permission to

include a limited size mikroC compiler on the CD-ROM that accompanies this book.

PICW, PICSTARTW, and MPLABW are all registered trademarks of Microchip

Technology Inc.

www.newnespress.com