MMDB - Group#8 - Lab Report#11
MMDB - Group#8 - Lab Report#11
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
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>
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 T0CON,TMR0ON
GOTO TOGGLE
Output:
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
LOOP
GOTO LOOP ; Infinite loop to keep the program
running
END
Output:
Figure 3: Schematic for Task 2
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
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
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
LOOP
GOTO LOOP ; Infinite loop to keep the program
running
END
Output: