0% found this document useful (0 votes)
10 views12 pages

MMDB - Group#8 - Lab Report#11

Uploaded by

Shafla Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

MMDB - Group#8 - Lab Report#11

Uploaded by

Shafla Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI

EC-310 Microprocessor and Microcontroller Based Design


LAB # 11: Use of Timer modules to program PIC-18
(Proteus based Design)

SUBMITTED TO:
Dr. Sagheer Khan
LE Ayesha Khanum

SUBMITTED BY:
Shafla Rehman; 412780
Areesha Batool ; 413055
Shaheer Mukhtiar ; 432017

CE-44, Syndicate B
Task#1:
Write a program in assembly language to toggle pin RB1 after every 1 sec.
Connect LED to RB1. Show status of the switch by LED connected to RB1.

Code:

LIST P=18F452
INCLUDE <P18F452.INC>

ORG 0x0000 ; Reset vector


GOTO MAIN ; Jump to main program

ORG 0x0008 ; Interrupt vector


REaTFIE ; Return from interrupt (not used
here)

MAIN:
; Initialization
CLRF PORTB ; Clear PORTB
CLRF LATB ; Clear output latch
BCF TRISB, 1 ; Set RB1 as output
MOVLW 0x06 ; Configure Timer0: 16-bit mode,
prescaler 1:256
MOVWF T0CON

TOGGLE:
; Load Timer0 with initial values
MOVLW 0xF0 ; Load TMR0H high byte
MOVWF TMR0H
MOVLW 0xC2 ; Load TMR0L low byte
MOVWF TMR0L

BCF INTCON, TMR0IF ; Start Timer0


BTG PORTB,1
BSF T0CON,TMR0ON
WAIT_OVERFLOW:
BTFSS INTCON, TMR0IF ; Check Timer0 overflow flag
GOTO WAIT_OVERFLOW ; Wait until Timer0 overflows

BCF T0CON,TMR0ON
GOTO TOGGLE

END ; End of program

Output:

Figure 1: After 1 second, LED is toggling


Figure 2: After 1 second, LED is toggling (off to on)

Task#2:
Take XTAL = 12 MHz, program a square wave of 60 Hz on pin 7 at PORTB. Consider
16-bit mode and a prescaler value of 64.
Code:
LIST P=18F452
#include <P18F452.INC>

ORG 0x0000
GOTO START

ORG 0x0018
ISR
BTFSS INTCON, TMR0IF ; Check Timer0 overflow flag
RETFIE
BCF INTCON, TMR0IF ; Clear Timer0 overflow flag
COMF PORTB, F ; Toggle all bits on PORTB
(including RB7)
MOVLW 0x0B ; Reload high byte of Timer0
MOVWF TMR0H
MOVLW 0xDC ; Reload low byte of Timer0
MOVWF TMR0L
RETFIE

ORG 0x0100
START
CLRF PORTB
CLRF TRISB ; Set PORTB as output
BSF RCON, IPEN ; Enable priority levels
BSF INTCON, GIEH ; Enable high-priority interrupts
BSF INTCON, PEIE ; Enable peripheral interrupts
BSF INTCON, TMR0IE ; Enable Timer0 interrupt

MOVLW 0x88 ; Timer0 ON, 16-bit mode, prescaler


= 64
MOVWF T0CON
MOVLW 0x0B ; Timer0 preload high byte
MOVWF TMR0H
MOVLW 0xDC ; Timer0 preload low byte
MOVWF TMR0L
BSF T0CON, TMR0ON ; Start Timer0

LOOP
GOTO LOOP ; Infinite loop to keep the program
running

END

Output:
Figure 3: Schematic for Task 2

Figure 4: Square wave of 60HZ

Task#3:
Implement Timer0 for LED Control: Write a program that uses Timer0 to control
LEDs connected to PORTB. The LED should turn on after a signal on PORTC5 stays
high for 3 seconds.

Code:
LIST P=18F452
#include <P18F452.INC>
ORG 0x0000
GOTO START

ORG 0x0018
ISR
BTFSS INTCON, TMR0IF ; Check Timer0 overflow flag
RETFIE ; Return if no overflow
BCF INTCON, TMR0IF ; Clear Timer0 overflow flag

INCF COUNT, F ; Increment counter variable


MOVF COUNT, W
SUBLW 0x48 ; Compare count with 72 (3
seconds)
BTFSS STATUS, Z ; Check if COUNT == 72
RETFIE ; Exit ISR if not 3 seconds yet

BSF PORTB, 0 ; Turn on LED (RB0) after 3


seconds
RETFIE

ORG 0x0100
START
CLRF PORTB ; Clear PORTB
CLRF TRISB ; Set PORTB as output
BSF TRISC, 5 ; Set PORTC5 as input
CLRF COUNT ; Clear counter variable

; Configure Timer0
MOVLW 0x87 ; Timer0 ON, 16-bit mode,
prescaler = 256
MOVWF T0CON
MOVLW 0x0B ; Load high byte for 50ms delay
MOVWF TMR0H
MOVLW 0xDC ; Load low byte for 50ms delay
MOVWF TMR0L

; Enable interrupts
BSF RCON, IPEN ; Enable interrupt priority
system
BSF INTCON, GIEH ; Enable global interrupts
(high priority)
BSF INTCON, PEIE ; Enable peripheral interrupts
BSF INTCON, TMR0IE ; Enable Timer0 interrupt

MAIN_LOOP
BTFSS PORTC, 5 ; Check if signal on RC5 is
high
CLRF COUNT ; Reset counter if signal is
low
GOTO MAIN_LOOP ; Continue monitoring RC5

END

Output:
Figure 5: Off for first 3 seconds

Figure 6: On after 3 seconds

Task#4:
User-Interactive Control System with Timer: Develop a system that takes a 4-bit
input from PORTD. Based on the input value, light up a corresponding number of
LEDs on PORTB. Explain your design in the report.

Code:
LIST P=18F452
#include <P18F452.INC>

ORG 0x0000
GOTO START

ORG 0x0018
ISR
BTFSS INTCON, TMR0IF ; Check Timer0 overflow flag
RETFIE
BCF INTCON, TMR0IF ; Clear Timer0 overflow flag
MOVF PORTD, W ; Read 4-bit input from PORTD
ANDLW 0x0F ; Mask to keep lower 4 bits
MOVWF PORTB ; Light up LEDs based on input
MOVLW 0x0B ; Reload high byte of Timer0
MOVWF TMR0H
MOVLW 0xDC ; Reload low byte of Timer0
MOVWF TMR0L
RETFIE

ORG 0x0100
START
CLRF PORTB ; Clear PORTB
CLRF TRISB ; Set PORTB as output
MOVLW 0x0F
MOVWF TRISD ; Set lower 4 bits of PORTD as input
BSF RCON, IPEN ; Enable priority levels
BSF INTCON, GIEH ; Enable global interrupts (high-
priority)
BSF INTCON, PEIE ; Enable peripheral interrupts
BSF INTCON, TMR0IE ; Enable Timer0 interrupt

MOVLW 0x88 ; Timer0 ON, 16-bit mode, prescaler


= 64
MOVWF T0CON
MOVLW 0x0B ; Timer0 preload high byte
MOVWF TMR0H
MOVLW 0xDC ; Timer0 preload low byte
MOVWF TMR0L
BSF T0CON, TMR0ON ; Start Timer0

LOOP
GOTO LOOP ; Infinite loop to keep the program
running

END

Output:

Figure 7: For input of 1000b, 8 LEDs are turned on


Figure 8: For input of 0010b, 2 LEDs are turned on

You might also like