#include #include #include "nrf.h" #include "nrf_gpio.h" #include #include #include "Wire.h" ADXL345 adxl; // variable adxl is an instance of the ADXL345 library #ifdef __cplusplus extern "C" { #endif //Timer period : 0.5ms void TIMER1_IRQHandler(void) { if (NRF_TIMER1->EVENTS_COMPARE[1] && NRF_TIMER1->INTENSET & TIMER_INTENSET_COMPARE1_Msk) { NRF_TIMER1->EVENTS_COMPARE[1] = 0; //Serial.println("Timer"); } } // This IRQ handler will trigger every 0.125s void RTC0_IRQHandler(void) { volatile uint32_t dummy; if (NRF_RTC0->EVENTS_COMPARE[0] == 1) { NRF_RTC0->EVENTS_COMPARE[0] = 0; NRF_RTC0->CC[0] = NRF_RTC0->COUNTER + 1; dummy = NRF_RTC0->EVENTS_COMPARE[0]; dummy; //Serial.println("RTC"); } } #ifdef __cplusplus } #endif void start_timer(void) //Timer 1 - 0.5ms { NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; NRF_TIMER1->TASKS_CLEAR = 1; NRF_TIMER1->PRESCALER = 0; NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit; NRF_TIMER1->CC[1] = 8000; NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos); NRF_TIMER1->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos); NVIC_EnableIRQ(TIMER1_IRQn); NRF_TIMER1->TASKS_START = 1; } void start_RTC(void) //RTC 0 - 0.125s { NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos; NRF_CLOCK->TASKS_LFCLKSTART = 1; while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0); NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_RTC0->PRESCALER = 4095; NRF_RTC0->CC[0] = 1; NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Enabled << RTC_EVTENSET_COMPARE0_Pos; NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Enabled << RTC_INTENSET_COMPARE0_Pos; NVIC_EnableIRQ(RTC0_IRQn); NRF_RTC0->TASKS_START = 1; } void setup() { Serial.begin(9600); // initialize serial communications Wire.begin(); // initialize I2C communications delay(5); pinMode(A0, INPUT); // make digital pin A0 in nrf51822 an input for ADXL345 interrupt adxl.powerOn(); //setting all interupts to take place on int pin 1 //set activity/ inactivity thresholds (0-255) adxl.setActivityThreshold(4); // 62.5mg per increment adxl.setInactivityThreshold(75); // 62.5mg per increment adxl.setTimeInactivity(1); // how many seconds of no activity is inactive? //look of activity movement on this axes - 1 == on; 0 == off adxl.setActivityX(1); adxl.setActivityY(1); adxl.setActivityZ(0); //ADXL345 interrupt mapping adxl.setInterruptMapping(ADXL345_INT_ACTIVITY_BIT, ADXL345_INT2_PIN); adxl.setInterruptMapping(ADXL345_INT_INACTIVITY_BIT, ADXL345_INT2_PIN); //ADXL345 interrupt setting adxl.setInterrupt(ADXL345_INT_ACTIVITY_BIT, 0); // register interupt actions - 1 == on; 0 == off adxl.setInterrupt(ADXL345_INT_INACTIVITY_BIT, 0); //********** //Timer //********** //start_timer(); //start_RTC(); } void loop() { sleepNow(); } void sleepNow() { Serial.println("Hello "); //**************************** //Nordic nrf51822 Sleep mode //**************************** __WFE(); __SEV(); __WFE(); adxl.getInterruptSource(); adxl.setInterrupt(ADXL345_INT_ACTIVITY_BIT, 1); attachInterrupt(A0, wake_up, CHANGE); } void wake_up() { Serial.println("Activity"); }