[SOLVED !] write to TMR0


Closed Thread
Results 1 to 8 of 8

Thread: write to TMR01283 days old

  1. #1
    lemans
    Guest lemans's Avatar

    0 Not allowed!

    Post write to TMR0

    I'm trying to write a value to TMR0 (PIC 16F648A) using the following code.
    to understand this and be able to control the results I start with writing a "zero" to TMR0 during an int_service_routine

    Code:
    flash:
    Context Save
    interrupt_handler:
    If TMR1IF  = 1 Then GoTo TMR1flag
    If T0IF = 1 Then GoTo TMR0flag
    If RBIF = 1 Then GoTo RBIflag
    If INTF = 1 Then GoTo RB0flag
    GoTo interrupt_end
    
    RBIflag:
    PORTB.6 = 1              
      int_D = PORTB.5         'uitlezen welke poort er een interrupt gegenereerd heeft
      int_S = PORTB.4
      
    If  latch_S = 1 Then          'dit is dus referentiepunt
        T0IE = 0
            S_Rest = TMR0
            T0IF = 0
            TMR0 = 0        
        T0IE = 1
        latch_S = 0
        S_ref = DD
        SS = 0    
        End If
      
    If  Latch_D = 1 Then          'dit is dus referentiepunt
        T0IE = 0
            D_Rest = TMR0
            T0IF = 0        
            TMR0 = 0
        T0IE = 1
        Latch_D = 0
    
    etc etc etc
    context restore
    so after execution of the ISR_RBIflag and Latch_S = 1 the following happens:
    -Latch_S is set to : "0"
    -TMR0 starts again at "0"

    running at 20MHz, TMR0 should then roll over after 51,2 micro-seconds and generate an interrupt.
    when using a 8-channel logic analyzer I'm able to see the occurence of this interrupt of TMR0.
    and sometimes (often) it occurs after 31 microseconds or another value.

    my conclusion is that TMR0 has not been set to zero during the RBIflag interrupt.

    what is going wrong?.

    regards Anne
    Last edited by lemans; 29th November 2016 at 20:33.

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: write to TMR0

    Your code is pretty limited, but what I can tell you is that it looks like you've got 'Context Save' in the wrong place, it should be within the ISR.
    Clearing TMR0 appears to be a conditional instruction. Are you sure there's always a condition that will do this? It's absolutely not clear if latch_s and/or latch_D will ever be 1.
    To be short, given this code snippet it is impossible to answer your question.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    lemans
    Guest lemans's Avatar

    0 Not allowed!

    Default Re: write to TMR0

    Hi Hadv,

    in this code I'm using a number of interrupts,
    TMR1 is just counting at F_osc/4
    TMR0 is also counting at F_osc/4

    both are keeping track of the time Between their interrupts TOIF and TMR1IF.

    Latch_S is set to 1 due to an interrupt on change (RBIF) and set to "0" by the same int on change, this int is generated by a rising and falling magnetic field and a Hall sensor. by setting PORTB.6 to "1" and clearing PORTB.6 I can keep track of these int_on_change with the logic analyser.

    so I can actually "see" Latch_S being set to zero by observating PORTB.6.
    when all is well an TOIF should be set at exactly 51,2 microsec after this PORTB.6 = 0 event

    but this is not always the case.

    what do you mean with context save should be inside the ISR. the ISR starts at "Flash:"

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    lemans
    Guest lemans's Avatar

    0 Not allowed!

    Default Re: write to TMR0

    complete code without main:

    Code:
    Device 16F648A    
          
    Config WDT_OFF, PWRTE_On, LVP_OFF, CP_On, MCLRE_OFF, HS_OSC
    Xtal 20                       
    All_Digital TRUE
    
    On_Hardware_Interrupt GoTo flash
    
    EData As Word 334, 117, 260, 390, 585, 781, 2343, 2604, 7812, 7812, 7812, 85, 127, 191, 264, 865, 969, 2907, 2907, 2907             
    
    ;In/Uitgangen instellen
    ;        76543210
    PORTA = %00000000             ;1 = 5V    0 = 0V
    PORTB = %00000000             ;1 = 5V    0 = 0V
    TRISA = %11110010             ;1 = input 0 = output
    TRISB = %00000011             ;1 = input 0 = output
    
    
    Symbol GIE  = INTCON.7        'Global Interrupt Enable bit
    Symbol PEIE = INTCON.6        'peripheral interrupt enable bit
    Symbol T0IE = INTCON.5        'timer 0 interrupt enable bit
    Symbol INTE = INTCON.4        'RB0 interrupt enable bit
    Symbol RBIE = INTCON.3        'RB Port Change Int Enable Bit
    Symbol T0IF = INTCON.2        'timer 0 overflow flag bit
    Symbol INTF = INTCON.1        'RB0 interrupt flag
    Symbol RBIF = INTCON.0        'RB Port Change Flag bit
    
    Symbol INTEDG = OPTION_REG.6  'rising/falling edge RBO-int
    Symbol T0CS   = OPTION_REG.5  'timermode timer0 instellen
    Symbol PSA    = OPTION_REG.3  'toewijzen prescaler aan timer0/WDT
    Symbol PS2    = OPTION_REG.2  'PRESCALER
    Symbol PS1    = OPTION_REG.1  'PRESCALER
    Symbol PS0    = OPTION_REG.0  'PRESCALER
    
    Symbol TMR1ON = T1CON.0       'TMR1 aan/uit; "1"
    Symbol TMR1CS = T1CON.1       'intern of externe klok, intern; "0"
    Symbol T1OSCEN = T1CON.3      'oscillator is shut off; "0"
    Symbol T1CKPS0 = T1CON.4      'prescaler value; "0"
    Symbol T1CKPS1 = T1CON.5      'prescaler value; "0"
    Symbol TMR1IF = PIR1.0        'int flag; "0"
    Symbol TMR1IE = PIE1.0        'int enable; "1"
    
    Symbol RCIE = PIE1.5
    Symbol RCIF = PIR1.5
    
    Symbol carry = STATUS.0
    
    Symbol ledA0 = PORTA.0
    Symbol ledA1 = PORTA.1
    Symbol ledB6 = PORTB.6
    Symbol ledB7 = PORTB.7
    Symbol bobineS = PORTB.3
    Symbol bobineD = PORTA.3
    
    Dim meting As Dword
    Dim M_High As Word
    Dim M_Low As Byte
    
    Dim RPMA As Word
    Dim GRDA As Word
    Dim RPMB As Word
    Dim GRDB As Word
    Dim DRPM As Word
    Dim DGRD As Word
    Dim d_meting As Word
    Dim d_grd As Dword
    Dim d_grd1 As Dword
    
    Dim DD As Word
    Dim SS As Word
    Dim d_grd2 As Dword
    
    Dim A As Byte
    Dim adress As Byte 
    
    Dim invoer[40] As Byte
    Dim waarde2 As Byte
    Dim i As Byte
    Dim CR As Byte
    Dim LF As Byte
    Dim T1 As Byte
    Dim W_Result As Byte
    Dim W_oud As Byte
    Dim W_delta As Byte 
    Dim S_delta As Byte
    Dim D_delta As Byte
    Dim S_Rest As Byte
    Dim D_Rest As Byte
    Dim T2 As Byte
    
    Dim metingflag As Bit
    Dim int_S As Bit
    Dim int_D As Bit
    Dim latch_S As Bit
    Dim Latch_D As Bit
    Dim aanflag As Bit
    Dim stopflag As Bit
    
    
    A = 0
    bobineS = 0
    bobineD = 0
    waarde_aan = 62000
    CR = 13
    LF = 10
    T1 = 0
    
    flash:
    Context Save
    interrupt_handler:
    If TMR1IF  = 1 Then GoTo TMR1flag
    If T0IF = 1 Then GoTo TMR0flag
    If RBIF = 1 Then GoTo RBIflag
    If INTF = 1 Then GoTo RB0flag
    GoTo interrupt_end
    
    RBIflag:
    PORTB.6 = 1              
      int_D = PORTB.5         'uitlezen welke poort er een interrupt gegenereerd heeft
      int_S = PORTB.4
      
    If  latch_S = 1 Then          'dit is dus referentiepunt linker cylinder @270 grd v BDP
        T0IE = 0
            S_Rest = TMR0
            T0IF = 0
            TMR0 = 0        
        T0IE = 1
        latch_S = 0
        S_ref = DD
        SS = 0    
        End If
      
    If  Latch_D = 1 Then          'dit is dus referentiepunt linker cylinder @270 grd v BDP
        T0IE = 0
            D_Rest = TMR0
            T0IF = 0        
            TMR0 = 0
        T0IE = 1
        Latch_D = 0
        bobineS = 0               'dit is BDP linker cylinder 
        D_ref = SS
        DD = 0    
        End If 
      
    If int_S = 0 Then             
        latch_S = 1  
        End If
           
    If int_D = 0 Then             
        Latch_D = 1    
        End If
    PORTB.6 = 0
    RBIF = 0                      'int on change resetten   
    GoTo interrupt_end
    
    
    RB0flag:                      'dit is BDP rechter cylinder
        PORTB.6 = 1    
        TMR1IE = 0                'uitlezen toerental
            M_High = TMR1H
            M_Low = TMR1L
            TMR1H = 0
            TMR1L = 0
        TMR1IE = 1
        meting = T1
        T1 = 0   
        INTF = 0
        metingflag = 1
        PORTB.6 = 0
    GoTo interrupt_end
    
    TMR0flag:
      PORTA.2 = 1        
      Inc SS
      Inc DD
      T0IF = 0                      'timer 0 overflow flag bit cleared by software
      PORTA.2 = 0                   
    GoTo interrupt_end
    
    TMR1flag:
    Inc T1
    TMR1IF = 0
    'GoTo interrupt_end
      
    interrupt_end:
    Context Restore
    'retfie
    
    
    main:
    GIE = 0
      T0CS = 0                    'TMR0 als counter instellen (ipv timer)
      PSA = 1                     'prescalar niet toewijzen aan tmr0
      T0IF = 0                    'timer 0 overflow flag bit cleared by software
      T0IE = 1                    'timer 0 interrupt enable bit
      
      INTF = 0                    'wel int op RB0, int flag = 0
      INTEDG = 1                  'trigger op de rising edge, als led uitgaat
      INTE = 1                    'int RB0 enabled
    
      RBIF = 0                    'reset van int on change flag  
      RBIE = 1                    'RB Port Change Int Enable Bit staat aan
      
        TMR1ON = 1                    'TMR1 aan/uit; "1"
        TMR1CS = 0                    'intern of externe klok, intern; "0"
        T1OSCEN = 0                   'oscillator is shut off; "0"
        T1CKPS0 = 0                   'prescaler value; "0"
        T1CKPS1 = 0                   'prescaler value; "0"
        TMR1IF = 0                    'int flag; "0"
        TMR1IE = 1                    'int enable; "1"
    
      TRISA = %11110010           '1 = input 0 = output  
      TRISB = %00110001           '1 = input 0 = output  
    
    GIE  = 1 
    
                        
    maxrpm = ERead 0           'inlezen max rpm waarde                  
    coil_on = ERead 2          'inlezen bobine_aan_tijd                   
    
    
    
    
    While 1 = 1
                
    
          
    Wend
    
    
    
    End

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: write to TMR0

    Quote Originally Posted by lemans View Post
    Latch_S is set to 1 due to an interrupt on change (RBIF) and set to "0" by the same int on change
    in the code presented latch_s is not set or cleared
    by setting PORTB.6 to "1" and clearing PORTB.6 I can keep track of these int_on_change with the logic analyser.
    I don't see any clearing of PORTB.6

    what do you mean with context save should be inside the ISR. the ISR starts at "Flash:"
    Well your code does not show that, so how are we to know???If you have a label "interrupt_handler" it seems obvious, to me at last, that that is the hander, not "flash".

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: write to TMR0

    Well this explains some problems. You forgot to set the interrupt bit PEIE that is needed for Timer1. It, however, does not explain why timer0 seems to be out of control.
    Take care of manipulating, it one of the 4 bits that trigger an RB interrupt, manipulating this inside the ISR may very well cause the interrupt handler to be called immediately after the retfie and this could result in problems not foreseen.

    The interrupt handler has three occasions to clear TMR0: PortB.4, Portb.5 and Tmr0 itself. This may very well be the source of the problem.
    Last edited by hadv215; 29th November 2016 at 21:32.

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Prolific Poster charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    1,416
    Thumbs Up
    Received: 48
    Given: 40
    Total Downloaded
    3.45 GB

    0 Not allowed!

    Default Re: write to TMR0

    Hadv pointed out what looks like a big error in your coding:

    You have

    Flash:
    context save
    Interrupt_handler:

    Your Context Save MUST be inside the interrupt loop otherwise you will restore rubbish to the registers on Context Restore

    Flash:
    Interrupt_handler:
    Context Save


    Charlie

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    lemans
    Guest lemans's Avatar

    0 Not allowed!

    Default Re: write to TMR0

    problem seems to be solved, thanks to your replies

    - removed an old, not operative, label "interrupt handler", the ISR starts at Flash:
    - enabled PEIE, not clear how I missed that one, thanks for pointing me in the right direction Hadv
    - changed the ISR-sequence; RB0Flag, then RBIflag, then etc

    did some lengthy runs with the logic analyser, randomly checked TMR0 roll-over period after RBIFlag and yess the roll-over period is 49,2 to 51,2 microseconds each an every time.

    problem seems to be solved. thanks
    Anne

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. TMR0 question
    By 775dsp in forum Proton Plus Compiler v3
    Replies: 24
    Last Post: 7th August 2010, 13:30
  2. [SOLVED !] 18F4550 TMR0 interrupt
    By wirecut in forum Proton Plus Compiler v3
    Replies: 15
    Last Post: 28th January 2010, 13:39
  3. Tmr0 based blinker
    By CharlieBrown in forum Proton Plus Compiler v3
    Replies: 29
    Last Post: 12th October 2009, 22:45
  4. Tmr0
    By pic-ignorant in forum The Lounge
    Replies: 14
    Last Post: 19th May 2009, 23:43
  5. Problem using TMR0
    By pdgnr in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 8th June 2006, 11:49

Tags for this Thread

Posting Permissions

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