PIC PROGRAMMING START. The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle.

Similar documents
Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

Flow Charts and Assembler Programs

CONNECT TO THE PIC. A Simple Development Board

Jordan University of Science and Technology Electrical Engineering Department Microcontrollers and Embedded Systems Spring 2011

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

Lecture (04) PIC16F84A (3)

Chapter 5 Sections 1 6 Dr. Iyad Jafar

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

PIC Discussion. By Eng. Tamar Jomaa

The University of Texas at Arlington Lecture 5

Binary Outputs and Timing

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

Assembly Language Instructions

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

ECE Test #1: Name

EEE111A/B Microprocessors

PIC 16F84A programming (II)

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

Input/Output Ports and Interfacing

16.317: Microprocessor-Based Systems I Summer 2012

SOLAR TRACKING SYSTEM USING PIC16F84A STEPPER MOTOR AND 555TIMER

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

Chapter 3: Further Microcontrollers

ECE Homework #3

Learning Objectives:

Figure 1: Pushbutton without Pull-up.

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

Physics 335 Intro to MicroControllers and the PIC Microcontroller

D:\PICstuff\PartCounter\PartCounter.asm

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

/ 40 Q3: Writing PIC / 40 assembly language TOTAL SCORE / 100 EXTRA CREDIT / 10

Done 1 NVB THF TLF T/R POL 1SHOT. Figure 1: Status register

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

Chapter 11: Interrupt On Change

Performance & Applications

Mechatronics and Measurement. Lecturer:Dung-An Wang Lecture 6

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

PIC Architecture & Assembly Language Programming. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

EXPERIMENT 4: Parallel Input/Output. Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing

Instuction set

ALU and Arithmetic Operations

Lesson 4 Fun with W and F

Application Note - PIC Source Code v1.1.doc

Micro II and Embedded Systems

Embedded Systems Programming and Architectures

Arithmetic and Logic Instructions. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

ME 515 Mechatronics. A microprocessor

Week1. EEE305 Microcontroller Key Points

16.317: Microprocessor-Based Systems I Spring 2012

Laboratory Exercise 5 - Analog to Digital Conversion

SOLUTIONS!! DO NOT DISTRIBUTE!!

LPTCOM. Bruce Misner Lakehead University h d3 RD2 pin 21. RD3 pin h d4. RD4 pin 27 RD5 pin h d5. RD6 pin 29 RD7 pin H d6

Locktronics PICmicro getting started guide

The University of Texas at Arlington Lecture 3

Arithmetic,logic Instruction and Programs

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

PART TWO LISTING 8 PROGRAM TK3TUT8 MOVF PORTA,W ANDLW B ADDWF COUNT,F MOVF COUNT,W MOVWF PORTB GOTO LOOP

DERTS Design Requirements (1): Microcontroller Architecture & Programming

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

Programmable Control. Name Class Teacher. Ellon Academy Technical Faculty

ME 6405 Introduction to Mechatronics

DISCONTINUED. SPI Communication with AMT bit Absolute Encoder

CENG-336 Introduction to Embedded Systems Development. Timers

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

Explanation of PIC 16F84A processor data sheet Part 1: overview of the basics

EEE111A/B Microprocessors

A Better Mouse Trap. Consumer Appliance, Widget, Gadget APPLICATION OPERATION: Ontario, Canada

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

Lesson 7 Multiple Precision Arithmetic

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

More (up a level)... Connecting the Nokia 3510i LCD to a Microchip PIC16F84 microcontroller

Outline. Micriprocessor vs Microcontroller Introduction to PIC MCU PIC16F877 Hardware:

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

EE 367 Introduction to Microprocessors Homework 6

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

Chapter 5. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

ECE 354 Introduction to Lab 2. February 23 rd, 2003

Professor E. Ambikairajah UNSW Sydney

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 2. PIC and Programming

Interrupts. ELEC 330 Digital Systems Engineering Dr. Ron Hayne. Images Courtesy of Ramesh Gaonkar and Delmar Learning

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

Getting Started Guide

How Stuff Works: Processors

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

Transcription:

