Thursday, November 1, 2007

MAKE A DELAY







Delay is very important when we use microcontroller. Why? For example microcontroller PIC16F84/A that use 4 Mhz of clock, will executed instruction every 1us ( 1/1000000 second ). This is very fast.

If we want to make a led that connected to a port of microcontroller to blink, we must add a delay, otherwise the led can’t blink. This is because led blinking very fast and we see the led just lit.

How To Make a Delay?
Below is the easy way to make a delay. I want to introduce a software that can produce instruction to perform delay function for microcontroller PIC16F84/A (PICmicro family ). The software name was PICDEL made by Pier Paolo Messagio.



Figure 1 PICDEL


How to Use This Software
Fist we must inputting the Frequency that use by microcontroller ( in this case 4 MHz ).
Suppose we want to make a subroutine of delay for 1 second. We simply input 1000 in “Delay time(ms)” because 1 second = 1000 milisecond. After that we click “Calculate Cycle”. The software will appear Delay Cycle = 1000000.



Figure 2 Setting delay 1 second

Finally we just click “Generate Code” and the software will produce instruction to make subroutine of delay 1 second.





Figure 3 Instruction generated by PICDEL


Copy and paste instruction generated by PICDEL into your main program. Here is the example. We can add a label for easy understanding. Suppose we give a label “delay_1_second”.



delay_1_second ; this label additional by us
;-------------------------------------------------------------
; Code generated by PDEL ver 1.0 on 11/1/2007 at 1:24:31 PM
; Description: Waits 1000000 cycles
;-------------------------------------------------------------
PDelay movlw .14 ; 1 set number of repetitions (C)
movwf PDel0 ; 1
PLoop0 movlw .72 ; 1 set number of repetitions (B)
movwf PDel1 ; 1
PLoop1 movlw .247 ; 1 set number of repetitions (A)
movwf PDel2 ; 1
PLoop2 clrwdt ; 1 clear watchdog
decfsz PDel2, 1 ; 1 + (1) is the time over? (A)
goto PLoop2 ; 2 no, loop
decfsz PDel1, 1 ; 1 + (1) is the time over? (B)
goto PLoop1 ; 2 no, loop
decfsz PDel0, 1 ; 1 + (1) is the time over? (C)
goto PLoop0 ; 2 no, loop
PDelL1 goto PDelL2 ; 2 cycles delay
PDelL2 clrwdt ; 1 cycle delay
return ; 2+2 Done
;-------------------------------------------------------------

Finally into main program we must add some instruction as below.

PDel0 equ 0CH ; location of RAM (PIC16F84/A)
PDel1 equ 0DH ; location of RAM (PIC16F84/A)
PDel2 equ 0EH ; location of RAM (PIC16F84/A)




Figure 4
Download PDF

No comments: