Example #1
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

}
Example #2
0
void	LowPowerClass::idle(period_t period, adc_t adc, 
							            timer1_t timer1, timer0_t timer0)
{
	if (adc == ADC_OFF)	
	{
		ADCSRA &= ~(1 << ADEN);
		power_adc_disable();
	}

	if (timer1 == TIMER1_OFF)	power_timer1_disable();	
	if (timer0 == TIMER0_OFF)	power_timer0_disable();	
	
	if (period != SLEEP_FOREVER)
	{
		wdt_enable(period);
		WDTCR |= (1 << WDIE);	
	}
	
	lowPowerBodOn(SLEEP_MODE_IDLE);
	
	if (adc == ADC_OFF)
	{
		power_adc_enable();
		ADCSRA |= (1 << ADEN);
	}

	if (timer1 == TIMER1_OFF)	power_timer1_enable();	
	if (timer0 == TIMER0_OFF)	power_timer0_enable();	
}
Example #3
0
void powermgmt() //Code to turn off periphery and define the type of sleep mode
{
	set_sleep_mode(SLEEP_MODE_ADC);	 //Sleep mode is ADC Noise Reduction
	power_spi_disable();
	power_timer0_disable();
	power_timer1_disable();
	power_twi_disable();
	power_usart0_disable();
}
Example #4
0
void powermgmt()
{
	set_sleep_mode(SLEEP_MODE_ADC);
	sleep_cpu();
	power_spi_disable();
	power_timer0_disable();
	power_timer1_disable();
	power_twi_disable();
}
Example #5
0
VOID Timer1_Close( VOID )
{
	cbi( TIMSK1, OCIE1A );
	cbi( TIMSK1, OCIE1B );
	cbi( TIMSK1, TOIE1 );

	Timer1_Unset_PwmPin( 0 );
	Timer1_Unset_PwmPin( 1 );

#ifdef power_timer1_disable
	power_timer1_disable();								// enable power-save mode
#endif
}
Example #6
0
void cube_shutdown(uint8_t unused)
{
	// Set harder powersave mode
	old_mode = mode;
	mode = MODE_SLEEP;
	power_adc_disable();
	power_spi_disable();
	power_timer0_disable();
	power_timer1_disable();

	/* Pulling BLANK down reduces current consumption to
	 * 50mA. Doing that. We pull all other signals low, too. */
	pin_high(BLANK);
	pin_low(XLAT);
}
Example #7
0
/*********************************************************************************************************
** Function name:           pwrDown
** Descriptions:            pwrDown
*********************************************************************************************************/
void xadow::pwrDown(unsigned long tSleep)
{
#if defined(__AVR_ATmega32U4__)
    power_adc_disable();
    power_usart0_disable();
    power_spi_disable();
    power_twi_disable();
    power_timer1_disable();
    power_timer2_disable();
    power_timer3_disable();
    power_usart1_disable();
    power_usb_disable();
#endif
    sleep.pwrDownMode();        //set sleep mode
    sleep.sleepDelay(tSleep);   //sleep for: sleepTime
}
Example #8
0
void main() {
  ADCSRA = 0; // DIE, ADC!!! DIE!!!
  ACSR = _BV(ACD); // Turn off analog comparator - but was it ever on anyway?
  power_adc_disable();
  power_usi_disable();
  power_timer1_disable();
  TCCR0A = _BV(COM0A0) | _BV(WGM01); // mode 2 - CTC, toggle OC0A
  TCCR0B = _BV(CS00); // prescale = 1 (none)
  OCR0A = 0; // as fast as possible
  DDRB = _BV(DDB0) | _BV(DDB1) | _BV(DDB2); // all our pins are output.
  PORTB = 0; // Initialize all pins low.

  // And we're done.
  while(1);

}
Example #9
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)
}
Example #10
0
void	Low_Power::idle(Period_t period, ADC_t adc,
                        Timer4_t timer4, Timer3_t timer3,
                        Timer1_t timer1, Timer0_t timer0,
                        SPI_t spi, USART1_t usart1,	TWI_t twi, usb_t usb)
{
    if (adc == ADC_OFF)
    {
        ADCSRA &= ~(1 << ADEN);
        power_adc_disable();
    }

    if (timer4 == TIMER4_OFF)	power_timer4_disable();
    if (timer3 == TIMER3_OFF)	power_timer3_disable();
    if (timer1 == TIMER1_OFF)	power_timer1_disable();
    if (timer0 == TIMER0_OFF)	power_timer0_disable();
    if (spi == SPI_OFF)				power_spi_disable();
    if (usart1 == USART1_OFF)	power_usart1_disable();
    if (twi == TWI_OFF)				power_twi_disable();
    if (usb == USB_OFF)				power_usb_disable();

    if (period != SLEEP_FOREVER)
    {
        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }

    lowPowerBodOn(SLEEP_MODE_IDLE);

    if (adc == ADC_OFF)
    {
        power_adc_enable();
        ADCSRA |= (1 << ADEN);
    }

    if (timer4 == TIMER4_OFF)	power_timer4_enable();
    if (timer3 == TIMER3_OFF)	power_timer3_enable();
    if (timer1 == TIMER1_OFF)	power_timer1_enable();
    if (timer0 == TIMER0_OFF)	power_timer0_enable();
    if (spi == SPI_OFF)				power_spi_enable();
    if (usart1 == USART1_OFF)	power_usart1_enable();
    if (twi == TWI_OFF)				power_twi_enable();
    if (usb == USB_OFF)				power_usb_enable();
}
// 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
}  
Example #12
0
int main(void) 
{
	
	initSPI();
	initNRF24L01();
	//initUSART();
	
	//Init LEDs and flash once to show script running
	LED_DDR|=(1 << LED_RED)|(1 << LED_GREEN);		// set LED pins for output
	LED_DDR2|=(1 << LED_BLUE);		// set blue LED pin for output
	flashLED();  				//Flash to show initiated
	
	initADC();
	ADCSRA |= (1<<ADSC);		// Start conversion - didn't do earlier as not all bits set

	//Reduce power
	power_timer0_disable();
	power_timer1_disable();
	power_timer2_disable();
	power_twi_disable();
		
	//Sleep modes
	set_sleep_mode(SLEEP_MODE_ADC);
	sleep_enable();
	sleep_bod_disable();
	sleep_cpu();
	sleep_mode();
	
	//Power down nrf24
	
	// Mainloop - use asm to stop empty infinite loop being optimised out.
  while(1)
  {
	asm("");
  }

  return 0;
}
Example #13
0
// Shutdown
void bg_pwr_down()
{
  // execute sleep routine
  if (bg_pwr_exec_sleep_routine_flag)
  {
    if (bg_pwr_on_sleep != NULL)
      bg_pwr_on_sleep();
    // the flag is needed because we want to execute sleep
    // routine only when we go from pwr up to pwr down
    bg_pwr_exec_sleep_routine_flag = 0;
  }

  //Shut off ADC, TWI, SPI, Timer0, Timer1, Timer2

  ADCSRA &= ~(1<<ADEN); //Disable ADC
  ACSR |= (1<<ACD); //Disable the analog comparator
  DIDR0 = 0xFF; //Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = (1<<AIN1D)|(1<<AIN0D); //Disable digital input buffer on AIN1/0

  power_twi_disable();
  power_spi_disable();
  power_usart0_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
#if defined(__AVR_ATmega1284P__)
  power_timer3_disable();
#endif

  //Power down various bits of hardware to lower power usage  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_mode();

  /*********/
  /* SLEEP */
  /*********/

  // The processor wakes up back here after interrupt

  //Turn on ADC, TWI, SPI, Timer0, Timer1, Timer2
  ADCSRA |= (1<<ADEN); // Enable ADC
  ACSR &= ~(1<<ACD);   // Enable the analog comparator

  // this should be set to reflect real usage of analog pins
  DIDR0 = 0x00;   // Enable digital input buffers on all ADC0-ADC5 pins
  DIDR1 &= ~(1<<AIN1D)|(1<<AIN0D); // Enable digital input buffer on AIN1/0

  power_twi_enable();
  power_spi_enable();
  power_usart0_enable();
  power_timer0_enable();
  power_timer1_enable();
  power_timer2_enable();
#if defined(__AVR_ATmega1284P__)
  power_timer3_enable();
#endif

  // check button press time and handle state
  unsigned long start = millis();
  while ( (ellapsed_millis(start) < BG_PWR_BUTTON_TIME) && (digitalRead(bg_pwr_switch_pin) == HIGH) );
  // if the button is pressed continuously for 2 seconds, swap to on state
  if (ellapsed_millis(start) >= BG_PWR_BUTTON_TIME)
    bg_pwr_state = BG_STATE_PWR_UP;

  // lower the button flag
  bg_pwr_button_pressed_flag = 0;

  // execute wake up routine only if we really woke up
  if (bg_pwr_state == BG_STATE_PWR_UP || sd_reader_interrupted)
  {
    // execute wake up routine if any is defined
    if (bg_pwr_on_wakeup != NULL)
      bg_pwr_on_wakeup();
    // next time we sleep execute sleep routine
    bg_pwr_exec_sleep_routine_flag = 1; 
  }
}
Example #14
0
/*******************************************************************************
* Name: idle
* Description: Putting microcontroller into idle state. Please make sure you 
*			   understand the implication and result of disabling module.
*
* Argument  	Description
* =========  	===========
* 1. period     Duration of low power mode. Use SLEEP_FOREVER to use other wake
*				up resource:
*				(a) SLEEP_15MS - 15 ms sleep
*				(b) SLEEP_30MS - 30 ms sleep
*				(c) SLEEP_60MS - 60 ms sleep
*				(d) SLEEP_120MS - 120 ms sleep
*				(e) SLEEP_250MS - 250 ms sleep
*				(f) SLEEP_500MS - 500 ms sleep
*				(g) SLEEP_1S - 1 s sleep
*				(h) SLEEP_2S - 2 s sleep
*				(i) SLEEP_4S - 4 s sleep
*				(j) SLEEP_8S - 8 s sleep
*				(k) SLEEP_FOREVER - Sleep without waking up through WDT
*
* 2. adc		ADC module disable control:
*				(a) ADC_OFF - Turn off ADC module
*				(b) ADC_ON - Leave ADC module in its default state
*
* 3. timer2		Timer 2 module disable control:
*				(a) TIMER2_OFF - Turn off Timer 2 module
*				(b) TIMER2_ON - Leave Timer 2 module in its default state
*
* 4. timer1		Timer 1 module disable control:
*				(a) TIMER1_OFF - Turn off Timer 1 module
*				(b) TIMER1_ON - Leave Timer 1 module in its default state
*
* 5. timer0		Timer 0 module disable control:
*				(a) TIMER0_OFF - Turn off Timer 0 module
*				(b) TIMER0_ON - Leave Timer 0 module in its default state
*
* 6. spi		SPI module disable control:
*				(a) ADC_OFF - Turn off ADC module
*				(b) ADC_ON - Leave ADC module in its default state
*
* 7. usart0		USART0 module disable control:
*				(a) USART0_OFF - Turn off USART0  module
*				(b) USART0_ON - Leave USART0 module in its default state
*
* 8. twi		TWI module disable control:
*				(a) TWI_OFF - Turn off TWI module
*				(b) TWI_ON - Leave TWI module in its default state
*
*******************************************************************************/
void	LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, 
							timer1_t timer1, timer0_t timer0,
							spi_t spi, usart0_t usart0,	twi_t twi)
{
	// Temporary clock source variable 
	unsigned char clockSource = 0;
	
	if (timer2 == TIMER2_OFF)
	{
		if (TCCR2B & CS22) clockSource |= (1 << CS22);
		if (TCCR2B & CS21) clockSource |= (1 << CS21);
		if (TCCR2B & CS20) clockSource |= (1 << CS20);
	
		// Remove the clock source to shutdown Timer2
		TCCR2B &= ~(1 << CS22);
		TCCR2B &= ~(1 << CS21);
		TCCR2B &= ~(1 << CS20);
		
		power_timer2_disable();
	}
	
	if (adc == ADC_OFF)	
	{
		ADCSRA &= ~(1 << ADEN);
		power_adc_disable();
	}
	
	if (timer1 == TIMER1_OFF)	power_timer1_disable();	
	if (timer0 == TIMER0_OFF)	power_timer0_disable();	
	if (spi == SPI_OFF)			power_spi_disable();
	if (usart0 == USART0_OFF)	power_usart0_disable();
	if (twi == TWI_OFF)			power_twi_disable();
	
	if (period != SLEEP_FOREVER)
	{
		wdt_enable(period);
		WDTCSR |= (1 << WDIE);	
	}
	
	lowPowerBodOn(SLEEP_MODE_IDLE);
	
	if (adc == ADC_OFF)
	{
		power_adc_enable();
		ADCSRA |= (1 << ADEN);
	}
	
	if (timer2 == TIMER2_OFF)
	{
		if (clockSource & CS22) TCCR2B |= (1 << CS22);
		if (clockSource & CS21) TCCR2B |= (1 << CS21);
		if (clockSource & CS20) TCCR2B |= (1 << CS20);
		
		power_timer2_enable();
	}
	
	if (timer1 == TIMER1_OFF)	power_timer1_enable();	
	if (timer0 == TIMER0_OFF)	power_timer0_enable();	
	if (spi == SPI_OFF)			power_spi_enable();
	if (usart0 == USART0_OFF)	power_usart0_enable();
	if (twi == TWI_OFF)			power_twi_enable();
}
Example #15
0
void main() {
  // power management setup
  clock_prescale_set(clock_div_16); // 500 kHz clock
  ADCSRA = 0; // DIE, ADC! DIE!!!
  power_adc_disable();
  power_usi_disable();
  power_timer1_disable();
  ACSR = _BV(ACD);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  // millisecond timer setup
  TCCR0A = _BV(WGM01); // mode 2 - CTC
  TCCR0B = _BV(CS01) | _BV(CS00); // prescale = 64
  TIMSK0 = _BV(OCIE0A); // OCR0A interrupt only.
  OCR0A = IRQ_CYCLE_LENGTH + 1;
  millis_counter = 0;
  irq_cycle_pos = 0;
  sei();
  
  // I/O port setup 
  DDRA = _BV(PA0) | _BV(PA1) | _BV(PA2) | _BV(PA3) | _BV(PA7);
  DDRB = _BV(PB0) | _BV(PB1) | _BV(PB2);
  PORTA = _BV(PA5); // enable the pull-up on the button pin
  PORTB = 0;
  
  unsigned char pattern = eeprom_read_byte(EE_PATTERN_NUM);
  if (pattern >= PATTERN_COUNT) pattern = 0;
  place_in_pattern = -1;
  milli_of_next_change = 0;
  button_debounce_time = 0;
  button_press_time = 0;
  ignoring_button = 0;

  while(1) {
    unsigned long now = millis();

    // poll for button events
    unsigned int event = checkEvent();
    switch(event) {
      case EVENT_LONG_PUSH:
        sleepNow();
        continue;
      case EVENT_SHORT_PUSH:
        if (++pattern >= PATTERN_COUNT) pattern = 0;
        place_in_pattern = -1;
        milli_of_next_change = 0;
        eeprom_write_byte(EE_PATTERN_NUM, pattern);
        continue;
    }

    // If it's time to move to the next step in a pattern, pull out its
    // mask and clear all of the LEDs.

    // Note that now > milli_of_next_change is wrong here because our
    // time wraps. now - milli_of_next_change > 0 *looks* the same,
    // but handles cases where the sign differs.
    if (milli_of_next_change == 0 || ((long)(now - milli_of_next_change)) >= 0) {
      // It's time to go to the next step in the pattern
      place_in_pattern++;
      const struct pattern_entry *our_pattern = (struct pattern_entry *)pgm_read_ptr(&(patterns[pattern]));
      struct pattern_entry entry;
      do {
        memcpy_P(&entry, (void*)(&(our_pattern[place_in_pattern])), sizeof(struct pattern_entry));
        // A duration of 0 is the end of the pattern.
        if (entry.duration != 0) break;
        place_in_pattern = 0;   
      } while(1); // This means a pattern of a single entry with 0 duration is kryptonite.
      current_mask = entry.mask;
      // Turn out all the lights
      for(int i = 0; i < LED_COUNT; i++) {
        *((unsigned char *)pgm_read_ptr(&(LED_PORT[i]))) &= ~_BV(pgm_read_byte(&(LED_PIN[i])));
      }
      milli_of_next_change = now + entry.duration;
      current_led = 99; //invalid
    }

    // Now multiplex the LEDSs. Rotate through all of the set bits in the
    // current mask and turn the next one on every pass through this loop.
    if (current_mask != 0) { // if it IS zero, then all the lights are out anyway.
      unsigned char next_led = current_led;
      while(1) { // we know it's not zero, so there's at least one bit set
        if (++next_led >= LED_COUNT) next_led = 0;
        if (current_mask & (1 << next_led)) break; // found one!
      }
      if (next_led != current_led) {
        // turn the old one off and the new one on.
        if (current_led < LED_COUNT) // is it safe?
          *((unsigned char *)pgm_read_ptr(&(LED_PORT[current_led]))) &= ~_BV(pgm_read_byte(&(LED_PIN[current_led])));
        *((unsigned char *)pgm_read_ptr(&(LED_PORT[next_led]))) |= _BV(pgm_read_byte(&(LED_PIN[next_led])));
        current_led = next_led;
      }
    }
  }
}
Example #16
0
void	Low_Power::idle(Period_t period, ADC_t adc, Timer5_t timer5,
                        Timer4_t timer4, Timer3_t timer3, Timer2_t timer2,
                        Timer1_t timer1, Timer0_t timer0, SPI_t spi,
                        USART3_t usart3, USART2_t usart2, USART1_t usart1,
                        USART0_t usart0, TWI_t twi)
{
    // Temporary clock source variable
    unsigned char clockSource = 0;

    if (timer2 == TIMER2_OFF)
    {
        if (TCCR2B & CS22) clockSource |= (1 << CS22);
        if (TCCR2B & CS21) clockSource |= (1 << CS21);
        if (TCCR2B & CS20) clockSource |= (1 << CS20);

        // Remove the clock source to shutdown Timer2
        TCCR2B &= ~(1 << CS22);
        TCCR2B &= ~(1 << CS21);
        TCCR2B &= ~(1 << CS20);

        power_timer2_disable();
    }

    if (adc == ADC_OFF)
    {
        ADCSRA &= ~(1 << ADEN);
        power_adc_disable();
    }

    if (timer5 == TIMER5_OFF)	power_timer5_disable();
    if (timer4 == TIMER4_OFF)	power_timer4_disable();
    if (timer3 == TIMER3_OFF)	power_timer3_disable();
    if (timer1 == TIMER1_OFF)	power_timer1_disable();
    if (timer0 == TIMER0_OFF)	power_timer0_disable();
    if (spi == SPI_OFF)			  power_spi_disable();
    if (usart3 == USART3_OFF)	power_usart3_disable();
    if (usart2 == USART2_OFF)	power_usart2_disable();
    if (usart1 == USART1_OFF)	power_usart1_disable();
    if (usart0 == USART0_OFF)	power_usart0_disable();
    if (twi == TWI_OFF)			  power_twi_disable();

    if (period != SLEEP_FOREVER)
    {
        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }

    lowPowerBodOn(SLEEP_MODE_IDLE);

    if (adc == ADC_OFF)
    {
        power_adc_enable();
        ADCSRA |= (1 << ADEN);
    }

    if (timer2 == TIMER2_OFF)
    {
        if (clockSource & CS22) TCCR2B |= (1 << CS22);
        if (clockSource & CS21) TCCR2B |= (1 << CS21);
        if (clockSource & CS20) TCCR2B |= (1 << CS20);

        power_timer2_enable();
    }

    if (timer5 == TIMER5_OFF)	power_timer5_enable();
    if (timer4 == TIMER4_OFF)	power_timer4_enable();
    if (timer3 == TIMER3_OFF)	power_timer3_enable();
    if (timer1 == TIMER1_OFF)	power_timer1_enable();
    if (timer0 == TIMER0_OFF)	power_timer0_enable();
    if (spi == SPI_OFF)			  power_spi_enable();
    if (usart3 == USART3_OFF)	power_usart3_enable();
    if (usart2 == USART2_OFF)	power_usart2_enable();
    if (usart1 == USART1_OFF)	power_usart1_enable();
    if (usart0 == USART0_OFF)	power_usart0_enable();
    if (twi == TWI_OFF)			  power_twi_enable();
}
Example #17
0
int main()
{



	if (_is_docked()){
		for(int j=0;(j<10);j++)
			for(int i=0;(i<200);i++)
				_delay_ms(5);
	}
				
	scounter=0;
	//Initialized data buffer
	dataIndex=0;
	dataSubindex=0;
	// Blink green for 5 seconds	

	_wocket_initialize();
		

	AC_NUMS=_SAMPLING_RATE *60;
	power_adc_disable();
  	power_spi_disable();
  	power_timer0_disable();
  	power_timer1_disable();
  	power_twi_disable();


	while(1){
			

			

		//Sample only in the main loop because of p
		if(sampleFlag){			
			power_adc_enable();
			_atmega_adc_turn_on();
			sampleFlag=0;
#ifdef _VERSION ==3




			x=_atmega_a2dConvert10bit(ADC0);
		
			y=_atmega_a2dConvert10bit(ADC1);

			z=_atmega_a2dConvert10bit(ADC2);
			//x=y=z=cc++;
			//if (cc>=1024)
			//	cc=0;

				vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

				if (_wPC>40){	//Skip the first samples						
					if (summary_count==0)
					{
						vmag=vmag/24;
						if (vmag>65535)
							acount[ci]=65535;
						else
							acount[ci]=(unsigned short) vmag;
				 		vmag=0;
						++ci;
						if (ci==AC_BUFFER_SIZE)
							ci=0;
						cseq++;
		
						if (ci==si)
						{
							si++;
							if (si==AC_BUFFER_SIZE)
								si=0;
							sseq++;
						}
						acount[ci]=0;
						summary_count=AC_NUMS;
					}else
						summary_count--;
				}
				else if (_wPC==40)
					vmag=0;

			
#else
			//x=_atmega_a2dConvert10bit(ADC3);
			//y=_atmega_a2dConvert10bit(ADC2);
			//z=_atmega_a2dConvert10bit(ADC1);		
#endif
		

		
			 m_SET_X(data[dataIndex],x,dataSubindex);
			 m_SET_Y(data[dataIndex],y,dataSubindex);
			 m_SET_Z(data[dataIndex],z,dataSubindex);

			 dataSubindex++;
			 if (dataSubindex>=4)
			 	dataSubindex=0;
		

			 
			 //Most of the time the data buffer with 750 will not overflow
			 //and will be enough to transmit the data, data will go from 0 up to a specific
			 //value

			if (_wTM==_TM_Continuous)
			{
									
				switch(dataSubindex){
				case 1:
						m_GET_X(x,data[dataIndex].byte1,data[dataIndex].byte2,0);
						m_GET_Y(y,data[dataIndex].byte2,data[dataIndex].byte3,0);
						m_GET_Z(z,data[dataIndex].byte3,data[dataIndex].byte4,0);
						break;
				case 2:
						m_GET_X(x,data[dataIndex].byte4,data[dataIndex].byte5,1);
						m_GET_Y(y,data[dataIndex].byte6,data[dataIndex].byte7,1);
						m_GET_Z(z,data[dataIndex].byte7,data[dataIndex].byte8,1);
						break;
				case 3:
						m_GET_X(x,data[dataIndex].byte8,data[dataIndex].byte9,2);
						m_GET_Y(y,data[dataIndex].byte9,data[dataIndex].byte10,2);
						m_GET_Z(z,data[dataIndex].byte11,data[dataIndex].byte12,2);
						break;
				case 0:
						m_GET_X(x,data[dataIndex].byte12,data[dataIndex].byte13,3);
						m_GET_Y(y,data[dataIndex].byte13,data[dataIndex].byte14,3);
						m_GET_Z(z,data[dataIndex].byte14,data[dataIndex].byte15,3);
						break;
				}
											
		
				if (justconnected==1)
				{
					_send_tm();
					justconnected=2;
				}		
				//_send_pdu(x,y,z);
				_send_uncompressed_pdu(x, y, z);

				
					//Send summary activity count
				/*	for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

			}
			else 
			{
				if ((dataSubindex==0) && (batch_counter<750))
					batch_counter++;
				if (connected){
					_greenled_turn_on();
						
					gotack=1;
					tester=0;

					if (_wTM==_TM_Continuous)
						continue;
						
					for (int ixz=0;(ixz<100);ixz++) {                                                                                      
       						_bluetooth_transmit_uart0_byte(0xff); 

							
						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}

					_send_fv();
					_send_sr();					 
					_send_tm();
					_send_batch_count((batch_counter-1)*4);																	
					_send_acs();
					//Send summary activity count
					/*for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

					
					if ((batch_counter>0) && (batch_counter<750)) // Go from 0 up to batch_counter
					{						
						for (int i=0;(i<(batch_counter-1));i++)
						{
							m_GET_X(x,data[i].byte1,data[i].byte2,0);
							m_GET_Y(y,data[i].byte2,data[i].byte3,0);
							m_GET_Z(z,data[i].byte3,data[i].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte4,data[i].byte5,1);
							m_GET_Y(y,data[i].byte6,data[i].byte7,1);
							m_GET_Z(z,data[i].byte7,data[i].byte8,1);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte8,data[i].byte9,2);
							m_GET_Y(y,data[i].byte9,data[i].byte10,2);
							m_GET_Z(z,data[i].byte11,data[i].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte12,data[i].byte13,3);
							m_GET_Y(y,data[i].byte13,data[i].byte14,3);
							m_GET_Z(z,data[i].byte14,data[i].byte15,3);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
						}

						
						if (batch_counter>0){
						//copy end item into start
						data[0].byte1=data[batch_counter].byte1;
						data[0].byte2=data[batch_counter].byte2;
						data[0].byte3=data[batch_counter].byte3;
						data[0].byte4=data[batch_counter].byte4;
						data[0].byte5=data[batch_counter].byte5;
						data[0].byte6=data[batch_counter].byte6;
						data[0].byte7=data[batch_counter].byte7;
						data[0].byte8=data[batch_counter].byte8;
						data[0].byte9=data[batch_counter].byte9;
						data[0].byte10=data[batch_counter].byte10;
						data[0].byte11=data[batch_counter].byte11;
						data[0].byte12=data[batch_counter].byte12;
						data[0].byte13=data[batch_counter].byte13;
						data[0].byte14=data[batch_counter].byte14;
						data[0].byte15=data[batch_counter].byte15;
						}


					}else{

						int current=dataIndex+1;
						int end =dataIndex;
						if (current>=750)
							current=0;
						while(current!=end)
						{
							m_GET_X(x,data[current].byte1,data[current].byte2,0);
							m_GET_Y(y,data[current].byte2,data[current].byte3,0);
							m_GET_Z(z,data[current].byte3,data[current].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte4,data[current].byte5,1);
							m_GET_Y(y,data[current].byte6,data[current].byte7,1);
							m_GET_Z(z,data[current].byte7,data[current].byte8,1);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte8,data[current].byte9,2);
							m_GET_Y(y,data[current].byte9,data[current].byte10,2);
							m_GET_Z(z,data[current].byte11,data[current].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte12,data[current].byte13,3);
							m_GET_Y(y,data[current].byte13,data[current].byte14,3);
							m_GET_Z(z,data[current].byte14,data[current].byte15,3);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							current++;
							if (current==750)
								current=0;

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
							
						}

						//copy end item into start
						data[0].byte1=data[end].byte1;
						data[0].byte2=data[end].byte2;
						data[0].byte3=data[end].byte3;
						data[0].byte4=data[end].byte4;
						data[0].byte5=data[end].byte5;
						data[0].byte6=data[end].byte6;
						data[0].byte7=data[end].byte7;
						data[0].byte8=data[end].byte8;
						data[0].byte9=data[end].byte9;
						data[0].byte10=data[end].byte10;
						data[0].byte11=data[end].byte11;
						data[0].byte12=data[end].byte12;
						data[0].byte13=data[end].byte13;
						data[0].byte14=data[end].byte14;
						data[0].byte15=data[end].byte15;
					}


			

					
					batch_counter=0;
					dataIndex=0;
					seconds_passed=0;
					while (seconds_passed<400)
					{
						_delay_ms(5);
						seconds_passed++;
						_receive_data();

						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}						
					//connected=0;

					//Don't turn off the radio if a request to switch mode has been received
					if (_wTM==_TM_Continuous)
						_bluetooth_turn_on();	
					else
						_bluetooth_turn_off();		
					
					command_counter=0;
					seconds_disconnected=0;
					_greenled_turn_off();
				
				}
			}
			_atmega_adc_turn_off();
			power_adc_disable();

			if ((dataSubindex==0) && (!connected))
				dataIndex++;			
			if (dataIndex==750)
				dataIndex=0;
			connected=0;			
			
		}	
		
			cli();
			set_sleep_mode(SLEEP_MODE_IDLE);
			//set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    		sleep_enable();
    		sleep_bod_disable(); 	
    		sei();
    		sleep_cpu();
    		sleep_disable();


	}

	return 0;
}
Example #18
0
void ArduboyAudio::off()
{
  audio_enabled = false;
  power_timer1_disable();
  power_timer3_disable();
}
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);	
				}
			}
		}
		
    }
}