Elapsed_Int-18 Alteration


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94

    Default Elapsed_Int-18 Alteration

    I've added a slight alteration to Darrel's Elapsed_Int-18 to get 48MHz oscillator. Could somebody please check if this is correct?

    Code:
    ' ------------------------------------------------------------------------------
    ' 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:

    Code:
    ; -----------------  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
    Last edited by Rob; - 30th January 2008 at 10:20. Reason: Further Information

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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,
    DT

  3. #3
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Thumbs up Massive thanks

    Ahhhh, you know when the penny drops... I just had one of those moments!

    Cheers Darrel - much appreciated!

  4. #4
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Default

    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:

    Code:
    </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!

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Try it like this ...
    Code:
    <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...
    Code:
    <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>
    DT

  6. #6
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Thumbs up All sorted!

    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

Similar Threads

  1. Config Settings for the 18 series
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 30th June 2009, 21:03
  2. PICPROTO 18 Prototyping Board Examples
    By rick hardy in forum General
    Replies: 9
    Last Post: - 21st April 2008, 05:19
  3. Replies: 3
    Last Post: - 31st January 2008, 07:30
  4. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03
  5. PIC16F62x 18 pin SMT breakout PCB
    By jdgrotte in forum Adverts
    Replies: 1
    Last Post: - 1st January 2004, 08:59

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