int main(void) { // initialize uint8_t i2cMessageBuf[I2C_MAX_MSG_SIZE]; USI_TWI_Master_Initialise(); // reset the power mgmt register to wake it up i2cMessageBuf[0] = (uint8_t) 0xD0; // address 0x68, write i2cMessageBuf[1] = 0x6b; // low x-axis i2cMessageBuf[2] = 0x00; USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x03); long_delay_ms(50); while (TRUE) { // construct a command to high-order x value of a 9150 accelerometer // register 59 = 0x3b; low-order register is 0x3c, then put them // together i2cMessageBuf[0] = (uint8_t) 0xD0; i2cMessageBuf[1] = 0x3b; // high x-axis //i2cMessageBuf[2] = 0x3c; // send the message USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x02); // wait long_delay_ms(50); // read the msg i2cMessageBuf[0] = 0xD1; // lsb == 1 => read i2cMessageBuf[1] = 0x3b; //i2cMessageBuf[2] = 0x3c; USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x03); //USI_TWI_Start_Read_Write(i2cMessageBuf,(uint8_t) 0x04); // turn on LED if not flat on table // flat on table: high is FA, low is FF if (i2cMessageBuf[1] != 0xFA) { DDRB |= 1 << PB3; PORTB &= ~(1<<PB3); } else PORTB |= 1 << PB3; // wait long_delay_ms(500); } // never reached return 0; }
int main(void) { // data direction register for port B to output DDRB |= (1 << LED); LED_LO; for (;;) { LED_XCHG; long_delay_ms(DELAY_MS); } return 42; }
void standbyTimerDone(uint32_t avgInputVolt) { if (avgInputVolt > 768) { // high level means NO bat. loading! now we can really go to sleep // shut down everything led_fader_disable(); DIGIWRITE_L(PORTB, PIN_LED); DIGIWRITE_L(PORTB, PIN_AUDIO_TRIGGER); // stop giving signal to audio playback DIGIWRITE_L(PORTB, ACCEL_PIN_X); // prepare going to sleep GIMSK |= (1 << INT0); // enable external interrupt on PB2 // NOTE: only LOW level will wake the MCU up again! // this does not work somehow: // MCUCR |= (1 << ISC01) | (1 << ISC00); // The rising edge of INT0 generates an interrupt request. // MCUCR |= (1 << ISC00); // Any logical change on INT0 generates an interrupt request. // wdt_disable(); // go to sleep sleep_enable(); sei(); sleep_bod_disable(); sleep_cpu(); // woken up! cli(); sleep_disable(); GIMSK &= ~(1 << INT0); // disable external interrupt on PB2 // test LEDs. 2 times blinking means "woken up" blinkLED(&PORTB, PIN_LED, 50); long_delay_ms(50); blinkLED(&PORTB, PIN_LED, 50); // start LED fading again startLEDDefaultMode(); shakeEnded(); // reset states // wake up watch dog // wdt_enable(WTD_TIME); } else { standby_timer_reset(); // try it again } // standby_timer_enable(); }