Reference.AttachInterrupt History

Hide minor edits - Show changes to markup

June 01, 2010, at 03:44 AM by Equipo Traduccion -
Changed lines 39-40 from:

volatile int state = LOW;

to:

volatile int estado = LOW;

Changed line 44 from:
  attachInterrupt(0, blink, CHANGE);
to:
  attachInterrupt(0, parpadeo, CHANGE);
Changed line 49 from:
  digitalWrite(pin, state);
to:
  digitalWrite(pin, estado);
Changed line 52 from:

void blink()

to:

void parpadeo()

Changed line 54 from:
  state = !state;
to:
  estado = !estado;
Changed lines 58-59 from:

See also

to:

Ver también

June 01, 2010, at 03:42 AM by Equipo Traduccion -
Changed lines 1-33 from:

attachInterrupt(interrupt, function, mode)

Description

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).

Parameters

interrupt: the number of the interrupt (int)

function: the function 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 contstants 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.

Returns

none

Note

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

Using Interrupts

Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. A good task for using an interrupt might be reading a rotary encoder, monitoring user input.

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the doorbell.

Example

to:

attachInterrupt(interrupcion, funcion, modo)

Descripción

Especifica la función a la que invocar cuando se produce una interrupción externa. Reemplaza cualquier función previa que estuviera enlazada a la interrupción. La mayoría de las placas Arduino tienen dos interrupciones externas: Las número 0 (en el pin digital 2) y la 1 (en el pin digital 3). La Arduino Mega tiene otras cuatro: Las número 2 (pin 21), 3 (pin 20), 4 (pin 19) y 5 (pin 18).

Parámetros

interrupcion: el número de la interrupción (int)

funcion: la función a la que invocar cuando la interrupción tiene lugar; esta función no debe tener parámetros ni devolver nada. Esta función es a veces referenciada como rutina de interrupción de servicio

modo define cuando la interrupción debe ser disparada. Hay cuatro constantes predefinidas como valores válidos:

  • LOW para disparar la interrupción en cualquier momento que el pin se encuentre a valor bajo(LOW).

  • CHANGE para disparar la interrupción en culquier momento que el pin cambie de valor.

  • RISING para disparar la interrupción cuando el pin pase de valos alto (HIGH) a bajo (LOW).

  • FALLING para cuando el pin cambie de valor alto (HIGH) a bajo (LOW)

Retorno

Ninguno

Nota

Dentro de la función enlazada, la función delay() no funciona y el valor devuelto por la función millis() no se incrementará. Los datos serie recibidos en el transcurso de esta interrupción pueden perderse. No deberías declarar como volátil cualquier variable que modifiques dentro de la función.

Usando las interrupciones

Las interrupciones son útiles para hacer que las cosas sucedan automáticamente en programas para microcontroladores, y puedan ayudar a resolver problemas de temporización. Una buena tarea en la que utilizar interrupciones podría ser leer un encoder rotacional, monitorizando la entrada del usuario.

Si quisieras asegurarte de que un programa siempre captura los pulsos de un encoder rotacional, sin perder nunca un pulso, sería muy complicado escribir un programa que haga otra cosa, puesto que el programa debería estar constantemente consultando las líneas del sensor del encoder, de forma que capture los pulsos cuando tienen lugar. Otros sensores tienen un interfaz dinámico similar, como intentar leer un sensor de sonido que intenta capturar un click, o un sensor de ranuras por infrarrojos (fotointerruptor) tratando de capturar el paso de una moneda. En todas estas situaciones, usar una interrupción, libera al microcontrolador de realizar otras tareas sin echar en falta el "timbre".

Ejemplo

May 27, 2009, at 02:38 PM by David A. Mellis -
Changed lines 6-7 from:

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).

to:

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).

May 27, 2009, at 02:37 PM by David A. Mellis -
Changed lines 6-7 from:

Specifies a function to call when external interrupt 0 or 1 occurs, on digital pin 2 or 3, respectively. Replaces any previous function that was attached to the interrupt.

to:

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).

Changed lines 10-11 from:

interrupt: the number of the interrupt (int): 0 or 1.

to:

interrupt: the number of the interrupt (int)

August 01, 2008, at 10:17 PM by David A. Mellis -
Changed lines 6-9 from:

Specifies a function to call when external interrupt 0 or 1 occurs, on digital pin 2 or 3, respectively.

Note: Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

to:

Specifies a function to call when external interrupt 0 or 1 occurs, on digital pin 2 or 3, respectively. Replaces any previous function that was attached to the interrupt.

Changed lines 10-14 from:

interrupt:: the number of the interrupt (int): 0 or 1.

function:: the function 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 contstants are predefined as valid values:

to:

interrupt: the number of the interrupt (int): 0 or 1.

function: the function 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 contstants are predefined as valid values:

Added lines 24-26:

Note

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

May 12, 2008, at 06:43 PM by David A. Mellis -
Changed lines 6-12 from:

attchInterrupt enables interrupts 0 or 1, on digital pins 2 or 3 respectively. It also specifies the function to call when an external interrupt occurs.

Note: millis() and delay() won't work in your interrupt-handling function. Any serial data received while in your interrupt-handling function will be lost.

You also should declare as volatile any variables that you modify within your interrupt handling function (see the example).''

to:

Specifies a function to call when external interrupt 0 or 1 occurs, on digital pin 2 or 3, respectively.

Note: Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

Changed lines 12-13 from:

interrupt:: the number of the interrupt (int), with a valid value of 0 or 1.

to:

interrupt:: the number of the interrupt (int): 0 or 1.

