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 = OFFdirPORTBIn' Input pins to monitor for interrupt signal. We will use PORTB.0 thru PORTB.5dirPORTB.6Out' Output pin to display that an interrupt has been generated.dirPORTB.7Out' Output pin to display activity during non-interrupt cycle.PORTB.6=OffPORTB.7=Off' Interrupt settings - This is from studying the datasheet for the ATTiny2313A and a lot of' trial and error. I'msurethere's a cleaner way to do this, though.SREG.I=1' Enable Global InterruptGIMSK.PCIE=1' Enable Pin Change InterruptGIMSK.INT1=1' Enable External Interrupt Request 0PCMSK.PCINT5=1' Enable Pin Change Interrupt for pins PORTB5PCMSK.PCINT4=1' Enable Pin Change Interrupt for pins PORTB4PCMSK.PCINT3=1' Enable Pin Change Interrupt for pins PORTB3PCMSK.PCINT2=1' Enable Pin Change Interrupt for pins PORTB2PCMSK.PCINT1=1' Enable Pin Change Interrupt for pins PORTB1PCMSK.PCINT0=1' Enable Pin Change Interrupt for pins PORTB0OnInterruptPinChangeCallBlinkMeMain:Do' Simple routine just to show that the chip is doing something when there aren't' any interrupts.PORTB.7=OnWait1sPORTB.7=OffWait1sLoopGoToMainSubBlinkMe' An interrupt has occurred. Do something...PORTB.6=1Wait250msPORTB.6=0Wait1s' Don'twantanotherinterruptcalledtoosoon,sokillsometimebeforereturning.EndSub
:END CODE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
:END CODE
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.
EDIT: oops...array constants corrected
Last edit: kent_twt4 2015-03-06
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.