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 vanes@ilink.nis.za cape town, south-africa Looking for HOLIDAY ACCOMODATION? http://www.nis.za/~vanes/