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. I've read through MicroChips APP Notes (e.g. AN556) and the PIC16F87x manual so I understand the problem. I just don't understand why it isn't a problem when you implement the same code. How did you get around the problem? (By the way, this shouldn't make any difference, but the delay code is implemented inside of the #asm / #endasm construct as most of my code is written in C using the Hi-Tech C compiler). -----Original Message----- From: Dwayne Reid [mailto:dwayner@PLANET.EON.NET] Sent: Saturday, September 09, 2000 9:26 PM To: PICLIST@MITVMA.MIT.EDU Subject: Re: [PIC]: usec delay routine At 11:37 AM 9/8/00 -0700, Sam Linder wrote: >Thanks to all who replied - however, none of the routines mentioned do what >I'm looking for. Olin seems to have the clearest picture of what I want. The >software usec delay routine should handle a call from somewhere else in the >program, loop as necessary then return. The time consumed should account for >the call, the overhead, the looping, and the return. Thus, it should look >like this: > SoftUsecDelay(57); // C code call Here is something posted quite some while ago by Mike Harrison - maybe its close to what you are looking for: BBS: MICROCHIP TECHNOLOGY CUSTOMER SUPPORT BB Date: 05-16-94 (14:52) Number: 31206 From: MHARRISON Refer#: 31202 To: MHARRISON Recvd: NO Subj: neat little routines Conf: (5) APPLICN --------------------------------------------------------------------------- Another handy routine ... this code generates a delay with a resolution of 1 cycle, which is handy for tone generation and pwm. the delay is a constant (determined by the rest of the code) plus a 10 bit delay value enter with 8 MS bits of delay in delhi, 2 LS bits of delay XOR 3 in delaylo bit 0,1 loop nop decfsz delhi goto loop ; coarse delay to nearest 4 cycles movf dello,w addwf pc ; fine delay by skipping 0,1,2 or 3 nops nop nop nop Here is the version I came up with (based upon the above) ;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) 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 I hope one of these is useful. 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: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu