Go Down

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

baum

I am working on a serial communication library for use between to avr's, provided they are using arduino cores and support basic functions like digitalWrite(), digitalRead(), etc. I want the slave device to utilize interrupt-driven receives by putting the clock line on an external interrupt pin. If clock goes high, data is read. I will therefore need a way of making sure the given clock pin for the slave is indeed an interrupt... but since I want the library to be universal, I can't test for the actual pin... (like if(clkpin==2 || clkpin == 3) as this would only work on the arduino.

So is there anyway I can check to see if a given pin has an external interrupt that will work across all AVRs?

baum

p.s. shame on me if the answer is in the datasheet. :(

graynomad

#1
Jul 22, 2011, 01:52 am Last Edit: Jul 22, 2011, 01:54 am by Graynomad Reason: 1
The only way I can think of to test for this at run time is a static table, much the same as used by Arduino for the pin mapping.

But you said "provided they are using arduino cores" so it will only work with Arduinos anyway won't it?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

baum

No I mean if they are using, for example the arduino-tiny cores, or some other 3rd party core. So I would need list of defines, maybe, for every AVR?

baum

graynomad

Quote
So I would need list of defines, maybe, for every AVR?

That's my thinking, isn't there pre-defined processor types like "__ATMega328__"? I remember playing with something like that a while back.

Otherwise use a #define at the top of the code, but of course this means the user has to figure out which pin is which

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

baum

Looking through various cores, it seems that the interrupt pins are defined: but the define names are not standardized. (some cores use INT0, some use CORE_PIN_INT0, etc.). I think I will just put a threat in the header file, mentioning the library will fail miserably if an external interrupt is not used. :)

baum

graynomad

In the Arduino install

C:\Program Files\Arduino\arduino-0021\hardware\tools\avr\avr\include\avr

there is a file called io.h

This has includes for every AVR type based on a value defined elsewhere (maybe on the GCC command line although I don't see it there).

Anyway if this definition is always in place (and it has to be to get the right processor include file) you can use that to have a processor-specific #define.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

baum

Great! I can just auto-include that file!

Thanks a lot!
baum

floresta

I'm not sure if I am interpreting your question properly but I hope you are aware that every I/O pin is capable of generating an interrupt.  There are only two 'External Inputs' which are capable of being implemented quite flexibly but all of the I/O pins can generate a 'Pin Change Interrupt'.  These pin change interrupts can only be used to detect a change of level on a pin and it is left to the ISR to determine which pin changed and which level is now present.

Don

graynomad

Quote
Great! I can just auto-include that file!

I think you still need to organize your own include file, io.h in turn includes ioxxx.h depending on the chip that's being used. However that included file does not have any definitions for the interrupt pins AFAIK. I am suggesting that you can use io.h as a template to create your own file in the format you need.

Quote
all of the I/O pins can generate a 'Pin Change Interrupt'.

Good point Don, I was assuming just the external int pins.

______
Rob


Rob Gray aka the GRAYnomad www.robgray.com

baum

floresta:

Quote
all of the I/O pins


not always. look at the tiny2313, for instance.

I want to use external interrupts so I don't need to waste time figuring out which pin triggered the interrupt. I think I will be unable to make the communication library universal, as every chip has a different way of turning on interrupts... I'll just make in support a few atmega's and attiny's.

Thanks for the help!

baum

floresta

Quote
...so I don't need to waste time figuring out which pin triggered the interrupt


Do you mean programming time or do you mean processor time?

Don

baum

Processor time. The data will be clocked in pretty fast, so I can't waste to much time on the interrupt. I also am using 12 pins, so for example:

Code: [Select]
old = PORTB;
//something goes here
if(PORTB = old) {
//figure out the pin
}


and than again for the other 4 pins.

floresta

Quote
The data will be clocked in pretty fast...

The processor is pretty fast as well.  If you divided your pins among the available ports you wouldn't have to check them all each time you got an interrupt.

Don

CrossRoads

Baum, Are  you talking about the PCINTs?
Don't those generate an interrupt per port? So you wouldn't need to check all the pins, just the port that interrupted.
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

I'll start over, and restate the problem:

I am making a project in which 12 buttons are connected to an ATtiny2313 (17GPIO). I'll connect 8 to port b, and the other 4 to port D. If the user presses a button, the ATtiny sends out the buttons that were pressed over serial to a master ATmega328 which is controlling the project. So I need an efficient and compact way to detect button presses (I already have the serial protocol) as the ATtiny only has 2k space.
I don't want to waste time doing digitalRead()'s for all 12 pins, and PCINTs are only available on port b, none of the other ports. I was thinking of testing the whole port at the same time, and then, if it was different, doing a bitwise and on the current and past state. Then I would get a 1-byte number consisting of all 0's and a single 1, from here maybe I could figure out what place the 1 was at to get the button press?

Any other ideas? I'm sort of cramped space wise.

baum

Go Up