Lesson 20--Microduino "Use Interrupt"

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

Understand the interruption, and through the button to learn the usage of the interrupt.

Interrupt

The basic principle of interruption

CPU is processing one event and interrupted by external another event, after when performing the external things, continue to do previous event. For example: the teacher in class, and a student enter the classroom suddenly interrupted the teacher, the teacher allow him into the classroom, and go on teaching, this is the interrupt.

Interrupt concept

Interrupt that menas the CPU in the normal operation of the program, due to internal/external events or by program pre-arranged event which cause the CPU stop running applications, and transfer to another service for internal/external events or service for a pre-arranged event program. After finished that service, return to execute the temporarily interrupted program.

Due to the occurrence of an event, the CPU suspended execution of a program, and then turn to process incoming event's program. After complete this program, then CPU recover the suspended program. This process is called the interrupt.

Interrupt classification

  • The location of the interrupt source
    • The interrupt source in the interior of the CPU, known as internal interrupt.
    • The interrupt source in the outside of the CPU, known as external interrupt.
  • According to interrupt pin or CPU interrupt response of different conditions: Maskable interrupt and nonmaskable interrupt.

Interrupt benefits

  • Improve the efficiency of the CPU by time sharing operation. Only when the service object sent a interrupt reques to CPU, then CPU will process it. So that we can use the interrupt function to support multiple objects at the same time, thus greatly improving the efficiency of the CPU.
  • Use the interrupt to achieve real-time processing. The service object can send the CPU interrupt at any time, CPU can find and process interrupt request immediately.

Equipment

Microduino-Core Microduino-FT232R

  • Other hardware equipment
    • Breadboard Jumper one box
    • Breadboard one piece
    • LED one
    • 220Ω resistor one
    • USB Data cable one
    • Button one

Experimental schematic

  • Microduino core: two external interrupt,that are D2 and D3;
  • Microduino core+: three external interrupt, that are D2, D3 and D6.
Lesson20-schematic.jpg

Program

  • Don't use the external interrupt
int button= 2;          // Define the input pin
int pin = 13;
volatile int state = LOW;
 
void setup()
{                
  //Set the input pin as input state and output pin as output state
  pinMode(button,INPUT); 
  pinMode(pin,OUTPUT);
}
 
void loop()                     
{
  state = digitalRead(button);      //Read the button's state
 
  digitalWrite(ledOut, state);    //Set the state value to LED
 
  //Simulate a long time process or a complex task
  for (int i = 0; i < 100; i++)
  {
    delay(10); 
  }
}
  • Use the external interrupt
int pin = 13;
volatile int state = LOW;

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

void loop()
{
  digitalWrite(pin, state);//Set the state value to LED
  //Simulate a long time process or a complex task
  for (int i = 0; i < 100; i++)
  {
    delay(10); 
  }
}

void blink()
{
  state = !state;
}

Result

If don't use the interrupt, push down the button, wait for a moment LED state will changed, otherwise use the interrupt LED state changes immediately.

Interrupt grammar

  • Grammar:
    • attachInterrupt(interrupt, function, mode)
  • Parameter:
    • interrupt:interrput pin number
    • function:when interrup happens, this function will be invoked to replace the running program. No parameter and no need return value for this function. It always called as Interrupt service routine(ISR).
    • mode:Define when the interrupt happened, has four contstants values:
      • LOW: When the pin is low electricity at ordinary times, raise the interrupt
      • CHANGE: When the pin signal changed, raise the interrupt
      • RISING :When the pin changed from low level to high level at ordinary times, raise the interrupt
      • FALLING :When the pin changed from high level to low level at ordinary times, raise the interrupt

Redistribute the interrupt

Interrupt setting can be changed by function attachInterrupt() at any time. If use function attachInterrupt() to redistribute,previously assigned interrupt will be removed from the corresponding pin.

Disable\enable interrupt

microduino can ignore all interrupt. If you don't want to handle the interrupt in special code segment, you can use the noInterrupts() to disable interrupt. After this code segment, you can use interrupts() to enable the interrupt.

Delete the interrupt

You also can delete the interrupt by detachInterrupt(interrupt_number) in terminical.

Note

When the interrupt function occurs, delya () and millis () values will not continue to change.When an interrupt occurs, the serial port receives the data may be lost.You should declare a variable to store variable when not interrupted.