PDA

View Full Version : Elapsed_Int-18 Alteration



Rob
- 30th January 2008, 09:43
I've added a slight alteration to Darrel's Elapsed_Int-18 to get 48MHz oscillator. Could somebody please check if this is correct?



' ------------------------------------------------------------------------------
' To calculate a constant for a different crystal frequency - see this web page
' http://www.picbasic.co.uk/forum/showthread.php?t=2031
' ------------------------------------------------------------------------------
Asm
IF OSC == 4 ; Constants for 100hz interrupt from Timer1
TimerConst = 0D8F7h ; Executed at compile time only
EndIF
If OSC == 8
TimerConst = 0B1E7h
EndIF
If OSC == 10
TimerConst = 09E5Fh
EndIF
If OSC == 16
TimerConst = 063C7h
EndIF
If OSC == 20
TimerConst = 03CB7h
EndIF
If OSC == 48 ; If an oscillator of 48MHz is used e.g.
TimerConst = 015A4h ; in USB PICs, set TimerConst to 015A4h.
BSF T1CON,T1CKPS0, 0 ; MUST also set to prescale of 2 NOT 1
EndIF
; ----------------- ADD TimerConst to TMR1H:TMR1L
ADD2_TIMER macro
; CHK?RP T1CON
BCF T1CON,TMR1ON, 0 ; Turn off timer
MOVLW LOW(TimerConst) ; 1
ADDWF TMR1L,F, 0 ; 1 ; reload timer with correct value
BTFSC STATUS,C ; 1/2
INCF TMR1H,F, 0 ; 1
MOVLW HIGH(TimerConst) ; 1
ADDWF TMR1H,F, 0 ; 1
endm


The timer seems to run doubly quick like the prescale setting is not being set correctly. If I set the prescale setting in the ADD2_TIMER macro though it works correctly but will increase the amount of instructions required therefore altering the amount of time a tick takes:



; ----------------- ADD TimerConst to TMR1H:TMR1L
ADD2_TIMER macro
; CHK?RP T1CON
BCF T1CON,TMR1ON, 0 ; Turn off timer
If OSC == 48 ; IF OSCILLATOR IS 48MHz THEN
BSF T1CON,T1CKPS0, 0 ; SET PRESCALE VALUE TO 2
EndIF
MOVLW LOW(TimerConst) ; 1
ADDWF TMR1L,F, 0 ; 1 ; reload timer with correct value
BTFSC STATUS,C ; 1/2
INCF TMR1H,F, 0 ; 1
MOVLW HIGH(TimerConst) ; 1
ADDWF TMR1H,F, 0 ; 1
endm


Thanks in advance

Rob

Darrel Taylor
- 30th January 2008, 19:22
Hi Rob,

The part that selects the Timer reload value only happens at compile time.
The BSF intruction never get's executed at run-time.

The best place to put it will be in the StartTimer: routine.

hth,

Rob
- 30th January 2008, 20:56
Ahhhh, you know when the penny drops... I just had one of those moments!

Cheers Darrel - much appreciated!

Rob
- 31st January 2008, 09:22
Hmmm,

although I said the penny had dropped, I don't seem to be able to make the code work where you said to put it:



</i></font>StartTimer:
<b>IF </b>OSC = 48 <b>THEN </b><font color="#000080"><i>; IF OSCILLATOR IS 48MHz THEN
</i></font>@ BSF T1CON,T1CKPS0, 0 <font color="#000080"><i>; SET PRESCALE VALUE TO 2
</i></font><b>ENDIF
</b>T1CON.1 = 0 <font color="#000080"><i>; (TMR1CS) Select FOSC/4 Clock Source
</i></font>T1CON.3 = 0 <font color="#000080"><i>; (T1OSCEN) Disable External Oscillator
</i></font>T1CON.0 = 1 <font color="#000080"><i>; (TMR1ON) Start TIMER1
</i></font><b>RETURN</b>


Now when I compile my program, it comes up with the error: Bad expression or missing THEN. (Elapsed_INT-18.bas)

Is this because the oscillator has been defined in my program and not in the Elapsed_INT-18? I've had problems similar to this before when I've tried to create simple modules like you do (not saying that yours are simple I mean my ones!). I like that style of programming I just can't seem to make it work.

Thanks for any help.

Rob

P.s. I looked up your thread "Show us your colours"... I like it!

Darrel Taylor
- 31st January 2008, 11:39
Try it like this ...

<font color="#000000"><b>StartTimer</b>:
<font color="#000080">@ IF OSC == 48 </font><font color="#0000FF"><b><i>; IF OSCILLATOR IS 48MHz THEN
</i></b></font><font color="#000080">@ BSF T1CON,T1CKPS0, 0 </font><font color="#0000FF"><b><i>; SET PRESCALE VALUE TO 2
</i></b></font><font color="#000080">@ ENDIF
</font><b>T1CON</b>.<font color="#800000"><b>1 </b></font>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>; (TMR1CS) Select FOSC/4 Clock Source
</i></b></font><b>T1CON</b>.<font color="#800000"><b>3 </b></font>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>; (T1OSCEN) Disable External Oscillator
</i></b></font><b>T1CON</b>.<font color="#800000"><b>0 </b></font>= <font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>; (TMR1ON) Start TIMER1
</i></b></font><font color="#008000"><b>RETURN
</b></font>
The OSC value isn't directly available to PBP.

Or, you could make it available, and do it that way...

<font color="#000000"><b>PicOSC </b><font color="#008000"><b>CON </b></font><b>EXT
</b><font color="#000080">@PicOSC = OSC

</font><b>StartTimer</b>:
<font color="#008000"><b>IF </b></font><b>PicOSC </b>= <font color="#800000"><b>48 </b></font><font color="#008000"><b>THEN </b></font><font color="#0000FF"><b><i>; IF OSCILLATOR IS 48MHz THEN
</i></b></font><font color="#000080">@ BSF T1CON,T1CKPS0, 0 </font><font color="#0000FF"><b><i>; SET PRESCALE VALUE TO 2
</i></b></font><font color="#008000"><b>ENDIF
</b></font><b>T1CON</b>.<font color="#800000"><b>1 </b></font>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>; (TMR1CS) Select FOSC/4 Clock Source
</i></b></font><b>T1CON</b>.<font color="#800000"><b>3 </b></font>= <font color="#800000"><b>0 </b></font><font color="#0000FF"><b><i>; (T1OSCEN) Disable External Oscillator
</i></b></font><b>T1CON</b>.<font color="#800000"><b>0 </b></font>= <font color="#800000"><b>1 </b></font><font color="#0000FF"><b><i>; (TMR1ON) Start TIMER1
</i></b></font><font color="#008000"><b>RETURN
</b></font>

Rob
- 31st January 2008, 11:51
Cool - it's working now! Cheers for showing me the second way as well as I think it may help me out with writing modules like you do!

Thanks very much

Rob