Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Programming PIC Microcontrollers with PicBasic
Nội dung xem thử
Mô tả chi tiết
Programming 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
APPENDIX A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
APPENDIX B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
x
Introduction
Electronics has been my hobby and profession for over 25 years. I started as a
young child building kits from Radio Shack and projects described in electronics
magazines and books. When microprocessors were first developed, I was fascinated
with them. I was a bit too young to really understand how they worked, but I could
see they would replace the batches of discrete integrated circuits (ICs) my previous
electronic projects depended on. I soon discovered microprocessors required many
more tools and resources (like money) than I could afford. This made it difficult to
build a home lab for micro-based designing so I never got involved during all the
early years of microprocessor development.
I went on to earn a bachelor’s degree in electrical engineering and made electronics my profession. Although I had learned how to program and work with some
of the best microprocessor tools, I still didn’t see the opportunity to build a home
lab for microprocessor development without spending a bunch of money.
Then I discovered the Microchip PIC family of microcontrollers. They were
inexpensive, easy to purchase through various sources, and development tools were
inexpensive. I bought a PIC programmer and started playing with electronics as a
hobby again. Although I developed some interesting projects using Microchip
assembly code, I really longed for a simple form of programming like the BASIC
language because I didn’t have a lot of spare time.
A company named Parallax began advertising a small PIC-based computer
module called the “Basic Stamp” that could be programmed in a form of BASIC. I
bought one and I started playing with it. It was easy to use, and I had a lot of fun
with it. But it had memory limitations and was a bit expensive to make permanent
designs with. I had spent a lot of time developing gadgets and really wanted to turn
a couple of my ideas into products I could market.
xi
I thought about developing my own Basic compiler for the Parallax computer
module that would allow me to program a PIC directly. Then I saw an advertisement
for a new product from microEngineering Labs called the PicBasic compiler. It
could convert a program written for the Parallax module into the code format
required to program a PIC. It used the same commands as the Parallax module along
with a few more. I purchased one immediately and began designing in PicBasic.
I found it to be a simple but very powerful compiler. I could develop complex
projects in a few days rather than weeks or months with assembly language. I
designed a few products and began to market them through my website at
www.elproducts.com. I also decided to write an article for Nuts and Volts magazine
about the Microchip PICs and fortunately got it published in July 1998. I was then
approached about writing a book on PICs. I never thought of myself as an author
but I saw it as an opportunity to share my knowledge about PICs and PicBasic with
those who might enjoy this stuff as much as I do.
As I wrote, many things got in the way and this book took far longer to write
than I had originally expected. But the delay allowed this Basic programming
method to become more popular. New compilers from other companies, new programming accessories and hardware began to show up all over the place. The PICs
and the PicBasic compilers improved as well.
As it evolved and my own experience increased, I tried to capture as much as
possible in this book but still keep it at the entry level. One result of my increasing
experience was to modify the original outline to include a chapter on robotics.
Robotics has become very popular during the time I wrote this book, and I believe
it’s because there were more people like me who were using all the new affordable
yet powerful microcontroller tools to develop robots in their home labs.
Using Basic to program microcontrollers began to be called embedded Basic
programming and recently I’ve seen job postings for PicBasic programmers. It’s
become harder to find people who are trained at programming in assembly code,
with so many electronic development companies switched to the C language. I
believe embedded Basic will be the next wave of programming for small module
high-volume designs since it’s so much easier to write and almost as efficient as C.
xii
I hope you find this book informative and challenging, not to mention enjoyable.
Everything in here was learned the hard way—by trial and error. Microchip has
some great components and the PicBasic compiler makes it easy for everyone to
become an embedded Basic designer. You can visit my website for more info on
some of the latest embedded Basic products. If you have any questions, I can be
reached via email.
Chuck Hellebuyck
Electronic Products
www.elproducts.com
xiii
Getting Familiar with
PICs and PicBasic
The PIC (Programmable Interface Controller) line of microcontrollers was originally developed by the semiconductor division of General Instruments Inc. The first
PICs were a major improvement over existing microcontroller because they were a
programmable, high output current, input/output controller built around a RISC
(Reduced Instruction Set Code) architecture. The first PICs ran efficiently at one
instruction per internal clock cycle, and the clock cycle was derived from the oscillator divided by 4. Early PICs could run with a high oscillator frequency of 20 MHz.
This made them relatively fast for an 8-bit microcontroller, but their main feature
was 20 mA of source and sink current capability on each I/O (Input/Output) pin.
Typical micros of the time were advertising high I/O currents of only 1 milliampere
(mA) source and 1.6 mA sink.
General Instruments eventually sold its semiconductor division, along with the
PIC manufacturing facility in Chandler, Arizona, to a venture capitalist group that
formed what is now known as Microchip Technology. PICs quickly became the
main components offered by the new company.
Initially the selections were small and none of them had common microcontroller features such as timer overflow or external interrupts. They also used a somewhat unusual banking arrangement for memory that still exists today in many of
Microchip’s parts. Despite these limitations, the PICs sold well and allowed
Microchip to develop new components with new features including interrupts, onboard A/D (Analog/Digital) conversion, on-board comparators, and more.
1
CHAPTER 1