PIC PROGRAMMING You have been introduced to PIC chips and the assembly language used to program them in the past number of lectures. The following is a revision of the ideas and concepts covered to date. PLANNING USING A FLOWCHART The first stage of any program is to write a flowchart to plan the actions required. The first symbol in any flowchart is an Oval that indicates the Start of the program. START The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle. SET UP THE PORTS All Symbols have a connecting line with an arrow showing the direction of progress. Once the Ports have been set up the main section of the program follows. For example if we are controlling a set of traffic lights the first job after the setting of the PORTS is to OUTPUT the first light sequence on to the Lights connected to the output PORTS. OUTPUT FIRST LIGHT SEQUENCE TO THE TRAFFIC LIGHTS 1

The Processes continue in this fashion until a question or an INPUT is needed to be checked or a DECISION is required. The symbol for a DECISION is a diamond shape that has two possible output routes. Usually one route is when the answer to the question is YES and the other when the answer is NO. IS SWITCH ON PORTA BIT 0 ON? NO END YES After you have finished all Processes required the Flowchart either has an END oval symbol or the last Process symbols connecting line is directed back into the flowchart so the program continues indefinitely, see example below. OUTPUT SECOND LIGHT SEQUENCE TO THE TRAFFIC LIGHTS OUTPUT THIRD LIGHT SEQUENCE TO THE TRAFFIC LIGHTS 2

SETTING UP THE PORTS With any program we must communicate with the outside world, whether that is taking information IN to the PIC chip from inputs like switches and sensors or sending information OUT of the PIC chip via the outputs like lights and motors etc. Therefore, the first task of every program is to set up the PORTS of the PIC chip to set them up as either INPUTS or OUTPUTS as required by the particular program. In ET3 we are mainly concerned with the PIC16F84 PIC chip which has 2 PORTS, that is PORTA which has 5 bits or physical connections to the outside world (but we always write 8 bits to the PORT) and PORTB which has 8 bits or connections to the outside world. 4 3 PORTA 2 1 0 7 6 5 PORTB 4 3 2 1 0 The way we set up the PORTS is always the same the only difference between programs is which bits you require to be INPUTS and which bits you require to be OUTPUTS on PORTA and PORTB. Once we have decided on which bits are INPUTS and which are OUTPUTS we then begin to write the code or program or instructions required. The setting up of the PORTS is performed when the PIC chip is in PAGE1 of the memory. Moving to PAGE1 requires one instruction BSF STATUS,5. Once in PAGE1 a 1 or a 0 is written in each position of the PORTS to make them either an INPUT = 1 or an OUTPUT = 0. This is performed in two steps, first the 1 s or 0 s to set up the s of the PORTS are set up in the W register using the instruction MOVLW b 11110000. 3

The second step is to write the information set up in the W register to the PORTS using the special names TRISA for PORTA and TRISB for PORTB, using the instruction - MOVWF TRISB. The special names are used to indicate that the PORTS are being set up as INPUTS or OUTPUTS. The binary number b 11110000 used in this example would make PORTB have 4 INPUTS and 4 OUTPUTS. REMEMBER the two steps above are written to set up PORTA and then PORTB i.e. do it twice. The last step is to move back to PAGE0, which requires one instruction BCF STATUS,5. This is the end of the setting up of the PORTS the main program follows next. RECAP Set up PORTA as all OUTPUTS and then PORTB as 4 INPUTS then/and 4 OUTPUTS. STEP1 Write the instruction to move from PAGE0 to PAGE1 of the memory BSF STATUS,5 ; Move to PAGE1 STEP2 Set up the 1 s and or 0 s for INPUTS and or OUTPUTS in the W register, for PORTA. MOVLW b 00000000 ; Set up 1 s and 0 s in W 4

STEP3 Write the information in W register to PORTA using the special names. MOVWF TRISA ; Write Wreg to TRISA STEP4 Set up the 1 s and or 0 s for INPUTS and or OUTPUTS in the W register, for PORTB. MOVLW b 11110000 ; Set up 1 s and 0 s in W STEP5 Write the information in W register to PORTA using the special names. MOVWF TRISB ; Write Wreg to TRISB STEP6 Move back to PAGE0 in preparation for the remainder of the program. BCF STATUS,5 ; Move to PAGE0 5

