Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <avr/sleep.h> // Sleep Modes
  2. #include <avr/power.h> // Power management
  3. #include <avr/wdt.h> // Watchdog timer
  4. #include <Wire.h>
  5. #include <CodeLibrary.h>
  6.  
  7. volatile unsigned int nbOfBlinks;
  8. unsigned int nbOfBlinksToBeSent = -1;
  9. bool dataRequested = false;
  10. CodeLibrary codeLibrary;
  11. byte i2cAddress = 47;
  12. byte CMD_ACKMESSAGE = 44;
  13.  
  14. /*
  15. Pin change interrupts.
  16.  
  17. Pin Mask / Flag / Enable
  18. D0 PCINT16 (PCMSK2 / PCIF2 / PCIE2)
  19. D1 PCINT17 (PCMSK2 / PCIF2 / PCIE2)
  20. D2 PCINT18 (PCMSK2 / PCIF2 / PCIE2)
  21. D3 PCINT19 (PCMSK2 / PCIF2 / PCIE2)
  22. D4 PCINT20 (PCMSK2 / PCIF2 / PCIE2)
  23. D5 PCINT21 (PCMSK2 / PCIF2 / PCIE2)
  24. D6 PCINT22 (PCMSK2 / PCIF2 / PCIE2)
  25. D7 PCINT23 (PCMSK2 / PCIF2 / PCIE2)
  26. D8 PCINT0 (PCMSK0 / PCIF0 / PCIE0)
  27. D9 PCINT1 (PCMSK0 / PCIF0 / PCIE0)
  28. D10 PCINT2 (PCMSK0 / PCIF0 / PCIE0)
  29. D11 PCINT3 (PCMSK0 / PCIF0 / PCIE0)
  30. D12 PCINT4 (PCMSK0 / PCIF0 / PCIE0)
  31. D13 PCINT5 (PCMSK0 / PCIF0 / PCIE0)
  32. A0 PCINT8 (PCMSK1 / PCIF1 / PCIE1)
  33. A1 PCINT9 (PCMSK1 / PCIF1 / PCIE1)
  34. A2 PCINT10 (PCMSK1 / PCIF1 / PCIE1)
  35. A3 PCINT11 (PCMSK1 / PCIF1 / PCIE1)
  36. A4 PCINT12 (PCMSK1 / PCIF1 / PCIE1)
  37. A5 PCINT13 (PCMSK1 / PCIF1 / PCIE1)
  38. */
  39.  
  40. void setup() {
  41. Serial.begin(115200);
  42. Serial.println("Setup.");
  43.  
  44. // Read-led change interrupt
  45. pinMode(8, INPUT);
  46. PCMSK0 = 0;
  47. PCMSK0 |= bit(PCINT0); // D8
  48. PCIFR |= bit(PCIF0); // clear any outstanding interrupts
  49. PCICR |= bit(PCIE0); // enable pin change interrupts for D0 to D7
  50.  
  51. Wire.begin(i2cAddress);
  52. Wire.onRequest(requestEvent);
  53. Wire.onReceive(receiveEvent);
  54.  
  55. Serial.println("Done Setup.");
  56. delay(10);
  57. }
  58.  
  59. // Called by interrupt service (master) routine when data is to be recieved
  60. void receiveEvent(int howMany) {
  61. byte cmd = Wire.read();
  62. if (cmd == CMD_ACKMESSAGE) {
  63. nbOfBlinksToBeSent = -1;
  64. //nbOfBlinks = 0;
  65. }
  66. }
  67.  
  68. // Called by interrupt service (master) routine when response is wanted
  69. void requestEvent() {
  70. if (-1 == nbOfBlinksToBeSent) {
  71. nbOfBlinksToBeSent = nbOfBlinks;
  72. }
  73.  
  74. byte buffer[] = { i2cAddress, nbOfBlinksToBeSent >> 8, nbOfBlinksToBeSent, 0 };
  75. buffer[3] = codeLibrary.crc8(buffer, sizeof(buffer) - 1);
  76. Wire.write(buffer, sizeof(buffer));
  77. }
  78.  
  79. // Handle pin change interrupt for D8
  80. ISR(PCINT0_vect) {
  81. // if (digit alRead(8) == HIGH) {
  82. nbOfBlinks++;
  83. // }
  84.  
  85. //Serial.println("PCINT0_vect"); //Serial is disabled in registers when this event occurrs
  86. }
  87.  
  88. void waitForExternalWakeup() {
  89. //byte old_ADCSRA = ADCSRA;
  90. // disable ADC
  91. ADCSRA = 0;
  92.  
  93. // Set sleep mode and disable those services we don't want
  94. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  95. noInterrupts();
  96. //power_all_disable();
  97. power_adc_disable();
  98. power_spi_disable();
  99. power_usart0_disable();
  100. power_timer0_disable();
  101. power_timer1_disable();
  102. power_timer2_disable();
  103. // Let TWI (i2c) be enabled
  104. //power_twi_disable();
  105.  
  106. // Go sleep
  107. sleep_enable();
  108. interrupts();
  109. sleep_cpu();
  110.  
  111. // ----------------
  112.  
  113. // Wake up again
  114. sleep_disable();
  115. power_all_enable();
  116.  
  117. //ADCSRA = old_ADCSRA;
  118.  
  119. // release TWI bus
  120. TWCR = bit(TWEN) | bit(TWIE) | bit(TWEA) | bit(TWINT);
  121.  
  122. // turn it back on again
  123. Wire.begin(i2cAddress);
  124. }
  125.  
  126. void loop() {
  127. while (true) {
  128. waitForExternalWakeup();
  129. Serial.print(String(millis()) + " Woke up. ");Serial.println(nbOfBlinks);
  130. delay(10);
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement