1、定义
    由于某种事件的发生,处理器暂停执行当前的程序,转而执行另一程序,以处理发生的事件,处理完毕后又返回原程序继续作业。
    2、中断优先级
    中断响应原则:
    ①首先响应高优先级的中断请求;
    ②如果优先级相同,按查询次序响应排在前面的中断;
    ③正在进行的中断过程不能被新的同级或低优先级的中断请求所中断;
    ④正在进行的低优先级中断过程,能被高优先级中断请求所中断。
    3、中断的作用:
    处理突发事件;安排事件处理的优先级。
    注:

    1. 中断过程中delay()不起作用,millis函数不包括中断时间,Arduino开发板接收到的串口数据可能丢失;
    2. 中断服务程序不能返回任何数值。所以应尽量在中断服务程序中使用全局变量;
    3. 中断服务程序中涉及的变量应声明为volatile类型。

    4、中断类型:
    (1)定时器中断:设定一个间隔时间,单片机定时器每个相同的间隔时间会产生一次定时器中断,单片机处理器接收到定时器中断信号后执行相应的中断处理程序;“间隔固定时间中断”

    1. #include<TimerOne.h>
    2. Timer1.initialize(interval) //interval是时间间隔,整数,以微秒为单位
    3. Timer1.attachInterrupt(ISR) //附上中断处理程序
    4. void ISR() //ISR是中断处理程序,空函数,无参数无返回值

    注:
    ①使用了TimerOne库,则板子上的 pin 9 和 pin 10 就不能再用做 PWM 输出;
    ②Servo库不能和TimerOne库一起使用;
    ③使用 TimerOne 库只能设定一件要定时做的事, 后面的 attachInterrupt( ) 会盖掉前面的 attachInterrupt( )。
    (2)外部中断:单片机外部中断引脚的电平的特定变化产生的中断事件;“让它中断就中断”
    中断配置函数: attachInterrupt(channel,ISR,mode)
    参数:
    channel:中断通道号,Ardunio UNO中通道编号0/1分别对应板子上引脚号2/3
    ISR:用户自定义的中断处理程序,空函数
    mode:中断触发模式
    LOW: 当引脚为低电平时触发中断服务程序
    CHANGE: 当引脚电平发生变化时触发中断服务程序
    RISING: 当引脚电平由低电平变为高电平时触发中断服务程序
    FALLING: 当引脚电平由高电平变为低电平时触发中断服务程序

    1. #include <TimerOne.h>
    2. const long interval = 2000000; //2s
    3. const int R=7,B=6,G=5;
    4. int counter=0;
    5. void setup()
    6. {
    7. pinMode(R,OUTPUT);
    8. pinMode(B,OUTPUT);
    9. pinMode(G,OUTPUT);
    10. digitalWrite(R,LOW);
    11. digitalWrite(B,LOW);
    12. digitalWrite(G,LOW);
    13. Timer1.initialize( interval ); // 初始化,每2秒产生一次定时器中断
    14. Timer1.attachInterrupt( Isr );
    15. }
    16. void Isr( )
    17. {
    18. if(counter==0)
    19. {
    20. digitalWrite(R,HIGH);
    21. digitalWrite(G,LOW);
    22. digitalWrite(B,LOW);
    23. counter++;
    24. }
    25. else if(counter==1)
    26. {
    27. digitalWrite(R,LOW);
    28. digitalWrite(G,HIGH);
    29. digitalWrite(B,LOW);
    30. counter++;
    31. }
    32. else
    33. {
    34. digitalWrite(R,LOW);
    35. digitalWrite(G,LOW);
    36. digitalWrite(B,HIGH);
    37. counter=0;
    38. }
    39. }
    40. void loop()
    41. {
    42. }
    1. const int B=7;
    2. const int interruptPin=2; //外部中断引脚
    3. int counter=0;
    4. int flag=0;
    5. void setup()
    6. {
    7. pinMode(B, OUTPUT);
    8. pinMode(interruptPin, INPUT_PULLUP);
    9. digitalWrite(B,HIGH);
    10. attachInterrupt(0, Isr, FALLING);//当int.0电平由高变低时,触发中断函数Isr
    11. }
    12. void loop()
    13. {
    14. if(flag==0)
    15. {
    16. flag=1;
    17. delay(1000);
    18. digitalWrite(B,HIGH);
    19. }
    20. else
    21. digitalWrite(B,HIGH);
    22. }
    23. void Isr()//中断函数
    24. {
    25. digitalWrite(B,LOW);
    26. flag=0;
    27. }
    1. while(digitalRead(SW)==1&&analogRead(Ry)<800){ //调Time
    2. if(analogRead(Rx)>800){
    3. if(Time<9){
    4. Time++;
    5. SetTimer();
    6. }
    7. else{
    8. Time=0;
    9. SetTimer();
    10. }
    11. }
    12. else if(analogRead(Ry)<200){
    13. if(Time>0){
    14. Time--;
    15. SetTimer();
    16. }
    17. else{
    18. Time=9;
    19. SetTimer();
    20. }
    21. }
    22. } //调Time结束
    1. #include <TimerOne.h>
    2. #include <LiquidCrystal.h>
    3. const int Rx=0,Ry=1,SW=3;
    4. const int rs=13,en=12,d4=11,d5=8,d6=7,d7=4;
    5. int flag=0,state=2,Time=0,count=0;
    6. const long interval=10000;//10ms
    7. LiquidCrystal LCD(rs, en, d4, d5, d6, d7);
    8. void wait(){
    9. LCD.print("Wait...");
    10. delay(1000);
    11. LCD.clear();
    12. }
    13. void SetTimer(){
    14. LCD.setCursor(0,0);
    15. LCD.print("Timer");
    16. LCD.setCursor(0,1);
    17. LCD.print(Time);
    18. }
    19. void setup() {
    20. LCD.begin(16,2);
    21. LCD.clear();
    22. LCD.setCursor(0,0);
    23. wait();
    24. pinMode(SW,INPUT_PULLUP);
    25. attachInterrupt(1, Isr, FALLING);//当int.0电平由高变低时,触发中断函数Isr
    26. }
    27. void loop() {
    28. if(state==1){
    29. wait();
    30. state=2;
    31. return;
    32. }
    33. if(state==2){
    34. SetTimer();
    35. //调Timer时间
    36. while(digitalRead(SW)==1&&analogRead(Ry)<800){ //调Time
    37. if(flag==0){
    38. flag=1;
    39. if(analogRead(Rx)>800){
    40. if(Time<9){
    41. Time++;
    42. SetTimer();
    43. }
    44. else{
    45. Time=0;
    46. SetTimer();
    47. flag=0;
    48. }
    49. }
    50. }
    51. else if(analogRead(Ry)<200){
    52. if(Time>0){
    53. Time--;
    54. SetTimer();
    55. }
    56. else{
    57. Time=9;
    58. SetTimer();
    59. }
    60. }
    61. } //调Time结束
    62. count=Time*1000;
    63. state=3;
    64. return;
    65. }
    66. if(state==3){
    67. Timer1.initialize( interval ); // 初始化,每10ms产生一次定时器中断
    68. if(count>0){
    69. Timer1.attachInterrupt( Isrtime );
    70. }
    71. else{
    72. state=4;
    73. return;
    74. }
    75. }
    76. if(state==4){
    77. LCD.clear();
    78. LCD.setCursor(0,0);
    79. LCD.print("Beep");
    80. delay(100);
    81. if(SW==0){
    82. state=1;
    83. return;
    84. }
    85. }
    86. }
    87. void Isr(){
    88. wait();
    89. state=2;
    90. }
    91. void Isrtime(){
    92. attachInterrupt(Rx, Isr2, RISING);
    93. if(count>0){
    94. LCD.setCursor(0,0);
    95. LCD.print("Timer");
    96. LCD.setCursor(0,1);
    97. LCD.print(count);
    98. count=count-10;
    99. }
    100. else{
    101. return;
    102. }
    103. }
    104. void Isr1(){
    105. }
    106. void Isr2(){
    107. LCD.setCursor(0,0);
    108. LCD.print("Timer");
    109. LCD.setCursor(0,1);
    110. LCD.print(count);
    111. if(analogRead(Rx)<300){
    112. Timer1.detachInterrupt();
    113. }
    114. }
    1. #include <TimerOne.h>
    2. #include <LiquidCrystal.h>
    3. const int Rx=0,Ry=1,SW=3;
    4. const int rs=13,en=12,d4=11,d5=8,d6=7,d7=4;
    5. int flag1=0,flag2=0,state=2,Time=0,count=0;
    6. const long interval=10000;//10ms
    7. LiquidCrystal LCD(rs, en, d4, d5, d6, d7);
    8. void wait(){
    9. LCD.print("Wait...");
    10. delay(1000);
    11. LCD.clear();
    12. }
    13. void SetTimer(){
    14. LCD.setCursor(0,0);
    15. LCD.print("Timer");
    16. LCD.setCursor(0,1);
    17. LCD.print(Time);
    18. }
    19. void setup() {
    20. Serial.begin(9600);
    21. LCD.begin(16,2);
    22. LCD.clear();
    23. LCD.setCursor(0,0);
    24. wait();
    25. pinMode(SW,INPUT_PULLUP);
    26. attachInterrupt(1, Isr, FALLING);//当int.0电平由高变低时,触发中断函数Isr
    27. }
    28. void loop() {
    29. if(state==1){
    30. wait();
    31. state=2;
    32. return;
    33. }
    34. else if(state==2){
    35. SetTimer();
    36. //调Timer时间
    37. if(analogRead(Ry)<800){ //调Time
    38. if(analogRead(Rx)>800){
    39. if(Time<9){
    40. Time++;
    41. SetTimer();
    42. }
    43. else{
    44. Time=0;
    45. SetTimer();
    46. }
    47. delay(500);
    48. Serial.println(Time);
    49. Serial.println(count);
    50. Serial.println(state);
    51. }
    52. else if(analogRead(Rx)<300){
    53. if(Time>0){
    54. Time--;
    55. SetTimer();
    56. }
    57. else{
    58. Time=9;
    59. SetTimer();
    60. }
    61. delay(500);
    62. Serial.println(Time);
    63. Serial.println(count);
    64. Serial.println(state);
    65. }
    66. return;
    67. }
    68. else{
    69. state=3;
    70. count=Time*1000;
    71. Serial.println(Time);
    72. Serial.println(count);
    73. Serial.println(state);
    74. return;
    75. }
    76. }
    77. else if(state==3){
    78. Serial.println(state);
    79. Timer1.initialize( interval ); // 初始化,每10ms产生一次定时器中断
    80. Serial.println(state);
    81. if(count>0){
    82. Timer1.attachInterrupt( Isrtime );
    83. }
    84. else{
    85. state=4;
    86. return;
    87. }
    88. }
    89. else if(state==4){
    90. LCD.clear();
    91. LCD.setCursor(0,0);
    92. LCD.print("Beep");
    93. delay(100);
    94. if(SW==0){
    95. state=1;
    96. return;
    97. }
    98. }
    99. }
    100. void Isr(){
    101. wait();
    102. state=2;
    103. }
    104. void Isrtime(){
    105. if(count>0&&SW==1){
    106. if(flag2==0){
    107. if(analogRead(Rx)<800&&analogRead(Rx)>400){
    108. LCD.setCursor(0,0);
    109. LCD.print("Timer");
    110. LCD.setCursor(0,1);
    111. LCD.print(count);
    112. count=count-10;
    113. }
    114. if(analogRead(Rx)>600){
    115. flag2=1;
    116. LCD.setCursor(0,0);
    117. LCD.print("Timer");
    118. LCD.setCursor(0,1);
    119. LCD.print(count);
    120. }
    121. }
    122. if(analogRead(Rx)<400){
    123. flag2=0;
    124. }
    125. }
    126. else if(SW==0){
    127. state=1;
    128. return;
    129. }
    130. else{
    131. return;
    132. }
    133. }
    1. else if(state==2){
    2. SetTimer();
    3. //调Timer时间
    4. if(analogRead(Ry)<800){ //调Time
    5. if(analogRead(Rx)>800){
    6. if(Time<9){
    7. Time++;
    8. SetTimer();
    9. }
    10. else{
    11. Time=0;
    12. SetTimer();
    13. }
    14. delay(500);
    15. Serial.println(Time);
    16. Serial.println(count);
    17. Serial.println(state);
    18. }
    19. else if(analogRead(Rx)<300){
    20. if(Time>0){
    21. Time--;
    22. SetTimer();
    23. }
    24. else{
    25. Time=9;
    26. SetTimer();
    27. }
    28. delay(500);
    29. Serial.println(Time);
    30. Serial.println(count);
    31. Serial.println(state);
    32. }
    33. return;
    34. }
    35. else{
    36. state=3;
    37. count=Time*1000;
    38. Serial.println(Time);
    39. Serial.println(count);
    40. Serial.println(state);
    41. return;
    42. }
    43. }
    1. void Isrtime(){
    2. if(count>0){
    3. if(flag==0){
    4. if(analogRead(Rx)<800&&analogRead(Rx)>400){
    5. LCD.clear();
    6. LCD.setCursor(0,0);
    7. LCD.print("Timer");
    8. LCD.setCursor(0,1);
    9. LCD.print(count);
    10. count=count-10;
    11. }
    12. else if(analogRead(Rx)>800){
    13. flag=1;
    14. LCD.clear();
    15. LCD.setCursor(0,0);
    16. LCD.print("Timer");
    17. LCD.setCursor(0,1);
    18. LCD.print(count);
    19. }
    20. }
    21. else if(analogRead(Rx)<400){
    22. flag=0;
    23. }
    24. }
    25. else{
    26. return;
    27. }
    28. }
    1. void Isrtime(){
    2. if(count>0){
    3. LCD.setCursor(0,0);
    4. LCD.print("Timer");
    5. LCD.setCursor(0,1);
    6. LCD.print(count);
    7. count=count-10;
    8. }
    9. else{
    10. return;
    11. }
    12. }
    1. #include <TimerOne.h>
    2. #include <LiquidCrystal.h>
    3. const int Rx=0,Ry=1,SW=3;
    4. const int rs=13,en=12,d4=11,d5=8,d6=7,d7=4;
    5. int flag=0,state=2,Time=0,count=0;
    6. const long interval=10000;//10ms
    7. LiquidCrystal LCD(rs, en, d4, d5, d6, d7);
    8. void wait(){
    9. LCD.print("Wait...");
    10. delay(1000);
    11. LCD.clear();
    12. }
    13. void SetTimer(){
    14. LCD.setCursor(0,0);
    15. LCD.print("Timer");
    16. LCD.setCursor(0,1);
    17. LCD.print(Time);
    18. }
    19. void Isr(){
    20. wait();
    21. state=2;
    22. }
    23. void setup() {
    24. Serial.begin(9600);
    25. LCD.begin(16,2);
    26. LCD.clear();
    27. LCD.setCursor(0,0);
    28. wait();
    29. pinMode(SW,INPUT_PULLUP);
    30. attachInterrupt(1,Isr,FALLING);//当int.0电平由高变低时,触发中断函数Isr
    31. }
    32. void loop() {
    33. if(state==1){
    34. wait();
    35. state=3;
    36. return;
    37. }
    38. else if(state==2){
    39. SetTimer();
    40. //调Timer时间
    41. if(analogRead(Ry)<800){ //调Time
    42. if(analogRead(Rx)>800){
    43. if(Time<9){
    44. Time++;
    45. SetTimer();
    46. }
    47. else{
    48. Time=0;
    49. SetTimer();
    50. }
    51. delay(500);
    52. Serial.println(Time);
    53. Serial.println(count);
    54. Serial.println(state);
    55. }
    56. else if(analogRead(Rx)<300){
    57. if(Time>0){
    58. Time--;
    59. SetTimer();
    60. }
    61. else{
    62. Time=9;
    63. SetTimer();
    64. }
    65. delay(500);
    66. Serial.println(Time);
    67. Serial.println(count);
    68. Serial.println(state);
    69. }
    70. return;
    71. }
    72. else{
    73. state=3;
    74. count=Time*1000;
    75. Serial.println(Time);
    76. Serial.println(count);
    77. Serial.println(state);
    78. return;
    79. }
    80. }
    81. else if(state==3){
    82. Timer1.initialize( interval ); // 初始化,每10ms产生一次定时器中断
    83. if(count>0){
    84. Timer1.attachInterrupt( Isrtime );
    85. }
    86. else{
    87. state=4;
    88. return;
    89. }
    90. }
    91. else if(state==4){
    92. LCD.clear();
    93. LCD.setCursor(0,0);
    94. LCD.print("Beep");
    95. delay(100);
    96. if(SW==0){
    97. state=1;
    98. return;
    99. }
    100. }
    101. }
    102. void Isrtime(){
    103. if(count>0){
    104. if(flag==0){
    105. if(analogRead(Rx)<800&&analogRead(Rx)>400){
    106. LCD.clear();
    107. LCD.setCursor(0,0);
    108. LCD.print("Timer");
    109. LCD.setCursor(0,1);
    110. LCD.print(count);
    111. count=count-10;
    112. }
    113. else if(analogRead(Rx)>800){
    114. flag=1;
    115. LCD.clear();
    116. LCD.setCursor(0,0);
    117. LCD.print("Timer");
    118. LCD.setCursor(0,1);
    119. LCD.print(count);
    120. }
    121. }
    122. else if(analogRead(Rx)<400){
    123. flag=0;
    124. }
    125. }
    126. else{
    127. return;
    128. }
    129. }