EXERCISE 1 Write the instructions required in each STEP to set up PORTA as 3 INPUTS then 2 OUTPUTS and PORTB as 4 INPUTS then 3 OUTPUTS then 1 INPUTS, as in the picture below. 4 3 PORTA 2 1 0 7 6 5 PORTB 4 3 2 1 OUT OUT IN IN IN IN OUT OUT OUT IN IN IN IN 0 STEP1= STEP2= STEP3= STEP4= STEP5= STEP6= Write your instructions into the VPIC simulator and start at address 005. Once you have written all your instructions step through the program and see if the PORTS are as required. A blue arrow pointing in represents an INPUT PORT and a red arrow pointing out represents an OUTPUT PORT. 6

THE MAIN PROGRAM Once the PORTS have been set up correctly as required we turn our attention to the main section of the program. Once we have set up the PORTS we will have to either get information IN to the PIC chip from the outside world and/or get information OUT to the outside world via the PORTS. We will begin with getting information OUT of the PIC chip via the PORTS. Outputting Information from the PIC chip There are mainly three instructions we will use to Output information to the PORTS of the PIC chip. Outputs can be Lights or Motors for example. The first instruction is as follows: BSF PORTB,1 ; SET PORTB 1 ON = 1. This instruction Sets (makes it a 1) 1 of PORTB, so puts the Output device ON. This instruction can be used with any PORT or File in fact and any of the 8 bits of the PORTS or File. The second instruction is as follows; BCF PORTB,1 ; CLEAR PORTB 1 or put OFF = 0. This is the opposite of the first instruction in that it is used to CLEAR or put OFF = 0 a single bit of an Output Port at a time. This instruction can be used with any PORT or File in fact and any of the 8 bits of the PORTS or File. The two instructions above can only put ON or put OFF a single at a time. If we need to put more than one of the PORTS or a File ON or OFF at the same time we need to 7

use the following instruction. The third instruction is as follows: MOVWF PORTB ; Move the contents of W register to the File PORTB. The W is the one and only Working Register that exists in the PIC 16 series. This is an 8-bit width register (store for 8 bits) used for general operations. The W register will be used all the time and therefore you need to get used to it really quick. Again the instruction can be used with any PORT or File, but 8 bits are written at the same time with this instruction. When used with PORTA of the PIC16F84 PIC chip in ET3 only the 5 least significant bits are transferred to PORTA as PORTA has only 5 bits in this chip. Usually the third instruction is usually used in conjunction with another instruction used to set up the 8-bit sequence required on the Output PORT, the instruction is: MOVLW h 0F MOVWF PORTB ; Set up the sequence required on PORTB in W register. ;Write the sequence Out to PORTB. The MOVLW k instruction is the literal value loading instruction where k is the value to be loaded. Literal means a number so MOVLW k means Move or load the number k in to the W register. As you will see, there is no direct way of loading a literal value to the Ports or a File register. First, the value needs to be loaded to the W register and then the 8

W register is moved to the Port or File register. Even though PORTB for example may not have 8 Output s as shown in EXERCISE 1 above, we still always write 8 s to the Port. The MOVLW instruction can be used with Binary, Hex or Decimal numbers, as shown in the examples below. MOVLW b 00111111 ; Set up the binary sequence in W register. MOVWF PORTB ;Write the sequence Out to PORTB. Or MOVLW h 3F MOVWF PORTB PORTB. ; Set up the hex sequence in W register. ;Write the sequence Out to Or MOVLW 0x3F MOVWF PORTB ; Set up the hex sequence in W register. ;Write the sequence Out to PORTB Or MOVLW d 63 ; Set up the decimal sequence in W register. MOVWF PORTB ;Write the sequence Out to PORTB. 9

