Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DIGPRA3 - Assignment 2 - 34006559

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

DIGPRA3

Student Number: 34006559

Assignment 2

Assignment 2.1

/Program Design for Pic16F1508 microcontroller

#include<pic.h>

#include<stdio.h>

//**********Port Configuration*******//

#define Upcounter_PushButton PORTAbits.RA4 //Upcounter_Pushbutton

#define Downcounter_PushButton PORTAbits.RA0 //Downcounter_Pushbutton

#define LED PORTD //LED define

//**********variable*******//

unsigned long decimalNum=0;

unsigned int isr_us=100;

unsigned int crystal_freq_MHZ=16; //16 MHz

unsigned int presclar_value=1;

unsigned long IsrDecimalValue=0,quotient;

unsigned int remainder;


unsigned int low_v,high_v,count;

unsigned char Up_counter_PushButtonPressed=0,Down_counter_PushButtonPressed=0;

//**********Function Declaration*******//

void isr_calulation();

void timer1_init(void);

//**********Function Definition*******//

void isr_calulation()

decimalNum = isr_us * ((crystal_freq_MHZ/4)*(1/presclar_value));

IsrDecimalValue = 65535 - decimalNum;

low_v = IsrDecimalValue & 0xff;

IsrDecimalValue = IsrDecimalValue/256;

high_v = IsrDecimalValue & 0xff;

//******************Timer1 Initialization ******************************//


void timer1_init()

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

void interrupt isr(void)

{
if(PIR1bits.TMR1IF==1)

{ //Peripheral Interrupt Request Flag registers

PIR1bits.TMR1IF=0;

TMR1L=low_v;

TMR1H=high_v;

--count;

if(Upcounter_PushButton==1) //PushButton is pressed

Up_counter_PushButtonPressed=1;

else if(Downcounter_PushButton==1) //PushButton is pressed

Down_counter_PushButtonPressed=1;

}
}

void delay_500msec(unsigned int time) //500 msec delay

count=(time*5000);

while(count!=0);

//**********Main Configuration*******//

void main()

//**********Port Define as Input/Output*******//

TRISAbits.TRISA4=1; //Up_counter PushButton defined as input

TRISAbits.TRISA0=0; //Down_counter PushButton defined as input

TRISD = 0x00; //LED defined as output on PORTD

LED = 0x00; //Initial off state of LED

isr_calulation();

timer1_init();
Up_counter_PushButtonPressed=0;

Down_counter_PushButtonPressed=0;

Up_Counter=0;

Down_Counter=0;

while(1)

if(Up_counter_PushButtonPressed==1) //if Up_counter Pushbutton is pressed

Up_counter_PushButtonPressed=0;

LED=UpCounter; // Fill up counter to LED

delay_500msec(1); // 1/2 sec delay

UpCounter++; // Increament counter

if(UpCounter>=255) // if upcounter reaches at 255(max value)

UpCounter=0; // reset Counter


LED=0; // All LED Off

else if(Down_counter_PushButtonPressed==1) //if Down_counter Pushbutton is pressed

Down_counter_PushButtonPressed=0;

LED=DownCounter; // Fill Down counter to LED

delay_500msec(1); // 1/2 sec delay

DownCounter++; // Increament counter

if(DownCounter>=255) // if Downcounter reaches at 255(max value)

DownCounter=0; // reset Counter

LED=0; // All LED Off

else if(Down_counter_PushButtonPressed==0 && Up_counter_PushButtonPressed==0)


//if both switches are in re// Program Design for Pic16F1508 microcontroller

#include<pic.h>

#include<stdio.h>

//********** Port Configuration*******//

#//*

**Timeri Initialization **

void timer1_init()

INTCONbits.GIE=1;

INTCONbits.PEIE=1;

T1CON=0x00;

PIElbits. TMR11E=1;

PIEl}

void delay_50 Omsec (unsigned int time) 1/500 msec delay

count=(time*5000);

while (count !=0);

*//

*Main Configuration*}

void delay_50 Omsec (unsigned int time) 1/500 msec delay

count=(time*5000);

while (count !=0);

*//

*Main Configuration*if (UpCounter>=255) // if upcounter reaches at 255 (max value)

UpCounter=0; // reset Counter

LED=0;
// All LED off

elselease position

LED=0; // All LED Off

UpCounter=0; // reset Up Counter

DownCounter=0; // reset Down Counter

}ease position

LED=0; // All LED Off

UpCounter=0; // reset Up Counter

DownCounter=0; // reset Down Counter

}
}
Assignment 2.2

Assembly instructions

BCF

This instruction clear the particular bit in the file register

Syntax-

BCF f,b

BSF

This instruction set the particular bit in the file register

Syntax-

BSF f,b

CLRF

This instruction clear the file register.

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

This instruction call subroutine by loading address of subroutine to PC and store


return address to stack.

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

If d=0, then destination is W register

If d=1, then destination is file register

GOTO

This is unconditional branch instruction which branches to given label without


checking any conditions.

Syntax-

GOTO Label

END

This is not an instruction but an assembler directive to terminate program.

STATUS register

This is 8 bit register which is used to select register banks and contain status flags.
IRPRP1RP0TOPDZDCC

RP0 - Register bank select bit

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,

Let 1 MHz be the crystall oscillator frequency.

So, the instruction clock frequency is,

So, the time taken to execute one instruction cycle

For 0.5 s number of instruction cycles required will be 125000.

Consider subroutine Delay,

The label Iloop consists of instructions DECFSZ and GOTO which consume 3
instruction cycles. The label iLoop exits when Count1 becomes 0.

So, number of instruction cycles consumed by Iloop will be,

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.

So, number of instruction cycles consumed by Oloop will be,

And in label Delay there are MOVLW , MOVWF and RETURN instruction, so total
number of instruction cycles = 125011.

So, approximately 0.5 seconds delay obtained.


So, code will be,

#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

Right: ; If RA4 alone pressed


MOVLW 0x80 ; Move 0x80 to port D
MOVWF PORTD
BCF STATUS,0 ; Clear carry flag
Rotater:
CALL Delay ; 0.5 seconds delay
RRF PORTD,1 ; Rotate PORTD to right
BTFSS PORTA,4 ; Check whether RA4 is kept pressed
GOTO None ; If RA4 released then Go to None
BTFSS PORTA,0 ; Check whether RA0 is pressed
GOTO Rotater ; If RA0 is not pressed then continue to
rotate
GOTO Flash ; If RA0 is pressed then go to Flash

Left: ; If RA0 alone pressed


MOVLW 0x01 ; Move 0x01 to port D
MOVWF PORTD
BCF STATUS,0 ; Clear carry flag
Rotatel:
CALL Delay ; 0.5 seconds delay
RLF PORTD,1 ; Rotate PORTD to left
BTFSS PORTA,0 ; Check whether RA0 is kept pressed
GOTO None ; If RA0 released then Go to None
BTFSS PORTA,4 ; Check whether RA4 is pressed
GOTO Rotatel ; If RA4 is not pressed then continue to
rotate
GOTO Flash ; If RA4 is pressed then go to Flash

None: ; If none of the button pressed the LED as


per previous rotation will be ON
GOTO Main

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

Delay: ; Delay subroutine for 0.5 seconds


MOVLW D'199'
MOVWF Count2
Oloop:
MOVLW D'207'
MOVWF Count1
Iloop:
DECFSZ Count1
GOTO Iloop
DECFSZ Count2
GOTO Oloop
RETURN

END
Screenshots

Initially,
When RA4 pressed
When RA4 released

When RA0 is pressed


When RA0 is released
When both pressed
Assignment 2.3

**Define the signal pins of all four displays***//

#define s1 RC0

#define s2 RC1

#define s3 RC2

#define s4 RC3

//***End of definition**////

int i = 0; //the 4-digit value that is to be displayed


int flag =0; //for creating delay
unsigned int a,b,c,d,e,f,g,h; //just variables
unsigned int seg[]={0X3F, //Hex value to display the number 0
0X06, //Hex value to display the number 1
0X5B, //Hex value to display the number 2
0X4F, //Hex value to display the number 3
0X66, //Hex value to display the number 4
0X6D, //Hex value to display the number 5
0X7C, //Hex value to display the number 6
0X07, //Hex value to display the number 7
0X7F, //Hex value to display the number 8
0X6F //Hex value to display the number 9
}; //End of Array for displaying numbers from 0 to 9

You might also like