timer value for PIC

Discussion in 'Embedded Systems and Microcontrollers' started by haran, Jun 11, 2008.

  1. haran

    Thread Starter Active Member

    Apr 6, 2008
    42
    0
    can anybody tell me the timing delay of the 12 microsecond timer. it says that it is a 12 microsecond timer but i have doubt on it.im using a PIC16F873 and using a 4Mhz crystal resonator:confused:


    ;*** 40KHz pulse send ( 0.5 msec )
    movlw d'20' ;Send-out pulse count
    movwf s_count ;Set count
    s_loop
    call pulse ;Call pulse send sub
    decfsz s_count,f ;End ?
    goto s_loop ;No. Continue


    ;*************** Pulse send-out Process ****************
    pulse
    movlw b'00100000' ;RC4=ON
    movwf PORTC ;Set PORTC register
    call t12us ;Call 12usec timer
    clrf PORTC ;RC4=OFF
    goto $+1
    goto $+1
    nop
    return

    ;*************** 12 microseconds timer *****************
    t12us
    goto $+1
    goto $+1
    goto $+1
    goto $+1
    nop
    return
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    The time delay should be 12ms, why you doubt it?

    Why you use an external crystal oscillator? Why you dont use the internal oscillator of the PIC? It can go up to 8MHz.
     
  3. haran

    Thread Starter Active Member

    Apr 6, 2008
    42
    0
    because there is no pulse coming out from the pic. how long does it take to execute the goto $+1 instruction?
     
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    I dont know the instructions in asm but when you put a delay in the program the program just counts this delay. To output a pulse after this delay you should use the instruction which makes your output high.
    If you want a pulse of 12ms then put the instruction which makes your output high, then put a delay of 12ms and finally put the instruction which makes your output low. This is for a single pulse. If you want a continuous pulse train then use an interrupt to repeat the above instructions at he frequency you want and use the delay to adjust the pulse width, thus you create a PWM, if this is what you want.

    Check if your PIC has a PWM module.
     
  5. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    This is two cycle instruction and the time depends on the clock.
     
  6. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    When you're posting source code, use the "Go Advanced" button, then the # button on the menu bar to insert CODE brackets - that'll preserve your formatting.
    Code ( (Unknown Language)):
    1. ;*** 40KHz pulse send ( 0.5 msec )
    2.         movlw   d'20'           ;Send-out pulse count
    3.         movwf   s_count         ;Set count
    4. s_loop
    5.         call    pulse           ;Call pulse send sub
    6.         decfsz  s_count,f       ;End ?
    7.         goto    s_loop          ;No. Continue
    8.  
    9.  
    10. ;***************  Pulse send-out Process ****************
    11. pulse
    12.         movlw   b'00100000'     ;RC4=ON
    13.         movwf   PORTC           ;Set PORTC register
    14.         call    t12us           ;Call 12usec timer
    15.         clrf    PORTC           ;RC4=OFF
    16.         goto    $+1
    17.         goto    $+1
    18.         nop
    19.         return
    20.  
    21. ;***************  12 microseconds timer *****************
    22. t12us
    23.         goto   $+1
    24.         goto   $+1
    25.         goto   $+1
    26.         goto   $+1
    27.         nop
    28.         return
    29.  
    Did you forget about the watchdog timer? Or did you disable it in a header file somewhere?
     
  7. haran

    Thread Starter Active Member

    Apr 6, 2008
    42
    0
    its enabled
    i just want 2 noe how long is this delay
    im using a 4Mhz resonator
    ;*************** 12 microseconds timer *****************
    t12us
    goto $+1
    goto $+1
    goto $+1
    goto $+1
    nop
    return
     
  8. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Hint: What happens when s_count becomes 0? :D Unless you have a good scope set to trigger once, you will probably miss the 21 pulses zooming by.

    --Rich
     
  9. haran

    Thread Starter Active Member

    Apr 6, 2008
    42
    0
  10. Denatron

    New Member

    Dec 27, 2009
    1
    0
    I'm pretty new at this but I believe this is how it works.
    You said you are using a 4Mhz oscillator = 4000000 clock cycles/sec. The pic requires 4 oscillator clocks per instruction cycle(not sure why) therefore is will complete 1000000 instructions cycles/second. This equals to 1 microsecond per instruction.
    goto instructions require 2 cycles.
    nop instructions require 1 cycle.
    return instructions require 2 cycles.
    Therefore your delay function requires a total of 2x4 + 1 + 2 = 11 cycles. So your next instruction will occur at the 12 microsecond mark. It also would have taken 2 cycles to call the delay function. So after PortC is set it should take 13 cycles or 13 microseconds to pass before PortC is cleared.
     
  11. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Code ( (Unknown Language)):
    1.  
    2. ;***************  13 microseconds timer *****************
    3. t13us
    4.         goto   $+1
    5.         goto   $+1
    6.         goto   $+1
    7.         goto   $+1
    8.         nop
    9.         return
    10.  
    11. ;***************  12 microseconds timer *****************
    12. t12us
    13.         goto   $+1
    14.         goto   $+1
    15.         goto   $+1
    16.         goto   $+1
    17.         return
    18.  
    19.  
    The code you posted was 13uS. See above.
     
  12. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This is a pretty old post.
     
  13. chat_eur

    New Member

    Dec 30, 2009
    7
    0
Loading...