Beispiel #1
0
void Pendant::sleepNow() {
  disableLEDs();

  PCMSK0 |= _BV(PCINT0);  // Watch pin PB1
  GIFR   |= _BV(PCIE0);   // clear any outstanding interrupts

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  power_all_disable();  // power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu();


  //
  // ... SLEEPING ...
  //


  // resumes here on wake
  sleep_disable();
  power_all_enable();    // power everything back on
  PCMSK0 &= ~_BV(PCINT0); // Turn off PB1 as interrupt pin

  sleepOnRelease = false;
  changeModeOnRelease = false;
  digitalWrite(PENDANT_LED_PWR_PIN, HIGH);
}
Beispiel #2
0
void SleepClass::sleep(uint8_t modules,uint8_t sm){
//  power_adc_disable();
  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
 // ACSR = (1<<ACD); //Disable the analog comparator
  if(!(modules & SPI_ON)) power_spi_disable();
  if(!(modules & TWI_ON)) power_twi_disable();
  if(!(modules & USART0_ON)) power_usart0_disable();
  if(!(modules & TIMER0_ON)) power_timer0_disable();
  if(!(modules & TIMER1_ON)) power_timer1_disable();
  if(!(modules & TIMER2_ON)) power_timer2_disable();

  set_sleep_mode(sm);
  cli();
  do{
	  sleep_enable();
#if defined __AVR_ATmega328P__
	  sleep_bod_disable();
#endif
	  sei();
	  sleep_cpu();     // System sleeps here
	  sleep_disable(); // System continues execution here when an interrupt woke up the divice
  } while(0);
  sei();
  power_all_enable();
  sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON

}
Beispiel #3
0
void
early_init()
{
    // XXX save reset cause for later use?
    if (MCUSR & WDRF) {
        wdt_reset_count++;
    }

    MCUSR = 0;

    // reset watchdog timer to a more generous value
    wdt_enable(WDTO_500MS);
    wdt_reset();

    // set the prescaler for maximum speed
    clock_prescale_set(clock_div_1);

    // turn everything on
    power_all_enable();

    // fill (most of) the stack with 0xff for sniffing purposes
    volatile uint8_t *p = &_end;

    while (p < (uint8_t *)SP) {
        *p++ = 0xff;
    }
}
void enterSleep(void)
{
  SMCR=(0<<SM2)|(0<<SM1)|(0<<SM0);       //set to idle mode
  sleep_enable();			 // enable sleep mode 
  power_spi_disable();		        //disabled all peripherals which is not used
  power_timer0_disable();
  power_timer2_disable();
  power_twi_disable();  
  sleep_mode();				//enter sleep mode
  sleep_disable(); 			//program continue where it stop
  power_all_enable();			//re-enable the peripherals
}
Beispiel #5
0
void enterSleep(void) {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  sleep_enable();

  /* Now enter sleep mode. */
  sleep_mode();

  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */

  /* Re-enable the peripherals. */
  power_all_enable();
}
Beispiel #6
0
//
// lowPowerOperation() - configures the arduino to go into the lowest power mode available
//			given the ChapR requirements
//
void lowPowerOperation()
{
     power_all_enable();	// turn everything on at first

     ADCSRA &= ~(1<<ADEN);	//Disable ADC  
     ACSR = (1<<ACD);		//Disable the analog comparator

     power_adc_disable();	// ADC disabled
     power_twi_disable();	// IC2/TWI disabled
     power_spi_disable();	// SPI disabled
//     power_timer0_disable();	// affects delay/milli
     power_timer1_disable();	// affects servos
     power_timer2_disable();	// affects tones (this is turned on/off in sound routines too)
     power_usart0_disable();	// disables communication via onboard USART (turned on in pairing mode)

  // at this point, we've left the USART on as well as timer 0 (for delay/millis)
}
Beispiel #7
0
void enterSleep() {
  // Disabling ADC
  ADCSRA = 0;

  // Disabling all peripherals
  PORTB = 0;

  // Disable Brown out Detection
  sleep_bod_disable();

  wdt_reset();
  /* Now enter sleep mode. */
  sleep_mode();

  /* Re-enable the peripherals. */
  power_all_enable();
}
Beispiel #8
0
/**
 * wakeUp
 *
 * Wake from sleep mode
 *
 * 'rxOn' Enter RX_ON state after waking up
 */
void AVRRTC::wakeUp(void) 
{
  // Exit from sleep
  sleep_disable();
  //wdt_disable();
  // Re-enable functions
  //clock_prescale_set(clock_div_1);
  power_all_enable();
  // Enable ADC
  ADCSRA |= (1 << ADEN);
  
  // If 32.768 KHz crystal enabled
//  if (rtcCrystal)
//  {
    // Disable timer2A overflow interrupt
    TIMSK2 = 0x00;
//  }
}
Beispiel #9
0
/*******************************************************************************
 * Power down processor
 * Don't forget to switch off the LED and the RF module for lowest consumption.
 * Board quiescent current budget:
 * charge circuit = 180 nA
 * ATmega328P deep sleep = 120 nA
 * regulators quiescent currents = 30 nA
 *******************************************************************************/
void TheAirBoard::powerDown() {
  byte br_high = UBRR0H, br_low = UBRR0L; // save baudrate register 
  Serial.end();
  digitalWrite(RX, LOW);           // reset internal pull-up serial link
  ADCSRA &= B01111111;             // disable ADC
  power_all_disable();
  /****************DON'T CHANGE************************************************/
  MCUCR = B01100000;               // BOD disable timed sequence
  MCUCR = B01000000;
  SMCR = B00000101;                // power down mode + sleep enable
  asm("sleep\n");
  sleep_disable();                 // first thing after wake up
  /****************************************************************************/
  power_all_enable();
  ADCSRA |= B10000000;             // enable ADC
  UBRR0H = br_high;                // set baud rate MSB
  UBRR0L = br_low;                 // set baud rate LSB
  UCSR0B |= (1<<RXCIE0)|(1<<UDRIE0)|(1<<RXEN0)|(1<<TXEN0); // enable uart
}
// Put the System to sleep when the power button is pressed, or the mode times out
void sleepNow()
{
  attachInterrupt(wakePin-2,wakeNow,RISING);
  set_sleep_mode(SLEEP_MODE_IDLE);  //PWR_DOWN);
  sleep_enable();
  power_adc_disable();
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
  attachInterrupt(wakePin-2,wakeNow,RISING);  // Attach our wakeup interrupt
  sleep_mode();  // Goodnight
  
  // Once we get here, it has woken up!
  sleep_disable();
  power_all_enable();
  attachInterrupt(wakePin-2,nop,RISING); // Debounce reset button input -- throw away any pending interrupts (detachInterrupt leaves them pending)
  delay(250);   // Debounce reset button input -- wait for button unpress
}  
Beispiel #11
0
void power_save()
{
  /* Enter power saving mode. SLEEP_MODE_IDLE is the least saving
   * mode, but it's the only one that will keep the UART running.
   * In addition, we need timer0 to keep track of time, timer 1
   * to drive the buzzer and timer2 to keep pwm output at its rest
   * voltage.
   */

  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();
  power_adc_disable();
  power_spi_disable();
  power_twi_disable();

  pin_write(LED_PIN, LOW);
  sleep_mode();    // Go to sleep
  pin_write(LED_PIN, HIGH);
  
  sleep_disable();  // Resume after wake up
  power_all_enable();
}
int main(void)
{
	char buf[7]; // antes apenas 7 era o suficiente
	uint8_t contador;

	uint8_t	ciclosWDTSono, min5s300;

	iniciaPORTAS();
	
	PORTD |= (1<<PD7);					// liga MOSFET
	_delay_ms(20);						// espera 20ms para energizar os circuitos
	
	// MCUSR – MCU Status Register
	// The MCU Status Register provides information on which reset source caused an MCU reset.
	
	printString("MCU Status Register:");
	printHexByte(  ((MCUSR | WDRF)  &0b1000 >> 3) );
	printString(",");
	printHexByte(  ((MCUSR | BORF)  &0b0100 >> 2) );
	printString(",");
	printHexByte(  ((MCUSR | EXTRF) &0b0010 >> 1) );
	printString(",");
	printHexByte(  ((MCUSR | PORF)  &0b0001     ) );
	printString("\r\nProj 07B v1.1b PSW (sem RTC)");	

	//WDTCSR – Watchdog Timer Control Register
	
	/*
	When the Brown-out Detector (BOD) is enabled by BODLEVEL fuses, Table 28-6 on page 293,
	the BOD is actively monitoring the power supply voltage during a sleep period. To save power, it
	is possible to disable the BOD by software for some of the sleep modes, see Table 10-1 on page
	40. The sleep mode power consumption will then be at the same level as when BOD is globally
	disabled by fuses. If BOD is disabled in software, the BOD function is turned off immediately
	after entering the sleep mode. Upon wake-up from sleep, BOD is automatically enabled again.
	This ensures safe operation in case the VCC level has dropped during the sleep period.
	
	When the BOD has been disabled,the wake-up time from sleep mode will be approximately 60
	µs to ensure that the BOD is working correctly before the MCU continues executing code.
	BOD disable is controlled by bit 6, BODS (BOD Sleep) in the control register MCUCR, see
	”MCUCR – MCU Control Register” on page 45. Writing this bit to one turns off the BOD in relevant 
	sleep modes, while a zero in this bit keeps BOD active. Default setting keeps BOD active,
	i.e. BODS set to zero.
	*/
		
	//sleep_bod_disable();
	/* faz o mesmo que abaixo */
	//MCUCR |= (1<<BODS) | (1<<BODSE);		// desabilita o BOD para o sleep mode
	//MCUCR |= (1<<BODS);						// desabilita o BOD
	//MCUCR &= ~(1<<BODSE);					//
	
	//SMCR |= (1<<SE);						// Sleep Enable
	//SMCR |= (1<<SM2) | (1<<SM1) | (1<<SM0);	// IDLE
	
	//power_tx_modulator_enable();
	
	// disable ADC
	//ADCSRA = 0;		//	With that there the power consumption drops a large amount, down from 335 µA to 0.355 µA! (that is, 355 nA)
	
	
			/*
		20.7.3 Receive Compete Flag and Interrupt
		The USART Receiver has one flag that indicates the Receiver state.
		The Receive Complete (RXCn) Flag indicates if there are unread data present in the receive 
		buffer. This flag is one when unread data exist in the receive buffer, and zero when the receive
		buffer is empty (i.e., does not contain any unread data). If the Receiver is disabled (RXENn = 0),
		the receive buffer will be flushed and consequently the RXCn bit will become zero.
		
		When the Receive Complete Interrupt Enable (RXCIEn) in UCSRnB is set, the USART Receive
		Complete interrupt will be executed as long as the RXCn Flag is set (provided thatglobal interrupts 
		are enabled). When interrupt-driven data reception is used, the receive complete routine
		must read the received data from UDRn in order to clear the RXCn Flag, otherwise a new interrupt 
		will occur once the interrupt routine terminates.
		*/
		UCSR0B |= (1<<RXCIE0);				// Habilita a Interrupcao de RX no controle da USART
		sei();
		_delay_ms(10000);
		
		
	printString("\r\nLoop Sleep Indefinido - RXINT.\r\n");
    while(1)
    {
		cli();
		for (contador=0;contador<3;contador++)
		{
			getEnv();
			_delay_ms(3333);
		}
			
		
		
		if(MODO=='0')		// o MODO 0 caracteriza-se por dormir indefinidamente
							// ate que uma interrupcao na USART acorde o MCU
		{
			set_sleep_mode(SLEEP_MODE_IDLE);	// configura o MODO de sleep
			sei();								// habilita todos interrupts
			
			liga_mcp23008();
			seqLed_mcp23008();
		
			//printString("Habilitando Sleep.\r\n");
			sleep_enable();						// habilita a dormirda
		
			power_adc_disable();
			power_spi_disable();
			power_timer0_disable();
			power_timer1_disable();
			power_timer2_disable();
			power_twi_disable();
		
			sleep_bod_disable();				// desliga o comparador de voltagem do BOD
		
			PORTD &= ~(1<<PD7);					// desliga MOSFET
		
			printString("dormindo...");
			sleep_mode();						// realmente coloca para dormir
			/*--------------------------------------------------------------*/
			printString("...Acordou!\r\n");
			sleep_disable();
			
			PORTD |= (1<<PD7);					// liga MOSFET
			_delay_ms(20);						// espera 20ms para energizar os circuitos
			
			power_all_enable();
			
		}
		else
		{
			printString("\r\nLoop Sono de 1h/5m (64s/64s).\r\n_");
			
			// Marca ZERO ciclos de Sleep com WDT
			ciclosWDTSono=0;	
			min5s300=0;			// contador multiplo de 5 minutos (ou 300 seg)
			
			while ( (MODO=='1') | (MODO=='2') )	// o Modo 1 eh o Sleep com WDT de no maximo 1h
											// o Modo 2 eh o Sleep com WDT sem hora para realmente acordar
			{
				//UCSR0B &= ~(1<<RXCIE0);	// Deabilita a Interrupcao de RX no controle da USART
				sei();					// Habilita interrupcoes, por causa do WDT
				
				PORTD &= ~(1<<PD7);					// desliga MOSFET
				
				habilitarWDT();		// coloca a CPU para dormir em SLEEP_MODE_PWR_DOWN
									// sendo acordada 8 segundos depois pelo WDT
									
				ciclosWDTSono++;	// computa mais um ciclo de WDT de 8 segundos
				
				if (ciclosWDTSono >= 37)	// ciclos de sleep+WDT forem maiores que 37 (8s*37 = 300s = 5min )
				{
					ciclosWDTSono=0;	// zera o contador de ciclos a cada "minuto" (ou mais segundos) de sono
					min5s300++;			// incrementa o contador de 5 minutos
					PORTD |= (1<<PD7);					// liga MOSFET
					_delay_ms(20);						// espera 20ms para energizar os circuitos
					
					liga_mcp23008();
					seqLed_mcp23008();
					
					////////////////////////////////////////////////////				

						printString( itoa(( min5s300 * 5) ,buf,10) );
						printString("min _ \r\n");
						getEnv();
						_delay_ms(2000);

					////////////////////////////////////////////////////				
					if ( (min5s300 == 12) & (MODO=='1'))	
						// testa se ja faz 1 hora que dormi
						{
							MODO='0';	// forcar para sair do MODO 1 (WDT) e voltar para o MODO 0 (USART RX INT)
							printString("Saindo do Modo Sono de 1 hora.\r\n_");
						}
					////////////////////////////////////////////////////
					_delay_ms(2000);	
				}
			}
		}
		
    }
}
/*this mode turns off everything not needed during the time in which only the main counter is running
 The only things needed during simple sleep are the operation of basic pins and the timer1 interrupt
 */
