PIC Projects and Applications using C
A Project-based Approach
Third Edition
David W. Smith
Table of Contents
Cover image
Title page
Copyright
Preface
Chapter 1: Introduction to the Microcontroller and C
Chapter 2: First C Program
MPLAB and MPLABX Installation
A Brief Introduction to C for the Microcontroller
Writing the Code Using MPLAB IDE
Types of Errors
Programming the Microcontroller
Writing the Code Using MPLABX
Creating a New Project
Chapter 3: Using Eight Outputs
Explanation of the Code
Looping in a Program
The Disco Code
The Header Program
Chapter 4: Inputs
IF Statement
IfElse
Using Several Inputs
Chapter 5: Keypad Scanning
Four Digit Code
Putting the Scan Routine in a Header File, dwsScan.h
Chapter 6: Analogue to Digital Conversion
Configuring the A/D Device
ADCON0 AD Control Register
ADCON1 AD Control Register
ADCON2 AD Control Register
ADRESH and ADRESL: A/D Result Registers High and Low Byte
The Thermistor
Making an A/D Reading with the Thermistor
The LM335 Temperature Sensing IC
Operation of the LM335
A/D Application
Question
Using Several AD Inputs
Solar Panel Heating System
Microcontrollers with More A/D Inputs
The PIC18(L)F43K22 28A/D Inputs
Chapter 7: Alpha Numeric Display
Configuring the Display
Program LCD.C
Using Header: dwsLCD.h
Program LCDh.C
Character Display Address
Displaying Measurements
Displaying Room Temperature
RoomTemperature.C
Chapter 8: Porting Code to Other Microcontrollers
The Header Code
Chapter 9: Timer/Counter Modules
TMR0 Prescaler
Timing an Event
An Accurate 1 s Time Period
An Accurate 1 min Delay
24 h Clock
30 min Delays and Longer
Chapter 10: Interrupts
Explanation of the Program RBinterrupt.C
TMR0 Overflowing Interrupt
Chapter 11: Fault Finding, Using the Simulator, and the In-Circuit Debugger
The Simulator Using MPLAB
Setting Breakpoints
Stopwatch
Further Information
Chapter 12: Radio Transmitters and Receivers
The Transmitter
The Receiver
Examples
Chapter 13: EEPROM Data Memory
Chapter 14: Projects
Project 1: Countdown Timer
Project 2: Chess Clock
Project 3: Resistance Meter
Project 4: Capacitance Meter
Project 5: Voltmeter
Chapter 15: C Extra
Data Types
Conditional Operators
Appendix A: Data Sheets
Appendix B: Useful Contacts
Index
Copyright
Newnes is an imprint of Elsevier
The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, UK
Wyman Street, Waltham, MA 02451, USA
First edition 2002
Second edition 2006
Third edition 2013
Copyright 2013 Elsevier Ltd. All rights reserved
No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the publisher. Details on how to seek permission, further information about the Publishers permissions policies and our arrangement with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found at our website:
This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may be noted herein).
Notice
Knowledge and best practice in this field are constantly changing. As new research and experience broaden our understanding, changes in research methods, professional practices, or medical treatment may become necessary.
Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any information, methods, compounds, or experiments described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility.
To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability 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.
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: 978-0-08-097151-3
For information on all Newnes publications visit our website at www.books.elsevier.com
Printed in the United States of America
Preface
DW Smith, BSc., in Electronics, Manchester Metropolitan University, April 2013
The aim of this book is to enable the reader to program the 18F series of PIC Microcontrollers in the C language using Microchips MPLAB C18 compiler. The program examples demonstrate the power of the C language, yet the reader does not have to be a C programmer in order to benefit from this technology as the C language is added and explained as required.
The chapters show numerous applications starting with switching outputs on, then using digital inputs such as switches and keypads. This book continues with making measurements from analogue inputs, writing to alpha numeric displays, using timers and interrupts, and transmitting data via radio links.
There is a section on fault finding using the MPLAB simulator and in- circuit debugger. So that faults can be located easier.
All of the chapters show applications on how to use the program examples. The programs are complete and are clearly explained.
My aim has been to show the reader how to use the Microcontroller to develop programs for projects. I have tried to keep the technical detail down to a minimum and have not gone into a deeper understanding of how the microcontroller is working inside. An understanding of the electronics inside the microcontroller is not necessary to enable the reader to program it.
The reader is encouraged to build the programs, see how they work, and then modify the code to enable a clearer understanding of the principles involved; a development kit is available to do this. My own students have been ingenious in developing faults in code and I have used their efforts, hopefully, to produce an easy-to-understand guide to programming the PIC micro in C.
The programs listed in this book and the details of the development kit are available from the books companion website: