Step 3.1: Configure ADC Peripheral Library
2
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.
Step 3.2: Configure EIC Peripheral Library for Switch
2
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.
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.
Step 3.3: Configure Event System Peripheral Library
2
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.