4.4.3 Warm-up and Filter Creation

This section of the code is run before the main loop as part of the initialization of the system. The warm-up time is just a delay loop to give the PIR sensor time to adjust to the IR radiation and can be adjusted in the define PIR_WARMUP_TIME_MS. The PIT is connected through the Event System to LED 0 on the ATtiny1627 Nano and will flash at 1 Hz through the entire warm-up and filter creation part of the code to indicate to the user that the system is warming up.

When the warm-up time is over, the ADC is started using the following command:
ADC0.COMMAND |= ADC_START_EVENT_TRIGGER_gc; 
and the MCU is put in Standby sleep mode.

The PIT is configured to start the ADC conversion through the Event System. When the PIT sends the event to start an ADC conversion, the ADC is started and performs the configured conversion and issues a result ready (REDRDY) interrupt on completion that wakes the MCU. How often the PIT sends an event can be configured in the define PIR_SAMPLE_RATE_PER_SECOND.

void warm_up_and_filter_creation()
{
	uint8_t i = 0;

	EVSYS.USEREVSYSEVOUTB = EVSYS_USER_CHANNEL2_gc; /* Flash LED0 at 1 Hz to indicate warm-up */

	/* Put warm-up delay/code here */
	_delay_ms(PIR_WARMUP_TIME_MS);
	/* End of warm-up delay/code */

	ADC0.COMMAND |= ADC_START_EVENT_TRIGGER_gc;		/* Enable ADC to be triggered by Event*/

	while(i < PIR_LONG_TERM_FILTER_RANGE)
	{
		sleep_mode();								/* Enter Standby sleep mode and wait for ADC to complete*/
		accumulated_long_term_average = accumulated_long_term_average + adc_result; 
	
		if (i < PIR_SHORT_TERM_FILTER_RANGE)
		{
			accumulated_short_term_average = accumulated_short_term_average + adc_result;
		}
		i++;
	}

	EVSYS.USEREVSYSEVOUTB = 0; /* Warm up and average filter creation complete, stop flashing LED0 */
}