PIC Microcontroller Projects in C
Basic to Advanced
Second Edition
Table of Contents
Copyright
Newnes is an imprint of Elsevier
The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, UK
225 Wyman Street, Waltham, MA 02451, USA
First edition 2008
Copyright 2014 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 Elseviers Science & Technology Rights Department in Oxford, UK: phone (+44) (0) 1865 843830; fax (+44) (0) 1865 853333; email: , 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
A catalogue record for this book is available from the British Library
Library of Congress Cataloging-in-Publication Data
A catalog record for this book is available from the Library of Congress
ISBN-13: 978-0-08-099924-1
For information on all Newnes publications visit our website at http://store.elsevier.com/
Printed and bound in the UK
14 15 16 17 18 10 9 8 7 6 5 4 3 2 1
Preface
A microcontroller is a single chip microprocessor system that contains data and program memory, serial and parallel inputoutput, timers, external and internal interrupts, all integrated into a single chip that can be purchased for as little as $2.00. About 40% of microcontroller applications are in office automation, such as PCs, laser printers, fax machines, intelligent telephones, and so forth. About one-third of microcontrollers are found in consumer electronic goods. Products like CD players, hi-fi equipment, video games, washing machines, cookers, and so on fall into this category. The communications market, automotive market, and the military share the rest of the application areas.
There are many different types of microcontrollers available from many manufacturers. This book is about the PIC18F family of high-end 8-bit microcontrollers, developed and manufactured by Microchip Inc. The highly popular PIC18F45K22 microcontroller is used in the projects in this book. Many simple, intermediate level, and advanced projects are given in the book. Most projects are developed using the highly popular mikroC Pro for PIC compiler as well as the MPLAB XC8 compiler. All the projects are fully documented where the following is given for each project: project description, project hardware (and project block diagram where appropriate), project PDL, project program, and for some projects suggestions are given for possible modifications and improvements. All the projects have been tested and are working.
Knowledge of the C programming language will be useful. Also, familiarity with at least one member of the PIC16F series of microcontrollers will be an advantage. The knowledge of assembly language programming is not required because all the projects in the book are based on using the C language.
This book is written for students, for practicing engineers, and for hobbyists interested in developing microcontroller-based projects using the PIC series of microcontrollers. Attempt has been made to include as many projects as possible, limited only by the size of the book.
presents the basic features of microcontrollers.
provides a short tutorial on the C language and then examines the features of the highly popular mikroC Pro for PIC programming language and compiler used in projects in this book.
is about the MPLB X IDE and the XC8 programming language and compiler. Both the mikroC Pro and the XC8 program listings are given for most projects in the book. The reader should be able to convert easily from one language to the other.
describes the commonly used program development tools, such as the PDL and flowcharts. Examples are given for both tools.
gives simple projects using the PIC18F45K22 microcontroller. In this chapter, the projects range from simple LEDs, 7-segment LED displays, LCD displays, sound projects, and so on.
provides intermediate level projects. The projects in this chapter range from using the interrupts, using a keypad, generating waveforms in real time, serial communications, GPS data decoding, various bus systems, and so on.
provides more advanced projects. Some of the projects covered in this chapter are using the Bluetooth communication, RFid, real-time clock, using graphics LCDs, SD cards, Ethernet-based projects, using the CAN bus, multitasking in microcontroller systems, stepping motors, and DC motors. Although the projects on motors are not advanced, they are given in this chapter for completeness.
Dogan Ibrahim
London, 2014
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 way, without their prior consent.
.
is taken from the web site of Parallax Inc.
are taken from the web site of mikroElektronica.
PIC, PICSTART, and MPLAB are all trademarks of Microchip Technology Inc.
Chapter 1
Microcomputer Systems
Abstract
This chapter is an introduction to microcontrollers. The basic features of the microcontrollers are described briefly with some typical application areas of microcontrollers.
Keywords
Microcontrollers ; microprocessors ; microcontroller features ; microcontroller applications
Chapter Outline
1.1 . Introduction
The term microcomputer is used to describe a system that includes a minimum of a microprocessor, program memory, data memory, and inputoutput (I/O) module. Some microcomputer systems include additional components such as timers, counters, interrupt processing modules, analog-to-digital converters, serial communication modules, USB modules, and so on. Thus, a microcomputer system can be anything from a large system having hard disks, keyboard, monitor, floppy disks, and printers to a single chip embedded controller.
In this book, we are going to consider only the type of microcomputers that consists of a single silicon chip. Such microcomputer systems are also called microcontrollers and they are used in many everyday household goods such as personal computers, digital watches, microwave ovens, digital TV sets, TV remote control units (CUs), cookers, hi-fi equipment, CD players, personal computers, fridges, etc.