Professional Documents
Culture Documents
Delays (Timers)
Lamp0 means D0 and Lamp7 means D7, because lamps are supposed to be
connected to output PORTD.
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 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
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
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.
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.
REMINDER
• To use “fire” in order to toggle RB3, you should be in the running mode
(animated).
GOOD LUCK