Menu

PinChange interrupt for ATTiny2313

Fred Stone
2015-03-06
2015-03-07
  • Fred Stone

    Fred Stone - 2015-03-06

    Hi All,

    I'm making a skeeball game for my grandkids and plan to use an ATTiny2313 to read the hole sensors (IR LEDs with IR photosensors) and to display the score on three 7-segment display units. I've been working on a test program to figure out how to implement Pin Change and I think I have it working - sort of. My code triggers an interrupt any time one of the six sensors goes low, and I can see that the interrupt routine is called properly. However, each sensor will have a different scoring value, so it is important that the program knows which sensor triggered the interrupt.

    Below is the test code I have come up with for the interrupt routine. It is just a test program, so there aren't any game features in it yet. Also, there's no code to distinguish which sensor triggered the interrupt - that's what I need help with. I would appreciate it if someone could tell me how to read which sensor caused the trigger.

    Thanks.

    BEGIN CODE:

    ' =========================================================================================
    ' Program Name:    Interrupt Test
    ' Version:         1.0
    ' Microchip:       ATTiny2313A
    ' Date:            03-05-15
    ' Author:          fstone35
    ' Description:     Tests the PinChange interrupt by temporarily putting a logic-level 0 on
    '           one of the interrupt sensor pins.
    '
    ' Wiring:                          
    '                       (PA2)  1 |...........| 20  (VCC) 
    '                           (PD0)  2 |...........| 19  (PB7) Activity Light
    '                           (PD1)  3 |...........| 18  (PB6) Interrupt Light
    '                       (PA1)  4 |...........| 17  (PB5) Interrupt sensor 5
    '                       (PA0)  5 |...........| 16  (PB4) Interrupt sensor 4
    '                           (PD2)  6 |...........| 15  (PB3) Interrupt sensor 3
    '                           (PD3)  7 |...........| 14  (PB2) Interrupt sensor 2
    '                           (PD4)  8 |...........| 13  (PB1) Interrupt sensor 1
    '                           (PD5)  9 |...........| 12  (PB0) Interrupt sensor 0
    '                                 GND 10 |...........| 11  (PD6) 
    '
    ' =========================================================================================
    
    
    #chip tiny2313, 8
    #config OSC = OSC_INTOSC_NOCLKOUT, MCLRE = OFF, PWRTE = OFF, CP = OFF WDT = OFF, BOREN = OFF
    
    
    dir PORTB In        ' Input pins to monitor for interrupt signal. We will use PORTB.0 thru PORTB.5
    dir PORTB.6 Out     ' Output pin to display that an interrupt has been generated.
    dir PORTB.7 Out     ' Output pin to display activity during non-interrupt cycle.
    
    PORTB.6 = Off
    PORTB.7 = Off
    
    ' Interrupt settings - This is from studying the datasheet for the ATTiny2313A and a lot of
    ' trial and error. I'm sure there's a cleaner way to do this, though.
    SREG.I = 1      ' Enable Global Interrupt
    GIMSK.PCIE = 1      ' Enable Pin Change Interrupt
    GIMSK.INT1 = 1      ' Enable External Interrupt Request 0
    PCMSK.PCINT5 = 1    ' Enable Pin Change Interrupt for pins PORTB5
    PCMSK.PCINT4 = 1    ' Enable Pin Change Interrupt for pins PORTB4
    PCMSK.PCINT3 = 1    ' Enable Pin Change Interrupt for pins PORTB3
    PCMSK.PCINT2 = 1    ' Enable Pin Change Interrupt for pins PORTB2
    PCMSK.PCINT1 = 1    ' Enable Pin Change Interrupt for pins PORTB1
    PCMSK.PCINT0 = 1    ' Enable Pin Change Interrupt for pins PORTB0
    
    On Interrupt PinChange Call BlinkMe
    
    Main:
        Do  ' Simple routine just to show that the chip is doing something when there aren't 
            '  any interrupts.
            PORTB.7 = On
            Wait 1 s
                PORTB.7 = Off
            Wait 1 s
        Loop
    GoTo Main
    
    
    Sub BlinkMe ' An interrupt has occurred. Do something...
        PORTB.6 = 1
        Wait 250 ms
        PORTB.6 = 0
        Wait 1 s    ' Don't want another interrupt called too soon, so kill some time before returning.
    End Sub
    

    :END CODE

     
  • kent_twt4

    kent_twt4 - 2015-03-06

    Looks like a fun project! I don't have a ATtiny2313 so speaking in general terms, I would immediately read PortB (or PINB). Then to see which pin is low, rotate the variable and test for carry bit, assign a value due to the bit position.

    Dim Score(6)
    Score(1) = 50
    Score(2) = 100
    etc.
    ...
    ...
    ...
    sub BlinkMe
    WhichPIN = PINB
    For Score_Value = 1 to 6
    Rotate WhichPIN Right
    If SREG.0 On Then
        Add_Value = Score(Score_Value)
    End if
    Next
    New_Score = New_Score + Add_Value
    end sub
    

    EDIT: oops...array constants corrected

     

    Last edit: kent_twt4 2015-03-06
  • Fred Stone

    Fred Stone - 2015-03-07

    Thanks for your idea and code. I will try it out and I'll post how it goes. Once I have the real code sorted out I'll post it here in case anyone is interested. Thanks again for your response.

     

Log in to post a comment.