Microprocessors System Interfacing

You might also like

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

Microprocessors System Interfacing

Question 1:
Generate a rectangular wave with PIC18F452 microcontroller with following
requirements.

i. Frequency=200Hz , Duty Cycle =20% & XTAL =20 MHz

Code:

LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX


config OSC=HS, OSCS=OFF, WDT=OFF, BORV=45, PWRT=ON,
BOR=ON, DEBUG=OFF, LVP=OFF, STVR=OFF
#include<p18f452.inc>
ORG 0h
Main:
CLRF TRISB ; Port B pins as output
MOVLW 0x08
MOVWF T0CON ; Timer0, 16 bit, no prescaler
CLRF PORTB
Loop:
BSF PORTB,0 ; Drive RB0 high
CALL Delayon ; 1 ms delay
BCF PORTB,0 ; Drive RB0 low
CALL Delayoff ; 4 ms delay
GOTO Loop

Delayon: ; 1 ms delay
BSF T0CON,7 ; Timer 0 on
MOVLW 0xEC ; Load 0xEC78 for 1 ms delay
MOVWF TMR0H
MOVLW 0x78

PAGE 1
MOVWF TMR0L
Again:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Again
BCF T0CON,7 ; Timer 0 off
BCF INTCON,2 ; Clear flag
RETURN

Delayoff: ; 4 ms delay
BSF T0CON,7 ; Timer0 on
MOVLW 0xB1 ; Load 0xB1E0 for 4 ms delay
MOVWF TMR0H
MOVLW 0xE0
MOVWF TMR0L
Check:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Check
BCF T0CON,7 ; Timer0 off
BCF INTCON,2 ; Clear flag
RETURN
END

ii. Frequency=50Hz , Duty Cycle =25% & XTAL =40 MHz

LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX


config OSC=HS, OSCS=OFF, WDT=OFF, BORV=45, PWRT=ON, BOR=ON,
DEBUG=OFF, LVP=OFF, STVR=OFF
#include<p18f452.inc>
ORG 0H

PAGE 2
Main:
CLRF TRISB ; Port B pins as output
MOVLW 0x02
MOVWF T0CON ; Timer0, 16 bit, prescaler 8
CLRF PORTB
Loop:
BSF PORTB,0 ; Drive RB0 high
CALL Delayon ; 5 ms delay
BCF PORTB,0 ; Drive RB0 low
CALL Delayoff ; 20 ms delay
GOTO Loop

Delayon: ; 5 ms delay
BSF T0CON,7 ; Timer 0 on
MOVLW 0xE7 ; Load 0xE796 for 5 ms delay
MOVWF TMR0H
MOVLW 0x96
MOVWF TMR0L
Again:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Again
BCF T0CON,7 ; Timer 0 off
BCF INTCON,2 ; Clear flag
RETURN

Delayoff: ; 15 ms delay
BSF T0CON,7 ; Timer0 on
MOVLW 0xB6 ; Load 0xB6C2 for 15 ms delay
MOVWF TMR0H

PAGE 3
MOVLW 0xC2
MOVWF TMR0L
Check:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Check
BCF T0CON,7 ; Timer0 off
BCF INTCON,2 ; Clear flag
RETURN
END

iii. Frequency=1Hz , Duty Cycle =50% & XTAL =40 MHz

LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX


config OSC=HS, OSCS=OFF, WDT=OFF, BORV=45, PWRT=ON, BOR=ON,
DEBUG=OFF, LVP=OFF, STVR=OFF
#include<p18f452.inc>
ORG 0H
Main:
CLRF TRISB ; Port B pins as output
MOVLW 0x07
MOVWF T0CON ; Timer0, 16 bit, prescaler 256
CLRF PORTB
Loop:
BSF PORTB,0 ; Drive RB0 high
CALL Delay ; .5s delay
BCF PORTB,0 ; Drive RB0 low
CALL Delay ; .5s delay
GOTO Loop

Delay: ; .5 s delay

PAGE 4
BSF T0CON,7 ; Timer 0 on
MOVLW 0xB3 ; Load 0xB3B5 for .5s delay
MOVWF TMR0H
MOVLW 0xB5
MOVWF TMR0L
Again:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Again
BCF T0CON,7 ; Timer 0 off
BCF INTCON,2 ; Clear flag
RETURN
END

iv. Frequency=4Hz , Duty Cycle =25% & XTAL =20 MHz

LIST P=PIC18F452, F=INHX32, N=0, ST=OFF, R=HEX


config OSC=HS, OSCS=OFF, WDT=OFF, BORV=45, PWRT=ON, BOR=ON,
DEBUG=OFF, LVP=OFF, STVR=OFF
#include<p18f452.inc>
ORG 0H
Main:
CLRF TRISB ; Port B pins as output
MOVLW 0x04
MOVWF T0CON ; Timer0, 16 bit, prescaler 32
CLRF PORTB
Loop:
BSF PORTB,0 ; Drive RB0 high
CALL Delayon ; 62.5 ms delay
BCF PORTB,0 ; Drive RB0 low
CALL Delayoff ; 187.5 ms delay

PAGE 5
GOTO Loop

Delayon: ; 62.5 ms delay


BSF T0CON,7 ; Timer 0 on
MOVLW 0xD9 ; Load 0x for 62.5 ms delay
MOVWF TMR0H
MOVLW 0xDA
MOVWF TMR0L
Again:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Again
BCF T0CON,7 ; Timer 0 off
BCF INTCON,2 ; Clear flag
RETURN

Delayoff: ; 187.5 ms delay


BSF T0CON,7 ; Timer0 on
MOVLW 0x8D ; Load 0xB6C2 for 187.5 ms delay
MOVWF TMR0H
MOVLW 0x8F
MOVWF TMR0L
Check:
BTFSS INTCON,2 ; Wait till TMR0IF flag set
GOTO Check
BCF T0CON,7 ; Timer0 off
BCF INTCON,2 ; Clear flag
RETURN
END

PAGE 6
PAGE 7
QUESTION 2
Implement a RADAR System using PIC18F452. Radar system uses a listening
sensor and listens for high value from sensor. Once it detects high value from sensor
it will generate a pulse with duration of 300ms. Use XTAL=24 MHz with PIC18F452

Code

#include<p18f452.inc>
ORG 0H
start
CLRF TRISB ; Port B as output
BSF TRISD,0 ; RD0 as input

PAGE 8
MOVLW 0x05
MOVWF T0CON ; Timer0, 16 bit with prescaler 64
CLRF PORTB
Loop:
BTFSS PORTD,0 ; Check whether high value from sensor
GOTO Loop ; If not then check again
BSF PORTB,0 ; If yes then drive RB0 high
CALL Delay ; Call Delay subroutine of 300 ms delay
BCF PORTB,0 ; Drive RB0 Low
GOTO Loop
Delay: ; Delay subroutine
MOVLW 0x92
MOVWF TMR0H ; Move the value 37411 which is 0x9223 to TMR0
MOVLW 0x23
MOVWF TMR0L
BSF T0CON,7 ; Timer0 On
Again:
BTFSS INTCON,2 ; Check whether TMR0IF is set
GOTO Again ; Check till it is set
BCF T0CON,7 ; Timer0 off
BCF INTCON,2 ; TMR0IF cleared
RETURN
END

PAGE 9
PAGE 10

You might also like