Professional Documents
Culture Documents
15EEE314
Microcontroller and Applications
Addition
• Addition of two numbers 0110 1100
• Requirement Operand 1 1011 0010
– Memory locations to store two operands Operand 2 1001 0110
– Memory location to store sum and carry
1 0100 1000
• PIC16F877A
– Default Bank selected: BANK0 CARRY SUM
– User Accessible Locations: 0x20 to 0x7F
• Assign
– 0x20 SUM
– 0x21 CARRY
2 3
5
Program Memory
#include<p16f877a.inc> ADDRESS CONTENT
6
Addition – Direct Addressing
• Operands are supplied in the Data Memory
• Labels can be used to identify specific memory locations
• Assign
– 0x20 OP1 (operand 1)
– 0x21 OP2 (operand 2)
– 0x22 SUM
– 0x23 CARRY
• Since sequential memory locations are required, CBLOCK directive can be used
– To complete the label assignment ENDC directive (End an Automatic Constant Block) is used
• For non-sequential memory locations EQU directive can be used
MOVLW 0x20
MOVWF FSR
MOVLW 0x08
MOVWF count
L1 CLRF INDF
INCF FSR,1
DECFSZ count,1
GOTO L1
Lp GOTO Lp
EEE, Amrita School of Engineering 11
Accessing Successive Memory Locations – Array 2
• Clear 8 successive locations starting from 0x20 - Indirect addressing mode
MOVLW 0X08
MOVWF VALUE INCF COUNT,1
MOVF COUNT,0
MOVLW 0X20
XORWF VALUE,0
MOVWF FSR BTFSS STATUS,Z
MOVLW 0X00
GOTO L1
MOVWF COUNT
LOOP GOTO LOOP
L1 CLRF INDF
INCF FSR,1
Org 0x00
L2 MOVLW 0x04
MOVWF 0x30
L1 DECFSZ 0x30,1
GOTO L1
GOTO L2
Difference between the exact time delay and approximate time delay is just 1µs
So for rapid calculation, the later method is used
26
Time Delay in Software with NOP
A EQU 0x30
B EQU 0x31 Number of instruction cycles for 7th bit in A to remain 0
N_Dly EQU 0x20 = 5*N_Dly + 4
ORG 0x00 Inside Outside
+
BCF A,7 Loop Loop
MOVLW N_Dly
MOVWF B Z=1
Z=0
NOP
L1 NOP 1 ins cycle 1 ins cycle
NOP
1 ins cycle 1 ins cycle
DECFSZ B,1
1 ins cycle 2 ins cycle
2 ins cycle
GOTO L1
BSF A,7
LP GOTO LP
EEE, Amrita School of Engineering 27
END
Time Delay in Software – Longer Delay
• Delay by decrementing 16-bit number to 0
• Nested loops
• Inner loop : decrement M to zero
• Outer loop : every time NL goes through 0
• decrement NH
• Delay = NH (256 3+3) instruction cycles ~
approximately
Process:
1. Halt main program, provide returning
2. Transfer control to subroutine
3. Execute subroutine
4. Return to main program execution
P1 P2 P3 P4
function ISR( )
{
GIE=0;
Assuming peripheral A and peripheral B
If( intA_flag==1)
generated interrupt at the same time.
{ do …. }
if ( intB_flag==1)
In the ISR peripheral A is given high priority.
{ do…. }
GIE=1;
} EEE, Amrita School of Engineering 39
Interrupt status saving
• Interrupt may come anytime
• It is advised to save the contents of Work register and STATUS register (as the
operations in ISR may alter the contents of W and STATUS)
• Saving the values are done as first steps in ISR, so that these values could be
loaded back once the ISR is completed
• The saved values could be used and main routine can continue execution
• Interrupt status saving should be done by programmer