SAM C21 Sigma-Delta ADC Example Project
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.
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:
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.
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.
The ATIO1-XPRO board provides a light sensor that is connected to the ADC input on the SAM C21. The light sensor is a single-ended analog output, but the SDADC has a differential input, so an ADC input is also needed. On the ATIO1-XPRO board, the ADC pin is connected to the output of an RC filter that can be driven by a PWM signal. In our case, we want the ADC 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 input.
Here is a simplified equivalent circuit:
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:
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:
Open the Solution
Select File > 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:
Next, under the project's Tool settings, select your EDBG target from the pull-down. Select SWD as the interface:
Save the tool setting by clicking 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.
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:
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:
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:
The Locals watch window, showing the value of the local variable result should be displayed:
Results
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:
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 measured values of result for covered and uncovered conditions vary based on your light source and sensor coverage.
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.