//Initialize the state passed in as a variable void initState(unsigned char state) { switch (state) { case sweep: tracker = 0x01; //Starting bitmask direction = 1; //Starting direction (1=ascending, 0=descending) start_timer1_compare(sweepDelay); break; case xor: ledPort = 0x0F; start_timer1_compare(xorDelay); break; case flash: ledPort = 0xFF; start_timer1_compare(flashDelay); break; case sleep: ledPort = 0x00; //Shut off LEDs sleep_now(); //Put chip to sleep, PCINT8 will wake it timer = 1; //Set timer flag. This will occur after waking up. break; } }
/** * \brief This will start a sleep operation. * * \param val Used for remembering the new menu to display after a wakeup. */ void menu_run_sleep(uint8_t *val) { /* Turn off LED, LCD, ADC, Timer 1, SPI */ led_off(); lcd_deinit(); key_deinit(); PRR |= (1 << PRTIM1) | (1 << PRSPI); /* Tell the 1284P to turn off the radio and sleep */ sleep_count=0; uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count); /* Turn off UART when transmission is complete */ while(!(UCSR0A & (1 << TXC0))); _delay_us(10000); //deinit trash clears done flag on 1284p uart_deinit(); /* Go to sleep until button is pushed */ sleep_now(0); /* Yawn, waking up, turn on LCD with Raven Logo */ lcd_init(); lcd_symbol_set(LCD_SYMBOL_RAVEN); /* Disable interrupts before powering everything up */ cli(); key_init(); PRR &= ~((1 << PRTIM1) | (1 << PRSPI)); uart_init(); /* Enable interrupts, Wake up 1284p and radio */ sei(); sleep_wakeup(); // uart_init();//flush receive buffer /* Wait for buttons up */ while (key_state_get() != KEY_NO_KEY) ; if (is_button()){ get_button(); } }
int main (void) { // Interrupt setup for sleep mode interrupt_init(); // temp for debugging DDRB |= (1 << PINB0); PORTB |= (1 << PINB0); // Select ADC channel uint8_t ch = 0b00000000; while(1) { if (asleep) { PORTB &= ~(1 << PINB0); // Disable interrupt cli(); // Power down sleep_now(); PORTB ^= (1 << PINB0); // Continue here at start up // Disable interrupt for setting registers cli(); external_clock_init(); interrupt_init(); timer_init(); uart_init(); adc_init(); sei(); } else if (!asleep) { // Temp for debugging _delay_ms(100); // PORTB ^= (1 << PINB0); // Read from ADC uint16_t result = adc_read(ch); // Send result uart_send_byte((result >> 8)); uart_send_byte(result); uart_send_byte('\n'); } }
/** * \brief This will start a sleep with wakes for temperature measurement and web requests. * * \param val Used for remembering the new menu to display after a wakeup. */ void menu_run_doze(uint8_t *val) { /* Turn off LED, LCD */ led_off(); lcd_deinit(); /* Debounce */ while (key_state_get() != KEY_NO_KEY) ; /* Stay in doze loop until button is pressed*/ while (ENTER_PORT & (1<<ENTER_PIN)) { /* Tell 1284p to sleep for 4 seconds */ /* It will ignore the request if TCP/IP sessions are active */ /* Alter these timings as desired, or comment out to sleep only the 3290p */ sleep_count=4; uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count); /* Wait for transmission complete, then sleep 3290p for 5 seconds */ while(!(UCSR0A & (1 << TXC0))); // uart_deinit(); sleep_now(sleep_count+1); // uart_init(); /* 1284p should be awake by now, update temperature and give it time to process */ menu_send_temp(); _delay_us(20000); } /* Wake LCD, turn on Raven logo */ lcd_init(); lcd_symbol_set(LCD_SYMBOL_RAVEN); sleep_wakeup(); /* Wait for buttons up */ while (key_state_get() != KEY_NO_KEY) ; if (is_button()){ get_button(); } }
void sleep_delay(int ii){ sleep_watchdog(ii); sleep_now(); }