Objective
This project shows you how to:
- Use Atmel START to add and configure an Analog-to-Digital Converter (ADC) and I²C driver for your project.
- Export your project for the Atmel Studio 7 IDE.
- Use Advanced Software Framework 4 (ASF4) example code to:
- Measure the analog output of a light sensor (using a SAM D21 ADC)
- Read the value from a temperature sensor over an I²C bus (using a SAM D21 SERCOM configured for I²C)
- Program the SAM D21 Xplained Pro Evaluation Kit to verify your code is working.
Materials
Hardware Tools
Tool | About | Purchase |
---|---|---|
SAM D21 Xplained Pro
Evaluation Kit |
| |
I/O1 Xplained Pro
Extension Kit |
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
Atmel® Studio
Integrated Development Environment |
| | | | |
Atmel® START (ASF4)
Integrated Software Framework |
| Web Based |
Optional Lab Manual
A hardcopy of this tutorial is available here:
Using Atmel START with the SAM D21 MCU >
Procedure:
Step 1: Create a new Atmel START project
Step 2: Add and configure an ADC driver
Step 3: Add and configure the temperature sensor middleware and its associated I²C driver
Step 4: Add a Delay driver (sensor measurements every second)
Step 5: Save and export the application for the Studio 7 IDE
Step 6: Import the Atmel START project into the Studio 7 IDE
Step 7: Modify ADC example code to read light sensor data
Step 8: Program the board
Step 9: Verify light sensor measurements
Step 10: Modify I²C example code to read temp sensor data and verify measurements
Step 1: Create a new Atmel START project
1
Open a web browser, go to http://start.atmel.com, and select CREATE NEW PROJECT.
The project is created in Atmel START and you now have access to the DASHBOARD view. Scroll to the bottom of this window to verify the ATSAMD21J18A is the selected device and explore its capabilities.
Step 2: Add and configure an ADC driver
1
The I/O1 Xplained Pro uses a TEMT6000 light sensor. The sensor data can be read by an ADC pin on any Xplained Pro MCU board. Determine which pin from the SAM D21 Xplained Pro interfaces with it.
- Determine which pin on the I/O1 Explained Pro board is used for the light sensor.
- The I/O1 Xplained Pro Extension Kit User Guide specifies this:
- Determine which SAM D21 pin connects to pin 3 on the EXT2 interface.
- The SAM D21 Xplained Pro Users Guide specifies this:
Notice SAM D21 pin number PA10 (function AIN[18]) is connected to the light sensor.
3
Configure the ADC driver:
- Click on the 'ADC_0' component block to start its configuration.
- Configure ADC Component Settings:
- Driver:HAL:Driver:ADC_Async
- Configure ADC Signals:
- AIN/18 - PA10: Enabled
- Configure ADC Basic Configuration:
- Positive Mux Input Selection: ADC AIN18 pin
- Negative Mux Input Selection: Internal ground
The ADC driver is now configured.
Step 3: Add and configure the temperature sensor middleware and its associated I²C driver
The I/O1 Xplained Pro extension board uses a Microchip AT30TSE758 temperature sensor chip with an 8 kb serial EEPROM inside. The sensor includes programmable high and low-temperature alarms, user selectable temperature resolution up to 12 bits and an I²C/SMBus™ compatible serial interface. The SAM D21 reads the temperature using its serial communication interface (SERCOM) peripheral configured for I²C.
1
Determine which pins from the SAM D21 Xplained Pro interface with the temperature sensor.
- Determine which pins on the I/O1 Explained Pro board is used for the temperature sensor.
- The I/O1 Xplained Pro Extension Kit User Guide specifies this:
- Determine which SAM D21 pins connect to pins 11 and 12 on the EXT2 interface.
- The SAM D21 Xplained Pro Users Guide specifies this:
Notice SAM D21 pin numbers PA08 and PA09 (function SERCOM2 I²C SDA and SCL) are connected to the temperature sensor.
Notice that adding the temperature sensor middleware automatically adds an I²C driver which is required to interface to the sensor.
The Temperature Sensor middleware is now added to the application.
If you click on the Temperature Sensor component block, you will see the dependencies of that middleware.
3
Configure the I²C driver.
- Click on the 'I2C_INSTANCE' component block to start its configuration.
- Configure the I²C Component Settings:
- Driver: HAL:Driver:I2C_Master_Sync
- Mode: I²C Master Standard/Fast-mode
- Instance: SERCOM2
- Configure I²C Signals:
- SCL: PA09
- SDA: PA08
- Configure I²C Basic Configuration:
- I²C Bus Clock speed (Hz): 100 kHz
The I²C driver is now configured.
Step 4: Add a Delay driver (sensor measurements every second)
This driver provides functions which allow you to add delays in µs or ms using the Cortex®-M0+ SysTick timer.
- Add the Delay driver:
- Select DASHBOARD and click ‘Add software component’.
- Expand ‘Drivers’, find the Delay driver and click on it.
- Click Add component(s).
The Delay driver is now added to the application.
Step 5: Save and export the project for the Studio 7 IDE
You have now created and configured your Atmel START based project. It’s a good idea to save this configuration so you can make changes to it sometime in the future. Atmel START allows you to restore any project using its configuration file (*.atstart file format).
Your Atmel START project has been exported for the Studio 7 IDE in a *.atzip file format (standard zip format automatically recognized by Studio 7).
Step 6: Import the Atmel START project into the Studio 7 IDE
Note a default project name and location have been selected for you. Feel free to change these.
Atmel START projects come with some useful examples to help you get started on the different peripherals you’re looking to use. You can find these in the driver_examples.c file in the examples folder.
Open the main.c file. You will see the only function called in the current project is the atmel_start_init() function:
The atmel_start_init() function calls the system_init() and temperature_sensors_init() functions.
The system_init() function calls the following functions:
- init_mcu(): initialize oscillators, clocks, flash wait states…
- ADC_0_init()
- I2C_INSTANCE_init()
- delay_driver_init()
Want to see what these functions do?
Right-click on the function name and select 'Go to Implementation' to see the function definition (C file) and description (header file).
For more details, see the ASF4 API Reference Manual.
Open the driver_init.c file to see these and other initialization functions generated in response to the selections you made in Atmel START. You may also want to check out the configuration header files (found in the Config folder).
Step 7: Modify ADC example code to read light sensor data
As mentioned above, the examples in the driver_examples.c file will be used to help you get started.
1
In step 2, you configured the ADC driver as 'HAL:Driver:ADC_Async'. This means that the ADC takes a measurement and then generates an interrupt. The ADC interrupt then calls a callback function that will have to be implemented.
- Open driver_examples.c (in the examples folder) and copy the following functions:
- convert_cb_ADC_0()
- ADC_0_example()
- Paste them above the main() function in the main.c file.
- Rename the ADC_0_example() function as ADC_light_init().
- Rename the convert_cb_ADC_0() function as convert_cb_ADC().
- Update convert_cb_ADC() callback call in ADC_light_init().
Your code should look like this:
2
Initialize the ADC by calling this function after atmel_start_init():
- ADC_light_init();
3
Start an ADC conversion every second.
First, use the Delay driver you added to implement a one second delay.
- Add this delay function inside the while(1) loop in main().
- delay_ms(1000);
Now you have to call a function to start the conversion. The ADC_light_init() function uses this start conversion function, but it doesn't need to be in there.
- Cut the following function from ADC_light_init() and paste it after the delay(1000) function:
- adc_async_start_conversion(&ADC_0)
Your main.c should look like this:
4
Add code for ADC callback function.
- Add the following variable declaration after the include startement in main.c. This is used to indicate if the conversion is completed or not.
- volatile bool conversion_done = false;
- Update the ADC callback to indicate the conversion is done:
- conversion_done = true;
- Wait for conversion done in the while loop:
- while(!conversion_done);
- conversion_done = false;
5
Declare a two-byte buffer to store the 12-bit ADC value from the light sensor.
- Add the following code to the variable declarations at the top of main.c:
- uint8_t ADC_buffer[2];
6
Read the light sensor data.
- Call the following function in the while loop, once data is converted to read the 12-bit value.
- adc_async_read_channel(&ADC_0, 0, ADC_buffer, 2);
Note: The ADC driver functions can be found in the hal_adc_async.c file (hal/src folder).
Your main.c file should look like this:
Step 8: Program the board
3
Compile the project and program the board.
- Compile the project by clicking on the 'Build Solution' icon or by typing ‘F7’, and verify that it builds successfully.
- Program the application by clicking on the 'Start Without Debugging' icon.
Note: If the firmware on the evaluation board is out of date, a Firmware Upgrade window will appear asking you if you want to upgrade the firmware. Select Upgrade and allow the process to complete.
Your application is now running on the evaluation board.
Step 9: Verify light sensor measurements
- Break the application by clicking the 'Break All' icon:
- In the main.c file, right click on the 'ADC_buffer' variable then 'Add Watch':
- The Watch 1 window will appear with your selected variable. Right-click on the Watch window value to select 'Hexadecimal Display'.
- Click on the 'Continue' icon to execute the application.
If you hide the light sensor (i.e., put your finger on it) then click the 'Break All' icon to break the application, you can verify the buffer value should approach 0x0FFF.
Congratulations!
The SAM D21 ADC is measuring the output of the light sensor!
Step 10: Modify I²C example code to read temp sensor data and verify measurements
1
Add your application code to retrieve the Temperature Sensor data over I²C.
- Call temperature_sensors_init() after ADC_light_init() as shown below:
Note: The temperature sensor initialization source code can be found in the temperature_sensor_main.c file.
A global variable will be used to get temperature data.
- Add temperature global variable:
- float temperature;
- Get the temperature using the at30tse75x_read() function:
- temperature = at30tse75x_read(TEMPERATURE_SENSOR_0);
The I²C Temperature Sensor implementation is now completed.
2
Use a Watch window to view the temperature sensor value.
- Stop the debugger.
- Compile the project by clicking the 'Build Solution' icon or by typing ‘F7’, and verify it builds successfully.
- Launch the debugger by clicking the 'Start Debugging' icon then break the application by clicking the 'Break All' icon.
- Right-click on the temperature variable then 'Add Watch' (Unselect Hexadecimal Display).
- Put a breakpoint after the at30tse_read call and execute the application to get the temperature value:
Place your warm finger on the temperature sensor and click the debugger’s 'Continue' icon to verify the temperature rises as expected.
Congratulations!
The SAM D21 SERCOM (configured for I²C) is receiving data from the temperature sensor over the I²C bus!
Table of Contents
|