Them interrupts, them interrupts, they drive me mad!


+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default Them interrupts, them interrupts, they drive me mad!

    Greetings good people. For reasons I can't really explain, I decided it was about time I started to learn how to use interrupts. My current project is an antenna analyser which requires the use of a rotary encoder. Now I've got the software working well provided I simply pole the code dealing with the encoder. Being stupid, rather than let things simple rest at this point, I want to try and get the encoder serviced by an interrupt.

    Now I understand very little about PIC interrupts as the code below will fully show. If any of you guys would be kind enough to take pity and point me in the right direction I would be most grateful.

    Code:
       'Device is a 18F46K22    
        Include "18F46K22_intOSC_64MHz.inc"
        Declare Hbus_Bitrate  800
        
        
        Include "Arial_8.inc"
        Include "SH1106_Def_v1.00.inc"
        Include "Graphic_Def_v0.4.inc" 
        Include "SH1106_Sub_v1.00.inc"   
        Include "Graphic_Sub_v0.4.inc" 
       
        TRISB = %00110000 'Set PortB input/output 
         
        Dim OldEncVal As Byte
        Dim EncVal As Byte 
        Dim NewPos As Byte
        Dim OldPos As Byte
        Dim flag As Bit
      
      
     GoTo Main: 
      
      
    '******************************************************************************  
    GetEncoder:
    '******************************************************************************
        'Works with Panasonic encoder.
        
    Context Save
    
        'Disable interrupts whilst ISR is serviced
        IntConBits_RBIE = 0 'Disable PortB interrupts
        IntConBits_GIE = 0 'Disable Global interrupts
        
        OldPos = NewPos                                                                       
        NewPos = (PORTB & %00110000)
        NewPos = NewPos >> 4                                                                        
        If OldPos = 3 And NewPos = 2 Then Inc EncVal                                  
        If OldPos = 0 And NewPos = 1 Then Inc EncVal                                          
        If OldPos = 0 And NewPos = 2 Then Dec EncVal                                            
        If OldPos = 3 And NewPos = 1 Then Dec EncVal
    
        'Interrupt Serviced so enable interrupts once more.
        IntConBits_RBIF = 0'Port B Interrupt-On-Change Interrupt Flag bit
        IntConBits_RBIE = 1 'Enable PortB interrupts
        IntConBits_GIE = 1 'Enable Global interrupts
    
     Context Restore 
       
    '******************************************************************************
    Main:
    '******************************************************************************
    
        INTCON = $00 'all interrupts disabled at this point
        
        IOCB.4 = 1  'Interrupt on change PortB pins <4:5>
        IOCB.5 = 1
    
        IntConBits_RBIE = 1 'Enable PortB interrupts
        IntConBits_GIE = 1 'Enable Global interrupts
        IntConBits_RBIF = 0'Port B Interrupt-On-Change Interrupt Flag bit. Necessary?
    
     On_Hardware_Interrupt GetEncoder
    
    Clear
    SelectFont(Arial_8)
    Cls
    OldEncVal = 255
    While
        If EncVal <> OldEncVal Then
        
        'disable interrupts while screen refresh takes place
        'Could I get away with just disabling global interrupts?
        
            IntConBits_RBIE = 0 'Disable PortB interrupts
            IntConBits_GIE = 0 'Disable Global interrupts
            Cls 
            Print At 0,30,"Encoder Value ", Dec EncVal
            RefreshDisplay()
            OldEncVal = EncVal
        'Refresh done, so enable interrupts again    
            IntConBits_RBIE = 1 'Enable PortB interrupts
            IntConBits_GIE = 1 'Enable Global interrupts
       EndIf
    Wend

    The PIC reacts to the rotation of the encoder, but not in a good way. My OLED display ends up blank after a few clicks of the encoder and the encoder variable (EncVal) is never updated.


    Thanks,

    Bob
    Last edited by Mellbreak; 1st April 2018 at 19:13.

  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 towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    2,354
    Thumbs Up
    Received: 83
    Given: 183
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    From memory, I arbitarily picked one terminal and used it to generate an edge interupt and read the other terminal for direction. Can't remember the truth table of hand but it will be obvious if you look at the output while turning in either direction. I seem to remember I had problems with key bounce, I added a cap to the pullup and a delay before I read to second terinal. Make sure you understand the output as I beleive there are different types.
    Last edited by towlerg; 1st April 2018 at 20:47.
    George.

  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
    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: Them interrupts, them interrupts, they drive me mad!

    As George says, there are different types of encoder. If yours works with a Panasonic encoder then you are on the right lines. There is no need to disable ints during an int, the chip automatically ignores any further ints until your "context restore" (unless you are using high/low level ints, which I doubt).

    I would guess that things hang because an int hs happened and you haven't serviced it, or you haven't cleared the int flag concerned.

    Charlie

  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
    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: Them interrupts, them interrupts, they drive me mad!

    Change your code within context save/restore like this:

    Context Save

    OldPos = NewPos
    NewPos = (PORTB & 110000)
    NewPos = NewPos >> 4
    If OldPos = 3 And NewPos = 2 Then Inc EncVal
    If OldPos = 0 And NewPos = 1 Then Inc EncVal
    If OldPos = 0 And NewPos = 2 Then Dec EncVal
    If OldPos = 3 And NewPos = 1 Then Dec EncVal

    IntConBits_RBIF = 0 'clear interrupt

    Context Restore

  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
    Member flosigud's Avatar
    Join Date
    Oct 2013
    Posts
    193
    Thumbs Up
    Received: 8
    Given: 15
    Total Downloaded
    3.16 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    Hi,

    Here is an example using IOC interrupt to read two encoders. It uses X4 encoding which means that it counts both falling and rising edges of both channels, which gives best resolution. http://www.protonbasic.co.uk/showthr...-with-TouchDRO
    Flosi Guđmundsson
    Reykjavík

  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
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    Many thanks folks for all the helpful pointers and code examples. I've now got my rotary encoder working reliably using interrupts. It's been a frustrating process, but I do feel as if I've learn't a thing or two! Thanks again.

    Bob

  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
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,599
    Thumbs Up
    Received: 341
    Given: 162
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    Good to hear Bob, and well done. Isn't learning so much fun? :-)

    If you're using the previously mentioned code example, here's a tip that will speed up the comparisons and optimise the code. This is always a good thing within an interrupt.

    Code:
    Context Save
    
    OldPos = NewPos
    NewPos = (PORTB & %11110000)
    NewPos = NewPos >> 4
    If OldPos = 3 Then If NewPos = 2 Then Inc EncVal
    If OldPos = 0 Then If NewPos = 1 Then Inc EncVal
    If OldPos = 0 Then If NewPos = 2 Then Dec EncVal
    If OldPos = 3 Then If NewPos = 1 Then Dec EncVal
    
    IntConBits_RBIF = 0 ' Clear the interrupt flag
    
    Context Restore
    The compiler uses a stack mechanism for And and Or within a comparison, which is efficient, but can be more efficient instead of using an And, use another Then If.

    An even more efficient method is:

    Code:
    Context Save
    
    OldPos = NewPos
    NewPos = (PORTB & %11110000)
    NewPos = NewPos >> 4
    If OldPos = 3 Then 
        If NewPos = 2 Then Inc EncVal
    ElseIf OldPos = 0 Then 
        If NewPos = 1 Then Inc EncVal
    ElseIf OldPos = 0 Then 
        If NewPos = 2 Then Dec EncVal
    ElseIf OldPos = 3 Then 
        If NewPos = 1 Then Dec EncVal
    EndIf
    IntConBits_RBIF = 0 ' Clear the interrupt flag
    
    Context Restore
    The extra efficiency is because the ElseIf commands use a branch, so each comparision is not tested if one is valid. It will simply jump past all the other OldPos comparisons, because it can only be one value.
    Last edited by top204; 4th April 2018 at 21:09.

  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
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,599
    Thumbs Up
    Received: 341
    Given: 162
    Total Downloaded
    1.99 GB

    1 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    I just noticed that OldPos is tested for 0 and 3 twice, so the code needs changing to:

    Code:
    Context Save
    
    OldPos = NewPos
    NewPos = (PORTB & 110000)
    NewPos = NewPos >> 4
    If OldPos = 3 Then 
        If NewPos = 2 Then 
            Inc EncVal
        ElseIf NewPos = 1 Then 
            Dec EncVal
        EndIf
    ElseIf OldPos = 0 Then 
        If NewPos = 1 Then 
            Inc EncVal
        ElseIf NewPos = 2 Then 
            Dec EncVal
        EndIf
    EndIf
    IntConBits_RBIF = 0 ' Clear the interrupt flag
    
    Context Restore
    This makes it even more efficient, because there are only 2 initial comparisons, then only 2 after it.

  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.

  17. #9
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    Thanks Les! I've just tried your code and, of course, it works a treat. It's always good to get "pro" tips and learn a bit more about how the compiler works.
    Last edited by Mellbreak; 5th April 2018 at 15:22.

  18. 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.

  19. #10
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,599
    Thumbs Up
    Received: 341
    Given: 162
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    You can also get rid of the right shift by 4 Bob, and do the comparisons on the high nibble values. I know this sound pedantic. LOL. But the quicker a piece of code operates within an interrupt, the better.

  20. 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.

  21. #11
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default Re: Them interrupts, them interrupts, they drive me mad!

    Oh yes, of course, and you save 6 bytes to boot!

  22. 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. how to use interrupts?
    By Zain in forum Interrupt Sources
    Replies: 16
    Last Post: 27th September 2013, 16:12
  2. [SOLVED !] Help with interrupts please...
    By CThom in forum Proton Plus Compiler v3
    Replies: 5
    Last Post: 11th March 2011, 15:23
  3. [SOLVED !] Help Interrupts
    By ADLIN SYSTEMS in forum The Lounge
    Replies: 5
    Last Post: 29th June 2009, 15:38
  4. Interrupts
    By chuckieboy in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 1st March 2007, 18:37
  5. [SOLVED !] interrupts?
    By aceman in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 7th June 2005, 09:28

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