Inputting Information Into the PIC chip The INPUTS could be from switches or sensors that give either a Logic 1 for ON and a Logic 0 for OFF. This is obviously Digital information but it is possible with some PIC chips to input Analogue information or a voltage that can vary anywhere from a Minimum value to a Maximum value. This could be from a Temperature sensor for example and the Analogue Input is then converted using an Analogue to Digital Converter inside the PIC chip to a Digital number. We will only be using Digital Inputs in ET3. We can either test if a certain INPUT is ON = 1 or OFF = 0 directly and then perform different sequences or steps depending on the answer to the test, or we can Input the information IN to the PIC chip and then manipulate or isolate the required bits before testing. Method 1 The first method where we test the Input directly has two instructions, one that tests if an Input is SET or ON=1, and one that tests if an Input is CLEAR or OFF=0. BTFSS PORTB,1 ; Test PORTB 1 Skip the next instruction if the bit is SET or ON=1. This instruction is easier to understand if we place address numbers before all the instructions in our program. 00B BTFSS PORTB,1 ; Test if PORTB 1 is ON=1. 00C GOTO SEQ2 ;NO, PORTB 1 is OFF=0 do SEQ2. 00D GOTO SEQ1 ; YES, PORTB 1 is ON=1 do SEQ1. Remember this corresponds to the Decision process or the diamond shape in our Flowchart. The question to test if the Input connected to PORTB 1 is SET or ON is at address 008. When PORTB 1 is OFF (or the answer to the question is NO) the program will go to address 009 and we then jump over 10

the next instruction at 00A by going to a different part of the program to do the second sequence (SEQ2) using the GOTO instruction. When PORTB 1 is SET or ON (the answer to the question is YES) the program will Skip or jump over the next address at 009 and go directly to address 00A and we then jump to a different part of the program to do the first sequence (SEQ1). We could have performed the first sequence in the addresses immediately following address 00A which is usually done, but the concept of going to two different locations depending on the answer to a question is sound. The instruction to test if an Input is Clear or OFF=0 is as follows: BTFSC PORTB,1 ; Test PORTB 1 Skip the next instruction if the bit is CLEAR or OFF=0. Again with the addresses we have: 00B BTFSC PORTB,1 ; Test if PORTB 1 is OFF=0. 00C GOTO SEQ2 ; NO, PORTB 1 is ON=1 do SEQ2. 00D GOTO SEQ1 ;YES,PORTB 1 is OFF=0 do SEQ1. The question to test if the Input connected to PORTB 1 is CLEAR or Off is at address 008. When PORTB 1 is ON (or the answer to the question is NO) the program will go to address 009 and we then jump over the next instruction at 00A by going to a different part of the program to do the second sequence (SEQ2) using the GOTO instruction. When PORTB 1 is CLEAR or OFF (the answer to the question is YES) the program will Skip or jump over the next address at 009 and go directly to address 00A and we then jump to a different part of the program to do the first sequence (SEQ1). 11

Before you write your Flowchart decide where the Inputs and Outputs will be connected to the PORTS of your PIC chip. This is best done with a picture or a table showing what is connected to where. RECAP Using the PORTS set up in Exercise1 as an example, and assuming that the code to set the PORTS up has been written start the program at address 00B. 4 3 PORTA 2 1 0 7 6 5 PORTB 4 3 2 1 OUT OUT IN IN IN IN OUT OUT OUT IN IN IN IN 0 Outside Light Motor1 Alarm On Switch Window Sensor Sensor Full Sensor Empty Sensor Motor2 Box Sensor Heater1 Off Switch Door Sensor The above is an example of Inputs and Outputs that are connected to a PIC chip to control a small production line. Write the program instructions required to turn ON Motor1 on PORTB 6, If the On Switch is pressed as sensed by the On Switch Sensor on PORTA 2. The program should continually check the On Switch and do nothing else until Motor1 is started. Assume that Motor1 has been switched OFF previously. On Switch Input = 0 if the On Switch has not been pressed. On Switch Input = 1 if the On Switch has been pressed. 12

