You are on page 1of 4

Microlab File 9 – by Joseph Massoud

Delays (Timers)

A- Program 1 : Double Flasher


1. Write a program which switches ON and OFF ( endlessly) lamp0 and lamp7
simultaneously with ON/OFF delay ( same amount of delay for both cases).

Lamp0 means D0 and Lamp7 means D7, because lamps are supposed to be
connected to output PORTD.

We will use the delay program as a function.

Endlessly means we need to have an infinite loop ( GOTO MAIN).

The delay function will keep decrementing a register (counter variable) till it becomes
zero. This will make the micro spend time while emptying the counter, and creating
thus a certain delay.

We will define the name of the counter register as REG1.

We will use the instruction: DECFSZ REG1, F : decrement file and skip is Z

This instruction decrements once the file ( here it is REG1) and tests if the file is
Zero , by testing the ZERO flag ( Z) .

So if Z=1 it means the register file REG1 has become ZERO , and meaning it has
spent all the time decrementing from its initial value to 0.
In our example we will fill REG1 by 12 decimal.

When REG1 is zero the program skips a line; if not zero yet it goes to the next line
and I keep it in a loop till it becomes zero.

1
Microlab File 9 – by Joseph Massoud

2. Create a program lab91.asm . Write the program, quickbuild, WATCH- ANIMATE

REG1 EQU 0X20

CONFI SAME as before

INIT CLRF PORTD

MAIN BSF PORTD,0


BSF PORTD,7
CALL DELAY
BCF PORTD,0
BCF PORTD,7
CALL DELAY
GOTO MAIN

DELAY MOVLW D’12’ ; we charge Reg1 (counter) by a certain value ; here 12 Decimal for example
MOVWF REG1
WAIT DECFSZ REG1, F ; decrement file reg1 and skip if it becomes zero
GOTO WAIT ; here means REG1 is not equal to zero yet , so I tell him to go and repeat
RETURN ; here means we skipped a line because REG1 became 00, so we leave by RETURN

END

3. During animate we will see how the counter will be decremented 12 times before
it comes back to the MAIN again .
The higher is the value in the counter (REG1) , more will be the total time of
the delay function loop.

Remark: The time of the delay is directly proportional to the number in the counter
register ( Reg1)

If for example we put 6 in REG1 instead of 12, the total time will be exactly the
half.

2
Microlab File 9 – by Joseph Massoud

4. You will see the counter (REG1) being filled with 12 , then decrementing till
it becomes 0. Then it will be refilled again when we switch off leds.
The cycle repeats.

3
Microlab File 9 – by Joseph Massoud

B- Assignment 19

5. Write a program LAB92.ASM . Quickbuild- WATCH- STIMULUS- ANIMATE

Write a program which keeps testing input switch B3 ;

If B3 pressed (On) the program switches ON the lamp5 ( D5) for a certain delay
and then switches it OFF for a delay double of the ON time.

If B3 not pressed the program will write 00 to PORTD.

Remarks:

• In this program, we need to write 2 delay functions, the first having double
time of the second. ( one delay for the ON time and another delay for the OFF
time)

• “Return” should be closing each of these 2 functions , and only one END at
the bottom of the whole thing.

• In this program we use and infinite loop ( GOTO MAIN not GOTO LOOP),
because the program keeps monitoring the input button RB3.

• In the stimulus, it is enough to define RB3 (Toggle) only; refer to file7.

REMINDER
• To use “fire” in order to toggle RB3, you should be in the running mode
(animated).

GOOD LUCK

You might also like