| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- /*
- attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)
- attachInterrupt(interrupt, ISR, mode) (not recommended)
- attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)
- Parameters
- interrupt: the number of the interrupt. Allowed data types: int.
- pin: the Arduino pin number.
- ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
- mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
- LOW to trigger the interrupt whenever the pin is low,
- CHANGE to trigger the interrupt whenever the pin changes value
- RISING to trigger when the pin goes from low to high,
- FALLING for when the pin goes from high to low.
- The Due, Zero and MKR1000 boards allow also:
- HIGH to trigger the interrupt whenever the pin is high.
- onst byte ledPin = 13;
- const byte interruptPin = 2;
- volatile byte state = LOW;
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(interruptPin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
- }
- void loop() {
- digitalWrite(ledPin, state);
- }
- void blink() {
- state = !state;
- }
- BOARD INT.0 INT.1 INT.2 INT.3 INT.4 INT.5
- Uno, Ethernet 2 3
- Mega2560 2 3 21 20 19 18
- 32u4 based (e.g
- Leonardo, Micro) 3 2 0 1 7
- */
- /*
- *
-
- http://arduinowo.pl/przerwania-typu-pcint-klawiatura/
- http://arduinowo.pl/generatory/pin_int/
-
-
- */
-
- // FOR ATMEGA
-
- // void setup ()
- #include <avr/interrupt.h>
- #define sei()
- #define cli()
- // Macros for writing interrupt handler functions
- #define ISR(vector, attributes)
- #define SIGNAL(vector)
- #define EMPTY_INTERRUPT(vector)
- #define ISR_ALIAS(vector, target_vector)
- #define reti()
- #define BADISR_vect
- //ISR attributes
- #define ISR_BLOCK
- #define ISR_NOBLOCK
- #define ISR_NAKED
- #define ISR_ALIASOF(target_vector)
- /*
- Introduction to avr-libc's interrupt handling
- It's nearly impossible to find compilers that agree on how to handle interrupt code. Since the C language tries to stay away from machine dependent details, each compiler writer is forced to design their method of support.
- In the AVR-GCC environment, the vector table is predefined to point to interrupt routines with predetermined names. By using the appropriate name, your routine will be called when the corresponding interrupt occurs. The device library provides a set of default interrupt routines, which will get used if you don't define your own.
- Patching into the vector table is only one part of the problem. The compiler uses, by convention, a set of registers when it's normally executing compiler-generated code. It's important that these registers, as well as the status register, get saved and restored. The extra code needed to do this is enabled by tagging the interrupt function with __attribute__((signal)).
- These details seem to make interrupt routines a little messy, but all these details are handled by the Interrupt API. An interrupt routine is defined with ISR(). This macro register and mark the routine as an interrupt handler for the specified peripheral. The following is an example definition of a handler for the ADC interrupt.
- */
- /*ISR(ADC_vect)
- {
- // user code here
- }
- */
- /*
- Catch-all interrupt vector
- If an unexpected interrupt occurs (interrupt is enabled and no handler is installed, which usually indicates a bug), then the default action is to reset the device by jumping to the reset vector. You can override this by supplying a function named BADISR_vect which should be defined with ISR() as such. (The name BADISR_vect is actually an alias for __vector_default. The latter must be used inside assembly code in case <avr/interrupt.h> is not included.)
- //#include <avr/interrupt.h>
- */
- /*
- ISR(BADISR_vect)
- {
- // user code here
- }
- */
- /*Nested interrupts
- The AVR hardware clears the global interrupt flag in SREG before entering an interrupt vector.
- Thus, normally interrupts will remain disabled inside the handler until the handler exits, where the RETI instruction
- (that is emitted by the compiler as part of the normal function epilogue for an interrupt handler) will eventually re-enable further interrupts. For that reason,
- interrupt handlers normally do not nest. For most interrupt handlers, this is the desired behaviour, for some it is even required in order to prevent infinitely
- recursive interrupts (like UART interrupts, or level-triggered external interrupts). In rare circumstances though it might be desired to re-enable the global
- interrupt flag as early as possible in the interrupt handler, in order to not defer any other interrupt more than absolutely needed. This could be done using an sei()
- instruction right at the beginning of the interrupt handler, but this still leaves few instructions inside the compiler-generated function prologue to run with global
- interrupts disabled. The compiler can be instructed to insert an SEI instruction right at the beginning of an interrupt handler by declaring the handler the following way:
- */
- ISR(PCINT0_vect, ISR_NOBLOCK)
- {
- // ...
- }
- //where XXX_vect is the name of a valid interrupt vector for the MCU type in question, as explained below.
- /*
- Two vectors sharing the same code
- In some circumstances, the actions to be taken upon two different interrupts might be completely identical so a single implementation for the ISR would suffice.
- For example, pin-change interrupts arriving from two different ports could logically signal an event that is independent from the actual port (and thus interrupt vector)
- where it happened. Sharing interrupt vector code can be accomplished using the ISR_ALIASOF() attribute to the ISR macro:
- */
- /*
- ISR(PCINT0_vect)
- {
- //...
- // Code to handle the event.
- }
- ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
- */
- /*
- Note
- There is no body to the aliased ISR.
- Note that the ISR_ALIASOF() feature requires GCC 4.2 or above (or a patched version of GCC 4.1.x).
- See the documentation of the ISR_ALIAS() macro for an implementation which is less elegant but could be applied to all compiler versions.
- */
- /*
- Empty interrupt service routines
- In rare circumstances, in interrupt vector does not need any code to be implemented at all. The vector must be declared anyway, so when the interrupt triggers it won't
- execute the BADISR_vect code (which by default restarts the application).
- This could for example be the case for interrupts that are solely enabled for the purpose of getting the controller out of sleep_mode().
- A handler for such an interrupt vector can be declared using the EMPTY_INTERRUPT() macro:
- */
- #define EMPTY_INTERRUPT ( vector )
- // Note There is no body to this macro.
- /*
- Manually defined ISRs
- In some circumstances, the compiler-generated prologue and epilogue of the ISR might not be optimal for the job, and a manually defined ISR could be considered particularly to speedup the interrupt handling.
- One solution to this could be to implement the entire ISR as manual assembly code in a separate (assembly) file. See Combining C and assembly source files for an example of how to implement it that way.
- Another solution is to still implement the ISR in C language but take over the compiler's job of generating the prologue and epilogue.
- This can be done using the ISR_NAKED attribute to the ISR() macro. Note that the compiler does not generate anything as prologue or epilogue,
- so the final reti() must be provided by the actual implementation. SREG must be manually saved if the ISR code modifies it, and the compiler-implied assumption
- of __zero_reg__ always being 0 could be wrong (e. g. when interrupting right after of a MUL instruction).
- ISR(TIMER1_OVF_vect, ISR_NAKED)
- {
- PORTB |= _BV(0); // results in SBI which does not affect SREG
- reti();
- }
- */
- /*
- Choosing the vector: Interrupt vector names
- The interrupt is chosen by supplying one of the symbols in following table.
- There are currently two different styles present for naming the vectors. One form uses names starting with SIG_, followed by a relatively verbose but arbitrarily chosen name describing the interrupt vector.
- This has been the only available style in avr-libc up to version 1.2.x.
- Starting with avr-libc version 1.4.0, a second style of interrupt vector names has been added, where a short phrase for the vector description is followed by _vect.
- The short phrase matches the vector name as described in the datasheet of the respective device (and in Atmel's XML files), with spaces replaced by an underscore and other non-alphanumeric characters dropped.
- Using the suffix _vect is intented to improve portability to other C compilers available for the AVR that use a similar naming convention.
- The historical naming style might become deprecated in a future release, so it is not recommended for new projects.
- Note
- being usable as an interrupt function, is not actually wired into the interrupt vector table. The compiler will generate a warning if it detects a suspiciously looking name of a ISR()
- function (i.e. one that after macro replacement does not start with "__vector_").
- Vector name Old vector name Description Applicable for device
- ADC_vect SIG_ADC ADC Conversion Complete AT90S2333, AT90S4433, AT90S4434, AT90S8535, AT90PWM216, AT90PWM2B, AT90PWM316, AT90PWM3B, AT90PWM3, AT90PWM2, AT90PWM1, AT90CAN128, AT90CAN32, AT90CAN64, ATmega103, ATmega128, ATmega1284P, ATmega16, ATmega163, ATmega165, ATmega165P, ATmega168P, ATmega169, ATmega169P, ATmega32, ATmega323, ATmega325, ATmega3250, ATmega3250P, ATmega328P, ATmega329, ATmega3290, ATmega3290P, ATmega48P, ATmega64, ATmega645, ATmega6450, ATmega649, ATmega6490, ATmega8, ATmega8535, ATmega88P, ATmega168, ATmega48, ATmega88, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega324P, ATmega164P, ATmega644P, ATmega644, ATtiny13, ATtiny15, ATtiny26, ATtiny43U, ATtiny48, ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85, ATtiny261, ATtiny461, ATtiny861, AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
- */
- char attributes;
-
- #define ISR ( vector, attributes )
- /*
- Introduces an interrupt handler function (interrupt service routine) that runs with global interrupts initially disabled by default with no attributes specified.
- The attributes are optional and alter the behaviour and resultant generated code of the interrupt routine. Multiple attributes may be used for a single function, with a space seperating each attribute.
- Valid attributes are ISR_BLOCK, ISR_NOBLOCK, ISR_NAKED and ISR_ALIASOF(vect).
- vector must be one of the interrupt vector names that are valid for the particular MCU type.
- */
- #define ISR_ALIAS ( vector, target_vector )
- /*
- Aliases a given vector to another one in the same manner as the ISR_ALIASOF attribute for the ISR() macro. Unlike the ISR_ALIASOF attribute macro however, this is compatible for all versions of GCC rather than just GCC version 4.2 onwards.
- Note
- This macro creates a trampoline function for the aliased macro. This will result in a two cycle penalty for the aliased vector compared to the ISR the vector is aliased to, due to the JMP/RJMP opcode used.
- Deprecated:
- For new code, the use of ISR(..., ISR_ALIASOF(...)) is recommended.
- Example:
- 1 ISR(INT0_vect)
- 2 {
- 3 PORTB = 42;
- 4 }
- 5
- 6 ISR_ALIAS(INT1_vect, INT0_vect);
- */
- #define ISR_ALIASOF ( target_vector )
- //The ISR is linked to another ISR, specified by the vect parameter. This is compatible with GCC 4.2 and greater only.
- //Use this attribute in the attributes parameter of the ISR macro.
- #define ISR_BLOCK
- /*
- Identical to an ISR with no attributes specified. Global interrupts are initially disabled by the AVR hardware when entering the ISR, without the compiler modifying this state.
- Use this attribute in the attributes parameter of the ISR macro.
- */
- #define ISR_NAKED
- /*
- ISR is created with no prologue or epilogue code. The user code is responsible for preservation of the machine state including the SREG register, as well as placing a reti() at the end of the interrupt routine.
- Use this attribute in the attributes parameter of the ISR macro.
- */
- #define ISR_NOBLOCK
- /*
- ISR runs with global interrupts initially enabled. The interrupt enable flag is activated by the compiler as early as possible within the ISR to ensure minimal processing delay for nested interrupts.
- This may be used to create nested ISRs, however care should be taken to avoid stack overflows, or to avoid infinitely entering the ISR for those cases where the AVR hardware does not clear the respective interrupt flag before entering the ISR.
- Use this attribute in the attributes parameter of the ISR macro.
- */
- #define reti ( )
- /*
- Returns from an interrupt routine, enabling global interrupts. This should be the last command executed before leaving an ISR defined with the ISR_NAKED attribute.
- This macro actually compiles into a single line of assembly, so there is no function call overhead.
- */
- #define sei ( )
- /*
- Enables interrupts by setting the global interrupt mask. This function actually compiles into a single line of assembly, so there is no function call overhead. However, the macro also implies a memory barrier which can cause additional loss of optimization.
- In order to implement atomic access to multi-byte objects, consider using the macros from <util/atomic.h>, rather than implementing them manually with cli() and sei().
- */
- #define SIGNAL ( vector )
- /*
- Introduces an interrupt handler function that runs with global interrupts initially disabled.
- This is the same as the ISR macro without optional attributes.
- Deprecated:
- Do not use SIGNAL() in new code. Use ISR() instead.
- */
- pinMode(ROTTARY_SW_sw, INPUT_PULLUP);
- pinMode(ROTTARY_SW_dt, INPUT_PULLUP);
- //Set PCIE0 to enable PCMSK0 scan.
- /*
- PCICR |= (1 << PCIE0);
- //Set PCINT0 (digital input 8) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT10); //Set PCINT0 (digital input 14) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT9); //Set PCINT0 (digital input 15) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT4); //Set PCINT0 (digital input 10) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT5); //Set PCINT0 (digital input 11) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT6); //Set PCINT0 (digital input 12) to trigger an interrupt on state change.
- PCMSK0 |= (1 << PCINT7); //Set PCINT0 (digital input 13) to trigger an interrupt on state change.
- */
- /*
- void loop ()
- {
- channel_1 = map(receiver_input_channel_1, fromLow, fromHigh, toLow, toHigh);
- channel_2 = map(receiver_input_channel_2, fromLow, fromHigh, toLow, toHigh);
- channel_3 = map(receiver_input_channel_3, fromLow, fromHigh, toLow, toHigh);
- channel_4 = map(receiver_input_channel_4, fromLow, fromHigh, toLow, toHigh);
- channel_5 = map(receiver_input_channel_5, fromLow, fromHigh, toLow, toHigh);
- channel_6 = map(receiver_input_channel_6, fromLow, fromHigh, toLow, toHigh);
- // debug
- Serial.print("Channel 1 = ");
- Serial.print(channel_1);
- Serial.print('\t');
- Serial.print("Channel 2 = ");
- Serial.print(channel_2);
- Serial.print('\t');
- Serial.print("Channel 3 = ");
- Serial.print(channel_3);
- Serial.print('\t');
- Serial.print("Channel 4 = ");
- Serial.print(channel_4);
- Serial.print('\t');
- Serial.print("Channel 5 = ");
- Serial.print(channel_5);
- Serial.print('\t');
- Serial.print("Channel 6 = ");
- Serial.print(channel_6);
- Serial.print('\n');
- }
- */
- /*
- ISR(PCINT0_vect)
- {
- current_time = micros();
- //Channel 1=========================================
- if(PINB & B00000001)
- { //Is input 8 high?
- if(last_channel_1 == 0)
- { //Input 8 changed from 0 to 1
- last_channel_1 = 1; //Remember current input state
- timer_1 = current_time; //Set timer_1 to current_time
- }
- }
- else if(last_channel_1 == 1)
- { //Input 8 is not high and changed from 1 to 0
- last_channel_1 = 0; //Remember current input state
- receiver_input_channel_1 = current_time - timer_1; //Channel 1 is current_time - timer_1
- }
- //Channel 2=========================================
- if(PINB & B00000010 )
- { //Is input 9 high?
- if(last_channel_2 == 0)
- { //Input 9 changed from 0 to 1
- last_channel_2 = 1; //Remember current input state
- timer_2 = current_time; //Set timer_2 to current_time
- }
- }
- else if(last_channel_2 == 1)
- { //Input 9 is not high and changed from 1 to 0
- last_channel_2 = 0; //Remember current input state
- receiver_input_channel_2 = current_time - timer_2; //Channel 2 is current_time - timer_2
- }
- //Channel 3=========================================
- if(PINB & B00000100 )
- { //Is input 10 high?
- if(last_channel_3 == 0)
- { //Input 10 changed from 0 to 1
- last_channel_3 = 1; //Remember current input state
- timer_3 = current_time; //Set timer_3 to current_time
- }
- }
- else if(last_channel_3 == 1)
- { //Input 10 is not high and changed from 1 to 0
- last_channel_3 = 0; //Remember current input state
- receiver_input_channel_3 = current_time - timer_3; //Channel 3 is current_time - timer_3
- }
- //Channel 4=========================================
- if(PINB & B00001000 )
- { //Is input 11 high?
- if(last_channel_4 == 0)
- { //Input 11 changed from 0 to 1
- last_channel_4 = 1; //Remember current input state
- timer_4 = current_time; //Set timer_4 to current_time
- }
- }
- else if(last_channel_4 == 1)
- { //Input 11 is not high and changed from 1 to 0
- last_channel_4 = 0; //Remember current input state
- receiver_input_channel_4 = current_time - timer_4; //Channel 4 is current_time - timer_4
- }
- //Channel 5=========================================
- if(PINB & B00010000)
- {
- if(last_channel_5 == 0)
- {
- last_channel_5 = 1;
- timer_5 = current_time;
- }
- }
- else if(last_channel_5 == 1)
- {
- last_channel_5 = 0;
- receiver_input_channel_5 = current_time - timer_5;
- }
- //Channel 6=========================================
- if(PINB & B00100000)
- {
- if(last_channel_6 == 0)
- {
- last_channel_6 = 1;
- timer_6 = current_time;
- }
- }
- else if(last_channel_6 == 1)
- {
- last_channel_6 = 0;
- receiver_input_channel_6 = current_time - timer_6;
- }
- }
-
- */
-
|