Motor1 is OFF if a 0 is placed on the corresponding Output and ON if a 1 is placed on the Output. The Program The first step is to write the instructions to ask the question to see if the On Switch has been pressed or not by Testing the On Switch Input on PORTA 2. You set up the Program Labels in the VPIC simulator at the correct positions, by clicking on the required position of the Label then pressing the Edit program labels button then double clicking on <add new>, then enter the name of your Label (e.g. ON in the example below) and then press OK. ON 00B BTFSS PORTA,2 ;Is the On Switch ON? The next step is to write the instructions to direct the program back to the section of the program where the question is asked IF the On Switch is OFF or has not been pressed. The best way to do this is to have a program Label at the required address, the Label used will be ON. 00C GOTO ON ; NO, go back to the question. The next step is to write the instructions to turn on Motor1 IF the On Switch is ON or has been pressed. 00D BSF PORTB,6 ;YES, turn on Motor1 on PORTB 6. The program would then continue. The whole program would be: ON 00B BTFSS PORTA,2 ; Is the On Switch ON? 00C GOTO ON ; NO, go back to the question. 00D BSF PORTB,6 ;YES, turn on Motor1 PORTB 6. 13

EXERCISE 2 Firstly, make sure the ROADWORKS add on has been selected in VPIC so you can see the ROADWORKS simulation underneath PORTA and PORTB. Now read through the whole exercise below first and then complete a full Flowchart (in the space provided below) for the complete program, that is, the setting up of the PORTS and the program for the Traffic Lights with the ON/OFF Switch and the Change Request W and E switch inputs. START 14

Using your Flowchart as a guide, write the instructions required in each STEP to set up PORTA as 2 INPUTS then 3 OUTPUTS and PORTB as 6 OUTPUTS then 2 INPUTS then, as in the picture below. This will set up the ports for the ROADWORKS add on in VPIC. 4 3 PORTA 2 1 0 7 6 5 PORTB 4 3 2 1 OUT OUT OUT IN IN IN IN OUT OUT OUT OUT OUT OUT Not Used Not Used Not Used Not Used Change Request W ON/OFF Switch Red light E Change Request E Red light W 0 Amber light E Amber light W Green light E Green light W STEP1= STEP2= STEP3= STEP4= STEP5= 15

STEP6= Below is the light sequence required for the traffic lights on the West and East side of the road works, where a 1 represents the lights being ON and a 0 represents the light being OFF. TRAFFIC LIGHTS WEST TRAFFIC LIGHTS EAST Sequence RED AMBER GREEN RED AMBER GREEN HEX NUMBER 1 1 0 0 0 0 1 21 2 1 0 0 0 1 0 22 3 1 0 0 1 0 0 24 4 1 1 0 1 0 0 34 5 0 0 1 1 0 0 0C 6 0 1 0 1 0 0 14 7 1 0 0 1 0 0 24 8 1 0 0 1 1 0 26 Now write the instructions required to put both Red Lights ON only, then check IF the ON/OFF Switch is ON or OFF. If the ON/OFF Switch is OFF = 0 go back and check again. If the ON/OFF Switch is ON = 1 then set Sequence 1 on the Traffic Lights. Remember that the ON/OFF Switch is connected to PORTB 7. The cars will now be moving East to West. Next we must look at the Change Request W input (PORTA 1) to see if there is a car waiting to go through the road works from the West side (You have to press the Change Request W switch with the mouse to activate the switch). If Change Request W input (PORTA 1) is OFF = 0 then we have to go back and check again. Remember this is done by setting up a Label on the Test instruction and then having a GOTO Label instruction in the position where the program goes if Change Request W input (PORTA 1) is OFF = 0. If Change Request W input (PORTA 1) is ON = 1 then we have to set Sequence 2 then Sequence 3 then Sequence 4 and finally Sequence 5 on the Traffic Lights. Now the cars will be moving from West to East. 16

