Go Down

Topic: Testing whether a pin is an interrupt (Read 6062 times) previous topic - next topic

floresta

Quote
I was thinking of testing the whole port at the same time,

This is essentially what the pin change interrupt does.  You would have it's ISR set a flag to tell your program that a bit has changed.

Quote
and then, if it was different, doing a bitwise and on the current and past state.

And this is what would then be done by your program when it discovers that the flag is set.

Don


Senso

Use direct port acess and save a lot of space.
You could also put your switch in a resistive lader and use analog input pins to connect then.

baum


Senso

I feared that..
But if the only thing that the ATtiny is doing is reading inputs and sending then via serial, what is the need to use interrupts, are you going to press buttons at 100Khz and have that as fast as your serial comm is?
If not just read and de-bounce then in a loop.

baum


Senso

you should debounce then, to prevent false multiple presses, and using direct port acess you can read all of the switchs in only 2 clock cycles, then to see if one as changed state is very simple.

floresta

Quote
...you can read all of the switchs in only 2 clock cycles, ...

Or you can use interrupts to do this in effectively zero clock cycles.... That is precisely the purpose of having a pin change interrupt.  The use of interrupts makes your programming easier, not more difficult as many fear.

Don

baum

But there are only 8 pcints, i need 12.

floresta

Quote
But there are only 8 pcints, i need 12.

If you have 'n' I/O pins then you have 'n' pin change interrupts.  You may have RTFD'ed but perhaps you have misunderstood.  Try this:  Go to http://web.alfredstate.edu/weimandn and scroll down to the section on Atmel ATmega Subsystem Diagrams.  Follow the Reference Diagrams For Programmers link and then look at the ATmega168 Pin Change Interrupts diagram.

Don

baum


Senso


Quote
...you can read all of the switchs in only 2 clock cycles, ...

Or you can use interrupts to do this in effectively zero clock cycles.... That is precisely the purpose of having a pin change interrupt.  The use of interrupts makes your programming easier, not more difficult as many fear.

Don


When the only thing that the micro is doing is reading two input ports and putting that into the serial in my opinio is just faster to use a loop doing just that, and well it might be easier to de-bounce the inputs.

baum

No need to debounce. tiny2313 sends first button press it sees, ignores everything else for ~10 seconds.

CrossRoads

Are you setup to get a low when the buttons are pressed? I would for that to start.  Use the internal pullup resisters.
Then, use diodes to make a diode-AND gate - each pin goes to a diode cathode, the anodes are connected together, and go to INT0 or INT1 (PD2, PD3).
Then when any pin goes low, it creates an interrupt, and you do a quick scan then to see who it was - basically a 1x12 keypad.
If you were sure only 1 button was being pressed at a time, then you could instead set up as a 4x3 keypad and save a bunch of pins.
If INT0 & INT1 are already tied up, then do 1x11 and use the 12th pin as a PCINT. Either way, only scan when you see a LOW interrupt.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

baum

Only one button should be pressed at a time, so I just get 12 diodes and run them from button to interrupt? I just don't get the 4x3 part...
baum

CrossRoads

The Keypad library lets you connect a 4x3 keypad (which is just buttons, after all) using just 7 pins.
The scheme in this diagram uses a combination of things.
It uses sleep mode; when in sleep mode, the columns are driven low, the row pins are pulled high. When a  keypad button is pressed, a diode cathode is pulled low and causes interrupt to wake up from sleep mode. The columns are then driven high and the main program kicks in for the keypad library to go and read the pin that was pressed.

Wire up the 12 buttons like the 4 I have here, when a button is pressed a cathode goes low, you get an interrupt, you scan to see which pin went low.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up