4 Software Architecture
This project has multiple functions. Each having a name describing its main purpose. There are two types of functions in this code: MCC-generated functions and custom application functions.
app_PrepareEnvelope
app_ChargingPulseGen
app_ThrshldTimeMeas
app_CheckEnvelopeThrsld
app_ResetTimer
app_ThrsldMeasDefaultValue
The SYSTEM_Initialize
function is called at the beginning of the
code and includes all the used peripherals initialization generated and configured
using MCC. It configures the main clock of the project, the pin management, the CMP,
the DAC, the FVR, the OPA, the CCP and the timers. Essentially, the project
development used all the peripherals. Other functions generated by MCC for each
peripheral will be used in the development.
The app_PrepareEnvelope
function is the first application-related
function and is used to prepare the LC sensor to detect the metal object. When the
external capacitor is discharging while the PWM signal is generated, the external
capacitor recharges. The PWM signal is generated by the app_ChargingPulseGen
function that starts the Timer2 counter and waits until the Overflow Interrupt flag
of Timer2 is set for a complete PWM cycle. It clears once the cycle has
completed.
The app_ThrshldTimeMeas
function generates the period that starts at
the peak of the envelope and stops when the set threshold is reached. A delay of 1
ms is applied to prevent any false spike while charging the external capacitor and
then while the function is running to measure the period of interest. Timer1 starts
counting internally when the comparator output is logic-low, which means it’s at the
peak of the envelope. The function above returns a 16-bit integer that represents
the TMR1 counter value that is directly proportional to the measured period and
stored in a local variable, measuredPeriod
.
The on-board LED is used as a visual confirmation of metal detection. The period
parameter's app_CheckEnvelopeThrshld
function has it’s stored value
compared with a previous set threshold if there are no objects in the LC sensor
proximity. If the measured period is lower than the set threshold, the LED turns on
and indicates the presence of a metal object. Otherwise, it will either turn off or
stay off.
The app_ThrsldMeasDefaultValue
function calibrates the threshold
value for metal detecting. This function determines a default threshold period for a
number of cycles set by the user to determine the average value of this threshold.
This value can be affected by the difference between values of the external
components so the average value of the threshold will be more precise. The LED will
blink until the calibration is fulfilled. The first measurement value will be
dismissed, and an error calibration factor deducted from the average determined
value to avoid inaccuracies.
The app_ResetTimer
function resets the TMR1 counter value and
prepares the timer for a new measurement cycle.
All the above functions are called in a logical, continuous cycle repeated once every 100 ms and represented in the diagram below.