0% found this document useful (0 votes)
4 views

Embedded Lab3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Embedded Lab3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

01/04/1443

Lab 3

Eng - Omeir
Building Assembler Programs

1
Initial Program Design – Flow Diagrams
Read Actual
Temperature TA
It is easy in Assembler to produce “spaghetti code”, ie
code which has no structure, with branches going
anywhere, and which is incomprehensible to any but Read Demand
the programmer, and incomprehensible even to him/her Temperature TD

after a week.

01/04/1443
Yes No
Therefore it is essential to plan a programme structure. TA > TD ?
While it is now viewed as quite an old technique, flow
Activate Switch off
diagramming is still an appropriate tool to use. Compressor Compressor

Eng - Omeir
No
TA >> TD ?

Yes

Activate
Alarm

2
Flow diagrams emphasise the linear nature of a programme, and they can be
drawn so that they can be readily translated into a programme, BUT they do
not in themselves exert good programming discipline.
Arithmetic operations

01/04/1443
Eng - Omeir
3
logic operations

Eng - Omeir 01/04/1443


4
Data movement operations

01/04/1443
Eng - Omeir
5
Control operations

Eng - Omeir 01/04/1443


6
Miscellaneous operations

01/04/1443
Eng - Omeir
7
Example
Assume you have a switch and an LED, the switch and
LED are connected as shown. Write a program that turns

01/04/1443
the LED on if the switch is closed, and turns the LED off if
the switch is not closed.

Eng - Omeir
8
BSF STATUS, RP0 ; select bank 1
BCF TRISA, 0 ; set RA0 as output

01/04/1443
BSF TRISB, 0 ; set RB0 as input
BCF STATUS, RP0 ; select bank 0

Eng - Omeir
loop BTFSC PORTB, 0 ; if B0 is 0 don't set A0
BSF PORTA, 0
BTFSS PORTB, 0
BCF PORTA, 0
GOTO loop
9
Same example but with two switches and two LEDs

BSF STATUS, RP0 ; select bank 1


BCF TRISA, 0 ; set RA0, RA1 as output
BCF TRISA, 1

01/04/1443
BSF TRISB, 0 ; set RB0, RB1 as input
BSF TRISB, 1
BCF STATUS, RP0 ; select bank 0

Eng - Omeir
loop BTFSC PORTB, 0 ; if B0 is 0 don't set A0
BSF PORTA, 0 ;
BTFSS PORTB, 0 ; if B0 is 1 don't clear A0
BCF PORTA, 0
BTFSC PORTB, 1 ; same for B1
BSF PORTA, 1
BTFSS PORTB, 1
BCF PORTA, 1
GOTO loop 10
Alarm System

01/04/1443
We want to design an alarm system, it is
composed of a motion detector, an enable
switch, and a buzzer. Assume that:

Eng - Omeir
• The motion detector gives a logic 1 if motion is
detected.
• The buzzer is turned on if it receives a logic 1
• The system is enabled if the switch is closed.

11
Alarm System

Eng - Omeir 01/04/1443


12
Alarm System (flow chart)

02/04/1443
Eng - Omeir
13
#include "p16f84a.inc"
ORG 0x000
BSF STATUS, RP0 ; select inputs and outputs
BSF TRISA, 0
BSF TRISA, 1

02/04/1443
BCF TRISB, 0
BCF STATUS, RP0
BCF PORTB, 0 ; initially, turn the buzzer off

Eng - Omeir
main BTFSS PORTA, 1 ; is system enabled ?
GOTO buzzer_off ; no, buzzer off
BTFSC PORTA, 0 ; is motion detected ?
BSF PORTB, 0 ; yes, turn alarm on
GOTO main
buzzer_off
BCF PORTB, 0 ; disable the system
GOTO main
END 14

You might also like