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

Similar documents
Lecture (04) PIC16F84A (3)

Assembly Language Instructions

PIC 16F84A programming (II)

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

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

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

EEE111A/B Microprocessors

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

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

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

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

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

Instuction set

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

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

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

Chapter 5 Sections 1 6 Dr. Iyad Jafar

SOLUTIONS!! DO NOT DISTRIBUTE!!

DERTS Design Requirements (1): Microcontroller Architecture & Programming

16.317: Microprocessor-Based Systems I Summer 2012

Chapter 13. PIC Family Microcontroller

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

D:\PICstuff\PartCounter\PartCounter.asm

Week1. EEE305 Microcontroller Key Points

ECE Test #1: Name

16.317: Microprocessor-Based Systems I Spring 2012

PIC16F84A 7.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

PIC Discussion. By Eng. Tamar Jomaa

CONNECT TO THE PIC. A Simple Development Board

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

Section 31. Instruction Set

16.317: Microprocessor Systems Design I Fall 2015

Embedded System Design

Application Note - PIC Source Code v1.1.doc

PIC16C63A/65B/73B/74B

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

Fortune. Semiconductor Corporation 富晶半導體股份有限公司. 8-bit MCU with 1k program ROM, 64-byte RAM, 1 R2F module and 3 13 LCD driver. TD Rev. 1.

