Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay Generation technique for PIC

Status
Not open for further replies.

c36041254

Member
Hi there,
I remember when first I tried to generate delay cycles with PIC (16f690 with that demo board from Microchip) it was really exhausting task to achieve the desired time of delay, well at least that was so for me so I have made this small explanation with example code, hope will be useful.:)
 

Attachments

  • Delay genration in PIC.pdf
    90.8 KB · Views: 364
Dear Forum Member c36041254,

Here's a delay sub-system I developed which may be a little more flexible and versatile. It allows you to specify exact delays in cycles, microseconds, or milliseconds with almost any clock (4, 8, 12, 16, or 20 MHz).

The sub-system uses a relatively low overhead 12 word sub-routine and uses 4 words for each DelayCy() macro call (minimum, depending on the clock speed and the delay parameter).

You must set the clock equate to your clock frequency (in MHz).

Code:
        radix   dec
clock   equ     8               ; user clock frequency in MHz
usecs   equ     clock/4         ; cycles/microsecond multiplier
msecs   equ     usecs*1000      ; cycles/millisecond multiplier

DelayCy macro   pDelay          ; cycles (Tcy), minimum 16
        local   cycles
        cycles = pDelay
     while cycles > 262032
        movlw   high((262016-16)/4)+1
        movwf   TMRH
        movlw   low((262016-16)/4)
        call    DelayLo-(262016%4)
        cycles -= 262016
     endw
        movlw   high((cycles-16)/4)+1
        movwf   TMRH
        movlw   low ((cycles-16)/4)
        call    DelayLo-(cycles%4)
        endm[COLOR=Red]
;                                                                 *
;  example code for simulation testing;                           *
;                                                                 *
SimTest DelayCy(1000*msecs)     ; 1 second delay
        nop                     ; put simulator break point here[/COLOR]
;                                                                 *
;  Delay(16..262159 Tcy) subroutine   Mike McLaren, K8LH, Jun'07  *
;                                                                 *
;  12 words, 1 RAM variable, 14-bit core                          *
;                                                                 *
Delay.16
        nop                     ; entry point for delay%4 == 3    |B0
        nop                     ; entry point for delay%4 == 2    |B0
        nop                     ; entry point for delay%4 == 1    |B0
DelayLo addlw   -1              ; subtract 4 cycle loop time      |B0
        skpnc                   ; borrow?  yes, skip, else        |B0
        goto    DelayLo         ; do another loop                 |B0
        nop                     ;                                 |B0
DelayHi addlw   -1              ; subtract 4 cycle loop time      |B0
        decfsz  TMRH,F          ; done?  yes, skip, else          |B0
        goto    DelayLo         ; do another loop                 |B0
        goto    $+1             ; burn off 2 cycles               |B0
        return                  ;                                 |B0
;
Here's an example of how you might duplicate the example in your PDF file where you flash an LED at 5 second intervals (on for 5 seconds, off for 5 seconds, in an endless loop). Please note that this delay sub-system allows you to compensate for instruction cycles in a loop.

Kind regards, Mike

Code:
loop
        movlw   b'00000001'     ;
        movwf   PORTC           ;
        DelayCy(5000*msecs-2)   ; delay 5 seconds minus 2 cycles
        movlw   b'00000000'     ;
        movwf   PORTC           ;
        DelayCy(5000*msecs-4)   ; delay 5 seconds minus 4 cycles
        goto    loop            ; loop forever
 
Last edited:
Hi Mike,
Thanks for that advanced code actually I put that PDF for any newbie so that it may be easy to understand for him/her but, I was really looking something like this its really well specified.Thanks again!
 
Hi Mike,
I'm trying to understand your delay..

What does these lines do..?
Code:
movlw   [B]low((262016-16)/4)[/B]
call    [B]DelayLo-(262016%4)[/B]
Ok, you move low((262016-16)/4) into the working register, but what is low.
After that you call DelayLo-(262016%4 function, but what is -(262016%4).
Can you please explain..?
I've jst started learning assembly so my understanding is limited.
Thanks..
 
Hi Mike,
Sorry but I ddn''t have time to look at your code, actually I'm trying to learn macro, I can see that you have used macro in above code so if you can help me then please look at the threadhttps://www.electro-tech-online.com/threads/need-help-with-macro-very-basic.42662/
Hi Chris,

I saw that thread but I didn't post a comment because it seems you're having trouble including files. Up to this point I have always used macros in my main source file so I'm afraid I can't help you Sir.

Good luck. Mike
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top