SAM C21 Sigma-Delta ADC Example Project

Last modified by Microchip on 2023/11/10 11:08

Objective

This page provides a simple SAM C21 Sigma-Delta ADC (SDADC) GCC code example for the ATSAMC21J18A MCU.

The application code configures the SDADC module, then manually triggers conversions of a light sensor signal connected to the development board. The project is intended to be run in debug mode, with a program memory breakpoint set and hit every time a conversion is completed, so the result can be examined in the Atmel Studio 7 IDE.

The application is designed to work using the SAM C21 Xplained PRO Evaluation Kit which contains the ATSAMC21J18A Arm® Cortex®-M0+ MCU. The IO1 Xplained Pro Extension Header is used to provide the light sensor input signal.

SAM C21 Xplained PRO Evaluation Kit and  The IO1 Xplained Pro Extension Header

This application uses:

  • Sigma-Delta Analog-to-Digital Converter (ADC) Module
  • PB13 (LED Output, to the IO1 Xplained Pro)
  • PB09 (ADC+ Input, from the Light Sensor circuit on IO1 Xplained Pro)
  • PB08 (ADC- Input, from the RC filter on IO1 Xplained Pro)
  • PB13 (PWM Out, to the RC filter on IO1 Xplained Pro)

This code example uses a direct/indirect MCU register C-coding style (i.e., no software framework) and is built using the GCC compiler toolchain within Atmel Studio 7 IDE.

Visit the following page to see a detailed description of how the SDADC registers are configured for this application:

Back to Top


Reference Materials

Hardware Tools

Software Tools

We recommend extracting the ZIP file to your C:\ folder.
You should see the folder C:\MTT\32arm\samc21\code-examples-gcc\sdadc\sdadc-example-project containing the solution sdadc-example-project.atsln.

Back to Top


Connection Diagram

SAM C21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMC21J18A using Serial Wire Debug (SWD) interface. The EDBG also includes a Virtual Com port interface over UART, a Data Gateway Interface (DGI) over SPI and TWI, and it monitors four of the SAM C21 GPIOs. Atmel Studio 7 is used as a front-end for the EDBG.

The application is connected to an LED output and light sensor circuit on the IO1 Xplained Pro (ATIO1-XPRO), which is connected to the EXT1 pin connections as shown in the accompanying image.

ATIO1-XPRO board provides a light sensor that is connected to the ADC(+) input on the SAM C21

The ATIO1-XPRO board provides a light sensor that is connected to the ADCadd input on the SAM C21. The light sensor is a single-ended analog output, but the SDADC has a differential input, so an ADCdelete input is also needed. On the ATIO1-XPRO board, the ADCdelete pin is connected to the output of an RC filter that can be driven by a PWM signal. In our case, we want the ADCdelete input to be grounded to work with the single-ended light sensor, so we will not use the PWM. Instead, we will drive that pin low to provide a ground reference to the ADC delete input.

Here is a simplified equivalent circuit:

Here is a simplified equivalent circuit for this example

Back to Top


VCC Selection Header

The SAM C21 Xplained Pro has a 3-pin header labeled VCC-SEL on the board. This header can be used to select between 3.3 V and 5.0 V as the supply voltage for the ATSAMC21J18A, peripherals, and extension headers by placing a jumper on the left (3.3 V) or right (5.0 V). Selecting 5.0 V supplies the kit directly from the USB or an external 5.0 V source. Selecting 3.3 V supplies the kit from an onboard regulator.

Select 3.3 V as VCC by jumpering the left 2 pins on this header as shown by the red box on the following image:

jumper the left 2 pins on this header as shown by the red box on the image

Back to Top


Procedure

Attach the IO1 Xplained Pro Extension header to EXT1. Set the VCC jumper on the SAM C21 Xplained Pro board to 3.3 V and then attach it to your computer using a USB A-to-MicroB cable as shown in the image above.

Start Atmel Studio 7.

If the board has been successfully enumerated, you should see the board image come up in Atmel Studio which also identifies the IO1 Xplained Pro Extension as connected:

you should see the board image come up in Atmel Studio

The board is identified by the last four digits of its serial number (see the sticker on the bottom of the board). In this example, the last four digits are "3514".

Open the Solution

Select File > Open > Project/Solution….

Atmel studio Open project solution

Navigate to the solution folder and select the sdadc-example-project.atsln solution file:


Configure the Debugger

Next, you need to configure the debugger in Atmel Studio to discover and connect to the target EDBG IC on your Xplained Pro board.

First, select the project in the Solution Explorer window, then select Project > Properties as shown:

Select the project in the Solution Explorer window, then select Project > Properties

Next, under the project's Tool settings, select your EDBG target from the pull-down. Select SWD as the interface:

under the project's Tool settings, select your EDBG target from the pull-down

Save the tool setting by clicking the Save All button:

Click the save all button


Rebuild/Program the Target for Debugging

Click on the Start Debugging and Break icon in Atmel Studio which creates a debug project build, downloads it into the SAM C21 on-board, and prepares the debugger in Atmel Studio to interact with this code.

Click on the Start Debugging and Break icon

If prompted, upgrade the EDBG firmware on the board:

If prompted, upgrade the EDBG firmware on the board

You may need to click on Start Debugging and Break icon again after a EDBG firmware upgrade in order to rebuild/program the debug code into the target.

You should see a set of debug controls displayed in the IDE, and the debug program counter position (yellow arrow) pointing to the start of the main( ) function:

You should see a set of debug controls displayed in the IDE, and the debug program counter position (yellow arrow) pointing to the start of the main( ) function


Set Program Breakpoint for Debugging

In main.c, scroll down to the __NOP() instruction on line 147 then click once in the left column as shown until you see a red dot as shown:

In main.c, scroll down to the __NOP() instruction on line 147 then click once in the left column

The program will run once around the while(1) loop and stop here after the light sensor signal is digitized.


Open Watch Window to Examine the Result

To see the digitized value in the local variable result, open a Locals debug window to display the value of result. Select Debug > Windows > Locals as shown:

Open a Locals debug window to display the value of result

The Locals watch window, showing the value of the local variable result should be displayed:

The Locals watch window, showing the value of the local variable result should be displayed

The initial value of result will be whatever was in the stack RAM memory at power up. It will be nonsensical.

Back to Top


Results

Cover the light sensor with your hand, then press the Continue button:

Cover the light sensor with your hand, then press the Continue button

The value of result should be in the high-end of an uint16_t result, since the light is being blocked, and the pull-up resistor will pull the signal voltage to 3.3 V:

The value of result should be in the high-end of an uint16_t result, since the light is being blocked, and the pull-up resistor will pull the signal voltage to 3.3 V

Uncover the light sensor, then press the Continue button again.
The value of result should be in the low-end of an uint16_t result, since the light is being absorbed by the phototransistor, causing a current flow, and pulling the signal voltage to 0.0 V:

The value of result should be in the low-end of an uint16_t result, since the light is being absorbed by the phototransistor, causing a current flow, and pulling the signal voltage to 0.0 V

The measured values of result for covered and uncovered conditions vary based on your light source and sensor coverage.

Back to Top


Conclusions

This project has provided a simple example of how to set up and use SDADC on the SAM C21 MCU. Follow the links below to learn more about this module.

Learn More

Back to Top