Delay question


Closed Thread
Results 1 to 22 of 22

Thread: Delay question

  1. #1
    kata's Avatar
    kata Guest

    Default Delay question

    I don't find any specific answer to this question : Is it possible to have delay (for a specific protocol maybe) less than 1mS (say 335 uS) or I have to use ASM code ? Thank you for your time !

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Try the PAUSEUS command.

    Here are the details as listed in the manual - http://www.melabs.com/resources/pbpmanual/ - (which should of been the first place to look)

    PAUSEUS

    PAUSEUS Period

    Pause the program for Period microseconds. Period is 16-bits, so delays can be up to 65,535 microseconds. Unlike the other delay functions (NAP and SLEEP), PAUSEUS doesn't put the microcontroller into low power mode. Thus, PAUSEUS consumes more power but is also much more accurate. It has the same accuracy as the system clock.

    Because PAUSEUS takes a minimum number of cycles to operate, depending on the frequency of the oscillator, delays of less than a minimum number of microseconds are not possible using PAUSEUS. To obtain shorter delays, use an assembly language routine in an ASM..ENDASM construct.

    OSC Minimum delay
    3 (3.58) 20us
    4 24us
    8 12us
    10 8us
    12 7us
    16 5us
    20 3us
    25* 2us
    32* 2us
    33* 2us
    40** 2us

    * PIC17Cxxx and PIC18Cxxx only.
    ** PIC18Cxxx only.

    PAUSEUS assumes an oscillator frequency of 4MHz. If an oscillator other that 4MHz is used, PBP must be told using a DEFINE OSC command. See the section on speed for more information.

    PAUSEUS 1000 ' Delay for 1 millisecond

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c
    Try the PAUSEUS command.

    Here are the details as listed in the manual - http://www.melabs.com/resources/pbpmanual/ - (which should of been the first place to look)

    That's amazing isn't it? You find the wierdest information in the oddest places. Especially if the information (the PBP book) came with the program package (bought directly from MeLabs).
    JDG

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Come on Skimask
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    Can someone please explain me how to use assembly to make shorter delay than pauseus can make ?

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The shortest possible delay is achieved by using the assembler command NOP (short for No OPeration):
    Code:
    @NOP       'Delay for OSC/4
    The NOP instruction takes 1uS at 4Mhz and 250nS at 20Mhz etc. If you want longer delay add more NOP instructions or build a loop, but then it's probably easier to use PauseUs anyway.

    /Henrik Olsson.

  7. #7
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    thank you, as i understand 1us is minimum limit for 4mhz crystal

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Therian,
    Yes, that is correct. With a 4Mhz X-tal one intruction cylce is 1uS and since the NOP command takes one cycle to execute the delay will be 1uS.

    /Henrik Olsson.

  9. #9
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    just curious, is 250nS enough to generate radio frequency directly from pic without any rf modules, it sound like cheap radio link

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Therian,
    Well, let's see:
    Code:
    Start:
      PortB.1 = 1     'Set pin high        
    @ NOP             'Wait....Two NOP's are neede if we want 50% dutycycle
    @ NOP             'since the GOTO takes two instruction cylcles.
      PortB.1 = 0     'Set pin low
    Goto Start        'Do it again. The GOTO takes the same time as two NOP's
    This, I think, will generate a pulsetrain with 50% duty cycle. The frequency would be ~833kHz. Not much of a radio frequency.... besides that, you can't do anything else in the code without slowing it down more...much more.

    /Henrik Olsson.

  11. #11
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    maybe overclocking might work ?

  12. #12
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    wait 833kHz is in range of medium AM broadcast 300-3000kHz and 30-300kHz is longwave AM broadcast but as i know 3-30kHz is enough for submarine communication. so what wrong with using low frequency, huge antenna ?
    Last edited by therian; - 5th March 2007 at 12:10.

  13. #13
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Therian,
    First all, I've stated 250nS in my previos posts. That is of course wrong and should be 200nS. NOT 250.

    With that being said you could go to a PIC capable of 40MHz, then a single cycle instruction (like NOP) is executed in 100nS instead of 200.

    But then again if you use software to generate the carrier frequency you'd be hard pressed to have any instruction cycles left to be able modulate it. I really believe you should look at a dedicated TX/RX chipset or module.

    Anyway sorry for the confusion. One instruction cylcle is 200nS at 20Mhz, nothing else.

    /Henrik Olsson.

  14. #14
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    im really thankful for you reply.
    I know about cheap and good modules out there, but dont anyone try to make some from scratch, it easy to fing FM bugs circuits in google but searching for rf module give really few results

  15. #15
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Not to muddy the waters because it sounds like you have decided on another route, but for completeness ...

    Code:
    Start:
      PortB.1 = 1     'Set pin high        
    @ NOP             'Wait....Two NOP's are neede if we want 50% dutycycle
    @ NOP             'since the GOTO takes two instruction cylcles.
      PortB.1 = 0     'Set pin low
    Goto Start        'Do it again. The GOTO takes the same time as two NOP's
    at 4 MHz, 3 instructions on and 3 instruction off = 6 instructions per cycle, therefore f=1000000/6=166666.667 Hz (twice that noted above)

    Interestingly enough the above code does 3 instructions on and 4 instructions off on 16F devices and 3 on 3 off on 18F (at least on an 18F877A and 18F452, respectively). To combat this on the 16F's, you could do something like this in ASM

    Code:
    ASM
    here
        bsf PORTB,1
        NOP
        NOP
        bcf PORTB, 1
        goto here
    ENDASM
    Good Luck
    Last edited by paul borgmeier; - 5th March 2007 at 21:02.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  16. #16
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi Paul,
    Thanks for filling in and for correcting me. My head seems to be completely messed up today - can't seem to get one thing right.....

    How come it differs between 16F and 18F parts? I haven't looked at the generated code but would've thought that the compiler generated just what you wrote in the 'pure asm' example.

    Anyway, I'm off to bed now. Not doing much good anyway.

    Thanks
    /Henrik Olsson.

  17. #17
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by HenrikOlsson View Post
    How come it differs between 16F and 18F parts?
    The 16F code inserts a "clrf PCLATH" command before the goto - the 18F uses the bra command (instead of goto) and does not - why PBP inserts the clrf PCLATH, I would guess because "Start:" is in page 0 where PCLATH = 0 (EDIT: and does not check to see if we are already in page 0, which we are)
    Last edited by paul borgmeier; - 5th March 2007 at 22:06.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  18. #18
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi Paul,
    New day, fresh head...
    You said:
    at 4 MHz, 3 instructions on and 3 instruction off = 6 instructions per cycle, therefore f=1000000/6=166666.667 Hz (twice that noted above)
    That is of course correct. I based my calculation on a 20Mhz so 5000000/6 = 833kHz which is what I stated in my post which is also correct (but not half of 166kHz).

    Thanks for the 16F vs 18F explanation. I guess it would take up more code to check which bank we're in before each goto instead of just setting the correct one?

    Thanks!
    /Henrik Olsson.

  19. #19
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Red face

    Henrik,

    My apologies - you are and were indeed correct (166.667 / 2 != 833.33).

    I am guessing the 16F PBP compiler code is not optimized to check to see if it is in the same page as the goto. I bet it sets the PCLATH every time it encounters a goto just to be safe. I do not think it would add PIC code - I think it would take more compiler code to do this check - MELABS must not have thought it was not worth it to potentially save one instruction (100% guess by me)?
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  20. #20
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I think it will use the CHK?RP macro simply for safety sake. sure the code is not optimized in any sort, but it's always solid as rock. When you need something tight, use ASM. Hence, for safety sake and for none 18Xxxxx your asm example could be...
    Code:
    ASM
        CHK?RP    ; make sure we are on the right BANK
    here
        bsf PORTB,1
        NOP
        NOP
        bcf PORTB, 1
        goto here
    ENDASM
    PORTB.1=0 call MOVE?CT macro. wich is
    Code:
    MOVE?CT macro Cin, Regout, Bitout
            CHK?RP  Regout
        if (((Cin) & 1) == 1)
            bsf     Regout, Bitout
        else
            bcf     Regout, Bitout
        endif
        endm
    there you discover the extra cycle
    Last edited by mister_e; - 6th March 2007 at 15:53.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  21. #21
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    separate coil-capacitor Oscillator and use pic only to turn on and of this oscillation we will get Pulse Modulation

  22. #22
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    it sound too simple to be true

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. RF Transmitter
    By et_Fong in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th October 2005, 16:34
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts