Advertisement
RuiViana

ContaSeed.ino

Oct 7th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. // Este sketch usa a facilidade de pinChage interrupt de todos ports do arduino
  2. // Copiado parcialmente de http://playground.arduino.cc/Main/PinChangeInterrupt
  3.  
  4. byte minhaMatriz[12];                                           // Matriz pra guardar os valores lidos do port apos um interrupt
  5. unsigned long countMatriz[12];                                  // Matriz pra acumular os valores lidos do port apos um interrupt
  6.  
  7. bool count = LOW;                                                // Controle para imprimir só se houve alteração do port
  8. //----------------------------------------------
  9. void pciSetup(byte pin)                                         // Definição de para qual port deve ser definido o interrupt
  10. {
  11.   *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin));   // enable pin
  12.   PCIFR  |= bit (digitalPinToPCICRbit(pin));                    // clear any outstanding interrupt
  13.   PCICR  |= bit (digitalPinToPCICRbit(pin));                    // enable interrupt for the group
  14. }
  15. //----------------------------------------------
  16. ISR (PCINT0_vect)                                               // handle pin change interrupt for D8 to D13 here
  17. {
  18.   for (int i = 2; i <= 12; i++)                                 // Se houve interrupt dos ports de 8 a 12
  19.   {
  20.     minhaMatriz[i] = digitalRead(i);                            // Salva valores dos ports na matriz
  21.     count = HIGH;                                                // Habilita impressao
  22.   }
  23. }
  24. //----------------------------------------------
  25. ISR (PCINT2_vect)                                               // handle pin change interrupt for D0 to D7 here
  26. {
  27.   for (int i = 2; i <= 12; i++)                                  // Se houve interrupt dos ports de 2 a 7
  28.   {
  29.     minhaMatriz[i] = digitalRead(i);                            // Salva valores dos ports na matriz
  30.     count = HIGH;                                                // Habilita impressao
  31.   }
  32. }
  33. //----------------------------------------------
  34. void setup()
  35. {
  36.   Serial.begin(9600);                                           // Inicializa a serial
  37.   for (int i = 0; i <= 12; i++)                                 // Define pinMode INPUT e em PULLUP para os ports (PULLUP e opcional)
  38.   {
  39.     pinMode(i, INPUT_PULLUP);                                   // pinMode
  40.   }
  41.   pciSetup(2);                                                  // enable interrupt for 10 pins
  42.   pciSetup(3);
  43.   pciSetup(4);
  44.   pciSetup(5);
  45.   pciSetup(6);
  46.   pciSetup(7);
  47.   pciSetup(8);
  48.   pciSetup(9);
  49.   pciSetup(10);
  50.   pciSetup(11);
  51.   for (int i = 2; i <= 12; i++)                                 // Endereca ports
  52.   {
  53.     minhaMatriz[i] = digitalRead(i);                            // Le condicao inicial dos ports e salva na matriz
  54.   }
  55. }
  56. //----------------------------------------------
  57. void loop()
  58. {
  59.   if (count == HIGH)                                             // Se impressao esta habilitada
  60.   {
  61.     for (int i = 2; i <= 11; i++)                               // Endereca matriz
  62.     {
  63.       countMatriz[i] = countMatriz[i] + minhaMatriz[i];
  64.       Serial.print(i); Serial.print(" : ");                     // Imprime
  65.       Serial.println(countMatriz[i]);
  66.     }
  67.     count = LOW;                                                 // Desabilita impressao
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement