Hubert Henry Ward
Leigh, UK
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/978-1-4842-6067-8 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-6067-8 e-ISBN 978-1-4842-6068-5
https://doi.org/10.1007/978-1-4842-6068-5
Hubert Henry Ward 2020
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 NY Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
This book looks at some useful aspects of the PIC microcontroller. It explains how to write programs in C so that you can use the PIC micro to control a variety of electronics and DC motors. After reading this book, you will be well on your way to becoming an embedded programmer using the C programming language.
The Aims and Objectives of This Book
The main aim of this book is to introduce you to some useful applications of programming PIC micros such as
Creating header files
Controlling seven-segment displays
Using an LCD display with two lines of 16 characters
Pulse width modulation
Using driver ICs such as the ULN2004A
Controlling DC motors, including stepper motors and servo motors
Using every aspect of the Capture, Compare and PWM, CCP module in the PIC
Using interrupts
Writing to the EEPROM
The Objectives of This Book
After reading through this book, you should be able to program the PIC to use all of the above. You should have a good understanding of some of the advance programming techniques for PIC micros. You should be able to download your programs to your PIC in a practical situation where you have the ability to design and build some useful projects.
The Prerequisites
There are none really, but understanding the C programming language will be useful. However, I will explain how each program works as we go through them.
Also, if you understand the binary and hexadecimal number systems, it will be an advantage but there is a section in the Appendix that will help you with that.
However, to get the full use out of this book, you will need to install the following software:
MPLABX, which is the IDE from Microchip. The version in the book is MPLABX Version 5.25. However, any version later than 2.20 is OK.
A C compiler for the 8-bit micro. I use XC8 (V2.10) but with some programs I use XC8 (V1.35) compiler software. However, you should be aware that some of the later compilers are missing some useful libraries. This is why I sometimes use version 1.35.
All of these programs are freely available from the Microchip web site.
Another useful piece of software is a suitable ECAD (electronic computer-aided design) software program that supports 8-bit micros. The ECAD software I use is PROTEUS. However, it is not free, so as well as showing you how to simulate the programs in PROTEUS, I will show you how to use a suitable prototype board to run the programs in a practical situation.
If you want to go down the practical route, you will need to purchase a programming tool and a prototype board.
The tools I use are either the ICD3 can (Microchip has now moved onto the ICD4 can) or the PICkit3 programmer to download the programs from MPLABX to the PIC.
The prototype board I use is the picdem2 plus DEMO BOARD and a prototype board from Matrix Multimedia (although Matrix no longer produces the more versatile board that I use).
This book was written based around using MPLABX V5.25. However, the principles of how to create projects and write programs are transferable to earlier and later versions of MPLABX. There may be some slight differences in the details, but they shouldnt cause too many problems.
The PIC that this book is based around is the PIC18F4525. This is a very versatile 8-bit micro that comes in a 40-pin dual-inline package. As long as the PIC you want to use has the same firmware modules, then the programs in the book can easily be used on other PIC micros with some minor modifications. However, you should always refer to the data sheet for the particular PIC you use because some of the SFRs (special function registers) may differ. For example, the PIC18F4525 uses the ADCON0, ADCON1, and ADCON2 SRFs to control the ADC module but the 16F88 uses the ANSEL, ADCON0, and ADCON1 registers.
Throughout the book, I include program listings and I go through an analysis of any new instructions that the listings introduce. With respect to the first listing, I will assume that all of the instructions are new to you, the reader.
Before we move into the book for real, I think it will be useful to you if I explained a bit about what MPLABX is. It is an industrial IDE created by Microchip. The term IDE stands for integrated development environment. It is actually a lot of programs collected together to create a programming environment:
There is an editor, which is slightly more than a simple text editor. However, in my early days, I used to write my programs in Notepad.
There is also a compiler program that converts your program instructions from C to the machine code that all microprocessor-based systems use. In the very early days of programming, the programmers used to write in this machine code. This was a bit before my time, although in my early days, I wrote all my programs in assembler. Assembler is the closet language to the actual machine code that all micros use.