4.4.1 Peripheral Initialization

The initialization of the peripherals in ATtiny1627 can be done using START drivers. As this application note is focusing on the ADC, only the bare metal ADC initialization is detailed here.

void ADC_0_init()
{
	ADC0.CTRLB = ADC_PRESC_DIV2_gc;		      /* System clock divided by 2 */
	ADC0.CTRLF = PIR_OVERSAMPLE_RATE;		    /* Samples accumulation */
	ADC0.CTRLC = ADC_REFSEL_1024MV_gc		    /* Internal 1.024V Reference */
		    | (TIMEBASE_VALUE << ADC_TIMEBASE_gp);  /* timebase value */
				
	ADC0.CTRLE = 0x12;				     /* Sample Duration */

	ADC0.DBGCTRL = ADC_DBGRUN_bm;			 /* Debug run: enabled */
	ADC0.COMMAND = ADC_DIFF_bm			    /* Differential ADC Conversion enabled */
		      | ADC_MODE_BURST_gc;		    /* Burst Accumulation */
				 
	ADC0.INTCTRL = ADC_RESRDY_bm;			 /* Result Ready Interrupt enabled */

	ADC0.MUXNEG = ADC_VIA_PGA_gc			  /* Via PGA */
		     | ADC_MUXNEG_AIN5_gc;		    /* ADC input pin 5, PIR sensor */
				
	ADC0.MUXPOS = ADC_VIA_PGA_gc			  /* Via PGA */
		     | ADC_MUXPOS_AIN9_gc;		    /* ADC input pin 9, PIR sensor */
				
	ADC0.PGACTRL = ADC_PGAEN_bm			   /* PGA Enabled */
		      | PIR_PGA_GAIN			   /* Gain */
		      | ADC_ADCPGASAMPDUR_15CLK_gc          /* PGA Sample Duration */
		      | ADC_PGABIASSEL_1_2X_gc;             /* PGA bias set to 1/2 */
				  
	ADC0.CTRLA = ADC_ENABLE_bm		    	/* Enable ADC */
		    | ADC_RUNSTDBY_bm;		 	/* Enable ADC to run in Standby Sleep mode */
			    
	ADC0.INTFLAGS = ADC_RESRDY_bm;			/* Clear ADC Result Ready Interrupt Flag */
}

In the example application, the ADC is initialized in ADC_0_init().

The PGA has been enabled to amplify the signal coming from the PIR sensor, and the gain can be adjusted by changing the define PIR_PGA_GAIN found in main.c. The ADC is running in Differential mode and Burst mode to accumulate samples and amplify the signal further. The number of accumulated samples can be adjusted in PIR_OVERSAMPLE_RATE.

In Differential mode, the voltage difference between the two inputs is measured by the ADC.

In Burst mode, a burst of n conversions is accumulated as fast as possible after a single trigger, and the conversion results are accumulated into a single ADC result.