Low Power Application on SAM L10 Using MPLAB® Harmony v3 Peripheral Libraries: Step 3

Last modified by Microchip on 2023/11/09 09:07

Configure Analog-to-Digital Converter (ADC) Peripheral Library (PLIB)

ADC Peripheral

Under the Available Components tab, expand Peripherals > ADC. Select and double click on ADC to add the ADC module to the project graph.

Under the Available Components tab, expand Peripherals > ADC. Select and double click on ADC to add the ADC module to the project graph.​​​​​

Select the ADC Peripheral Library and configure it to sample and convert the light sensor input. The ADC is also configured to generate an interrupt (and thereby wake the CPU) when the ADC result is greater than a set Window Monitor Low Threshold value (3000 for this example).

ADC Interrupt Diagram

  • ​​​​​When the light sensor is not covered (light is falling on the sensor), the phototransistor is turned on. The ADC input is ~0 V and the ADC RESULT register is close to 0x000.
  • When the light sensor is covered (light is not falling on the sensor), the phototransistor is turned off. The ADC input is ~3.3 V and the ADC RESULT register will be saturated (0xFFF).
  • The ADC RESULT register is compared with the Window Lower Threshold (WINLT) which is set to 3000.
  • When the light sensor is covered (ADC RESULT > WINLT), an ADC Window Monitor Interrupt is generated. This interrupt is used to bring the CPU out of Idle/Standby Sleep mode when you cover the light sensor.

ADC Table

Select Prescaler: Peripheral clock divided by 2 divides the ADC input clock with the configured pre-scaler value and provides more sampling time (CLKADC = 1 MHz/2 = 500 kHz).
Select Reference: VDDANA.
Select Conversion Trigger: HW Event Trigger.
Start Event Input: Enabled on Rising Edge.
Select Positive Input: ADC AIN0 Pin (the light sensor is connected to AIN0 pin of ADC).
Select Result Resolution: 12-bit result (ADC conversion value range from 0 to 4096).

Open the Window Mode Configuration panel:

  • Select Window Monitor mode: Result > WINLT. A Window Monitor Interrupt is generated when the ADC result is greater than the configured WINLT value.
  • Window Upper Threshold: Sets the upper threshold of the window comparator. It is set to 0 as the ADC resolution is 12-bit.
  • Window Lower Threshold: Sets the lower threshold of the window comparator. It is set to 3000. This means that the ADC Window Monitor Interrupt will be generated when the ADC result is greater than 3000. This value is decided based on the light sensor voltage when we cover the light sensor; it approximately generates 2.2 V. Hence, the threshold is set near to this voltage.
  • Enable Window Monitor Interrupt: A Window Monitor Interrupt is generated when the ADC result is greater than the configured WINLT.

Open the Sleep Mode Configuration panel:

  • Enable Run During Standby.
  • Enable On Demand Control.

Enable Run During Standby and On Demand Control​​

Back to top


ADC Pin

In the MPLAB® Harmony Configurator (MHC), select the Pin Settings tab and then scroll-down to PORT pin PA02 in Pin ID column and configure this pin as ADC_AIN0.

In the MPLAB® Harmony Configurator (MHC), select the Pin Settings tab and then scroll-down to PORT pin PA02 in Pin ID column and configure this pin as ADC_AIN0

Back to top


ADC Clock for Low-Power

In the MHC, select the Clock Easy View tab and configure the ADC Clock by opening the Peripheral Clock Configuration tab by clicking on the button in the Peripheral Clock Configuration.

Peripheral Clock Configuration to Peripherals

The ADC peripheral clock is by default set to GCLK0. However, to benefit from the SAM L10 clock tree, the ADC peripheral clock is fed by GCLK1 to run at 1 MHz. Running a slower clock on the ADC reduces the power consumption during Sleep mode.

This completes the configuration of the ADC PLIB. The ADC is configured to start the conversion on a hardware event trigger and generate an interrupt if the converted value is higher than a defined WINLT value.

Back to top


Configure External Interrupt Controller (EIC) PLIB for Switch Button

In this step, the switch button will be configured as per the following USER_BUTTON design schematic on SAM L10 Xplained Ultra Evaluation Kit.

USER_BUTTON design schematic on SAM L10 Xplained Ultra Evaluation Kit

EIC Pin

In the MHC, select the Pin Settings tab and then scroll down to 25 in the Pin Number column and configure the PORT pin PA27 as an external interrupt pin for switch functionality as shown below. Internal pull-up is enabled to avoid false edge detection as there is no external pull-up on the SAM L10 Xplained Pro Evaluation Kit.

Configure EIC Pin

Back to top


EIC Peripheral

Select the Project Graph tab.

Back to top


Under the Available Components tab, expand Peripherals > EIC.
Select and double click on EIC to add the EIC module to the project.

Available Components tab EIC

Back to top


Configure the EIC block to generate an interrupt every time the user presses the switch SW0 as shown in the accompanying figure, and enable filter functionality to avoid electrical noise on switch pin.

Configuration Options > EIC Clock Source Selection and Enable EIC Channel5

In the Peripheral Clock Configuration, EIC is by default connected to the GCLK0. However, the EIC clock configuration is set to run at 32 kHz using the OSCULP32K source clock directly, as defined in the Configuration Options of the EIC peripheral.

This completes the configuration of the EIC PLIB. The EIC is configured to produce an interrupt which will wake the device up from Sleep mode whenever the switch is pressed. This feature is used to measure the wakeup time from Sleep mode (Idle or Standby). The EIC is configured to run on the internal low power 32 kHz clock regardless of its configuration in the Peripheral Clock Configuration.

Back to top


Configure Event System (EVSYS) PLIB

The EVSYS is added by default to the project graph. Launch the Event System Configuration window by clicking on Tools > Event System Configuration.

Tools > Event System Configuration

Once open, you will see the following window on your screen:

Event System Manager User Configuration

Back to top


Configure the EVSYS channel 0 on the Event System Manager window:

  • Set the Real-Time Clock Compare 0 (RTC_CMP_0) event as the event generator. The event is configured to appear asynchronously and to run in Standby mode with the on-demand feature enabled
  • Set the ADC Start of Conversion (ADC_START) as the event user.

Event System Manager Add User

Make sure that the status of the event and user (Event Status and User Ready) is green. If it is red, verify that the Event Output and Event Input are enabled in the respective PLIB configuration (RTC and ADC for this application example).

Back to top