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

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

Configure ADC Peripheral Library

Under the bottom left tab, Available Components, expand Peripherals > ADC.
Double-click, or drag and drop, ADC1 to add the ADC Peripheral Library (PLIB) to the project graph.
Expanding Peripherals > ADC under Available Components

Configure ADC PLIB to sample and convert the light sensor input. ADC is also configured to generate an interrupt (and thereby wakeup CPU) when the ADC result is greater than a set threshold value.
Configuring ADC PLIB to sample and convert the light sensor input​​​​

  • When the light sensor is not covered (light is falling on the sensor), the phototransistor is turned on. The ADC input is ~0V and the ADC RESULT register is close to 0x00.
  • When the light sensor is covered (light is not falling on the sensor), the phototransistor is turned off. The ADC input is ~3.3V and the ADC RESULT register will be saturated (0xFF).
  • The ADC RESULT register is compared with the Window Lower Threshold (WINLT), which is set to 150.
  • 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 the user covers the light sensor.

ADC configuration table

Select ADC1 and configure as shown below.

a. Select Prescaler: Divides the ADC input clock with the configured pre-scaler value and provides more sampling time (CLKADC = 48 MHz/256 = 187500 Hz).

b. Select Reference: ADC reference voltage = 3.3V

c. Select Conversion Trigger: Use hardware trigger

d. Enable Start Event Input: Configures ADC to start conversion when an event is received from the Event System

e. Select Positive Input: The light sensor is connected to AIN10 pin of ADC

f. Select Result Resolution: 8-bit result (ADC conversion value range from 0 to 255)

g. Window Mode Configuration: Allows the conversion result in the RESULT register to be compared to predefined threshold values.

h. Select Window Monitor Mode: Mode1: Result > WINLT. A Window Monitor Interrupt is generated when the ADC result is greater than the configured Window Lower Threshold value.

  • Window Upper Threshold: Sets the upper threshold of the window comparator. It is set to 0 as the ADC resolution is 8-bit.
  • Window Lower Threshold: Sets the lower threshold of the window comparator. It is set to 150. This means that the ADC Window Monitor Interrupt will be generated when the ADC result is greater than 150. This value is decided based on the light sensor voltage when we cover the light sensor it approximately generates 2.2V. 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 Window Lower Threshold.

Enable Window Monitor Interrupt

Open the Pin Configuration tabs by clicking MHC > Tools > Pin Configuration. Go to the Pin Settings tab and configure PA08 (Pin #17) as ADC_AIN10 pin.

Opening the Pin Configuration tabs

Back to Top


Configure EIC Peripheral Library for Switch

Under the Available Components tab, expand Peripherals > EIC. Double-click on EIC to add the project.

Expanding Peripherals > EIC
Select the EIC peripheral library and configure it to produce an interrupt when switch SW0 is pressed.

Selecting the EIC peripheral library

Open the Pin Configuration tabs by clicking MHC > Tools > Pin Configuration. Go to the Pin Settings tab and configure PA28 (Pin #53) as EIC_EXTINT8 pin.

Opening the Pin Configuration tabs

Note: EIC is configured to produce an interrupt whenever switch is pressed. This feature is used to measure the wake up time from Sleep mode. EIC is configured to run on internal low power 32 kHz clock. A switch press is also one of the sources to wake the device up from Sleep mode.

Back to Top


Configure Event System Peripheral Library

Event System (EVSYS) is added by default to the project graph. Launch Event System Configuration window by going to the MHC tab in MPLAB X IDE and then selecting Tools > Event System Configuration.
Launching Event System Configuration window
A new tab, EVENT0 Easy View, will open.
EVENT0 Easy View tab

Add and configure Event System Channel 0 with:
• RTC compare 0 match as the event generator
• ADC1 Start of Conversion as the event user

Note: Make sure that the status of the event and user (Event Status and User Ready) is green in color. If it is red, verify if the Event Output and Event Input is enabled in the respective (RTC and ADC) PLIB configuration.

Event System Manager

Back to Top