3.1 Performing an AD Conversion

Description

When you perform an A/D conversion, you are getting a digital reading of an analog voltage signal applied to one of the A/D board's analog input channels. The A/D board uses a device called an analog-to-digital (A/D) converter to convert the real-world analog signal (temperature, pressure, tank level, speed, etc.) into a digital value that the digital computer electronics can handle.‌

Typically the digital value, or the underlying voltage, is then converted to engineering units, such as temperature or speed, using the appropriate conversion formula for the device or sensor that generated the original voltage signal. These conversion formulas are provided by the manufacturer of the device or sensor. Both conversions (digital to volts and volts to engineering units) can be combined into a single formula if desired. The SAMD51 function name for A/D conversions is DSCSAM_ADSample.‌

Step-By-Step Instructions

Create and initialize an A/D settings structure (DSCSAM_ADSETTINGS). Call DSCSAM_ADSetSettings and pass it a pointer to this structure in order to setup the driver for A/D operations.‌

Call DSCSAM_ADSample and pass it a pointer to your sample variable. This will generate an A/D conversion and the result will be stored in your sample variable.‌

Example of Usage for DSCSAM_ADSample.

...

DSCSAM_ADSETTINGS dscadsettings; // structure containing A/D conversion settings
ERRPARAMS errparams;  // structure for returning error code and error string
SWORD sample;

...

/* Step 1 */ 

// These are example values; your application will vary. 
// See DSCADSETTINGS struct and hardware manuals for details. 

dscadsettings.Adchset=0;
dscadsettings.Currentch=0;
dscadsettings.Lowch=0;
dscadsettings.Highch=0;
dscadsettings.Sedi=0;
dscadsettings.ScanEnable = 0;
dscadsettings.Adtrig=0;
dscadsettings.Adref=0;
dscadsettings.Adres=0;
dscadsettings.Adsamples=1;
dscadsettings.Adtag=0;

/* Step 2 */ 

if( DSCSAM_ADSetSettings(&dscadsettings ) != DE_NONE )
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_ADSetSettings error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}

/* Step 3 */ 

if( DSCSAM_ADSample(&sample ) != DE_NONE )
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_ADSample error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}
			
printf("Sample value = 0x%X\n",(unsigned short)sample);

...		

Last updated