Next we must do the same thing as above but with the Change Request E input (PORTA 0) to see if there is a car waiting to go through the road works from East to West. If Change Request E input (PORTA 0) is OFF = 0 then we have to go back and check again. Remember this is done by setting up a Label on the Test instruction and then having a GOTO Label instruction in the position where the program goes if Change Request E input (PORTA 0) is OFF = 0. If Change Request E input (PORTA 0) is ON = 1 then we have to set Sequence 6 then Sequence 7 then Sequence 8 and finally back to Sequence 1 on the Traffic Lights. Now the cars will be moving from East to West. In the final step to set up Sequence 1 we must jump back (using a GOTO Label instruction) to the start of the program where we set up Sequence 1 initially instead of setting it up for the second time. This is important because we want to keep using the same Traffic Light Sequences all the time that the ON/OFF Switch is ON. 005 006 007 008 009 00A 00B 00C 00D 00E 00F 010 17

011 012 013 014 015 016 017 018 019 01A 01B 01C 01D 01E 01F 020 021 022 023 After you have completed the instructions write them into the VPIC simulator and start at address 005. Once you have written all your instructions step through the program and see if the PORTS are as required. Then check to see if Sequence1 comes ON only when the ON/OFF Switch (PORTB 7) is pressed, both Red Lights should be ON if the ON/OFF Switch is not pressed. Then press the Change Request W 18

and see if the Traffic Lights change so that the cars travel from East to West. Method 2 The second method where we Input the information IN to the PIC chip and then manipulate or isolate the required bits before testing. This method is used where we want to check more than one bit of information at a time, whether that is 2 bits or a full 8 bits. An example could be when we have a keypad connected to PORTB as in a safe door, where we would have to check if the correct code was entered into the keypad before the safe was opened. The instruction is as follows: MOVF PORTB,W ; Move file PORTB into the W register. When the instruction is written into an Assembler program the W is replaced with 0, which means move into the W register. If it is replaced with a 1 it means move the file back into the file, which at first looks strange but as the Zero flag is affected by this instruction it can be used to check if the file contains nothing or zero. Once the information is in the W register we usually save the information in a general purpose register we have set up previously, for this example we will have 3 set up registers Num1, Num2 and Num3. The instruction to save the W register in one of our registers is as follows: MOVWF NUM1 ; Move the W reg into register Num1. Once the information is saved in the new registers we can then do a number of tests or checks to see if the register contains a given number or the number is greater or less than a predefined number. 19

In this example we need to check if the number saved is equal to a predefined number as in a code. This is done using the following instructions. We will check if NUM1 is equal to the number 4 or said another way, has the number 4 been input into the keypad. TEST1 MOVLW 04 ; Move 04 into the W register. XORWF NUM1,W ; Exor NUM1 with the number 04. BTFSS STATUS,2 ; Was NUM1 = 04? GOTO LOCK ; NO, go to LOCK. GOTO TEST2 ; YES, go to TEST2 to check NUM2. The first instruction loads the number 04 into the W register. The second instruction EXOR s NUM1 with the W register which has been loaded with 04. Why use EXOR? The EXOR of two bits gives an output of 0 if the bits are the same, and an output of 1 if the bits are different. Example 1 When NUM1 has 07 stored inside. W reg = 04H = 00000100 NUM1 = 07H = 00000111 RESULT 00000011 = 03H Example 2 When NUM1 has 04 stored inside. W reg = 04H = 00000100 NUM1 = 04H = 00000100 RESULT 00000000 = 00H As can be seen from the two Examples above when the numbers are different the RESULT of EXORing is NOT ZERO, 20

when the numbers are the same the RESULT of EXORing is ZERO. Therefore, after EXORing the Zero Flag 2 of the STATUS register can be checked to see if the two numbers are equal or different. If the Zero Flag is 1 the two numbers are the same or equal. If the Zero Flag is 0 the two numbers are different. So the XORWF instruction in conjunction with the testing of the Zero Flag BTFSS STATUS,2 gives two possible outcomes, one where the numbers are Equal and one where the numbers are Different. The full set of instructions would then be: MOVF PORTB,W ; Move file PORTB into the W register MOVWF NUM1 ; Move the W reg into register Num1. TEST1 MOVLW 04 ; Move 04 into the W register. XORWF NUM1,W ; Exor NUM1 with the number 04. BTFSS STATUS,2 ; Was NUM1 = 04? GOTO LOCK ; NO, go to LOCK. GOTO TEST2 ; YES, go to TEST2 to check NUM2. EXERCISE 3 Write the instructions required to Input the information on PORTB and save this information in the register NUM2. Then Test to see if NUM2 is equal to 0AH and if it is Not equal to 0AH go back and keep Inputting the information on PORTB and Testing until NUM2 (the information on PORTB) is equal to 0Ah. Then put PORTA bit 0 ON, then go back to the START of the program. Start at address 00A. 00A 00B 00C 00D 21

