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


< Back to Lab Start


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

Configure ADC Peripheral

1

Click on the Resource Management [MCC] tab. In Device Resources, expand Peripherals > ADC. Double-click or drag and drop ADC to add the ADC PLIB to the project graph.

adc_box.png

2

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_schematic.png
  • 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_config_table.png

a

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).

b

Select Reference: VDDANA

c

Select Conversion Trigger: HW Event Trigger

d

Start Event Input: Enabled on Rising Edge

e

Select Positive Input: ADC AIN18 Pin (the light sensor is connected to AIN18 pin of ADC.)

f

Select Result Resolution: 12-bit result (ADC conversion value range from 0 to 4096.)

g

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 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.

h

Open the Sleep Mode Configuration panel:

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

Configure ADC Pin

3

Open the Pin Configuration tab by clicking Project Graph > Plugins > Pin Configuration.

pin_settings_tool.png

4

Once the MPLAB® Code Configurator (MCC) Pin Settings window is opened, scroll-down to pin number 19 and configure this pin:

  • Enable ADC_AIN18 on pin number 19:
    • Pin ID: PA10
    • Custom Name: ADC_AIN18
    • Function: ADC_AIN18
adc_pin_settings.png

Configure ADC Clock for Low-Power

5

Open the Clock Configuration tab by clicking Project Graph > Plugins > Clock Configuration.

clock_configuration.png

6

Open the Peripheral Clock Configuration tab by clicking on the button in the Peripheral Clock Selection.

peripheral_clock_settings.png

7

Once the window is opened, scroll down to the ADC peripheral and select GCLK1 (1 MHz) as the source clock to generate the peripheral clock frequency.

adc_clock_configuration.png

The ADC peripheral clock is by default set to GCLK0. However, to benefit from SAM L21 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 conversion on a hardware event trigger and generate an interrupt if the converted value is higher than a defined WINLT value.

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

Configure EIC Peripheral

1

Click on the Resource Management [MCC] tab, In the Device Resources, expand Peripherals > EIC. Double click or drag and drop EIC to add the EIC PLIB to the project graph.

eic_box.png

2

Configure the EIC PLIB to produce an interrupt when the switch button SW0 is pressed.

eic_configuration_options.png

Configure EIC Pin

3

Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration.

pin_settings_tool.png

4

Once the MCC Pin Settings window is opened, scroll-down to pin number 3 and configure this pin:

  • Enable EIC_EXTINT2 on pin number 3:
    • Pin ID: PA02
    • Custom Name: SW0
    • Function: EIC_EXTINT2
eic_pin_settings.png

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.

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 Project graph > Plugins > Event Configurator.

event_configurator_tool.png

Once open, you will see 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 ADC Start of Conversion (ADC_START) 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 ADC for this application example).



< Back to Lab Start


© 2024 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.