Low Power Application on SAM L21 Using MPLAB® Harmony v3 Peripheral Libraries: Step 3
Configure ADC, Event System and EIC Peripheral Libraries
Configure Analog-to-Digital Converter (ADC) Peripheral Library (PLIB)
Configure ADC Peripheral
Under the bottom left tab, Available Components, expand Peripherals > ADC. Double-click or drag and drop ADC to add the ADC PLIB 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).
- 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.
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: VCCANA
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
Configure ADC Pin
Open the Pin Configuration tabs by clicking Tools > Pin Configuration.
Once the MPLAB® Harmony Configurator (MHC) 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
Configure ADC Clock for Low-Power
Open the Clock Configuration tab by clicking on Tools > Clock Configuration.
Open the Peripheral Clock Configuration tab by clicking on the button in the Peripheral Clock Selection.
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.
Configure External Interrupt Controller (EIC) PLIB for Switch Button
Configure EIC Peripheral
Under the bottom left tab, Available Components, expand Peripherals > EIC. Double click or drag and drop EIC to add the EIC PLIB to the project graph.
Configure the EIC PLIB to produce an interrupt when the switch button SW0 is pressed.
Configure EIC Pin
Open the Pin Configuration tabs by clicking Tools > Pin Configuration.
Once the MHC 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
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.
Once open, you will see following window on your screen:
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.