void Radian_power_saver::EnterSimpleSleep(unsigned int IdlePWM ){
    
  /*  
    
    //Disable ADC and internal Vref
    POWER_OFF;
    ADMUX &= ~( (1<<REFS1) | (1<<REFS0));
    ADCSRA &= ~( (1<<ADEN) );
    //Disable AnalogComparator
    ACSR |= (1<<ACD); 
    
    
    
    SMCR = 1; //go into idle mode
    sleep_mode();
    POWER_ON;
   
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
    sleep_enable();
    interrupts();
 //   attachInterrupt(0,sleepHandler, FALLING);
    sleep_mode();  //sleep now
    //--------------- ZZZZZZ sleeping here
    sleep_disable(); //fully awake now
    POWER_ON; //turn the power back on
   
  */
    
    
    if(DEBUG) Serial.println("Entering simple Sleep Mode");
    if(IdlePWM == 0) SetIdlePins(PAN);
    else SetIdlePins(TILT);
//    power_all_disable(); //turn off all modules
    
    //turn necessary idle ports back on
    power_usart0_enable();
    power_timer1_enable();
 //   power_timer4_enable(); //leave PWM module on, should make something smarter for future
    
    set_sleep_mode(SLEEP_MODE_IDLE);  
    sleep_enable();
    //  interrupts();
    //  attachInterrupt(0,sleepHandler, FALLING);
    
    sleep_mode();  //sleep now
    //--------------- ZZZZZZ sleeping here
    sleep_disable(); //fully awake now
   // Serial.println("Woke up");
    POWER_ON;
    power_all_enable();
    if(IdlePWM == 0) WakeUpPins(PAN);
    else WakeUpPins(TILT);

    
    /*power_adc_enable();
    power_timer0_enable();
    power_usart1_enable();
    power_useart0_enable();
     
     */
    
}