Low Power Application on SAM D21 Using Harmony v3 Peripheral Libraries: Step 3

Step 3.1: Configure Analog Comparator (AC) Peripheral Library (PLIB)

Configure AC Peripheral

1

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

add_ac.png

2

Select the AC Peripheral Library and configure it to compare the light sensor input with reference to the VDD Scaler voltage. The AC is also configured to generate an interrupt (and thereby wake the CPU) when the AC comparator output is rising.

ac_schematic.png
  • When the light sensor is not covered (light is falling on the sensor), the phototransistor is turned on. The AC input is ~0 V and there is no change in AC output.
  • When the light sensor is covered (light is not falling on the sensor), the phototransistor is turned off. The AC input is ~3.3 V and rises on AC output.
  • The AC analog input will be compared with the VDD Scaler voltage (Analog input/Light Sensor output as positive input and VDD Scaler as negative input).
  • When the light sensor is covered (AC Input > VDD Scaler), an AC Comparator Interrupt is generated. This interrupt is used to bring the CPU out of Idle/Standby Sleep mode when you cover the light sensor with your hand.

a

Select Enable Run in Standby.

b

Enable Comparator 0 Settings and configure the Comparator 0 settings.

c

Enable Comparator Interrupt Enable.

d

Select Enable Single Shot Mode.

e

Verify Positive Input Mux Selection is set as I/O Pin 0 (externally, the light sensor analog output (Ext1 pin 3) is connected to AIN0 pin (Ext1 pin 17) of AC).

f

Configure Negative Input Mux selection as VDD scaler. Set the scaling factor for VDD as 15. Based on the V scale calculation, the Vscale value is (3.3 / 4)V, i.e. 0.825V.

vdd_scaler_formulae.png

g

Set Output Edge Type as The output of COMPn is not routed to the COMPn I/O port, as we don't need comparator output on the pin.

h

Set Interrupt Selection as Interrupt on comparator output rising. AC will generate interrupt only when we cover the light sensor.

i

Enable Event Input to start the conversion on a hardware event trigger (In this example, RTC timeout is event trigger).

ac_configuration.png

Configure AC Pin

3

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

ac_pin_settings.png

Configure AC Clock for Low-Power

4

In MHC, select the Clock Easy View tab and configure the AC Clock by clicking on the Peripheral Clock Configuration button to open the Peripheral Clock Configuration tab.

peripheral_clock_config.png
ac_clock_configuration.png

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

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

Step 3.2: Configure External Interrupt Controller (EIC) PLIB for Switch Button

In this step, we configure the switch button per the following USER_BUTTON design on the SAM D21 Xplained Pro Evaluation Kit.

user_button_schematic.png

Configure EIC Pin

1

In MHC, select the Pin Settings tab and then scroll down to 32 in the Pin Number column. Configure the PORT pin PA15 as an external interrupt pin for switch functionality. The internal pull-up is enabled to avoid false edge detection as there is no external pull-up on the SAM D21 Xplained Pro Evaluation Kit.

eic_pin_configuration.png

Configure EIC Peripheral

2

Select the Project Graph tab.

3

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

eic_peripheral_adding.png

4

Configure the EIC peripheral to generate an interrupt every time the user presses the switch SW0, and enable filter functionality to avoid electrical noise on the switch pin.

eic_configuration_setup.png

5

In MHC, select the Clock Easy View tab and configure the EIC Clock by clicking the Peripheral Clock Configuration button to open the Peripheral Clock Configuration tab.

peripheral_clock_config.png
eic_clock_configuration.png

The EIC peripheral clock is by default set to GCLK0. However, to benefit from the SAM D21 clock tree, the EIC peripheral clock is fed by GCLK1 to run at 1 kHz. Running a slower clock on the EIC to overcome the switch debounce and electrical noise on the EIC pin and also reduces the power consumption during Sleep mode.

This completes the configuration of the EIC PLIB. The EIC is configured to produce an interrupt that will wake the device up from Sleep mode whenever the switch is pressed. This feature is used to measure the wake-up time from Sleep mode (Idle or Standby).

Step 3.3: Configure Event System (EVSYS) PLIB

1

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

open_evsys_configuration.png

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

event_0_easy_view_not_configured.png

2

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 AC Start of Conversion (AC_SOC_0) as the event user.
event_0_easy_view.png

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 AC for this application example).



Next Step >

© 2022 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.