Searching \ for 'One second loop' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=one+second+loop
Search entire site for: 'One second loop'.

Truncated match.
'One second loop'
1997\07\11@102809 by

Hello,
I trying to write a program which will set RA3 high for exactly one
second then set the pin low. The crystal used for the PIC is 4Mhz.
Therefore it will take 1 us per instruction.  I'm using MPLAB to write the
program.  Any help would be great.
Thanks
Ray
Ray,

>      I trying to write a program which will set RA3 high for exactly
>      one
> second then set the pin low. The crystal used for the PIC is 4Mhz.

This is one of my standard library tricks.  So exactly HOW ACCURATE
do you need to be?  This one times out at (theoretically) 1.049
seconds.

;   WAITASEC  subroutine
WAITASEC

CLRF ACCaLO
CLRF ACCaHI
MOVLW        .252
MOVWF        ACCbLO

WAITLOOP
CLRWDT
INCFSZ       ACCaLO, F
GOTO WAITLOOP
INCFSZ       ACCaHI, F
GOTO WAITLOOP
INCFSZ       ACCbLO, F
GOTO WAITLOOP

RETLW        0

This disadvantage of this approach is the micro does NOTHING ELSE
while counting to 262,000.

-- Lawrence Lile
Ray Gallant wrote:
>
> Hello,
>      I trying to write a program which will set RA3 high for exactly one
> second then set the pin low. The crystal used for the PIC is 4Mhz.
> Therefore it will take 1 us per instruction.  I'm using MPLAB to write the
> program.  Any help would be great.
>                          Thanks
>                                Ray
Hi!

You can set the delay by adjusting the value of PRESET and the 17.

You can calculate this as follows:

Total delay = loop*[prescaler*(256*preset)+delay+6]

Loop = Period/65536 (round up to integer)

Prescaler = period/loop/256 ((round UPwards to 256,128,64,etc)

Preset = 256-[period/loop/prescaler] round UP to integer<256

delay = [period/loop] - [prescaler*(256-preset)] - 6

====================================================
PRESET  EQU     D'61'           ;Delay loop counter (1 msec delay)

MOVLW   PRESET                  ;Get preset value ready
MOVWF   TMR0                    ;Write to timer
MOVLW   4                       ;Set up 4 * 50 msec loop
MOVWF   CNT4
CLRF    UDBIT

;Wait for Timer roll Over Routine

LOOP1   BCF     INTCON,T0IF

TEST    BTFSS   INTCON,T0IF
GOTO    TEST

MOVLW   D'17'         ;SET TO 17
CALL    VARDLY
NOP
MOVLW   PRESET
MOVWF   TMR0
DECFSZ  CNT4,f
GOTO    LOOP1
=================================================

>>>>>rest of program<<<<<<

VARDLY looks like this:
==================================================
;DELAY LOOP SUBROUTINE: 1 milli-second (993 Cycles)
;CORRUPT W,COUNTD. STACK:1 LEVEL

COUNTD  EQU     2F      ;AT END OF USER RAM

DELAY   MOVLW   D'247'          ;FIXED DELAY ENTRY POINT
VARDLY  MOVWF   COUNTD          ;VARIABLE DELAY ENTRY POINT
LOOPD   NOP                     ;(DELAY=5+4*PRESET IN W)
DECFSZ  COUNTD,f
GOTO    LOOPD
RETURN                  ;EXIT POINT
==========================================================

Have lots & lots of fun!
--
eric van es