00E 00F 010 011 22

THE DELAY ROUTINE In most programs we will need to either wait for a predetermined length of time before an action is required, or slow down a given set of outputs or sequences. Therefore, a Delay routine or sometimes called a subroutine is required. A routine is a separate program from the Main Program that is written once but can be used many times, without having to be written again. The Flowchart for a simple Delay Routine is as follows: START OF DELAY1 SET LENGTH OF THE DELAY IN DEL DEL = DEL - 1 NO YES IS DEL = 0? RETURN 23

We now are in a position to write the instructions required to perform the actions in the Flowchart. As you can see from the Flowchart the way we make a DELAY in software is to place a big number in a register then decrease it by 1, check if it is zero and if it is the DELAY is over and the program needs to Return back to where it was Called from, if it isn t zero the program loops back and decreases the register by 1 again. As every single instruction in programming takes a given amount of time to perform we can work out accurately the length of time to perform the steps described above. Then by performing the loop many times we can set the DELAY to a required length by increasing or decreasing the set number placed into the register at the start of the DELAY Routine. When a Delay is required in the main program the following instruction begins the Delay Routine. CALL DELAY1 ; Jump to the Delay Routine. The CALL Instruction is different from the GOTO instruction in that when a CALL instruction is encountered by the program, the address of the next instruction to the CALL is saved, in preparation for a RETURN instruction, when the address is recovered and the program will continue from the instruction immediately below the original CALL. This does not happen with a GOTO instruction so the program has no way of returning back to the next address below a GOTO instruction. The first instruction of the Delay routine would be: DELAY1 MOVLW h 0F ; Set the Length in W register. 24

The first thing to note is the Program Label before the instruction that is Called by the Main Program. Following the Label is the instruction that sets up the number that will correspond to the length of the Delay. A large number will give a long Delay and a small number will give a short Delay. However, this number in the W register has to be saved in to the DEL register that has been set up by the programmer for this specific use. The instruction that does this is: MOVWF DEL ;Save the Length of Delay number in the DEL register. The next step is to take 1 away from the number in the DEL register and save the result back into DEL register, or said another way decrease DEL register by 1. D1 DECF DEL,F ; Decrease DEL by 1. Next we have to check if the result of decreasing DEL register by 1 gives an answer of zero. Checking the Zero flag that is located in the STATUS register 2 does this. BTFSS STATUS,2 ; Is DEL = 0? There are two possibilities, the first is that DEL is NOT zero therefore we have to loop back to label D1 to decrease the DEL register by 1 again. GOTO D1 ;NO, DEL>0 loop back do again. The second possibility is that DEL register IS zero and the DELAY is over and the program needs to RETURN back to 25

where it was called. RETURN ; YES DEL=0 DELAY over, Return. As stated earlier when a RETURN instruction is encountered the program goes back to where it was Called by recovering the address that was saved when the CALL instruction occurred. So the full DELAY1 routine would be: DELAY1 MOVLW h 0F MOVWF DEL ; Set the Length in W register. ; Save the Length in DEL reg. D1 DECF DEL,F ; Decrease DEL by 1. (STEP3) BTFSS STATUS,2 ; Is DEL = 0? (STEP4) GOTO D1 ; NO,DEL>0 loop back do again. RETURN ;YES DEL=0, DELAY over, Return to Calling program. There are a number of alterations or changes we can do to the first DELAY routine seen here. The first change is a small change that replaces the two instructions at Step 3 and 4 with a single instruction that performs the two steps in one. The instruction is: DECFSZ DEL,F ;Decrease DEL by 1 and then check if DEL = 0? The action of this instruction is exactly the same as the two instructions it can be used to replace. The choice of which 26

