Dwayne: I'm about to take off for the day but wanted to generate this quick reply. I'll study how to implement your boundary check when I return to work on Monday. Thanks for the reply. How are you calling or instantiating the routine? Is it inline assembly or is it a subroutine? What will the compiler do when you change PCLATH? My program is primarily written in C. I'm using Hi-Tech Compiler - PIC16F877. The call is done via the following method: #asm // request 100usec delay movlw 0x19 movwf _delayU clrf _delayL call Delay10bit #endasm Using MPLAB's Stop Watch, I calculate 123usec's on a call requesting 100usec. I implemented your code as follows (just dropped it into my module outside of any subroutines): unsigned char delayU, delayL; // C code #asm Delay10bit // delay is 10 bit value + 7 cycles ( + call + return, if any) incf _delayU,f // correct for dec & test instead of test & dec Dly10bLoop: comf _delayL,w // invert LSBs decfsz _delayU,f goto Dly10bLoop // coarse delay to closest 4 cycles andlw 3 // this additional code checks for the // page boundary and sets pclath correctly // **** movwf _delayL movlw low offset addwf _delayL movlw high offset btfsc status,C addlw 1 movwf pclath movf _delayL,w // **** addwf pcl,f offset: nop nop nop return #endasm -----Original Message----- From: Dwayne Reid [mailto:dwayner@PLANET.EON.NET] Sent: Friday, September 15, 2000 10:12 AM To: PICLIST@MITVMA.MIT.EDU Subject: Re: FW: [PIC]: usec delay routine A-----Original Message----- >From: Sam Linder [mailto:SamL@IN-INC.COM] >Sent: Wednesday, September 13, 2000 11:40 AM >To: PICLIST@MITVMA.MIT.EDU >Subject: Re: [PIC]: usec delay routine > >Dwayne: >I tried implementing your delay routine but ran into addressing problems. >Whenever the code gets to the following line: > addwf PCL,F >it vectors to the wrong address. > >When I step through the code, I notice that PCLATH is usually set to 0x07 or >0x0F (depending upon where the routine is linked in by my compiler), yet my >code's PC is an address like 0x3A4 (for example). When I add the contents of >the w register (for example 0x03) to PCL, my code vectors to 0xFA8 instead >of 0x3A8. I know it's because PCLATH is set to 0x0F right before I execute >the addwf PCL,F instruction. Sorry for taking so long to reply, Sam. You are going to have to set PCLATH before you call the routine. How are you calling or instantiating the routine? Is it inline assembly or is it a subroutine? What will the compiler do when you change PCLATH? If you are dropping the code inline (as a macro or whatever), you can the extra lines I've shown in front of the routine: ;10 bit delay routine providing 1 cycle resolution. Works with both 12 & 14 bit core PICs. ;Original idea from Mike Harrison. This version by Dwayne Reid ; ;enters with upper 8 bits of delay in DELAYU, lower 2 bits in bits 1,0 of DELAYL ;bits 7..2 in DELAYL can be used as flags for whatever purpose desired. ;returns with W destroyed Delay10bit ;delay is 10 bit value + 7 cycles ( + call + return, if any) if (high ($+7)) != (high ($+.10)) ;make sure jump does not span a boundary goto (high ($+.10)) org (high ($+.10)) endif movlw high ($+7) movwf PCLATH ;original routine starts here incf DELAYU,F ;correct for dec & test instead of test & dec Dly10bLoop comf DELAYL,W ;invert LSBs decfsz DELAYU,F goto Dly10bLoop ;coarse delay to closest 4 cycles andlw b'00000011' addwf PCL,F nop nop nop Do note that this might upset the compiler because it may assume that PCLATH is set the way it left it. This is normally not a problem for me - I write in assembler and my macros take care of the housekeeping. dwayne Dwayne Reid Trinity Electronics Systems Ltd Edmonton, AB, CANADA (780) 489-3199 voice (780) 487-6397 fax Celebrating 16 years of Engineering Innovation (1984 - 2000) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Do NOT send unsolicited commercial email to this email address. This message neither grants consent to receive unsolicited commercial email nor is intended to solicit commercial email. -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics