Easy PIC’n: A Beginner’s Guide to Using Pic16/17 Microcontrollers from Square 1 0965416208, 9780965416207

1,994 388 16MB

English Pages 152 [169] Year 1996

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Easy PIC’n: A Beginner’s Guide to Using Pic16/17 Microcontrollers from Square 1
 0965416208, 9780965416207

Citation preview

,.....---------------------------.. ,

A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM

VERSION 3.1

David Benson

Ple'n

A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM

DAVID BENSON VERSION 3.1

NOTICE The material presented in this book is for the education and amusement of students, hobbyists, technicians and engineers. Every effort has been made to assure the accuracy of this information and its suitability for this purpose. Square 1 Electronics and the author assume no responsibility for the suitability of this information for any application nor do we assume any liability resulting from use of this information. No patent liability is assumed for use of the information contained herein. All rights reserved. No part of this book shall be reproduced or transmitted by any means (including photo copying) without written permission from Square 1 Electronics and the author. Copyright © 1997

Revised April, 1999

TRADEMARKS PIC is a registered trademark of Microchip Technology Inc. in the U.S.A. PICmicro is a registered trademark of Microchip Technology, Inc. PIC16j17 is a registered trademark of Microchip Technology, Inc. MPLAB is a registered trademark of Microchip Technology, Inc. MPASM is a registered trademark of Microchip Technology, Inc. MPSIM is a registered trademark of Microchip Technology, Inc. PICSTART Plus is a registered trademark of Microchip Technology, Inc. MS-DOS is a registered trademark of Microsoft Corporation. Windows is a registered trademark of Microsoft Corporation.

ISBN 0-9654162-0-8

PUBLISHER

Square 1 Electronics P.O. Box 501 Kelseyville,CA 95451

U.S.A.

voice (707)279-8881 FAX (707)279-8883 EMAIL [email protected] http://www.sq-1.com

A BEGINNER'S GUIDE TO USING PIC MICROCONTROLLERS FROM

INTRODUCTION

1

PIC PRODUCT OVERVIEW

3

PIC16F84

5

Pins and Functions Package Clock Oscillator Configuration bits Reset Ports Special Features Watchdog Timer Power-up Timer Sleep Mode PIC16 Architecture Program Memory Weird Hex Notation Fi le Registers Working Register (W) Option Register Stack Reset Vector Interrupt Vector Option Register Program Counter Status Register

5 5 6 7 7 8 8 8 8 8 9 9 9

10 11 11 11 11 11 12 13 15

CIRCUIT FOR EXPERIMENTS

16

'84 On A Board

17

CHOOSING DEVELOPMENT TOOLS MPLAB/Windows vs. MPASM/MS-DOS Choosing A Device Programmer

20 20 20

USING AN ASSEMBLER Source Code For The Assembler Semicolon (;) Tabs Style Headers Labels Equates Literals Origin End Program Format Files Used By Assembler Files Created By Assembler Preventing Some Gotchas

USING THE MS-DOS BASED MPASM ASSEMBLER How To Assemble A Program

MPLAB OVERVIEW - GETTING STARTED Using MPLAB - Getting Started Toolbars Select Development Mode And Device Type Project Te xt Editor Edit Project Assembler Mo r e MPLAB Operations

22 22 23 24 25 26 26 27 28 29 30 30

31 31 33

34 34

37 37 37

38 38 39 39 40 40

PROGRAMMING A DEVICE

41

WRITI NG PROGRAMS

43

Programming Concepts Instruction Set Weird Move Instruction Instruction Format For The Assembl~r Byte -Oriented I nstructions Bit-Oriented Instructions Literal Instructions Control Instructions (CALL and GOTO) Destination Designato r (d) He xadecimal Numbers vs . MPASM Assembler Binary And Decimal Numbers vs. MPASM Assembler ASCII Characters v s . MPASM As s e mbl e r Addressing Modes Immediate addressing Direct Ad dre s s ing Indirect Ad dres s i n g Re l a t ive Addressi ng Use Of The RETLW Inst ruction For Accessing Tables of Data Via Re l a t i v e Addressing

43 46 48 49 49 50 50 50 51 52 53 53 54 54 54 54 55 56

using The Ports Data Direction Port Read/Write Flags Simple Data Transfers Loop - Endless Loop With a Counter Loop Until Comparisons Bit Manipulation Using Bit Manipulation Instructions Bit Manipulation Using Logic Instructions Using Bit Manipulation Sequencing Subroutines Time Delay Loop Lookup Tables INTERRUPTS

58 58 58 59 59 61 62 65 66 69 71 73 77 80 82 91 94

Interrupt Control Register 95 Interrupt Sources 96 External 96 Timer/Counter 96 Port B Interrupt On Change - Bits 7,6,5,4 97 Global Interrupt Enable Flag 97 Saving Status During An Interrupt (Context Saving) 97 Where To Put The Interrupt Service Routine 98 Interrupt Latency 99 Multiple Interrupt Sources 100 Example - External Interrupt 101 TIMING AND COUNTING Digital Output Waveforms Timing And Counting Using The PIC16F84's TMRO On-Board Timer/Counter Prescaler Changing Pres caler Assignment Using The Timer/Counter Timer/Counter Experiments Digital Output Using TMRO - Internal Clock Single Time Interval - Internal Clock Single Time Interval - External Clock Free Running Mode - Internal Clock Free Running Mode - External Clock Counting Events (Pulses)

107 107 108 110 111 112 113 113 115 118 121 125 126

PIC16C54 Pins And Functions Packages Clock Oscillator Configuration bits Ports Architecture Program Memory File Registers Stack Reset Vector Program Counter Option Registe r Status Register No Timer/Counter Overfl ow Interrupt/Flag Out put Fewer Instructions PIC16C54 Programming Examp le Timing And Counting Timer/Counter Description Using The Timer/Counter Timer/Counter Experiment MENDING OUR WAYS Sink vs. Source File Register Bank Switching Bank Switching Demo Interrupts And Bank Switching Program Memory Paging Include Files I/O CONVERSION Input Conversion Output Conversion MORE PIC'n BOOKS

131 132 1 32 133 133 133 1 34 1 34 13 4 1 35 13 5 1 36 136 13 7 13 7 13 7 138 139 1 41 142 142 143 143 144 145 1 48 1 48 1 48 151 15 1 15 4 157

APPENDICES Appendi x A - Sources Appendi x B - Hexadecimal Numbers Appendi x C - Program Listings vs. Page Number

159 161 1 62

INTRODUCTION The PIC was originally designed as a Peripheral Interface Controller (PIC) for a l ri-bit microprocessor. It was essentially an I/O controller and was designed to be very fast. It had a small microcoded instruction set. This design became the basis for Microchip Technology's PICI6C5X family of micro controllers. The PICI6F84 (or PICI6F84A) microcontroller is unique because its program memory is made using Flash technology. It can be programmed, tested in-circuit and reprogrammed if neces sary in a matter of a few minutes and without the need for a UV EPROM eraser. It is a small device (18-pin), readily available to all including hobbyists and students at a cost of $6.00 (at this writing) in single quantity. The PICI6F84 replaces the PICI6C84 which will soon be obsolete. Think of the PIC I6F84 as a custom I/O handler. It looks at its inputs and, based on what it sees, it sends signals out its outputs. You can customize it to do what you want via programming. It is not a heavy duty number cruncher or data manipulator. The MPASM (tm) assembler from Microchip Technology (manufactu rer of PIC family) is the PIC assembler of choice for use in this book because: • It is free from Microchip. • Most people who are into PICs speak "MPASM". • Most examples in magazines and on the Internet are written in the MPASM dialect. • It is included in MPLAB (tm), a popular development software package (available free from Microchip). A variety of programmers are available for the PICI6F84 from indep endent programmer manufacturers (see sources in Appendix A). Some range in cost from $40 to $80. Some come with their own assemblers but will accept hex files created by MPASM (detail s later). Microchip makes the PICSTART Plus (tm) programmer which will program all PIC microcontrollers. The cost is currently about $200. The PICI6F84, MPASM assembler (either stand alone or as part of MPLA B) and a low cost PICI6F84 programmer make a very inexpensive development system and a great way to get started creating microcontroller based projects. What you learn using the PICI6F84 is directly applicable to the whole line of PIC microcontrollers. The appro ach taken with the PICI6F84 in the first part of book is backwards compatible with the PICI6C54 (explained toward the end of the book). The hassles of file register bank switching and program memory paging are avoided as they are confusing to a beginner but manageable after some experience is gained.

1

Learn ing how PICs work and how to apply them involves study in three areas: • IBM compatible computer use (as needed). • Assembler. • PIC itself. PIC s are not easy to write code for by hand because they are not intuitive. Many instructions requi re setting or clearing a bit to specify the destination for the result of executing the instruction. This is OK as an initial learning experience, but would become tedious very soon. So use of "power tools" is essential. This means learning to use an IBM compatible computer if you haven't already done so. It also means learning to use an assembler which converts English-like readable instructions into machine language understood by the PIC itself. Finally, learning about the PIC's inner workings is possible once use of the power tools is understood. The object is to make this process as easy and enjoyable as possible. Once you get through this and you have programmed a PIC16F84 for the first time, a whole new world awaits. You will be able to create more interesting projects and have more fun! The usual approach to teaching the use of the PIC has been to get into all of the assembler commands and then show advanced examples. There has been no simple example showing how to get started. As usual, only 5 percent of this information is needed to get started, but which 5 percent. The approach taken here will be to give you the 5 percent you need to get going . The assumption is made that you know how to do the following on an IBM compatible computer: • Create a new directory called \PIC. • Copy the contents of a floppy disk to the directory \PIC. • Use a simple text editor to create a text file, save it, make a copy of it, print it, and copy it to the directory \PIC.

2

PIC16/17 PRODUCT OVERVIEW This book uses the PIC16F84 and later the PIC16C54 as examples to get you started. The emphasis is on the base-line (12-bit core) and mid-range (14-bit core) products. The 12-bit core designates the instruction word length. You really won't care how many bits there are in an instruction word, but it is the basis on which Microchip divides up their product line. The following brief tables are for reference:

Stack Levels Interrupts Instructions Serial Programming File Registers Example

Base-Line

Mid-Range

12-bit Core

14-bit Core 8 Yes 35 * Yes 256 max ** PIC16F84

2

No 33 No 128 max ** PIC16C54

* We will use 37 - details later ** Varies with part number PARTS IS PARTS

As the Microchip rnicrocontroller product line grows, and I am glad that it is, the part numbering convention/system seems to be getting a bit involved . Also, the microcontroller product line designation has been changed from PIC16/17 (tm) to PICm icro (tm), Microchip is going to do what it will. You and I, however, need a way to keep things straight between us. The real key to sanity here is to recognize that there are three fundamental product groups in terms of how they function and, to some extent, how program code must be written for them. The three product groups have 12-bit, 14-bit and 16-bit cores. We don't really care how many bits are in an instruction word. We just need to know which set of rules and features apply when writing code for a selected chip. Here are some popular examples: Device PIC12C508 12C671 16C5 4 16C55 4 16C63 16C71 16C74 1 6F8 4

Core Size 12 14 12 14 14 14 14 14

Pins 8 8

18 18 28 18 40 18

3

In days gone by, the 12CXXX parts had 12-bit cores, the PIC16C5 whatever parts had 12-bit cores, etc. But no more. Forget the part numbering scheme and look at the data book or summary for core size. This book talks only about the 12-bit core base-line and 14-bit core mid-range parts . We will use PIC16 to designate the 12 and 14-bit core parts for lack of anything better to do. The PIC16C554 is pin-far-pin compatible with the very popular PIC16C54 and PIC16C54A and currently sells for the same price! The PIC16C554 has a 14-bit core and the features that go with it such as interrupts and an 8-level stack, plus 2.5 times the number of file registers. I would guess that the 14-bit core parts will eventually (long term) replace the 12-bit core parts . As an aside, notice that the PIC16C556A might be useful as a OTP version of the PIC16F84, less the EEPROM data memory. Food for thought. The PIC16C84 has been replaced by the PIC 16CF84. There is a PIC16F84A which is identical to the PIC16F84 for our purposes and you can use either one. Microchip is now publishing a Product Line Card revised quarterly which will tell you what's new and what's current. I find it to be very useful.

4

PIC16F84 PINS AND FUNCTIONS

RA2 RA3 RA4!TOCKI MCLR Vss RBO/INT RB1 RB2 RB3

1 2 3 4 5 678-

PIC16F84

9-

18 17 16 15 14 13 12 11 10

RA1 RAO OSC1/CLK IN OSC2/CLK OUT Vdd RB7 RB6 RB5 RB4

The PICI6F84 is fabricated using CMOS technology. It consumes very little power and is fully static meaning that the clock can be stopped and all register contents will be retained. The maximum sink or source current for a port at any given time is as follows:

Any I/O Pin

Sink current Source current

25 rnA 20 rnA

Port A

Port B

80 rnA 50 rnA

150 rnA 100 rnA

Supply current is primarily a function of operating voltage, frequency and I/O pin loading and is typically 2 rnA or so for a 4MHz clock oscillator. This drops to approximately 40 microamps in the sleep mode. Because the device is CMOS, all inputs must go somewhere. All unused inputs should be pulled up to the supply voltage (usually +5 VDC). PACKAGE The PICI6F84 is available in an I8-pin DIP package suitable for the experimenter. The part number is PICI6F84-04/p for the 4 MHz part.

5

CLOCK OSCILLATOR Four different types of clock oscillators may be used with PIC16F84 parts in general. The clock oscillator types are: RC - resistor/capacitor. XT - crystal or ceramic resonator - external clock. HS - high speed crystal or ceramic resonator - external clock. LP - low power crystal - external clock. At programming time, the part must be told via configuration bits what type of oscillator it will be connected to. The details of various oscillator circuits and components are given in the Microchip data book. One possible clock circuit for experimentation is a 4 MHz crystal-controlled oscillator in a can. They are accurate, always start and the math required for designing time delay loops is simple. The external clock frequency is divided by 4 internally, so the internal clock frequency is 1 MHz. Almost all instructions are executed in one instruction (internal clock) cycle or 1 microsecond in this case. This is plenty fast for experimenting. The oscillator type code is XT.

+5VDC 14

1

J'L/"1.M..

I

.... r'

8

fosc/4'-- OSC2

4MHz Clock Oscillator 1

OSC1

PIC16F84

1 -1-7 N.C.

Ceramic resonators with built-in capacitors are small and inexpensive while providing good accuracy (+/- 1.3 percent or better). They look a lot like a small ceramic capacitor except for the fact they have three leads. A typical device looks like this:

OSC1 ,..---

Note: Devices are symmetrical center lead connects to ground, remaining 2 leads connect to OSC1/0SC2 (doesn't matter which is which).

:DI~ OSC1

OSC2

OSC2

PIC16F84

I-rl~

---

Ceramic Resonator

A standard frequency is 4.00 MHz which offers the advantages mentioned above. Use XT for oscillator type. Keep the wires/traces short.

6

An RC clock circuit may also be used where cost is an issue and timing accuracy is not. Examples are shown. The oscillator type code is RC.

+5VDC

t - - - - - - I OSC1

C

T

fosc/4 ~ OSC2

PIC16F84

R

c

10K 10K

20 pf 220 pf

10K 0.1

~

fosc (approx)

f

625 KHz 80 KHz

85 Hz

CONFIGURATION BITS

The configuration bits are located in Flash memory outside the main part of Flash memory used for program storage. There are five configuration bits. Two select clock oscillator type, one is the watchdog timer enable bit, one is the power-up timer enable bit and one is the code protection bit. The device programmer accesses these bits during the device programming procedure. RESET

The PICl6F84 has built-in power-on reset which works as long as the power supply voltage comes up quickly. Commonly the MCLR pin is merely tied to the power supply. A switch may be used to regain control if things run away.

+5VDC 10K";:,-

Reset

PIC16F84

7

PORTS Port A is 5 bits/lines wide and port B is 8 bits/lines wide or byte-wide. Each port line may be individually programmed as an input line or output line. This is done using a special instruction which matches a bit pattern with the port lines. A "0" associated with a port line makes it an output, a "I" makes it an input. Examples follow. The PIC16F84 has port data direction registers as many other microcontrollers do. The PIC16C54 does not (more on this later). Using the port data direction registers involves file register bank switching which I have chosen to avoid for now. Doing it in the manner I am about to describe goes against the recommendation of Microchip (concerned about compatibility with future products). My primary concern is to make learning about PIC16s as simple as possible. So the initial programming examples will use an obsolete instruction (fRIS) to avoid having to deal with bank switching at this early stage. A benefit of this is backward compatibility. You will need to learn to use this technique if you choose to work with base-line parts (pIC16C5X series). Ignore the "Obsolete Instruction" warning messages generated by MPASM. The Port B lines have weak pullup resistors on them which may be enabled or disabled under software control. All 8 resistors are enabled/disabled as a group via the RBPU bit in the option register. The pullup resistor on an individual port line is automatically turned off when that line is configured as an output. The pullups are disabled on power-on reset. Port A, bit 4 is shared with the external timer/counter input called TOCK!. As a digital input line, the input is Schmitt trigger. As a digital output line, it is open drain, so a pullup resistor is required. The output cannot source current, it can only sink current. All unused port lines should be tied to the power supply (CMOS rule - all inputs must go somewhere). On reset, all port lines are inputs.

SPECIAL FEATURES

Watchdog Timer The watchdog timer is useful in some control applications where a runaway program could cau se a safety problem. We will not deal with it except to say that it is important to select "watchdog timer off' when programming the configuration bits.

Power-up Timer The power-up timer should be selected "ON" when programming the configuration bits.

Sleep Mode The feature of the "sleep mode" is drastically reduced power consumption achieved by turning off the main clock oscillator. .

8

PIC16 ARCHITECTURE PIC16 microcontrollers have two separate blocks of memory; program memory and file registers.

Program Memory The PICI6F84 program memory is 14 bits wide and lK words long. Program memory is Flash. Program memory is read-only at run time. PIC16s can only execute code contained in program memory.

Program Memory

Pointed To By -""" Reset Vector ~

OxOOO 001 Pointed To By --"'" 002 Interrupt Vector ~ 003 004

Ox3FF

A limited amount of data may be stored in program memory as part of RETLW instructions (see programming chapter).

Weird Hex Notation Some assembler listings show two-digit hexadecimal addresses in the form OXXX where the upper case x's mean don't care. The "Ox" means hexadecimal. The Ox notation comes from the C programming language. The main thing is, when you see OxOF, it means hexadecimal OF. Ox004 means hexadecimal 004.

9

File Registers The file registers are 8 bits wide with the exception of the PCLA1H register which is 5 bits wide. The PIC16F84 has a 80 file register address space (OxOO - Ox4F), but not all addresses are used. File RegIsters

OxOO Indirect Address 01 TMRO 02 PCL 03 Status 04 File Select 05 Port A Data 06 Port B Data 07 08 EEDATA (Iqnore) 09 EEADR (Iqnore) OA PCLATH OB INTCON OC

Indirect Address Pointer * Timer/Counter Program Counter Low Order 8 Bits Status Register - Flags Indirect Pointer Port A Port B

Program Counter Latch High Order 5 Bits Interrupt Control

General Purpose File Registers Think Of This Area As RAM (Data Memory) Note: Bank Switching And Bank 1 Ignored

Ox4F

*

Not Physically Irrplemented

Twelve file registers have specific dedicated purposes which will be of interest to you (to be described later). 68 file registers are there for your use and may be thought of as RAM or data memory for storing data during program execution.

ill

Hex Address OxOO

f1

OxOl

f2

Ox02

f3 f4

Ox03 Ox04

f5 f6

Ox05 Ox06 Ox07 Ox08

f7 f8

10

Indirect data addressing register. See indirect addressing section in programming chapter. Timer/counter register (TMRO). See timing and counting chapter. Program counter low byte. See relative addressing section in programming chapter. Status word register. File select register (FSR). See indirect addressing section in programming chapter. Port A - 5-bit, bits 5 - 7 are not implemented and read as O's. Port B - 8-bit (bit 0 is also INT bit). Not used. EEDATA (Ignore - used in EEPROM data memory programming).

f9 fA fB

fC f4F

EEADR (Ignore - used in EEPROM data memory programming). PCLATH - write buffer for upper 5 bits of program counter. See relative addressing section in programming chapter. OxOB INTCON - interrupt control register. See interrupts chapter. OxOC- General purpose registers (RAM or data memory). Ox4F

Ox09 OxOA

Note: File registers at Ox81, Ox85 and Ox86 are in bank 1 and are ignored for now. Bank switching is required to access them.

Working Register (W) The PICl6F84 has an 8-bit working register called W. It is a lot like the accumulator in other microcontrollers. Its use will be illustrated in the programming chapter.

Option Register The option register controls the port B weak pullup resistors, interrupt signal edge select, the prescaler for the timer/counter and watchdog timer (shared) and also the input to the timer/counter.

Stack The PIC16F84 has an 8-level stack separate from the other registers (not part of the file register address space = data memory). It is used to save the program counter contents when subroutines are called so the microcontroller will know where to resume program execution when returning from the subroutine. This is also true of interrupts. There are no stack manipulation instructions and the stack is not accessible to the programmer.

Reset Vector On reset which occurs on power-up or when the reset switch is used to pull MCLR low, the PIC16F84 will go to program memory location OxOOO where the first instruction is stored. It then begins executing instructions stored sequentially in memory.

Interrupt Vector The interrupt vector points to OxOO4, so if interrupts are used, the first instruction of the interrupt service routine must be at that location.

11

OPTION REGISTER

7

6

5

43210

I RBPO IINTEDG I ToCS I TOSE IpSA I PS2 IPS1 IPSO I

POR .. OxFF

~ Prescaler Value

Prescaler Assignment 0- TMRO 1- WDT -

~

TMRO External Clock Edge Select o - Increment On Rising Edge 1 - Increment On Falling Edge

Prescaler Value PS2 0 0 0 0 1 1 1 1

PS1 0 0 1 1 0 0 1 1

pSO 0 1 0 1 0 1 0 1

::t..6Y 2 4 8 16 32 64 128 256

TMRO Clock Source Select o .. Internal Instruction Cycle Clock 1 - External Via TOCKI Pin

Interrupt Edge Select o - Interrupt On Falling Edge 1 - Interrupt On Rising Edge -

Port B Pullup Enable O-Enabled 1 .. Disabled

For ease of use while learning and for backward compatibility with the PIC16C54, we will think of the PICl6F84 option register as a "buried" register (not included in the file register address space and having no address) and will write to it using the OPTION instruction. Microchip considers the OPTION instruction obsolete, but we won't let that bother us. All of this avoids file register bank-switching which can be dealt with later. The MPASM assembler may generate a warning message when you use the OPTION instruction. Don't let that bother you either.

12

To change the contents of the option register, we will use the OPTION instruction which loads the contents of the W register into the option register.



movlw option

b'11000000'

;bit pattern defined ;sends bit pattern to option register

;

,._----------------------------------------------------,· bit 7 = 1 port B pullups disabled ,· bit 6 = 1 interrupt on rising edge ,· bits 5,4,3,2,1,0 = 0 - details in ,·

timing and counting chapter

i------------------------------------------------------

PROGRAM COUNTER

The PICl6F84 has a 13-bit program counter. GOTO and CALL instructions include an ll-bit address which is enough for a 2K program memory address space. The PICl6F84 has lK of program memory.

PCH 12

11

10

PCl 9

8

7

0

[JTTT] U,-------,,-------,,-----,-------,,-----ITJ

I '---.- - -----' Included In OP Code

'--v--'I

For PartsWith More Than2K (1 Page) Of Progam Memory

Enough To Define 2K PIC16F84 Has 1K Program Memory

The program counter high (PCH) cannot be read or written to directly. It is loaded from a 5-bit latch called program counter latch high or PCLATH.

PCLATH 43210

[JTTT] L--y---J

I For PartsWith More Than2K (1 Page) Of Progam Memory

13

For the 14-bit core parts (pICI6CXXX and PIC 16FXX but not including PICI6C5X), a program memory page is defined as 2K (not 256 bytes as with many other 8-bit microcontrollers) because 2K can be accessed using the 11 address bits contained in GOTO and CALL instructions. Since the PIC16F84 has lK or one-half page of program memory, we won't need to contend with program memory paging. Avoiding this is a big help in getting started using PICI6s. Locating tables of data in program memory requires a little thought. Accessing a table requires the use of a computed address, so the table should not cross the boundary of a 256-location segment of program memory because the PCLATH bits(s) are involved. To keep things simple, tables should be located in the first 256 program memory locations at this stage of your learning process. The details are described in "Plein Up The Pace". For computed addresses which involve PCH, the bits come from PCLATH.

12

PCH 11 10 9

PCl 8

o

7

ITTTTI From PCLATH

Computed Result

PCLATH comes up ---00000 on power-on reset, so you shouldn't have to be concerned after that unless your program writes to PCLATH. Bits 4,3 must be 0 and 0 (bits provided to handle larger program memory space but must be 00 to make the PIC 16F84 work properly). Don't sweat these details for now. Their importance will come into focus after you have done some programming examples.

14

STATUS REGISTER

7

6

5

4

3

o

2

POR- 00011 XXX

'---y---J For File RegisterBank Select - Not Used In This Book. Write 00 WhenWriting To The Status Register.

J

Carry/Borrow Flag ADDWFAndADDLW Instructions Digit Carry/Borrow Flag ADDWFAndADDLW Instructions ZeroFlag o... ClearIf Not Zero 1 - Set If The Result Of An Arithmetic Or Logic Operation Is Zero PowerDownBit o- By Execution Of SLEEP Instruction 1 - AfterPower-Up Or By CLRWDT Instruction Execution Time-OutBit o=A WDTTimeoutHasOccured 1 - After Power-Up And By CLRWDTAnd SLEEP Instructions Bank Select (Ignore)

Care must be taken when reading the status register so as not to affect its contents. Using bit instructions (BTFSS or BTFSC) will not affect the contents while using MOVF does affect the Z flag. Writing to the status register using BSF, BCF or MOVWF instructions will not affect the status register in any way other than the write itself (ie. no flags are affected by the execution of the instruction).

15

CIRCUIT FOR EXPERIMENTS CIRCUIT FOR EXPERIMENTING WITH THE PIC16F84 AND PIC16C54

A circuit for experimenting with the PIC 16F84 and PIC16C54 is shown. It has all the components required for most of the experiments in this book. Building it saves building one circuit for each experiment. Providing a ZIP socket for the microcontroller is a good idea because it will be inserted and removed many times. +5V 10K

+5V

Typical For 10K Digital 1/0 External Clock Source

>

---

RA3, RA2. RA1,RAO

1 RA2

RA1 18

2RA3

RAO 17

3 RA4ffOCKI

OSC1 16

4 MCLR

OSC2 15

+5V PIC16F84 Or Vdd 14 PIC16C54 6 RBO/INT RB7 13

-, +5V 10K

5 Vss

7 RB1

RB612

-

8RB2

RB511

--

9RB3

RB410

100n

Reset

i

N.C. 4MHz Clock Oscillator

Decoupling

0.1~fT

N.C.

680n

~

Typical For RB 7 Through RB 1

----« INT Input

Note: A 7-Segment LED Display May Be Substituted For The Individual LED s For One Of The Experiments

16

'84 ON A BOARD As an alternative, a simple more general purpose circuit module may be assembled for use in all of the experiments in this book. It includes a PIC16F84 (or PIC16C54) in a socket with clock oscillator, reset, power supply decoupling capacitor, and port pullup resistors. These items are required in or are common to all of the experiments. The pullup resistors are used in the experiments primarily for the purpose of preventing unused inputs from floating. There are pullups on port B built into the PIC16F84. I decided not to use them because they just add confusion to the if you program examples and detract from explanation of the applications themselves. So use the circuit module, remember to activate (via DIP switches) pullup resistors on all unused port lines (input or output). You can save refinements for later. I recommend connecting the port lines to a 16-pin DIP socket. A 16-conductor ribbon cable terminated with 16-pin DIP plugs may then be used to connect the '84 on a board to a solderless breadboard for doing the experiments. The wiring done on the solderless breadboard for each experiment is minimal and chances are you won't want to preserve the specialized part of the circuit after you have done the experiment anyway (on to better things). I would definitely consider use of a ZIF socket for the '84 to avoid bending or damaging the pins. The '84 on a board may also be used for the experiments in "Ple'n Up'n1.e J)aee".

t

+5V

--

1RA2

RA1 18

2 RA3

RAO 17

10KPuliup

,.--------+----. Typical For All

13 Port Lines

3 RA4fTOCKI

OSC1 16

4 MCLR

OSC215

+5V N.C.

PIC16F84

_I +5V 10K 1000

Reset

----

---

5 Vss

Vdd 14 0.11·tf

6 RBO/tNT

RB713

7 RB1

RB612

8 RB2

RB511

9 RB3

RB410

DeCOUPlingT

4MHz Clock Oscillator

l 17

As an example to illustrate how the '84 on a board could be used, let's assume that we want to breadboard the circuit shown:

TESTCIRCUIT

+5VDC

PIC16F84 RS

10K

RA

i The experiment calls for an LED at a port B pin. A 680 ohm resistor and LED could be installed in the solderless breadboard. The corresponding pullup resistor DIP switch on the '84 on a board should be in the open position (see drawing on page 19). The experiment also calls for a pullup resistor and switch on a port A pin. We could use the pullup resistor on the '84 on a board and connect the port A line to a second switch (for use in the experiment) on the solderless breadboard.

18

+SVDC

'84ON A BOARD

10K Not Utilized

DIP Switch Open

+5VDC 10K Utilized

PIC16F84

DIP Switch Closed

RB

Via Ribbon Cable

SOLDERLESS BREADBOARD

i

The '84 on a board is used in every experiment as-is. Pullup resistors are available and switehselectable making life a little easier. The parts or connections specific to each experiment are located on the solderless breadboard to make changes simple and easy.

19

CHOOSING DEVELOPMENT TOOLS MPLAB/Windows vs. MPLAB/MS-DOS MPLAB from Microchip is an integrated development environment (IDE) meaning the text editor, assembler (MPASM) and device programmer software (PICSTART Plus) are integrated into one program which runs under Windows (3.1 or 95) on a PC. By opening one program, MPLAB, all of these functions are available via clicking the mouse . MPLAB is Microchip's primary development software and will be updated continuously. MPASM is also available as a stand-alone program for use under MS-DOS. The DOS version is no longer being updated by Microchip, but will be available in its current form for some time. Both MPLAB and MPASM are available free of charge from Microchip. They can be downloaded from their Web site or obtained from the factory or a sales office. MPLAB may be used in conjunction with most simple low-cost device programmers as a tool for creating the assembler source code and the assembled .HEX file. It is then necessary to quit MPLAB and use the programmer manufacturer's device programmer driver software to stuff the .HEX file into a PIC 16. CHOOSING A DEVICE PROGRAMMER Selecting a programmer for PIC16 development is a personal choice based on the following criteria: • • • •

MS-DOS vs. Windows programmer control software. Range of parts which can be programmed. Support (level, long-term). Price.

Simple, inexpensive device programmers are available which run under MS-DOS and which program either a single device (PIC16F84) or multiple devices (such as all of the 14-bit core mid-range devices). Windows-based software may become available for these simple programmers . As product offerings are continually changing in this fast-paced market, I suggest you contact the programmer manufacturers directly for the latest information. Sources are listed in appendix A. Note that some programmers are connected to the PC's parallel (printer) port and some are connected to a serial port (usually COM2).

20

Microchip's PICSTART Plus will program their entire line of devices. Their intent is to continually provide software and firmware upgrades to keep it that way. Firmware? The PICSTART Plus contains a PIC17 which converts information received from the PC's serial port to the appropriate signals to program each device. Since these devices have differing requirements, the code in the PIC17 must be updated to cover the requirements of new devices as they become available. The PICSTART Plus is used to program a blank PIC17 using the updated code provided by Microchip (free on their Web site). The PICSTART Plus operates under Wmdows (3.1 or 95) and is connected to one of the PC's serial ports (usually COM2). The control software for the PICSTART Plus is incorporated in MPLAB. MPLAB is bundled with each PICSTART Plus.

21

USING AN ASSEMBLER An assembler is a program which, in this case, runs on an mM compatible computer and converts readable instructions contained in a text file to hexadecimal code understood by the PIC16. The MPASM assembler from Microchip has lots of capabilities. The advantages of using it are: • It's free (obtainable from Microchip). • Most people use it, i.e, most people speak MPASM. • If you start out with it, you won't have to relearn anything as you expand your capabilities. • Since Microchip manufactures both the chips and the assembler, you know you are on their path and not a side road. You won't get left in the dust in the future. • The examples in Microchip's "Embedded Control Handbook" and in magazine articles are, for the most part, in Microchip's assembler dialect - so why not go mainstream? • The error detection feature will be very helpful. You will need only a small portion of the MPASM assembler's capabilities to get started. An explanation follows:

SOURCE CODE FOR THE ASSEMBLER Code for PIC16s may be created using a simple text editor. If you use an MS-DOS version of MPASM, a simple text editor such as QEdit (designed for programmers), PC Text or EDLIN (included in MS-DOS) may be used. Any simple text editor may be used as long as it generates an ASCII file without additional formatting or printer control commands. They tend to confuse the assembler. A text editor is built in to MPLAB for use with the version of MPASM included in MPLAB (which runs under Windows). Specific information such as which PIC16 part the code is written for and where the program starts in the chip's program memory plus the instructions and labels (mnemonic names for addresses) are typed in a specific format so that the assembler program can fmd them. A very simple example will be used to show how all this is done. First, the program teaches a PICl6F84's port B that all eight port lines are output lines (as opposed to input lines). Then the program makes the least significant four lines logic 1 (HI) and the most significant four lines logic 0 (LO). Finally, the microcontroller sits in a loop. Defmitions for these terms follow. The source code is easy to layout using a text editor.

22

Teach Port 8 All Outputs

i=======PICT1.ASM===========================10/14/97== list p=16f84 radix hex

;----------------------------------------------------i

portb

cpu equates (memory map) equ Ox06

;----------------------------------------------------org

OxOOO

movlw tris

OxOO portb

movlw movwf

OxOf portb

goto

circle

i

start

,· i

circle



iload W with OxOO iCOpy W tristate, port B outputs iload W with OxOF iload port B with contents of W idone

end

i----------------------------------------------------iat blast time, select: memory unprotected i , watchdog timer disabled (default is enabled) ; standard crystal (using 4 MHz osc for test) , power-up timer on

· · ;======================================================= Semicolon (;) The semicolon (;) is a delimiter which tells the assembler to ignore everything following the semicolon on that line of text. If the semicolon is the first character on a line, the whole line is ignored by the assembler. This is useful for putting in your own symbols to divide the text into sections visually (--------- or =======) or for putting in comments or notes to explain and document the program. On the line which begins with "start" in the example, the information following the semicolon "load w with OxOO" explains what the MOVLW instruction is being used for.

23

Tabs The tab capability of the text editor is used to arrange code in columns. The assembler expects to find specific items in three columns. The three columns are used five ways, one way for the header section, a second way for the equate section (defined later), a third way for org statements, a fourth way for the program section, and a fifth way for the end statement. Assembler Directive

~ list p=16f84 radix hex

Header

Label

This Is An Equate

Hex Address Or Number

r--A--.~~ equ

Equates

Not Used

This Is An ORG

Hex Address

~~~ org

Origin (ORG)

Label

Instruction

~~ Program

This Is End

~ End

24

end

Literal Or Label

r----A--....

The assembler program "reads" each text file line, ignores it if it begins with a semicolon, and steps through the three columns. If it finds "EQU", "ORO", or "END" in the second column, it treats that as an EQUATE, ORO, or END command. If it finds one of the PICl6F84 program commands in the second column, the assembler creates machine language code in hexadecimal. Tab position (i.e, 8th column vs 9th or 10th, etc.) is not critical. When the assembler sees a tab (or a space does the same thing) as it moves from left to right along the line, it knows there is a transition from one column to the next.

STYLE As you look at source code examples (text code), you will see quite a range of styles in dividing up the page, placement of comments, etc. Don't let this confuse you. It is just a matter of differing styles of the individuals who created the programs. It is what's in the three columns not blanked out with the use of ;'s that counts.

portb start

circle

list radix equ org movlw tris movlw movwf goto end

p=16f84 hex

Ox06 OxOOO OxOO portb

OxOf portb circle

25

HEADERS The information at the top of the source file is called a header.

;======PICT1.ASM================================10/14/97== list p=16f84 radix hex

i----------------------------------------------------- - - - The line

list

p=16f84

indicates which PIC16 part the program will be placed in. "List" is an assembler directive which has a bunch of meanings depending on what follows it. This is the only way we will use the list directive. The line radix

hex

indicates that the numbering system is hexadecimal unless otherwise specified in a specific instruction. "Radix" is an assembler directive. For all examples in this book, it is assumed that a header similar to the the one shown above is used.

LABELS A label is a mnemonic symbolic name assigned to an address. "portb" means port B whose real hexadecimal address is OX06 in the file register memory space in a PICI6F84. Labels are nice because once assigned via an EQUATE statement equating the symbol with a hexadecimal physical address, you no longer have to remember port B is at Ox06. Just refer to it in your program via the label "portb". Labels are also assigned merely by putting them in the label column of the source file. This is done when the address can be anywhere, i.e. it does not have to be related to something physical such as a port with a specific address. Labels defined in this way are automatically assigned addresses by the assembler. We don't need to know what they are.

26

The rules for defining labels are: • All labels must start in the first position in column 1. • Labels must begin with an alpha character or an underbar. • Labels may contain alphanumeric characters, the underbar and the question mark. • Labels may be up to 31 characters long (you will probably run into the next column before you run out of characters). • Labels are case sensitive by default. The underbar ( _ ) is useful as a means of separating words because spaces are not allowed. An example is temp_file. EQUATES

An equate statement may serve to assign a label to a specific address in the PIC16 designated by a hexadecimal number.

r

portb

" :

Tells Assembler This Is An Equate

~~

equ

Means Hexadecimal

_I "-

Hexadecimal Address (File Register f6 At ax06)

Equates may also be used to assign names to numbers. For example, we can assign the name "min" to bit 1 in a register labeled "flags" with two equates.

min flags

equ equ

1

OxOc

;min = bit 1 ;flags = file register OxOc

27

In a program, we could have a line with says:

bsf

flags,min iset bit 1 in file register OxOc

As the assembler assembles a program, it comes to a label and looks up its corresponding address or number (not the reverse). The Bit Set File (BSF) instruction sets a bit in a file register. Setting a bit makes it a "1". Clearing a bit makes it a "0". LITERALS Literals are constants or numbers, usually hexadecimal numbers. Literals are defmed using the MOVLW and some logic and arithmetic instructions which are detailed in the programming chapter.

Instruction

/

start

-

label

/

movlw

-

OxOO

\-

28

Literal (Constant) HexadecimalOxOO For Th is Example

ORG ORO stands for origin. ORO statements will be used for three purposes in this book. • ORO defines the address where the program code starts.

.:

org

Tells Assembler This Is An ORG Statement

OxOOO

Hex Address OxOOO, The First Program Memory Location

• ORO is used to establish the reset vector for the PIC16C54 (details later). Tells Assembler This Is An ORG Statement

;-

org goto

Hex Address ox1 FF - The Reset Vector Points To This Location

Oxlff start

InstrucliO\- Address Of First Program Instruction

• ORO is used to establish the start of the interrupt service routine for the PIC16F84. Tells Assemb ler This Is An ORG Statement

org

.:

Hex Address Ox004 - The Start Of The Interrupt Service Routine

Ox004

First Instruction Goes Here

29

END An END statement is used to tell the assembler it has reached the end of the program.

.:

Tells Assembler The Program Ends Here

end

An END statement must be used to end a program.

PROGRAM FORMAT All program examples in this book are assumed to have the following format even though the header, equates and end are not shown:

Standard Header

Equates

Program

End

end

* Explained in destination designator section of programming chapter. Remember, the first line is always the processor type designation and the last line is always the END statement.

30

FILES USED BY THE ASSEMBLER Your program source files created using a text editor must have a file name with the extension .ASM. The file name for the first example in the book is PICT1.ASM. The assembler will only assemble files with the .ASM extension.

FILES CREATED BY THE ASSEMBLER The assembler will create two files from your source code file which will be of interest to you. They will have file names the same as the source file except for the extensions. .LST .HEX The file whose name has the extension .LST is the assembler-generated listing of your program. MPASM 01.40 Released

LOC OBJECT CODE VALUE

PICT1.ASM

10 -5-1997

12:25:37

PAGE

1

LINE SOURCE TEXT

00001 00002 00003 00004 . 0 0 0 05 00000006 00006 00007 00008 0000 00009 00010 0000 3000 Warning[224]: Use of this 0001 0066 00011 00012 0002 300F 00013 00014 0003 0086 00015 00016 0004 2804 00017 00018

;=======PICT1.ASM===========================10/1 0 /9 7== list p=16f84 radix hex

i----------------------------------- ------------ - --- portb

cpu equates (memory map) equ 06

;-------------------------------------------- --~ - ----

org

OxOOO

start movlw OxOO ;load w with OxOO instruction is not recommended. tris portb ;copy w tristate, port B outputs movlw OxOf ;load w with OxOF movwf portb ;load port B with contents of w circle goto circle ;done end

31

PICT1.ASM

MPASM 01.40 Released

SYMBOL TABLE LABEL

10-5-1997

12:25:37

PAGE

2

VALUE 00000001 00000004 00000006 00000000

16F84 circle portb start

MEMORY USAGE MAP ('X'

=

Used,

'-' =

Unused)

0000 : XXXXX----------- ---------------- ---------------- ---------------All other memory blocks unused. Program Memory Words Used: Program Memory Words Free:

Errors Warnings Messages

o 1 reported, o reported,

5 1019

o o

suppressed suppressed

Note: The warning message regarding the use of the TRIS instruction will be ignored in this book. The file whose name has the extension .HEX is the hexadecimal object code which will be used by the programmer to program the PIC16 chip. :08000000003066000F3086009D :020008000428CA :OOOOOOOlFF

32

PREVENTING SOME GOTCHAS More Than One Way To Skin A Cat

There may be two or more ways to accomplish creating various aspects of a program with an assembler. Specifying the microcontroller type can be handled using a "list" instruction (as done in this book) or via an assembler command line option (not used in this book). As an example, the approach taken here is to use one way consistently. Hopefully, it is the easiest way. UPPER/lower Case

Use of upper vs. lower case letters seems to be very inconsistent in the program listings I have studied. Sometimes it is significant and sometimes it is not (labels are case sensitive for example). Instruction names and port designations (A vs. B) are upper case in the text in this book because that format seems to be the norm. All text in the assembler program listings except comments are lower case to make them less confusing to read and so that case sensitivity is not an issue.

33

USING THE MS-DOS BASED MPASM ASSEMBLER We will assume that you have installed MPASM on your computer's hard drive with the driver software for your device programmer in the same directory. We will also assume that you have access to your text editor while working in that directory. HOW TO ASSEMBLE A PROGRAM Just to get the feel of how all this stuff works, you may want to type the example program on page 23 using your text editor and assemble it. • Type it exactly as shown so it will work. There will be plenty of opportunities to make mistakes of your own later. • Note that if the text is too wide, it will flow into the next line. The overflow will look like a label to the assembler (example: ====== . = is an illegal character for a label). • Name the file PICfl.ASM. • Assemble using MPASM. PICfl.ASM and MPASM must be in the same directory. • Type MPASM. • Press return. • Wait. • A list of files, etc. will appear.

Source File

: I * .ASM ....

Processor Type : INone Error File Cross Reference File Listing File Hex Dump Type Assemble To Object File

34

I

Yes

I

No

I

Yes

I

INHXSM.HEX No

Blinking Cursor

Note that as you work with MPASM that many of the menu selections may be toggled using the return key. • • • • •

Press return. All .ASM files are shown. Use arrow keys to select (highlight) PICTl.ASM. Press return. Observe that PICTl.ASM has been entered in the appropriate places automatically.

• Arrow down - highlight processor type. • Press return. Processor Type : 16F84 • Arrow down - highlight error file.

Error File : Yes • Press return.

Error File

No

35

• The result is:

• Press FlO to assemble. • When assembly is complete, a list will appear on-screen showing how many errors there are (hopefully 0) and where they are. • Press any key to get out. • When the assembler has finished, use the text editor to open the file PICfl.LST to see what the assembler created. Errors will be noted if there are any. • If you make mistakes, consider them as serving to create an opportunity to fmd out what the error codes mean. They may be corrected by opening the .ASM text file using the text editor. Then repeat the assembly process.

36

MPLAB OVERVIEW - GETTING STARTED MPLAB is a large program with lots of whiz-bang features, so it would take a whole book to . describe it completely. As with most computer programs these days, you will only need to know a little bit to get started using it. That's next. Microchip continually releases new versions of MPLAB. The following instructions will help get you started if you are using v3.40 or later. MPLAB v4.00 is the current version as this is being written. If significant changes are made to MPLAB, updates to the following instructions will be posted on the Square 1 Electronics website (http://www.sq-1.com) . USING MPLAB - GETTING STARTED

If you have had experience using a text editor, MPASM (MS-DOS version) and a device programmer to program the PIC16s, using MPLAB may seem a bit strange at first because of the termin ology and because the look and feel of MPLAB is quite different. Once these things become familiar, it will be easy to use. Having the text editor, MPASM, and PICSTART Plus (if you use it) software open/accessible simultaneously and to be able to switch back and forth quickly at will is nice. We will assume the use of Windows 95 for the following example. Open MPLAB by double -clicking the Microchip MPLAB icon. A menu bar will appear at the top of the screen when the MPLAB is ready for use. Toolbars MPLAB has four toolbars, one of which is displayed at a time across the top of the screen below the menu bar. There are lots of buttons, but you will not need to know about any of them to get started. The four tool bars are: • • • •

Edit Debug Project User Defined

The default toolbar is the last one you used before shutting down the computer the last time you used it. The button farthest to the left on all four toolbars is used to select the toolbar you want.

37

Position the cursor over the toolbar button second from the left end of the toolbar and then move the cursor horizontally to the button at the far left and leave it there. Notice that "swap toolbar" appears in the gray stripe at the lower left corner of the screen. Moving the cursor from midscreen directly to the swap toolbar button does not always cause "swap toolbar" to appear (for whatever reason). Moving the cursor across the toolbar a button at a time will allow you to see the button functions displayed at the lower left corner of the screen. Click on the "swap toolbar" button to look at the other toolbars. The toolbar names appear at the lower right corner of the screen in the status bar. Select the Debug toolbar. We won't be using the toolbar buttons, but the Debug toolbar must be selected to allow PICSTART Plus to work (may be true for other programmers as well). The debug toolbar is now the default toolbar (unless/until you select another one for some reason). Select Development Mode And Device Type Click "Options" in the menu bar. Click "Development Mode" in the drop-down list (Options>Development Mode). In the Development Mode dialog box: • Select MPLAB-SIM Simulator. , "Editor Only" won't work. In the MPSIM (tm) Simulator mode it is possible to move freely back and forth between edit, assemble and the device programmer. • Select Processor: "PIC16F84". • Click "Reset". Project A principal new term you will need to know to use MPLAB is "project". A project is one or more files grouped together and used to make code for a PIC16. The best way to learn this stuff is by doing a simple example. Click "Project" in the menu bar. Click "New Project" in the drop-down list (Projects-New Project). In the New Project dialog box: • Select a directory for the new project (click on "MPLAB" to highlight it). • Type the file name. PICTl.PIT "-y--J '-yJ

L

l - means project your project name

• Click "OK". The Edit Project dialog box will appear. In the box you will see a Project Files window with the name of your file (with the extension .hex) displayed. • Click on your file name to highlight (select) it.

38

Another term used in MPLAB is "node" which means a file associated with a project. The Node Properties button is now active (black). • Click on the Node Properties button. The Node Properties dialog box will appear. We will use the default settings. • Click "OK". It may seem like going to the Node Properties box was a waste. Things won't go well if you skip this step! The Edit Project dialog box is now back on the screen. • Click "OK". Text Editor

A text editor is built into MPLAB. In the file menu, click "New". A window labeled "Untitled" will appear. Now you can type in some sample code. We will use PICTI.ASM from page 23. When you are finished, save the file (Filec-Save). In the "Save File" dialog box, edit the file name. Use the .ASM extension (PICTl.ASM). • Click "OK". Edit Project

Part of the process of editing a project assigns a source code file to a project. In the Project menu, select "Edit Project" (Project-Edit Project). The Edit Project dialog box will appear. • Click on the Add Node button. The object is to assign our file to the project. Project file names appear in a window. Find PICTl.ASM and select it (click on file name). Click "OK". The project now has a source file assigned to it. The Edit Project dialog box is on-screen again. The file name PICTI.ASM appears in the window. • Click "OK".

39

Assembler

To assemble your source code (with your project open and the source code on screen), click "Make Project" under the project menu (Project>Make Project). The terminology is a bit strange. Clicking "Make Project" causes the source code to be assembled by MPASM which is built into MPLAB. The result is a file with your file name and the extension .HEX. The assembly process ends with the Build Results window being displayed. If your code assembles properly, the message "Build completed successfully" will appear. If there are errors or warnings, each error or warning in the list will have a source code line number associated with it. If you open the source code window's drop down list, you will see that the last item on the list is "Toggle line numbers". Clicking on this will cause line numbers to appear in the left margin of the source code. Clicking again causes the line numbers to go away. Ignore the three warning messages associated with this code example concerning processor type, radix, and use of the TRIS instruction. The processor type is also specified as a selection in the Development Mode dialog box (takes precedence over the source code). The radix is also specified via the default setting in the Node Properties dialog box (which takes precedence over the source code). Last, save your project via Project>Save Project. Then close your project via Project>Close Project. Close MPLAB. MORE MPLAB OPERATIONS To View And Print A .LST File

In the file menu, select "View". A dialog box will appear. Type in the name of the file you would like to view and print (example: PICTl.LST). Click "OK". The file will appear on screen. To print the file, select "Print" in the file menu. To Delete A Project

Delete the project files using Windows Explorer. It is the only way it can be done.

40

PROGRAMMING A DEVICE By now you have had an opportunity to assemble the source code file PICTl.ASM and are ready to proceed with programming the .HEX file created by MPASM into a PIC16F84 device. MS-DOS BASED MPASM Follow the instructions which came with your programmer. The file PICTl.HEX created by MPASM and the device programmer driver software should be in the same directory on your hard drive so the driver can find the .HEX file. MPLAB The "make project" process in MPLAB creates a .HEX file (PICT1.HEX in this case). If you have a PICSTART Plus, the device programming instructions follow. If you have another programmer, you may need to close the MPLAB program and open the driver software for the device programmer. The driver software and the .HEX file should be in the same directory. Follow the device programmer manufacturer's instructions. USING PICSTART PLUS Begin by opening your existing project in MPLAB. Choose "Open Project" in the project menu followed by selecting your project by name (pICTl.PJT). Choose "Open Source" in the file menu and select "PICTl.ASM". To Enable PICSTART Plus Click on PICSTART PLUS in menu bar. Click "Enable Programmer" on drop down list. Programmer status dialog box. Select device type. Some items will be enabled (black)or grayed out depending on device type selected. Voltages cannot be changed with PICSTART Plus programmer. Set configuration bits by clicking on arrows (or do it in MPASM via _Config directive). Ignore items where arrows are grey. Ignore ill and voltages.

41

To ProgramNerify A Device With "Programmer Status" dialogue box open: Click "PICSTART PLUS" on menu bar. Click "Program/Verify". Program/Verify dialog box. Un-check "program/verify ID's" and "EEPROM data" if not applicable. Click "Program" button. Message will appear if device not blank. Programmer status will be displayed in text on same line as "Start Address" "Programming" (in process), "Success" or "Failure". To Read A Device With "Programmer Status" window open: Click "PICSTART PLUS" on menu bar. Click "Read Device". THE BIG TEST! Insert the programmed PIC 16F84 into the socket on your test board (power off!). Power up the test board. The LEDs connected to port B should be off/on as follows:

Bit

7

6

5

4

3

2

1

0

Status

0

0

0

0

1

1

1

1

Controlling LEDs is a BIG DEAL at this point! You now know how to use the computer, text editor, assembler, device programmer and program a microcontroller. Wow! Time to celebrate I'd say.

42

WRITING PROGRAMS

PROGRAMMING CONCEPTS

The PIC16F84 microcontroller will respond to a series of coded instructions stored in program memory. When a designer (which may be you) thinks of something which he or she would like to control, the natural thing to do is to think through the control process in logical steps. Creating a flow chart is a good way to visualize these steps. The control process might consist of sensing outside world events such as light vs. darkness, cows passing through a gate, temperature, a key stroke etc., testing the data which has come from the sensors followed by taking one of two possible program paths (branching) based on the test results, and controlling some outside device such as a digital display, indicator light, motor, heater, etc. Instructions for repetitive operations can be repeated in long strings, but that wastes valuable memory space. It may even result in a program which is too long to fit in the available memory space. Instead, one sequence of instructions can be used over and over in a loop and the microcontroller goes 'round and 'round until something forces it to stop. • Loops can go around forever or until the plug is pulled or an anvil is dropped on the microcontroller chip. • Or until a counter counts up to a predetermined number or down to O. • Or until a test result says to move on.

43

An example of the use of a loop is a program used to read an input port and display the data received (or the status of the lines) at an output port.

Loops are useful when it is necessary to perform an operation a certain number of times (n).

Load Counter with 0

Initialize

Do Increment

44

The use of a loop prevents having to write the code n times and the requirement for memory space to store it. Another technique for keeping programs short and manageable is the use of subroutines. If the same task is to be performed in two or more places in a program, the code can be written once and stored in one location. When the points in the program are reached where the subroutine is to be used (called), a CALL instruction is used. The last instruction in a subroutine is always a return from subroutine (RETURN or RETLW) instruction. The program then continues where it left off. An important concept to keep in mind when using micros is that they can only do one thing at a time, one very simple thing. They execute many very simple instructions and they do it blindingly fast. The PICl6F84 executes roughly a million instructions every second with a 4MHz clock oscillator. In situations where events of interest to the micro occur only once in a while, perhaps randomly, it may be desirable to use a micro feature called the interrupt. In simplified form, if an event occurs in the outside world which demands the micro's attention, the sensor monitoring the event can be wired to direct a signal (pulse) to an interrupt line (pin) on the microcontroller. When the signal arrives, the microcontroller drops what it was doing (after finishing the instruction it was executing), and then jumps to a special program called an interrupt service routine. The purpose of the routine is to do whatever the designer/programmer thinks is required when the outside event occurs. Micros do only one thing at a time, but they can be instructed to drop one task, take care of another, and resume the original task. Interrupts are a special area closely tied to the hardware used to make their occurance known, so they will be discussed in detail in the interrupt section of this manual. Finally another basic concept is that there is no way to write directly to a memory location or to an I/O Port. Data must be put into the W register and then the W register contents must be stored in the final location. A MOVLW or MOVF instruction loads the W register. A MOVWF instruction stores a copy of the W register contents in a data memory location (file register) leaving the contents of the W register unaltered.

PROGRAMMING EXAMPLES The best way to learn how micros are used is to think of applications and write programs to implement them. In this section, we will start out with very simple programs to demonstrate the concepts we have just discussed. By the time you have finished this section, you will be able to think in micro terms and will see micro applications in your work or hobbies. You will also be able to visualize the methods for implementing micro solutions. Simple programs used as examples will illustrate the use of the various types of instructions.

45

INSTRUCTION SET The PICI6F84 microcontroller officially has 35 instructions in its instruction set. We will temporarily use two obsolete instructions because it makes life much simpler (total 37 instructions to get started). The instruction set is presented categorized by type of operation. The use of k, f, d, etc. will be explained later. PIC16 instruction words used by the chip itself contain both the binary code for the instruction and the binary address or some constant data. For the PICI6F84, instruction words are 14 bits wide. How these words are constructed is described in the Microchip data book. I have found that you simply won't need to know how this is done . Getting the assembly language instructions and designators (d) correct should be the focus.

Move or Define Data MOVLW

k

Loads W with literal.

MOVF

f,d

Moves copy of selected register contents into W or f.

MOVWF

f

Moves copy of W contents into selected register.

Change Register Contents CLRF

f

CLRW

46

Clears selected register to O. Clears W register to O.

COMF

f,d

Complements selected register. All 1's to O's, all O's to 1's Result in W or f.

DECF

f,d

Decrements selected register. Decrementing when contents of register is OXOO results in 0xFF. Result in W or f.

INCF

f,d

Increments selected register. Incrementing when contents of register is 0xFF results in OxOO. Result in W or f.

BCF

f,b

Clears selected bit in selected register to O.

BSF

f,b

Sets selected bit in selected register to 1.

RLF

f,d

Rotates bits in selected register one position to the left. Bits rotate through carry flag. Result in W or f.

RRF

f,d

Rotates bits in selected register one position to the right. Bits rotate through carry flag. Result in W or f.

SWAPF

f,d

Exchanges MS and LS nibbles of selected register. Result in W or f.

Control Program Flow OOTO

k

00 to specified address.

CALL

k

Call subroutine at specified starting address. Return from subroutine.

RETURN RETLW

k

RETFIE

Return from subroutine. Loads W with literal. Return from interrupt.

BTFSC

f,b

Tests specified bit in specified register. Skips the next instruction if bit tested is clear (0).

BTFSS

f,b

Tests specified bit in specified register. Skips the next instruction if bit tested is set (1).

DECFSZ

f,d

Decrements specified register. Skips next instruction if register contents = O. Destination W or f.

INCFSZ

f,d

Increments specified register. Skips next instruction if register contents = O. Destination W or f.

Nothing Do nothing for one instruction cycle (time delay, save room for future code mods, debugging = break).

NOP

Control Microcontroller CLRWDT

Clear watchdog timer (reset to zero). Also resets the prescaler of the watchdog timer. Status bits TO and PD are set.

OPTION

W contents (bit pattern) sent to option register to control prescaler ratio, real time clock trigger edge, and real time counter/clock source.

SLEEP

Puts microcontroller to sleep to reduce power consumption. Wakeup via reset, watchdog timer, or external real time input.

TRIS

f

W bit pattern determines port line input vs. output on line-by-line basis for selected port.

47

Logic ANDLW

k

AND's contents of W with literal (mask) contained in instruction. Result in W.

ANDWF

f,d

AND's contents of W with contents of selected register. Result in W or f.

IORLW

k

OR's contents ofW with literal (mask) contained in instruction. Result in W.

IORWF

f,d

OR's contents of W with contents of selected register. Result in W or f.

XORLW

k

XOR's contents of W with literal (mask) contained in instruction. Result in W.

XORWF

f,d

XOR's contents of W with contents of selected register. Result in W or f.

ADDWF

f,d

Adds contents of W to contents of selected register. Result in W or f.

ADDLW ·

k

Add literal to W. Result in W.

SUBLW

k

Subtract W from literal. Result in W. (NOT subtract literal from Was name indicates!).

SUBWF

f,d

Subtracts contents ofW from contents of selected register by 2's complement arithmetic. Result in Worf.

Arithmetic

WEIRD MOVE INSTRUCTION Referring to: MOVF

f,d

Moves copy of selected register contents into W or f.

MOVF f,w makes sense (i.e, move the contents of a file register into the W register). MOVF f,f (moving the contents of a file register out and putting it back in) doesn't make sense except that it can be used as a method for testing the Zero (Z) flag. The Z flag is affected by the MOVF instruction. This will make more sense to you later. You can think of the MOVF instruction as meaning MOVFW (move the contents of a file register to the W register). MOVFW is not a legal op code but the version of MPLAB I use accepts it. If you choose to use the illegal op code, MOVFW moves data one way and MOVWF moves it the other way making remembering the op codes a lot easier. Test this with the version of MPLAB you use to be sure it works for you.

48

INSTRUCTION FORMAT FOR ASSEMBLER

First, some definitions: f = file register d = destination o= W register 1 = file register k = constant (literal) (literal instructions) . k = address label (call or goto instructions) b = bit designator (bit-oriented instructions) b = binary (literal instructions) d = decimal (literal instructions) There are four categories of instructions when it comes to formatting instructions for the assembler. Byte-Oriented Instructions

File Register Designator Hex Address (2-Digit) or Label Specifies Which File Register Is To Be Used By Instruction

Instruction

Destination Designator Specifies Where Result Of Operation Is To Be Placed

d d

= =

0 1

W Register File Register

49

Bit-Oriented Instructions

File RegIster DesIgnator Hex Address (2-Digit) or Label Specifies Which File Register Is To Be Used By Instruction

Instruction

BIt DesIgnator Hex O~ 70r Label (Defined By Equate)

Literal Instructions

LIteral

=

Constant

Uterals: Hexadecimal Binary Decimal

Instruction

Control Instructions (CALL and GOTO)

r

___Ik Instruction

50

Addr••• L.b.1

OxOf b'00001111 ' d'16'

DESTINATION DESIGNATOR (d)

Byte-oriented instructions are accompanied by a designation designator referred to as "d". d =0 d=1

Destination is W register (result in W). Destination is a file register (result in f).

Trying to remember if d should be 0 or 1 is a nuisance. MPLAB takes care of this for you as you can use w and f in the source code and MPLAB knows that for w d=O and that for f d=l. The MS-DOS version of MPLAB does NOT take care of this automatically. The solution is to put the following equates at the beginning of every program: w f

equ equ

0 1

decf

counter,f

movf

counter,w

Examples:

,.

if means result in file register means result in w register iW

Note that if the contents of a file register are operated on and the destination of the result is W, the contents of the file register remain unchanged. incf rrf

value,w value,w

iresult to W iresult to W

In both cases, the result goes to Wand the contents of the file register labeled "value" are unchanged. The examples of complete programs in this book assume the use of MPLAB and the w and f equates are not shown. If you are using an MS-DOS version ofMPASM, you will need to remember to add them to your source code.

51

HEXADECIMAL NUMBERS vs. MPASM ASSEMBLER The use of hexadecimal numbers with PIC16s is full of inconsistencies! You will see this when you look through program listings from other sources. For example port B may be equated to the file address hexadecimal 06 in the following ways:

portb

equ

6

06 06h h'06' Ox06 The MOVLW instruction is used to load the W register with hexadecimal literals as follows :

movlw

00 OOh h' 00' OxOO Of ff ffh h'ff' Oxff

won't work won't work

If 00 and Ofwork, why doesn't ff work? It looks like the same form to me. The important thing is to be aware of the inconsistencies and use a format that always works. The first character in the literal expression must be "0" or "h" for the assembler to work. Sooooo to make things manageable we will settle on a standard/uniform way of doing things for the examples in this book. Single hex digits by themselves will be used for: Equates Instructions

bits 0 ~ 7 bit designator b 0

~

7

Hex addresses will be in the following form: File registers = data memory Program memory

52

OxXX OxXXX

Hex numbers in literal instructions will be in the following form:

r

2 Hex Digits

r-L-,

OxXX

~

I

Think Of This As SignRying Hex

OxOf

movlw

'-r'y

Signffies Hex

~

' - OF Hex

The programs in this book are written using these conventions. All references to hexadecimal numbers in the text of the book will use the Ox notation. BINARY AND DECIMAL NUMBERS vs. MPASM ASSEMBLER Binary and decimal literals may be written as shown:

movlw movlw

b'OOOOllll'

ibinary idecimal

d'16'

Note that the' is the apostrophe on the same key as " on the keyboard. Some listings I have seen works with MPASM. appear to have the literal bracketed in' '. Only' I

ASCII CHARACTERS vs. MPASM ASSEMBLER Most ASCII characters may be included in a program by doing one of the following:

movlw

a'C'

iascii capital C

movlw

'C'

iascii capital C

or simply

53

ADDRESSING MODES • IMMEDIATE - using "literal" instruction which loads data from program memory into Wregister. • DIRECT - specific address. • INDIRECf - using file select register (FSR) (f4). • RELATIVE via bit test and skip (2 addresses away). via loading computed addresses into the program counter. Could be out into the middle of a bunch of RETLWs (lookup table) or GaTOs (jump table).

Immediate Addressing The literal instruction has the data (literal) built into the instruction word.

Direct Addressing The direct addressing mode is straightforward. The address is specified following the instruction. clrf

temp

;clear file labeled temp

G;direCI Addressing

T~fi;~'tfileregIsterfO (indirect address or indirect file, INDF) at address OxOO is not physically implemented (it ain't there!). Using OxOO as an address actually calls for the contents of the file select register f4 (FSR at Ox04) to be used as the address of a file register. The indirect address register is bogus. Using its address tells the PIC16 that what you really want is the address pointed to by the FSR. Weird, but that's how it works. If you want to take off relative to a known file register address, add an offset or index to that address, store the result in the FSR and use OxOO as the file register designator for the instruction which takes you to the resulting address. This is like "indexed addressing" with other microcontrollers. Since there are only 68 general-purpose file registers in a PIC16F84, you won 't be indexing far.

54

; assume address is in file labeled "hold"

;

movf movwf movf continue

hold,w fsr OxOO,w

;contents of hold (address) to W ;W to fsr ;load data at address into W

To review. using OXOO (the address of fO) causes the contents of the FSR (at Ox04) to be substituted and used as the file register address of the instruction. FOis useful as an address pointer. . A practical application for this might be sending characters from 16 file registers Ox20 to Ox2F to an LCD display.

next

movlw movwf movf call incf btfss goto

Ox20 fsr indf,w send fsr,f fsr,4 next

;initialize pointer ;get data pointed to ;sub sends byte to LCD ;inc pointer ;done ? ;no, again

To implement this. one could load the "display RAM" in advance using MOVLW instructions.

Relative Addressing Relative addressing for the PIC16 involves altering the contents of the program counter. This constitutes a computed jump from wherever the program counter is to some address relative to that point For OOTO and CALL instructions on the PIC16F84. this is handled automatically because the address is included in the instruction. The 5 high bits of the PC are loaded from PC latch high (PCLATH) by any instruction which writes to the PC. All computed jumps must be preceded by being sure that PCLATH contains the correct bits. Since we are avoiding program memory paging for now. we are limited to using the first 256 locations of program memory for computed jumps. So. if you want to take off from where the PC is. add the offset or index to the PC and execution will continue at the resulting address. Use the contents of a file register as an offset or index to jump to a code chunk:or to a OOTO.

55

Use Of The RETLW Instruction For Accessing Tables of Data Via Relative Addressing

The RETLW instruction causes the W register to be loaded with the literal value following the RETLW instruction. This is useful for implementing data tables in program memory. Limited amounts of data can be stored in program memory and accessed this way. As an example, let's say we have an application where a counter (file register) is incremented (0 to some number = 9 or less) and we want to display the result on a 7-segment LED display. Seven segment codes are stored as a table in program memory with each code attached to a RETLW instruction (coupled with RETLW instructions). This is a way to store 8-bit data in 14bit program memory and gain access to it. The code which must access the table calls the subroutine which contains the table. The counter is labeled "count". After the counter has been incremented the last time, the counter contents are loaded in Wand the subroutine is called. The first instruction in the subroutine adds the contents ofW to the program counter (used as an index or offset). Execution jumps to a RETLW instruction containing the 7-segment code corresponding to the counter contents. Execution returns to the main program and the W register now contains the 7-segment code. The code is then sent to the 7-segment display via port B. The details for this example follow at the end of the programming chapter.

56

ADDWF

Return From Subroutine

RETLW RETLW RETLW RETLW Table

RETLW

"

Move Data Returned To Port B For Display

pc count

segments

equ equ

Ox02 OxOC

iprogram counter icounter

movf call movwf

count,w segments portb

icounter contents to w icall sub idisplay results

addwf retlw retlw retlw retlw retlw retlw retlw retlw retlw retlw

pc,f Ox3f Ox06 Ox53 Ox4f Ox66 Ox6d Ox7d Ox07 Ox7f Ox6f

iO seven-segment i1 i2 i3 i4 i5 i6 i7 i8 i9

iadd offset to pgm ctr

57

USING THE PORTS Port Data Direction

Port data direction (input vs. output) is controlled using a special register called the tristate register. Like the option register, we will think of the tristate (1RIS) register as a "buried" register, not part of the file register address space and having no address. We will use the TRIS instruction to write to it. This is the second of two obsolete PICI6F84 instructions which we will use because it makes life more manageable. File register bank switching and the use of Microchip's intended methods will come later. The TRIS and OPTION methods are the only ones available for the PICI6C54, so learning them now is part of the backward compatibility plan. The TRIS register has 8 bits.

7

TRIS Register

o

o=TTI,---,-----,------,-----,

Loading a "0" in a TRIS register bit makes the corresponding port bit an output. Conversely, a "1" results in an input.

movlw

,.

tris

b'00001111' iteach port B portb ibits7,6,5,4 outputs bits 3,2,1,0 inputs

The TRIS instruction transfers the contents of the W register into the TRIS register which selects port data direction. Port ReadlWrite

An input port may be read as a whole by using the MOVF portX, W instruction. Data is valid as of the instant the port is read. An individual port line may be read using the BTFSC or BTFSS instructions.

58

The bit-oriented instructions BCF and BSF read the whole port contents into an internal central processing unit (CPU) register, change a bit, and write the contents of the internal CPU register back to the port. This is called read-modify-write. The port is read at the beginning of an instruction cycle and written to at the end of the instruction cycle. If a read immediately follows a write to the same port, problems may occur. Time should be allowed for the port to stabilize. Inserting a NOP instruction between the write and read should prevent this problem from occurring. Doing two consecutive writes to a port may cause the same problem to occur. Again, inserting a NOP should prevent trouble. FLAGS

A flag is a one-bit register which is set (1) or cleared (0) by execution of one or more types of instructions. The setting or clearing generally takes place automatically. After execution of an instruction, the affected flag may be tested to see if it was set or cleared. The path taken by the program depends on the status of the flag being tested. There are three flags in the PICI6F84. We will be interested in two of them (Z and C). For now, it is sufficient to know that these flags exist in the status word register S. Their significance will be discussed as the applications for them arise.

FLAGS Z C

Zero Carry

SIMPLE DATA TRANSFERS

This program moves data from port B (file register f6 at address Ox06) to a file register labeled "hold".

movf ffiovwf

portb,w hold

iffiove contents of port B to W reg iffiove contents of W register to ifile register labeled hold

This requires moving data via the W register. There is no direct way to move data from one file register to another.

59

A data transfer is used to send a bit pattern to an output port. Let's arbitrarily choose the following bit pattern and send it to the parallel output port B located at Ox06 which we will assume drives 8 LEDs.

Bit Pattern Hexadecimal movlw tris movlw movwf

OxOO portb OxOf portb

0000 1111 OxO OxF iload W with OxOO iteach port B outputs iload W with OxOF iload port B with contents of W

To set an output port to all O's as part of a program, you can use the CLRF instruction which clears the selected register to OxOO.

clrf

portb

Input ports are read using data transfers. DIP switches are assumed to be located at port A, bits 3,2, 1,0 (LO 4 bits) and LEDs are assumed to be located at port B.

i=======PICT2.ASM==============================5/19/96== idata transfer demo i----------------------------------------------------- - list p=16f84 radix hex ;------------------------------------------------------, cpu equates (memory map) porta equ OxOS portb equ Ox06 i----------------------------------------------------- - OxOOO org start movlw Oxff iload w with OxFF tris porta iCOPY w tristate, port A inputs i movlw OxOO iload w with OxOO tris portb iCOPY w tristate, port B , outputs movf porta,w iread port A, result in W iswitch status display at port B movwf portb circle goto circle icircle

·

·



end i----------------------------------------------------- - iat blast time, select: , memory unprotected , watchdog timer disabled (default is enabled) standard crystal (using 4 MHz osc for test) i , power-up timer on

· · · ;=======================================================

60

Set the switches at the input port and run the program. Compare the resulting bit pattern at the display with the pattern you set in. Change the input pattern and rerun the program.

LOOP· ENDLESS Above we used an endless loop to fake a halt (the PIC 16F84 does not have a halt instruction). The GOTO (go to) instruction goes to the location labeled "circle" which is the address of the GOTO instruction. The PIC16F84 sits in an endless loop (circle). Next we will use the GOTO instruction to make a more useful loop. The program will read the input port A and display the 4 bits of port A continuously at the lowest 4 bits of port B. This time, the program repeats over and over by jumping back to "start" each trip through the loop. The code is the same as before except for the address in the GOTO instruction.

The program is the same as above except for the line beginning with the label "circle" is replaced with:

gata

start

iagain

Now vary the switch settings while the program is running. This loop will run forever unless you press the reset switch or pull the plug (sometimes called "absolute reset").

61

LOOP WITH COUNTER

It is often necessary to perform some operation a specified number of times. To do this, a file register may be set aside to be used as a counter. Each time the operation is performed, a one is added to the counter. This is called incrementing the counter and the instruction is INeE When the number in the counter becomes equal to the number of times the operation is to be performed, the program can stop or go on to something else. A simple example will illustrate this concept.

Load Counter With OxOO

Initialize

Again

Move Counter Contents

To Port 8

Do

Add 1

We will use file register fc (general purpose file register = RAM) labeled "count" as the counter. Storing the counter contents at port B is just something for the program to do. We will find more exciting things to do later in this section. ; =======PICT3.ASM==============================5/22/96== ;loop with counter demo count up ;

,-------------------------------------------------------list radix

p=16f84 hex

i----------------------------------------------------- - ;

status portb count

cpu equates (memory map) equ Ox03 equ Ox06 equ OxOc

i----------------------------------------------------- - -

start

org movlw

OxOOO OxOO

tris

portb

clrf clrf

portb count

;

62

;load w with OxOO ;copy w tristate, port B outputs ;outputs LO ;clear counter

again

movf movwf incf movlw subwf

,.

c i r cle

btfs s goto g ot o

count,w ;counter contents to W portb ;counter contents to port B count,f ;inc count, result in count Ox05 ; load w with Ox05 count,w ;subtract Ox 05 from counter, result in W register status,2;test Z flag aga in ;not = Ox05 circ le ; = Ox 05, done

;

end ; --------- --- - ---- -------- ------ ------------------------

;at b la s t t ime, s el e ct: , memo r y unprotecte d , wat c h d o g t i me r di s abl e d (de fa ult is enabled) ; standard c r y s t a l (using 4 MHz osc for test) power-up timer on ;

. .

i===== === = = = = = = == = = = = = = = = = = = === = = = = = = ========== = = ===== ==

Port B will indicate Ox04 in binary (00000100). This program illustrates a very important concept, the power of microcontrollers to make decisi ons. A comparison is made using the MOVLW and SUBWF instructions to see if two things are the same. If they are, the program stops. If not, the program loop continues until the counter hits the number we have chosen . The subtraction and bit test instructions (comparison) determine the flow or path of the program. Load the program and run it to see what happens . It will be finished in a few microseconds. Port B should contain Ox04. You may want to try other numbers in the counter. You may find it easier to load the counter with the number of times an operation is to be performed and decrement the counter using the DECFSZ instruction. When the count falls to zero, the next instruction (a GOTO) is skipped sending execution to the code following. The previous and following examples send the counter contents to LEDs at port B 5 times. When incrementing, the counter contents are 0 when the first operation occurs and 4 when the last one occurs. When decrementing, the counter contents are 5 when the first operation occurs and 1 when the last operation occurs. •

63

Load Counter With No.

Initialize

Again Move Counter Contents

To Port B

Do

Subtract

1 DECFSZ Instruction

Done (Circle)

i=======PICT22.ASM=============================2/16/99== ilOOP with counter demo i count down i----------------------------------------------------- - -

list radix

p=16f84 hex

i----------------------------------------------------- - i

portb count

cpu equates (memory map) equ Ox06 equ OxOc

i----------------------------------------------------- - -

start

again

circle

org movlw tris clrf movlw movwf movf movwf decfsz goto goto

,. end

64

OxOOO OxOO portb portb OxOS count count,w portb count,f again circle

iload w with OxOO iCOpy w tristate, port B outputs iport B lines low iload w with OxOS iW contents to counter icounter contents into W idisplay at port B r dec counter, result in count icount not = OxOO icount = OxOO, done iOxOl at port B

;------------------------------------------------------;at blast time, select: ; memory unprotected ; watchdog timer disabled (default is enabled) , standard crystal (using 4 MH z osc for test) ; power-up timer on

.

;=======================================================

Port B will indicate OxOI in binary (00000001) . This demo is boring, but it illustrates the concept. This technique will be used in time delay loops later. A key consideration in designing program loops using counters is what order to do things in. The three things going on are doing whatever the loop is designed to do, incrementing or decrementing the counter and testing the counter. If these operations are done in the wrong order, the program will produce strange results.

LOOP UNTIL

Another use for a loop is a loop-until situation.

No

Yes

An example follows in the section on bit manipulation. The program goes into a loop until a switch is opened.

65

COMPARISONS

The contents of the W register can be compared with a literal value N to determine their relative magnitudes. This is done this by subtracting the contents of W from the literal value.

,.

,.

movlw sublw

OxXX N

btfsc/s

status, 2

i

iload W with literal isubtract contents of W from literal N - Result in W itest Z-flag, skip next instruction if result 0/1

goto

Result: W = N W :# N

Zero flag is set if Zero flag is clear if

A branch may be taken or not depending on the results of the comparison. Remember that the SUBLW instruction really subtracts W from the literal value. A further result is: Carry flag is set if Carry flag is clear if

W W

s

>

N N

Possible tests are: Test For

Flag Tested

W = N W :# N W N W> N

Z Z C C

s

Flags: Set

=

Set Clear Set Clear

1, Clear

=

0

Follow the subtraction with "bit test, skip if' to send the microcontroller to whatever code is appropriate depending on the results of the comparison. Here is a simple program to demonstrate a method of testing the comparison procedure. LEOs are assumed to be connected to port "B" for use as an indicator.

66

Load W With Test Number

Status Register To

Port B LEO"s

2

1

0

=rzTTCJ Status

--------------------------W Z N C --------------------------W < N OxOS Ox04 1 0 W = N OxOS OxOS 1 1 W > N OxOS Ox06 0 0 ;=======PICTS.ASM=============================10/12/97== list p=16f84 radix hex ;------------------------------------------------------, cpu equates (memory map) 03 status equ portb equ 06 ;------------------------------------------------------org 0

.

;

start

circle

movlw tris movlw sublw movf movwf goto

OxOO portb Ox04 OxOS status,w portb circle

;load w with OxOO ;copy w tristate, port B outputs ;test number in w ;subtract W from N (OxOS) ;get status ;display status via LEDs ; done

;

end i--------------- -------------------------------------- - -

67

iat blast time, select: i i i

,.

memory unprotected watchdog timer disabled (default is enabled) standard crystal (using 4 MHz osc for test) power-up timer on

;=======================================================

Note that subtraction followed by a check to see if the carry flag is set tests for W "less than" or "equal to" the literal value N. To test for W "less than" the literal N exclusively, use the desired test number (N) minus one. M = N-l.

M-N -1

~---.

W>M

No-Carry Flag Clear

SoW~N

WsM SoW

N N

C Set C Clear

To test for W