Low Power Application on SAMC2x Using MPLAB® Harmony v3 Peripheral Libraries: Step 3
Configure ADC Peripheral Library
Under the bottom left tab, Device Resources, expand Harmony > Peripherals > ADC.
Double-click or drag and drop ADC1 to add the Analog-to-Digital Converter (ADC) Peripheral Library (PLIB) to the project graph.
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.
- 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.
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.
Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration. Go to the Pin Settings tab and configure PA08 (Pin #17) as ADC_AIN10 pin.
Configure EIC Peripheral Library for Switch
Under the Device Resources tab, expand Harmony > Peripherals > EIC.
Double-click on EIC to add the project.
Select the EIC peripheral Library and configure it to produce an interrupt when switch SW0 is pressed.
Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration. Go to the Pin Settings tab and configure PA28 (Pin #53) as EIC_EXTINT8 pin.
Configure Event System Peripheral Library
A new tab, EVENT0 Easy View, will open.
Add and configure Event System Channel 0 with:
• RTC compare 0 match as the event generator
• ADC1 Start of Conversion as the event user