Example #1
0
int main()
{
    //disable watchdog to avoid reset
    MCUSR = 0;
    wdt_disable();
    _atmega_initialize(CPU_CLK_PRESCALAR_1024);

    word=eeprom_read_word((uint16_t *)((uint16_t)BAUD_RATE_ADDRESS));
    if (word==BAUD_9600)
        word=ATMEGA324P_BAUD_9600;
    else if (word==BAUD_19200)
        word=ATMEGA324P_BAUD_19200;
    else if (word==BAUD_28800)
        word=ATMEGA324P_BAUD_28800;
    else if (word==BAUD_38400)
        word=ATMEGA324P_BAUD_38400;
    else if (word==BAUD_57600)
        word=ATMEGA324P_BAUD_57600;
    else
    {
        word=ATMEGA324P_BAUD_38400;
        eeprom_write_word((uint16_t *)BAUD_RATE_ADDRESS,BAUD_38400);
    }
    _atmega324p_init(word);
    _atmega324p_green_led_off();

    led_counter=0;
    _rn41_on();
    _mma7260qt_wakeup();




    while(sleep==0)  {            /* Forever */

        //set_sleep_mode(SLEEP_MODE_PWR_SAVE);
        //sleep_enable();
        //MCUCR = 0x60;            // Disable BOD during sleep to reduce power consumption (used to monitor supply voltage)
        //MCUCR = 0x40;
        //sleep_cpu();
        //sleep_disable();
        _delay_ms(5);
        if (!_atmega324p_shutdown())
            sleep=1;

        led_counter++;
        if (_rn41_is_connected()) {
            _atmega324p_yellow_led_off();
            if (led_counter==1)
                _atmega324p_green_led_on();
            else if (led_counter==2)
                _atmega324p_green_led_off();
            else if (led_counter>=2000)
                led_counter=0;
        } else {
            if (led_counter==1)
                _atmega324p_yellow_led_on();
            else if (led_counter==2)
                _atmega324p_yellow_led_off();
            else if (led_counter>=2000)
                led_counter=0;
        }

    }


    _atmega_finalize();
    //shutdown to minimize power
    //_atmega324p_reset();
    //make sure watchdog timer is disabled
    MCUSR = 0;
    wdt_disable();

    cli();
    TIMSK2=0;
    _atmega324p_yellow_led_off();
    _atmega324p_green_led_off();
    //_rn41_off();
    _mma7260qt_sleep();
    sleep_enable();
    set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    sleep_cpu();


    //won't be executed as long as the power save is entered correctly
    _atmega324p_yellow_led_on();
    _atmega324p_green_led_on();

    return 0;
}
Example #2
0
void _wocket_initialize(void) //This function initializes the wocket
{
	// Disable the watchdog timer. It has to be done at the beginning of the program.
	_atmega_disable_watchdog();
	_atmega_initialize(CPU_CLK_PRESCALAR_1024);
	num_skipped_timer_interrupts = 10;//(F_CPU/1024)/PERFECT_SAMPLING_FREQUENCY;
	
	unsigned short battery = _atmega_a2dConvert10bit(IN_VSENSE_BAT);
	if (battery < 700)
	{// Blink yellow 3times for 5 seconds if the battery is not fully charged 
		for (int i = 0; (i < 3); i++){
			_yellowled_turn_on();		
			for (int j = 0; (j < 200); j++)
				_delay_ms(5);
			_yellowled_turn_off();
			for (int j = 0; (j < 200); j++)
				_delay_ms(5);
		}
	}
	
	if (battery > 100)
	{//Load the status byte from the EEPROM  
		_INITIALIZED = eeprom_read_byte(&_NV_INITIALIZED);		
	}
	else
	{ //turn on the yellow led for 5 seconds then shutdown 
		_yellowled_turn_on();		
		for(int  i = 0; (i < 1000); i++)
			_delay_ms(5);
		_yellowled_turn_off();
		_delay_ms(500);
		_atmega_finalize();
		return;
	}
	
	// If the wocket has been initialized before, read the parameters from EEPROM and blinks green once	
	if (_INITIALIZED == _WOCKET_INITIALIZED)
	{		
		if (battery > 300)
		{
			_SAMPLING_RATE = eeprom_read_byte(&_NV_SAMPLING_RATE);
			_wTM = eeprom_read_byte(&_NV_WTM);

			_wTCNT2 = eeprom_read_byte(&_NV_TCT);
			_wTCNT2_reps = eeprom_read_byte(&_NV_TCTREPS);
			_wTCNT2_last = eeprom_read_byte(&_NV_TCTLAST);

			_wBTCAL100 = eeprom_read_word(&_NV_BTCAL100);
			_wBTCAL80  = eeprom_read_word(&_NV_BTCAL80);
			_wBTCAL60  = eeprom_read_word(&_NV_BTCAL60);
			_wBTCAL40  = eeprom_read_word(&_NV_BTCAL40);
			_wBTCAL20  = eeprom_read_word(&_NV_BTCAL20);
			_wBTCAL10  = eeprom_read_word(&_NV_BTCAL10);

			_wX1G_CAL  = eeprom_read_word(&_NV_X1G_CAL);
			_wXN1G_CAL = eeprom_read_word(&_NV_XN1G_CAL);
			_wY1G_CAL  = eeprom_read_word(&_NV_Y1G_CAL);
			_wYN1G_CAL = eeprom_read_word(&_NV_YN1G_CAL);
			_wZ1G_CAL  = eeprom_read_word(&_NV_Z1G_CAL);
			_wZN1G_CAL = eeprom_read_word(&_NV_ZN1G_CAL);

			_wPDT = eeprom_read_byte(&_NV_PDT);

			_greenled_turn_on();		
			for(int i = 0; (i < 200); i++)
				_delay_ms(10);
			_greenled_turn_off();
		}
	}

		
	// If the wocket has never been initialized, write the default settings and blink green 3 times 
	else
	{ 
		_SAMPLING_RATE = 40; 
		_wTM = _WTM_Continuous;
		//_wTM = _WTM_Burst_60;

		// Calculate the timer variables used to sample at the right frequency
		_wocket_initialize_timer2_interrupt();
		
		// Set the overflow interrupt timer 		
		switch(_wTM)
		{
			case _WTM_Continuous:	
				_MAX_SAMPLING_RATE = 126; //This limitation is due to the definition of SEND_SR  and GET_SR commands
				break;					  // The MCU provided in wocket able to sample at higher rates 								
			case _WTM_Burst_30:    	//a transfer mode that send the burst every 30 secs
				_MAX_SAMPLING_RATE = 80;		
				break;
			case _WTM_Burst_60:		//a transfer mode that send the burst every 60 secs
				_MAX_SAMPLING_RATE = 40;		
				break;
			case _WTM_Burst_90:		//a transfer mode that send the burst every 90 secs
				_MAX_SAMPLING_RATE = 30;		
				break;
			case _WTM_Burst_120:		//a transfer mode that send the burst every 120 secs
				_MAX_SAMPLING_RATE = 20;		
				break;
			default:
				break;
		}
		
		if (_SAMPLING_RATE > _MAX_SAMPLING_RATE)
		{
			_SAMPLING_RATE = _MAX_SAMPLING_RATE;
		
		}
		if (_SAMPLING_RATE < _MIN_SAMPLING_RATE)
		{
			_SAMPLING_RATE = _MIN_SAMPLING_RATE;		
		}
			
		
		if (battery > 300)
		{	
			// Write the parameters to the EEPROM
			eeprom_write_byte(&_NV_SAMPLING_RATE,_SAMPLING_RATE);
				
			eeprom_write_byte(&_NV_TCT,_wTCNT2);
			eeprom_write_byte(&_NV_TCTREPS,_wTCNT2_reps);
			eeprom_write_byte(&_NV_TCTLAST,_wTCNT2_last);			

			eeprom_write_byte(&_NV_WTM,_wTM);
			eeprom_write_byte(&_NV_STATUS_BYTE,0x00);
			eeprom_write_byte(&_NV_SENS,_wSENS);

			//Set default battery calibration values
			eeprom_write_word(&_NV_BTCAL100,_DEFAULTBTCAL100);
			eeprom_write_word(&_NV_BTCAL80, _DEFAULTBTCAL80);
			eeprom_write_word(&_NV_BTCAL60, _DEFAULTBTCAL60);
			eeprom_write_word(&_NV_BTCAL40, _DEFAULTBTCAL40);
			eeprom_write_word(&_NV_BTCAL20, _DEFAULTBTCAL20);
			eeprom_write_word(&_NV_BTCAL10, _DEFAULTBTCAL10);

			_wBTCAL100 = _DEFAULTBTCAL100;
			_wBTCAL80  = _DEFAULTBTCAL80;
			_wBTCAL60  = _DEFAULTBTCAL60;
			_wBTCAL40  = _DEFAULTBTCAL40;
			_wBTCAL20  = _DEFAULTBTCAL20;
			_wBTCAL10  = _DEFAULTBTCAL10;

			//Set default Accelerometer calibration values
			eeprom_write_word(&_NV_X1G_CAL, _DEFAULT_X1G_CAL);
			eeprom_write_word(&_NV_XN1G_CAL,_DEFAULT_XN1G_CAL);
			eeprom_write_word(&_NV_Y1G_CAL, _DEFAULT_Y1G_CAL);
			eeprom_write_word(&_NV_YN1G_CAL,_DEFAULT_YN1G_CAL);
			eeprom_write_word(&_NV_Z1G_CAL, _DEFAULT_Z1G_CAL);
			eeprom_write_word(&_NV_ZN1G_CAL,_DEFAULT_ZN1G_CAL);

			_wX1G_CAL  = _DEFAULT_X1G_CAL;
			_wXN1G_CAL = _DEFAULT_XN1G_CAL;
			_wY1G_CAL  = _DEFAULT_Y1G_CAL;
			_wYN1G_CAL = _DEFAULT_YN1G_CAL;
			_wZ1G_CAL  = _DEFAULT_Z1G_CAL;
			_wZN1G_CAL = _DEFAULT_ZN1G_CAL;

			_wPDT = _DEFAULT_PDT;
			eeprom_write_byte(&_NV_PDT, _wPDT);
		}

		// Set the initialized flag in the status byte
		_INITIALIZED = _WOCKET_INITIALIZED;

		// Write the status byte to the EEPROM		
		eeprom_write_byte(&_NV_INITIALIZED,_INITIALIZED);
				
		// Blink green for 5 seconds	
		for (int i = 0; (i < 3); i++){
			_greenled_turn_on();		
			for(int j = 0;(j < 200); j++)
				_delay_ms(5);
			_greenled_turn_off();
			for(int j = 0; (j < 200); j++)
				_delay_ms(5);
		}		
	}

	_DEFAULT_SHUTDOWN = (unsigned long)_wPDT * (unsigned long)_SAMPLING_RATE * (unsigned long)60;
	_wShutdownTimer = _DEFAULT_SHUTDOWN;
	
    // Enable Timer 2 
    _atmega_enable_timer2(CPU_CLK_PRESCALAR_1024); 
}