/* 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.

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

Arithmetic,logic Instruction and Programs

PIC16C84. 8-bit CMOS EEPROM Microcontroller PIC16C84. Pin Diagram. High Performance RISC CPU Features: CMOS Technology: Peripheral Features:

PTK8756B 8 Bit Micro-controller Data Sheet

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

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.

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

ME 515 Mechatronics. A microprocessor

PIC16F8X 18-pin Flash/EEPROM 8-Bit Microcontrollers

Performance & Applications

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

Chapter 3: Further Microcontrollers

Learning Objectives:

ME 6405 Introduction to Mechatronics

Experiment 7:The USART

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

SOLAR TRACKING SYSTEM USING PIC16F84A STEPPER MOTOR AND 555TIMER

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

PIC16F8X. 8-Bit CMOS Flash/EEPROM Microcontrollers PIC16F8X PIC16CR8X. Pin Diagram. Devices Included in this Data Sheet:

Micro II and Embedded Systems

M PIC16F84A. 18-pinEnhanced FLASH/EEPROM 8-Bit Microcontroller. High Performance RISC CPU Features: Pin Diagrams. Peripheral Features:

16.317: Microprocessor Systems Design I Spring 2015

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

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

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

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

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

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

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

16.317: Microprocessor-Based Systems I Fall 2012

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)

Section 4. Architecture

PIC16C432 OTP 8-Bit CMOS MCU with LIN bus Transceiver

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

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

ALU and Arithmetic Operations

The University of Texas at Arlington Lecture 5

Flow Charts and Assembler Programs

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

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

CENG-336 Introduction to Embedded Systems Development. Timers

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

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

The University of Texas at Arlington Lecture 7

Chapter 11: Interrupt On Change

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

Lesson 4 Fun with W and F

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

PIC Discussion By Eng. Tamar Jomaa

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

Mod-5: PIC 18 Introduction 1. Module 5

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

Model Answer Microcontrollers. MCQ Problems Total Q1 Q2

DESIGN AND FABRICATION OF FARE METER OF TAXICAB USING MICROCONTROLLER

Appendix D: Source Codes. PDF created with pdffactory Pro trial version

PIC16F84A. 18-pin Enhanced Flash/EEPROM 8-Bit Microcontroller. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

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

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

Transcription:

LAB WORK 1 We are studying with PIC16F84A Microcontroller. We are responsible for writing assembly codes for the microcontroller. For the code, we are using MPLAB IDE software. After opening the software, we need to do some configurations. - Project-Project Wizard-Next-Choose Device(PIC16F84A)-Next-Next-Select a destination for your.mcp file-next-finish - File-New--> the file that we will write the code. We need to save this file as.asm file. - After saving the file, we need to add this.asm file under the Source Files - Click build all to check there is a mistake in your code or not. These are the steps to run the code that we have written. 1) The following code is needed to be run. list p=16f84a include p16f84a.inc clrf PORTB; PORTB=00000000, PORTB is an output bsf STATUS, RP0; Change to Bank 1 clrf TRISB; TRISB=00000000 bcf STATUS, RP0; Change to Bank 0 bsf PORTB, 0; Make the zeroth bit of PORTB 1 2) We need to open Program Memory Map in MPLAB. For that, - Debugger-Select Tool-MPLAB SIM - View-Program Memory By these steps above, we can trace the program by pressing F7. 3) In the.hex files, 0186 CLRF 0x6 is written instead of clrf PORTB Every command has its own 14-bit instruction word. 0186 is 14bit instruction word of clrf PORTB command. The address of the PORTB is 06h, and on Assembly Language Instruction Set, across the clrf command 00 0001 1fff ffff is written. For the f we need to write address of PORTB, because by clrf PORTB command we want to clear PORTB s content. In the other words, 00 0001 1000 0110 means clear PORTB. The value of this 14-bit number in hexadecimal form is 0186. In bsf STATUS RP0 command s 14-bit instruction word is 1683. On the table, across bsf command 01 01bb bfff ffff is written. Instead of b we need to write bit number of RP0 which is the fifth bit of the STATUS register, because we want to make this bit 1. Instead of f we need to write the STATUS register s address because we want to change the bit of STATUS Register. For b s we need to write 101 which is 5 in decimal, for f s 0000011 which is address of the STATUS Register (03 in hexadecimal form). To conclude, 01 0110 1000 0011 means, make the fifth bit(rp0) of STATUS Register. While we are writing 14-bit instruction word, we need to check which bank we are work at.

LAB WORK 2 1) We need to determine the errors for the following code: config _cp_off&_wdt_off&_xt_osc clrf portb; PORTB is needed to be written in capitals, this is the error for this question. 2) We need to determine the errors for the following code: clrf PORT; bsf STATUS, RPO; clrf TRISB; In clrf PORT line, there is not any indication whether PORTA or PORTB is used. There is not any register called PORT In bsf STATUS, RPO line, there is not any bit named RPO (letter o), there is RP0(zero). These are the errors for the second question. 3) clrf PORTB; PORTB is CLEARED bsf STATUS, RP0; Change to Bank 1 clrf TRISB; TRISB is cleared bcf STATUS, RP0; Change to Bank0 movlw 0xFF; Move literal(h FF ) to Working Register(W), W=h FF movwf 0x0C; Move the contents of the W into file at the address 0Ch, 0Ch=W=h FF movlw.64; Move literal (decimal 64) to W, W=.64 movwf 0x0D; Move the contents of the W into file at the address 0Dh, 0Dh=W=d 64 movlw 11110000b; Move literal (binary 11110000) to W, W=b 11110000 movwf PORTB; Move the contents of W into PORTB, PORTB=W=b 11110000 Lab Work 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button Address 06h 03h 86h 83h 0Ch 0Dh 06h Content 0x00 We are only interested in fifth bit of STATUS Register, xx1x xxxx 0x00 We are only interested in fifth bit of STATUS Register, xx0x xxxx 0xFF 0x40 0xF0

2) View-Special Function Registers Observe the changes. 3) MOVLW b 11111 MOVWF PORTA CLRF PORTB MOVF PORTA,0 MOVWF PORTB LAB WORK 3 1) Command cycle: is referred to a certain amount of time for the CPU to execute an instruction. In PIC16F84A, 1 instruction cycle takes 4 oscillator periods. For 4 MHz oscillator, command cycle=4x(1/4mhz) = 1us For 16 MHz oscillator, command cycle=4x(1/16mhz) =0.25us For 32 MHz oscillator, command cycle=4x(1/32mhz) =0.125us 32 MHz oscillator is the fastest compared to 4MHz and 16Mhz. 2) The following code is needed to be written in MPLAB SIM: clrf PORTB; bsf STATUS, RP0; clrf TRISB; bcf STATUS, RP0; bsf PORTB,0; bcf PORTB,0; After building the code, we want to observe the changes in bit RB0 Debugger-Select Tool-MPLAB SIM View-Logic Analyzer For observing RB0, Channel-RB0-Add-OK Press the play button and trace the program by F7 After bsf PORTB, 0 command, RB0 should be 1 through 5 cycles, until bcf PORTB, 0 command.

3) The following code will be built and Logic Analyzer should be opened. We are observing RB0. clrf PORTB; bsf STATUS, RP0; clrf TRISB; bcf STATUS, RP0; bsf PORTB,0; bcf PORTB,0; nop bsf PORTB,0; nop bcf PORTB,0; RB0 is 1(high) through 3 cycles then it is low through 4 cycles and again high through 2 cycles and low for through 1 cycle. 4) The command that take 2cycles: - GOTO - CALL - RETURN - RETLW - RETFIE - INCFSZ; if number of register is 0 take 2 instruction cycle, else take 1 instruction cycle. - DECFSZ; if number of register is 0 take 2 instruction cycle, else take 1 instruction cycle. - BTFSC; if tested bit equal 0 take 2 instruction cycle, else take 1 instruction cycle. - BTFSS; if tested bit equal 1 take 2 instruction cycle, else take 1 instruction cycle. 5) Counter equ 0x0C; free RAM location 12, counter is a register at the address of 0Ch N equ 0x0A; decimal constant 10, N is a variable with the value decimal 10 clrf PORTB; PORTB is cleared bsf STATUS, RP0; Change to Bank1, Make the fifth bit of status Register 1 clrf TRISB; TRISB is cleared bcf STATUS, RP0; Change to Bank0 movlw N; Move literal(n=0x0a) to W, W=N=.10 movwf Counter; Move the content of W into Counter, Counter=W=0x0A bsf PORTB,0; Make the zeroth bit of PORTB(RB0) 1 LOOP decfsz Counter, F; Decrement Counter skip if zero, write decremented value into Counter goto LOOP; This loop will continue until Counter=0 bcf PORTB,0; Make RB0=0

Lab Work Calculating the number of cycles needed for 100us delay by using: a) 100KHz oscillator Internal Frequency = 100KHz/4 = 25KHz ICT = 4 x (1/Oscillator Freq) = 1 / (Internal Freq) = 1/25KHz = 0.04ms = 40us ICN = Time Delay / ICT = 100us / 40 us = 2.5 cycles b) 4MHz oscillator Internal Freq = 4MHz/4 = 1MHz ICT = 1/1Mhz = 1us ICN = 100us / 1us = 100 cycles c) 16MHz oscillator Internal Freq = 16MHz/4 = 4MHz ICT = 1/4Mhz = 0.25us ICN = 100us / 0.25us = 400 cycles d) 32MHz oscillator Internal Freq = 32MHz/4 = 8MHz ICT = 1/8Mhz = 0.125us ICN = 100us / 0.125us = 800 cycles 1msec delay for 4MHz Oscillator: Internal Freq= 4/4MHz=1MHz ICT=1/1MHz=1us ICN=1ms/1us=1000 cycles We need to write a code that takes 1000 cycles for making 1miliseconds of delay.

LAB WORK 4 i. decfsz f, d: decrement f, skip if zero. If d=1, write the decremented value into file If d=0, write the decremented value into W ii. decf, d: decrement f iii. incfsz f, d: increment f, skip if zero iv. incf d: increment f v. sublw k: k-w, write the result of substation into W vi. subwf f, d: f-w, if d=1, write the result into file If d=0, write the result into W vii. rrf f, d: rotate right file viii. rlf f, d: rotate left file ix. comf f, d: complement f 2) Subroutines are often used to perform task that need to be performed frequently. This makes a program more structured in addition to saving memory space. In PIC16F84, call and return instructions are used for subroutines. The stack is read/write memory (RAM) used by the CPU to store some very critical information temporarily. This information usually is an address, but it could be data as well. The 14-bit core PICs have a stack of eight 13-bit registers which are exclusively used to hold subroutine return addresses. Remember that the Program counter is 13-bits wide to hold the 13-bit addresses for the Program store. 3) There is a loop inside loop. We are using subroutines to make a delay. Unless we are using subroutines, we need to write 1 million of nop command to create 1 second delay while using 4MHz oscillator. We need to compute the delay amount created by the following program segment: Counter_Outer equ 0x0C; free RAM location 12 Counter_Inner equ 0x0D; free RAM location 13 N equ.255; decimal constant 255 movlw N; 1cycle movwf Counter_Outer; 1cycle movwf Counter_Inner; 1cycle Loop_Outer movwf Counter_Inner; 1x255=255cycles Loop_Inner decfsz Counter_Inner, F; 1x255x255= 65025cycles goto Loop_Inner; 2x255x255=130050 cycles decfsz Counter_Outer, F; 255 cycles goto Loop_Outer; 2x255=510 cycles

To find the delay amount, firstly, we need to find total numbers of cycles which is 196098 cycles. 198098 cycles x 1us =196.098 msec delay 4) We need to write 1msec delay for 4MHz oscillator, for that need to write a subroutine that takes 1000 cycles. For example; Delay_1ms MOVLW h 0A ; W=.10 MOVWF R1 LOOP_1 MOVLW h 67 ; W=.100 MOVWF R2 LOOP_2 DECFSZ R2, GOTO LOOP_2 DECFSZ R1, f GOTO LOOP_1 RETURN END 5) We need to write 0.2sec delay for 4MHz oscillator. For example; Delay_0.2s MOVLW h FF MOVWF R1 LOOP_1 MOVLW h FF MOVWF R2 LOOP_2 DECFSZ R2, GOTO LOOP_2 DECFSZ R1, f GOTO LOOP_1 RETURN END 6) For 0.8sec delay for 4MHz oscillator, we need to write call delay_0.2s command 4 times. Because the maximum delay amount by using 2 loops is 0.2sec. 7) a) INCLUDE P16F84A.INC R1 EQU h 0C ; use loc 0DH for counter_1 R2 EQU h 0D ; use loc 0DH for counter_2 MEM EQU h 0E clrf PORTB BSF STATUS,5 CLRF TRISB BSF TRISA,0 BCF STATUS,5 CLRF MEM AGAIN BTFSS PORTA,0 GOTO AGAIN INCF MEM, f; MEM=MEM+1

MOVF MEM, W; W=MEM SUBLW d 10 ; W= 10-W BTFSC STATUS,2 GOTO ON GOTO AGAIN ON MOVLW h FF MOVWF PORTB CLRF PORTB LOOP GOTO LOOP DELAY_0.2S MOVLW h FF ; WREG=FF outer loop count value MOVWF R1; load FF into loc 25H (outer loop count) LOOP_1 MOVLW h FF ; WREG=FF inner loop count value MOVWF R2; load FF into loc 26H (inner loop count) LOOP_2 DECFSZ R2, f; decrement counter, skip if count=0 GOTO LOOP_2; repeat until count_2 becomes 0 DECFSZ R1, f; decrement counter, skip if count=0 GOTO LOOP_1; repeat until count_1 becomes 0 RETURN END - We need see the results of our program on experiment card. b) INCLUDE P16F84A.INC R1 EQU h 0C ; use loc 0DH for counter_1 R2 EQU h 0D ; use loc 0DH for counter_2 clrf PORTB BSF STATUS,5 CLRF TRISB BCF STATUS,5 LOOP MOVLW h FF MOVWF PORTB

CLRF PORTB MOVLW h 00 MOVWF PORTB LOOP GOTO LOOP DELAY_0.2S MOVLW h FF ; WREG=FF outer loop count value MOVWF R1; load FF into loc 25H (outer loop count) LOOP_1 MOVLW h FF ; WREG=FF inner loop count value MOVWF R2; load FF into loc 26H (inner loop count) LOOP_2 DECFSZ R2, f; decrement counter, skip if count=0 GOTO LOOP_2; repeat until count_2 becomes 0 DECFSZ R1, f; decrement counter, skip if count=0 GOTO LOOP_1; repeat until count_1 becomes 0 RETURN END We need see the results of our program on experiment card. c) INCLUDE P16F84A.INC R1 EQU h 0C ; use loc 0DH for counter_1 R2 EQU h 0D ; use loc 0DH for counter_2 clrf PORTB BSF STATUS,5 CLRF TRISB BCF STATUS,5 LOOP MOVLW h F0 MOVWF PORTB CLRF PORTB MOVLW h 0F MOVWF PORTB LOOP GOTO LOOP DELAY_0.2S MOVLW h FF ; WREG=FF outer loop count value MOVWF R1; load FF into loc 25H (outer loop count) LOOP_1

MOVLW h FF ; WREG=FF inner loop count value MOVWF R2; load FF into loc 26H (inner loop count) LOOP_2 DECFSZ R2, f; decrement counter, skip if count=0 GOTO LOOP_2; repeat until count_2 becomes 0 DECFSZ R1, f; decrement counter, skip if count=0 GOTO LOOP_1; repeat until count_1 becomes 0 RETURN END We need see the results of our program on experiment card. LAB WORK 5 The second bit of the STATUS Register is called Zero Bit or Z-Flag. If the result of the arithmetic or logic operation is 1 then Z-flag is 1, otherwise it is 0. 1) The following command do not affect the Z flag of the STATUS register movlw k movwf f swapf f, d 2) The following command affect the Z flag of the STATUS register, movf f, d clrf f 3) The following program segment clears the memory location 0x0C without affecting the Z flag of the STATUS register: Counter equ 0x0C; free RAM location 12 movlw 0; movwf Counter; loop goto loop; There is movlw and movwf command that affects the Z flag of the STATUS Register. In MPLAB, after building the code, Debugger-Select Tool-MPLAB SIM View-File Registers The address of the STATUS register is 03h and we need to observe the changes in 03h 4) The following program segment clears the memory location 0x0C affecting the Z flag of the STATUS register. Inspect the program segment below: Counter equ 0x0C; free RAM location 12 clrf Counter;

loop goto loop; There is not any command that affects the Z flag of the STATUS Register. Observe the value of the STATUS Register by using File Registers. 5) The following program transfers the content of 0x0C to PORTB without affecting the Z flag of the STATUS register. ; set PORTB as output port Counter equ 0x0C; free RAM location 12 movlw 0xF0; does not affect Z-flag movwf 0x0C; location 0x0C contains 0xF0, does not affect Z-flag swapf 0x0C, F; does not affect Z-flag swapf 0x0C, W; does not affect Z-flag movwf PORTB; does not affect Z-flag loop goto loop; The bit 1 of the STATUS Register is called Digit Carry/Barrow bit or DC-Flag In addition, operation if there is a carry from 4th low order bit of the result, DC=1, otherwise DC=0. In subtraction operation if barrow is needed from 4th low order bit of the result DC=0, if not DC=1 The bit 0 of the STATUS Register is called Carry/Barrow bit or C-Flag. At the of the addition operation, if there is a carry C-Flag is 1, if not it is 0. But at the of the subtraction, if barrow is needed, C-Flag is 0, otherwise it is 1. The following commands affect the carry-borrow flag of the STATUS register, addwf f, d subwf f, d addlw k sublw k 6) Trace the following program and see how half carry flag of the STATUS register is affected. bcf STATUS, 0; bcf STATUS, 1; bcf STATUS, 2; movlw 0x0F; does not affect Z-C-DC-flag addlw.1; affects all the flags, Z=0, DC=1, C=0 loop goto loop;

7) Trace the following program and see how carry flag of the STATUS register is affected. Note the content of the working register. bcf STATUS, 0; clear zero flag bcf STATUS, 1; clear half-carry flag bcf STATUS, 2; clear carry flag movlw 0x01; W=00000001, affects all the flags sublw.0; a borrow is needed, carry flag is reset, h 00 -h 01, C=1, DC=0, C=0 loop goto loop; 8) bsf STATUS, RP0; clrf TRISB; bcf STATUS, RP0; clrf PORTB; bcf STATUS, 0; Z=0 bcf STATUS, 1; DC=0 bcf STATUS, 2; C=0 movlw 0xFF; W= FF addlw.1; W+.1=255+1=0, Z=1, DC=1, C=1, affects all the flags movwf PORTB; does not affect the flags loop goto loop; 9) bcf STATUS,0; Z=0 bcf STATUS,1; DC=0 bcf STATUS,2; C=0 movlw 0xFF; does not affect Z-C-DC-flag, W=h FF addlw.1; affects all the flags, 255+1=0, Z=1, C=1, DC=1 bcf STATUS,1; clear half carry flag bcf STATUS,0; clear zero flag movlw.1; W=h 01 sublw.0; affects all the flags, h 00 -h 01, Z=0, DC=0, C=0 loop goto loop;

LAB WORK 6 1) 13-bit Program counter which acts as an Instruction pointer addressing the instruction currently being fetched into the pipeline. The Program counter potentially can address up to 2^13 = 8K = 8192 instructions, although the PIC16F84A has only 1,024 = 1K instruction capacity. The Program counter normally increments up from instruction 1 at location h 000, but can skip or jump if commanded by a relevant instruction. Program counter (PC) is 13-bit; low 8-bit is PCL and high 5-bit is PCH. 10-bits are used for PIC16F84A. PCLATH is used to write data to PCH After building the code, Debugger-MPLAB SIM View-File Registers Trace the code with F7 button The address of the PCL is 02h so we will observe the changes in this address. Every time, when we push F7, the value of PCL will increase by 1. But after Call Subroutine_A command the value of PCL will become 0E which is 14 in decimal. Subroutine_A starts in the line 14, so that value of PCL will also become 14. When the subroutine s, with RETURN command the value of PCL will become 05h because 05h is where we are before we call the subroutine. 2) bcf STATUS, 0; clear carry flag bcf STATUS, 1; clear half-carry flag bcf STATUS, 2; clear zero flag movlw.1; movlw b'00000001' sublw.3; borrow is not needed, carry and half-carry-flags are set sublw b'00000011' movlw.5; b'00000101' sublw.2; b'00000010'; borrow is needed from low and high nibbles, carry and halfbarrow flags are reset movlw b'10000000'; sublw b'00000001'; only half carry-flag is set, carry flag is reset loop goto loop; This time, we need to observe the changes in STATUS register in File Registers. The address of the STATUS Register is 03h. At the beginning the value of the STATUS Register is 18h which is 00011000, all flags are 0. After the subtraction operation (3-1), barrow is not needed neither low nor high nibbles, the value of the STATUS Register will become 1Bh which is 00011011, carry and half carry flags will become 1 but z flag does not affect because the result won t be 0. After the subtraction (2-5), barrow will be needed from either low and high nibbles, C and DC flags will become 0, Z-flag won t be affected. The value of the STATUS Register become 00011000 which is 18h.

After the subtraction operation of (1-128) only half carry flag will be 1, carry flag will be 0, the value of the STATUS Register will become 0Ah which is 00011010. 3) If the numbers greater than 1 byte (8 bit), we can subtract these numbers using 16-bit subtraction. When subtracting two 16-bit data operands, we need to be concerned with the propagation of a carry from the lower byte to the higher byte. When the first byte is subtracted, there is a carry (E7-8D=59, C=1, positive). Subtract high byte directly. After the low byte subtraction; If C=0 subtract 1 from high byte of first number. And then subtract higher bytes. And control the carry again. If C=1, show output directly. If C=0, take 2 s complement of output and show the result. AL equ 0x0C AH equ 0x0D BL equ 0x0E BH equ 0x0F RL equ 0x10; low byte result RH equ 0x11; high byte result ; A=0x1206 movlw 0x06; movwf AL; movlw 0x12; movwf AH; ; B=0x0814 movlw 0x14; movwf BL; movlw 0x08; movwf BH; subtraction_operation movf BL, W; subwf AL, W; affects the status flags movwf RL; does not affect the status flags. Low-byte result is written to location RL btfss STATUS, 0; btfss STATUS, C; check carry flag for zero, if barrow is needed (c=0), decrement AH if not skip the next line decf AH, F; movf BH, W; subwf AH, W; movwf RH; high-byte result is written to location RH

LAB WORK 7 In this work, we will do seven segment display control by using PIC Microcontroller. 1) RETLW (Return with literal in W); The working register is loaded with the 8-bit literal k. The program counter is loaded from the top of the stack (the return address) syntax: RETLW k ; W=k 2) First of all, in this code,.2 (decimal 2) is written into w-register. Then, look up table is called. In the look up table, in the first line there is addwf PCL,f it adds contents of the PCL and W Register. PCL is the lower 8 bits of the Program Counter. So that, the pointer will jump 2 lines(to the RETLW h 02 ), in the other words, it will write h 02 into W-Register and exit from the Look Up Table. Then, the contents of W-Reg will be written into PORTB. 3) There is an another Look Up Table, we need to update the look up table in question 2 with this one. It will write h FF into W-Register and exit from the Look Up Table. Then, the contents of W-Reg will be written into PORTB. 4) Contents of the PORTB: i) PORTB=h 0F ii) PORTB=h BB iii) PORTB=h 44 iv) PORTB=h 00 v) PORTB=h FF 5) Hex.num output bits data in 7-segment pins number in 7-segment display for PORTB gfedcba h 00 h 3F 00111111 0 h 01 h 06 00000110 1 h 02 h 5B 01011011 2 h 03 h 4F 01001111 3 h 04 h 66 01100110 4 h 05 h 6D 01101101 5 h 06 h 7D 01111101 6 h 07 h 07 00000111 7 h 08 h 7F 01111111 8 h 09 h 6F 01101111 9 h 0A h 77 01110111 A h 0B h 7C 01111100 B h 0C h 39 00111001 C h 0D h 5E 01011110 D h 0E h 79 01111001 E h 0F h 71 01110001 F NOKTA h 80 10000000. R1 EQU 0X0C

R2 EQU 0X0D COUNTER_REG EQU 0X0E bsf STATUS, RP0; clrf TRISB; bcf STATUS, RP0; clrf PORTB; CLRF COUNTER_REG main_part MOVF COUNTER_REG,W; call my_lookup_table; movwf PORTB; CALL DELAY CALL DELAY CALL DELAY MOVLW.10 SUBWF COUNTER_REG,0; BTFSC STATUS,2; CLRF COUNTER_REG BTFSS STATUS,2; INCF COUNTER_REG,F; GOTO main_part; DELAY MOVLW h'ff' ; 1 cycle MOVWF R1 ; 1 cycle LOP_1 MOVLW h'ff' ;1xM (M=number in R1) MOVWF R2 ; 1xM LOP_2 DECFSZ R2,f ; 1xMxN (N=number in R2) GOTO LOP_2 ; 2xMxN DECFSZ R1,f ; 1xM GOTO LOP_1 ; 2xM RETURN ;2 cycle my_lookup_table addwf PCL, F; retlw 0x3F retlw 0x06 retlw 0x5B retlw 0x4F retlw 0x66 retlw 0x6D

retlw 0x7D retlw 0x07 retlw 0x7F retlw 0x6F retlw 0x77 retlw 0x7C retlw 0x39 retlw 0x5E retlw 0x79 retlw 0x71 retlw 0x80 6) R1 EQU 0X0C R2 EQU 0X0D COUNTER_REG EQU 0X0E bsf STATUS, RP0; clrf TRISB; bcf STATUS, RP0; clrf PORTB; CLRF COUNTER_REG main_part MOVF COUNTER_REG,W; call my_lookup_table; movwf PORTB; CALL DELAY CALL DELAY CALL DELAY MOVLW.5 SUBWF COUNTER_REG,0; BTFSC STATUS,2; CLRF COUNTER_REG BTFSS STATUS,2; INCF COUNTER_REG,F; GOTO main_part; DELAY MOVLW h'ff' ; 1 cycle MOVWF R1 ; 1 cycle LOP_1 MOVLW h'ff' ;1xM (M=number in R1) MOVWF R2 ; 1xM

LOP_2 DECFSZ R2,f ; 1xMxN (N=number in R2) GOTO LOP_2 ; 2xMxN DECFSZ R1,f ; 1xM GOTO LOP_1 ; 2xM RETURN ;2 cycle my_lookup_table addwf PCL, F; retlw 0x3F retlw 0x5B retlw 0x66 retlw 0x7D retlw 0x7F LAB WORK 8 1) org 0x00; address of main program org 0x04; address of ISR In MPLAB, Debugger-Select Tool-MPLAB SIM Debugger-Stimulus-New Workbook For Pin select RP0 For Action select toggle Debugger-Animate It is in infinite loop, it have not entered in ISR part. When we click Fire in Workbook we are creating an interrupt originated from RB0. 2) When we trace the program, using stimulus it gives only one interrupt and the program enters into an infinite loop. The reason is there is not any goto my_isr part 3) When we trace the program, using stimulus it gives only one interrupt and the program enters into an infinite loop again. The difference from the previous question is, in ISR the related flag is not cleared. 4) In MPLAB, Debugger-Stimulus-New Workbook For Pin select RB4,RB5,RB6,RB7(any of them can be chosen) For Action select toggle It is in infinite loop, it have not entered in ISR part. When we click Fire in Workbook we are creating an interrupt originated from RB pins. 5) HomeWork

LAB WORK 9 1) In this program timer interrupt is enabled and as clock course RA4/TOCKI is used. When we s pulses to RA4 using stimulus, there will be an interrupt. When we click 512 times to fire button in the workbook, there will be an interrupt. 2) In this question, there is a number in TMR0 which is 253 which means the clock starts counting from 253. When we click 6 times to fire button in the workbook, there will be an interrupt. 3) bcf OPTION_REG, T0CS; At each clock cycle, timer value will be incremented bsf OPTION_REG, PSA; Prescalar value is assigned to WDT by default So, in this code internal clock is used and its value is incremented at each clock cycle. When timer register TMR0 counts from h FF to h 00 the interrupt will be occur simultaneously. It will take 255 cycles. 4) In this question, TMR0 starts counting from.240 so that after 16 cycles, the interrupt will be occur 5) In this question, the difference from previous question, the timer value is incremented every 2 clock cycles. So that, after 32 cycles interrupt will be occur LAB WORK 10 1) We are using Watch Dog Timer as a counter. There is prescalar value for WDT, prescalar value bits are PS0, PS1 and PS2. These bits are all 0. From the table of prescalar value, WDT will be incremented at each clock cycle. 2) In this question, 001 is assigned as a prescalar value to the WDT. From the table, WDT will be incremented at 2 clock cycles. 3) The difference between this question and the first one is there is SLEEP command. When we are using WDT, SLEEP command makes Bit-4 of the STATUS register (TO ) 1 on reset or when WDT is cleared and makes Bit-3 of the STATUS register (PD) 0. It has value 0 when WDT overflows. In addition, the prescalar value of WDT will be set as 000. 4) There is an interrupt which is originated from RB0. Again, there is a SLEEP command but this time, there is not any prescalar value and we are using Internal Clock.