Table Of ContentProgramming PIC
Microcontrollers with PicBasic
Programming PIC
Microcontrollers with PicBasic
by Chuck Hellebuyck
Newnes is an imprint of Elsevier Science.
Copyright ©2003,Elsevier Science (USA). 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.
Recognizing the importance of preserving what has been written,Elsevier Science prints its books on acid-free
paper whenever possible.
Library of Congress Cataloging-in-Publication Data
A catalogue record for this book is available from the Library of Congress
British Library Cataloguing-in-Publication Data
A catalogue record for this book is available from the British Library.
The publisher offers special discounts on bulk orders of this book.
For information,please contact:
Manager of Special Sales
Elsevier Science
200 Wheeler Road
Burlington,MA 01803
Tel:781-313-4700
Fax:781-313-4882
ISBN 1 5899 5001 1
For information on all Newnes publications available, contact our World Wide Web home page at:
http://www.newnespress.com
10 9 8 7 6 5 4 3 2 1
Printed in the United States of America
Dedication
This book is dedicated to my wife Erin and my children Chris, Connor, and
Brittany.
This book would never have happened without your support.
Contents
Introduction xi
Chapter One:Getting Familiar with PICs and PicBasic . . . . . . . . . . . .1
PIC Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Software for PICs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Assembly Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
PicBasic Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Chapter Two:PicBasic Compiler (PBC) . . . . . . . . . . . . . . . . . . . . . . . .13
How PBC Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Variables, Memory, and I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Program Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
PBC Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Using PBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
vii
Chapter Three: The PicBasic Pro Compiler . . . . . . . . . . . . . . . . . . . .51
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Numeric and ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
I/O Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
I/O Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
Math Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Binary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
PBPro Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
Chapter Four:Inside the PIC Microcontroller . . . . . . . . . . . . . . . . .117
Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117
Program Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118
Reset Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119
Data Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
STATUS Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
I/O Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
A/D Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Peripheral Interrupt Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
OPTION Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
viii
INTCON Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Chapter Five:Simple PIC Projects . . . . . . . . . . . . . . . . . . . . . . . . . .133
Project #1—Flashing an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Project #2—Scrolling LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
Project #3—Driving a 7-Segment LED Display . . . . . . . . . . . . . . . . . . . . .146
Chapter Six:Moving on with the 16F876 . . . . . . . . . . . . . . . . . . . . . .153
Project #4—Accessing Port A I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Project #5—Analog-to-Digital Conversion . . . . . . . . . . . . . . . . . . . . . . . .162
Project #6—Driving a Servomotor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Chapter Seven:Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
Project #7—Driving a LCD Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
Project #8—Serial Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195
Project #9—Driving a LCD with a Single Serial Connection . . . . . . . . . . .204
Chapter Eight:Memory and Sound . . . . . . . . . . . . . . . . . . . . . . . . . .221
Project #10—Using External Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . .222
Project #11—Accessing Internal Memory . . . . . . . . . . . . . . . . . . . . . . . . .232
Project #12—Making Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241
ix
Chapter Nine:Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249
Project #13—Robot Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
Project #14—Line Tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
Project #15—Obstacle Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284
APPENDIXA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
APPENDIX B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
x