Deleted lines 30-31:

Interrupts can be used with internal hardware too, such as timers, but as of Arduino 008, this requires AVR code and is not supported in the Arduino core.

July 27, 2007, at 04:20 AM by Paul Badger -
Changed lines 32-33 from:

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor which it trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the doorbell.

to:

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the doorbell.

June 17, 2007, at 05:33 AM by Paul Badger -
Changed lines 6-8 from:

attchInterrupt enables interrupt 0 or 1, on digital pins 2 and 3 respectively. It also specifies the function to call when an external interrupt occurs.

to:

attchInterrupt enables interrupts 0 or 1, on digital pins 2 or 3 respectively. It also specifies the function to call when an external interrupt occurs.

June 17, 2007, at 05:31 AM by Paul Badger -
Changed lines 32-37 from:

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly "listen" for the encoder, in order to catch pulses when they occured.

to:

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor which it trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the doorbell.

Interrupts can be used with internal hardware too, such as timers, but as of Arduino 008, this requires AVR code and is not supported in the Arduino core.

June 13, 2007, at 05:37 AM by Paul Badger -
Changed lines 17-18 from:

function:: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as the interrupt service routine.

to:

function:: the function 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.

June 13, 2007, at 05:36 AM by Paul Badger -
Changed line 19 from:

mode: when the interrupt should be triggered. Four contstants are predefined as valid values:

to:

mode: defines when the interrupt should be triggered. Four contstants are predefined as valid values:

June 13, 2007, at 05:35 AM by Paul Badger -
Changed line 19 from:

mode: when the interrupt should be triggered:

to:

mode: when the interrupt should be triggered. Four contstants are predefined as valid values:

June 13, 2007, at 05:32 AM by Paul Badger -
Changed lines 15-18 from:

interrupt: the number of the interrupt (int), with a valid value of 0 or 1.

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as the interrupt service routine.

to:

interrupt:: the number of the interrupt (int), with a valid value of 0 or 1.

function:: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as the interrupt service routine.

Added lines 29-37:

Using Interrupts

Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. A good task for using an interrupt might be reading a rotary encoder, monitoring user input.

If you wanted to insure that a program always caught the pulses from a rotary encoder, never missing a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly "listen" for the encoder, in order to catch pulses when they occured.

June 13, 2007, at 05:23 AM by Paul Badger -
Changed lines 15-19 from:

interrupt: the number of the interrupt (int), with a valid value of 0 or 1.

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing.

mode: when the interrupt should be triggered:

to:

interrupt: the number of the interrupt (int), with a valid value of 0 or 1.

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as the interrupt service routine.

mode: when the interrupt should be triggered:

June 13, 2007, at 05:21 AM by Paul Badger -
Changed lines 15-16 from:

interrupt: the number of the interrupt (int)

to:

interrupt: the number of the interrupt (int), with a valid value of 0 or 1.

Changed lines 19-20 from:

mode: when the interrupt should be triggered: 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, and FALLING for when the pin goes from high to low.

to:

mode: when the interrupt should be triggered:

  • 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.
June 13, 2007, at 05:19 AM by Paul Badger -
Changed lines 6-7 from:

Specifies the function to call when an external interrupt occurs. There are two external interrupts (0 and 1), on digital pins 2 and 3 respectively.

to:

attchInterrupt enables interrupt 0 or 1, on digital pins 2 and 3 respectively. It also specifies the function to call when an external interrupt occurs.

Changed lines 11-12 from:

Note: you may need to declare as volatile any variables that you modify within your interrupt handling function (see the example).

to:

You also should declare as volatile any variables that you modify within your interrupt handling function (see the example).''

May 03, 2007, at 07:31 AM by David A. Mellis -
Added lines 10-11:

Note: you may need to declare as volatile any variables that you modify within your interrupt handling function (see the example).

Changed lines 27-28 from:

int state = LOW;

to:

volatile int state = LOW;

Added line 37:
  digitalWrite(pin, state);
Deleted line 42:
  digitalWrite(pin, state);
March 12, 2007, at 08:03 AM by David A. Mellis -
Changed lines 8-9 from:

Note: millis() and delay() won't work in your interrupt-handling function. Any serial data received while in your interrupt-handling function will be lost.

to:

Note: millis() and delay() won't work in your interrupt-handling function. Any serial data received while in your interrupt-handling function will be lost.

March 12, 2007, at 07:53 AM by David A. Mellis -
Added lines 8-9:

Note: millis() and delay() won't work in your interrupt-handling function. Any serial data received while in your interrupt-handling function will be lost.

December 02, 2006, at 05:09 PM by David A. Mellis -
Changed line 22 from:

[@ int pin = 13;

to:

[@int pin = 13;

December 02, 2006, at 05:09 PM by David A. Mellis -
Changed lines 22-23 from:
 [@

int pin = 13;

to:

[@ int pin = 13;

December 02, 2006, at 05:08 PM by David A. Mellis -
Added lines 22-42:
 
int pin = 13;
int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}

void loop()
{
}

void blink()
{
  state = !state;
  digitalWrite(pin, state);
}
December 02, 2006, at 05:05 PM by David A. Mellis -
Added lines 1-24:

attachInterrupt(interrupt, function, mode)

Description

Specifies the function to call when an external interrupt occurs. There are two external interrupts (0 and 1), on digital pins 2 and 3 respectively.

Parameters

interrupt: the number of the interrupt (int)

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing.

mode: when the interrupt should be triggered: 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, and FALLING for when the pin goes from high to low.

Returns

none

Example

See also

Share