timer value for PIC

Thread Starter

haran

Joined Apr 6, 2008
42
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
 

mik3

Joined Feb 4, 2008
4,843
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.
 

Thread Starter

haran

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

mik3

Joined Feb 4, 2008
4,843
because there is no pulse coming out from the pic. how long does it take to execute the goto $+1 instruction?
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.
 

SgtWookie

Joined Jul 17, 2007
22,230
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.
Rich (BB code):
;*** 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
Did you forget about the watchdog timer? Or did you disable it in a header file somewhere?
 

Thread Starter

haran

Joined Apr 6, 2008
42
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
 

RiJoRI

Joined Aug 15, 2007
536
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
 

Denatron

Joined Dec 27, 2009
1
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.
 

THE_RB

Joined Feb 11, 2008
5,438
Rich (BB code):
;***************  13 microseconds timer *****************
t13us
        goto   $+1
        goto   $+1
        goto   $+1
        goto   $+1
        nop
        return

;***************  12 microseconds timer *****************
t12us
        goto   $+1
        goto   $+1
        goto   $+1
        goto   $+1
        return
The code you posted was 13uS. See above.
 
Top