technique to use is up to the programmers preference. The second change that can be made is related to the length of the DELAY possible. With the 8-bit W register and the DEL register we used in the delay example previously the largest number possible is 0xFF Hex which is 11111111 Binary or 255 Decimal. So this limits the Maximum size of the DELAY we can produce with this method. With clock frequencies of the PIC 16F84 in the 1 to 4MHz range, this would give a relatively small DELAY in the range of a few milliseconds or a few thousandths of a second. Therefore, if we require a longer DELAY we have to do more. Longer or NESTED Delays If we wanted a minutes DELAY using a watch, for example, we would count 60 seconds using the second hand of the watch. So each click of the second hand provides a DELAY of length one second. We then count 60 of these 1 second DELAYs to give us a minutes DELAY. This is the technique we are going to use to provide longer DELAYs than is possible with the original technique shown on the previous pages. The technique is called NESTED DELAYS, which is a method of having many DELAYs within another DELAY Routine. The Flowchart on the following page shows the steps required to perform a NESTED DELAY Routine with 2 stages. The only real difference to the original DELAY Routine seen previously is the inclusion of the CALL DELAY1 instruction in between the Set the Length of the Delay Block and the DEL = DEL 1 Block. So in effect we are having DEL2 x DELAY1 delays in this Nested Delay Routine called DELAY2. 27

START OF DELAY2 SET NUMBER OF DELAY1 s REQUIRED SAVE IN DEL2 CALL DELAY1 DEL2 = DEL2-1 RETURN IS DEL2 = 0? NO YES 28

The instructions for this NESTED DELAY Routine called DELAY2 would begin with the Main Program Calling the DELAY. CALL DELAY2 ;Jump to the DELAY2 Routine. The DELAY2 then begins in a similar way to DELAY1. DELAY2 MOVLW h 04 MOVWF DEL2 ;Set the number in W register. ;Save the number of DELAY1 s required in the DEL2 register. Then DELAY2 Calls DELAY1 to delay for a certain amount of time. CALL DELAY1 ;Jump to the DELAY1 Routine. After DELAY1 has finished control returns to the next instruction which is: D2 DECF DEL2,F ;Decrease DEL2 by 1. We then check if DEL2 is equal to zero. BTFSS STATUS,2 ;Is DEL2 = 0? Then both possibilities are encoded. GOTO D2 RETURN ;NO, DEL2>0 loop back to do again. ;YES,DEL2=0 DELAY2 is over Return to Calling program 29

So the full DELAY2 routine would be: DELAY2 MOVLW h 04 MOVWF DEL2 CALL DELAY1 ;Set the number in W register. ;Save the number of DELAY1 s required in the DEL2 register. ;Jump to the DELAY1 Routine. D2 DECF DEL2,F ;Decrease DEL2 by 1. BTFSS STATUS,2 ;Is DEL2 = 0? GOTO D2 RETURN ;NO, DEL2>0 loop back to do again. ;DELAY2 is over, Return to Call -ing program. DELAY2 is an example of a 2 stage Nested Delay Routine, this technique can be continued as many times as needed to provide the Delay Length required. So to make a Delay of 1 Hour we could have a 3 stage Nested Delay Routine, that is have 60 x 1 Second Delays to give 1 Minute, then 60 x 1 Minute Delays to give 1 Hour. As you can see we could extend this idea to give longer Delays. The registers DEL1, DEL2, NUM1 and NUM2 used in the previous examples and pages, must be set up by the programmer. This is done at the beginning of the program using an equate statement as shown below. DEL1 EQU 0CH DEL2 EQU 0DH NUM1 EQU 0EH 30

NUM2 EQU 0FH There are specific addresses/locations used for these General Purpose Registers (GPRs) in the PIC chip memory. When using the VPIC simulation program, these registers are made by clicking on the Set up program Registers icon, then double clicking on the first address under the GPR table, (usually 0C) then insert the Name then the description. The register is then available for use. 31

The next Stage within our program is to count: 32