DIGPRA3 - Assignment 2 - 34006559
DIGPRA3 - Assignment 2 - 34006559
DIGPRA3 - Assignment 2 - 34006559
Assignment 2
Assignment 2.1
#include<pic.h>
#include<stdio.h>
//**********Port Configuration*******//
//**********variable*******//
//**********Function Declaration*******//
void isr_calulation();
void timer1_init(void);
//**********Function Definition*******//
void isr_calulation()
IsrDecimalValue = IsrDecimalValue/256;
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
T1CON=0x00;
PIE1bits.TMR1IE=1;
PIE1bits.TMR1GIE=0;
T1GCONbits.TMR1GE=0;
isr_calulation();
TMR1H=high_v;
TMR1L=low_v;//0x90;
T1CONbits.TMR1ON=1;
/*********************ISR*********************/
{
if(PIR1bits.TMR1IF==1)
PIR1bits.TMR1IF=0;
TMR1L=low_v;
TMR1H=high_v;
--count;
Up_counter_PushButtonPressed=1;
Down_counter_PushButtonPressed=1;
}
}
count=(time*5000);
while(count!=0);
//**********Main Configuration*******//
void main()
isr_calulation();
timer1_init();
Up_counter_PushButtonPressed=0;
Down_counter_PushButtonPressed=0;
Up_Counter=0;
Down_Counter=0;
while(1)
Up_counter_PushButtonPressed=0;
Down_counter_PushButtonPressed=0;
#include<pic.h>
#include<stdio.h>
#//*
**Timeri Initialization **
void timer1_init()
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
T1CON=0x00;
PIElbits. TMR11E=1;
PIEl}
count=(time*5000);
*//
*Main Configuration*}
count=(time*5000);
*//
LED=0;
// All LED off
elselease position
}ease position
}
}
Assignment 2.2
Assembly instructions
BCF
Syntax-
BCF f,b
BSF
Syntax-
BSF f,b
CLRF
Syntax-
CLRF f
MOVLW
This instruction moves 8 bit literal value to W register. This value can be in any
format. This instruction consume one instruction cycle.
Syntax-
MOVLW k
MOVWF
This instruction move the content of W register to file register. This instruction
consume one instruction cycle.
Syntax-
MOVWF f
BTFSS
This is bit test instruction which check whether the bit in the specified file register is
set or not, if set next instruction is skipped.
Syntax-
BTFSS f,b
CALL
Syntax-
CALL Subroutine
RETURN
This instruction return from subroutine by popping return address from stack and
load to PC. This instruction consume 2 instruction cycles.
DECFSZ
This instruction decrement the file register and store result at destination selected by
destination operand. If the result is zero then skip next instruction. This instruction
consume one instruction cycle if result is non zero and consume 2 instruction cycles
if skipped.
Syntax-
DECFSZ f,d
GOTO
Syntax-
GOTO Label
END
STATUS register
This is 8 bit register which is used to select register banks and contain status flags.
IRPRP1RP0TOPDZDCC
RP0Register bank
0 Bank 0
1 Bank 1
The oscillator frequency is divided by 4 and used for instruction clock. So, the
instruction clock frequency is,
The label Iloop consists of instructions DECFSZ and GOTO which consume 3
instruction cycles. The label iLoop exits when Count1 becomes 0.
The label Oloop consists of Iloop and instructions MOVLW, MOVWF ,DECFSZ and
GOTO which consume 5 instruction cycles. The label OLoop exits when Count2
becomes 0.
And in label Delay there are MOVLW , MOVWF and RETURN instruction, so total
number of instruction cycles = 125011.
#include<p16f877a.inc>
Count1 EQU 0x20
Count2 EQU 0x21
BSF STATUS,5 ; Select register bank 1
BSF TRISA,0 ; Configure RA0 and RA4 as digital input
pins
BSF TRISA,4
MOVLW 0x07
MOVWF ADCON1
CLRF TRISD ; Configure port D as output pin
BCF STATUS,5 ; Select register bank 0
CLRF PORTD ; Initially all LEDs off
Main:
BTFSS PORTA,4 ; Check whether RA4 pressed
GOTO Next ; If not Go to Next
BTFSS PORTA,0 ; If RA4 pressed check whether RA0 pressed
GOTO Right ; If not then only RA4 pressed so, Go to
Right
GOTO Flash ; If RA0 is also pressed then Go to Flash
Next:
BTFSS PORTA,0 ; If RA4 is not pressed check whether RA0
pressed
GOTO None ; If RA0 is not pressed then both buttons
not pressed so Go to None
GOTO Left ; If RA0 pressed and RA4 not pressed then
go to Left
Flash:
MOVLW 0x00 ; Turn off all LEDs
MOVWF PORTD
CALL Delay ; 0.5 seconds delay
MOVLW 0xFF
MOVWF PORTD ; Turn on all LEDs
CALL Delay ; 0.5 seconds delay
GOTO Main
END
Screenshots
Initially,
When RA4 pressed
When RA4 released
#define s1 RC0
#define s2 RC1
#define s3 RC2
#define s4 RC3
//***End of definition**////