3 Peripherals Used

Peripherals used in this application:

  • CCP1
  • TMR1/TMR2
  • OPAMP1
  • CMP1
  • DAC1
  • FVR
  • GPIO
Figure 3-1. Block Diagram

The CCP is a peripheral that allows the user to time and control different events and generate PWM signals. The PWM mode generates signals of various frequencies and duty cycles.

In this application, CCP1 is configured to the PWM output, using TMR2 as the default module to set the desired period and timing of the signal. The TMR2 is configured in One-Shot Mode because the MCU generates just one PWM signal at a specific time. The TMR2 is enabled by setting the ON bit and stops when the Timer Counter register (T2TMR) matches the Timer Period register (T2PR). The PWM connects to the input of the LC circuit.

Figure 3-2. Hardware Representation of LC Circuit

The OPA module features a standard general-purpose, three-terminal device with programmable gain options, adjustable input offset voltage, and hardware override control capabilities.

The OPA module is configured to operate in Unity Gain mode, which means that the output of the OPA is connected internally to the inverting input, and the OPA operates with unity gain feedback. The positive OPA input connects to the output signal of the LC circuit, and the negative input internally connects to the output.

The OPA module is configured as a peak detector by connecting the external capacitor to the output pin of the peripheral. This mode discharges the external capacitor used for peak detection. The discharge time depends on whether the load and the external capacitor are used. The Peak Detect mode configuration generates the next envelope used for metal detection. The output pin of OPA is connected to the RC circuit to filter the signal with RC time constant and ensures that the envelope follows all the signal peaks.

Figure 3-3. Hardware Representation of RC Circuit

The CMP peripheral interfaces the analog circuits to a digital circuit by comparing two analog voltages and providing a digital indication of their relative magnitudes. Comparators are helpful mixed-signal building blocks since they provide analog functionality independent of program execution.

The negative CMP1 input is the filtered signal from the RC circuit, the envelope. The positive input is the DAC peripheral output. The comparator is configured to run at a 30 ns High Speed mode while the Hysteresis feature is enabled to ensure the noise signal will not affect the measurement.

The DAC supplies a variable voltage reference, a ratiometric to the input source, with programmable selectable output levels. The output of this peripheral is routed internally to CMP. The DAC will also need a positive and negative reference to get a specific wanted output voltage. In this application, the positive reference is selected using the Fixed Voltage Reference (FVR) peripheral, and the VSS is the negative reference.

FVR is a stable voltage reference, independent of VDD, with 1.024V, 2.048V or 4.096V selectable output levels. The FVR output can be configured to supply a reference voltage to analog peripherals such as ADC, CMP or DAC.

A suggestive PIC18-Q71 device family feature is the presence of Timer1 with gate control, a 16-bit timer/counter accessed through the Timer Counter register (TMR1). When using an internal clock source, the peripheral is a timer incrementing on every instruction cycle.

The Timer1 Gate is configured to count by enabling and disabling the Timer1 gate circuitry. When the Timer1 Gate is on Enable mode, Timer1 will increment on the rising edge of the Timer1 clock source. When the Timer1 Gate signal is inactive, the timer/counter register TMR1 will not increment and hold the current count. When Enable mode is disabled, no incrementing will occur, and Timer1 will keep the current count.

The CMP1 output signal defines the Timer1 Gate source and starts counting the cycles when the output is logic “0”. This counter will help to compare the values between the peak of the envelope and the set threshold level to differentiate a metal object from a non-metal one. This counter value can also measure the distance between the metal and the LC sensor.

The device’s I/O pins are controlled by the events of the PORT peripheral registers. Each PORT instance has up to eight I/O pins. Each PORT pin has a corresponding bit in the Data Direction, Output Latch, Analog Select, Weak Pull-Up, and other registers to enable that pin as an output or input and to define the state.

In this application, the following pins are used:

  • RA1 – output pin (OPA1)
  • RA2 – output pin (DAC1)
  • RA4 – output pin (CMP1)
  • RA5 – input pin (OPA1+)
  • RB0 – output pin (CCP1)
  • RB3 – input pin (CMP1-)
  • RC7 – output pin (LED)

The LED is configured to turn on if a metal object is in the proximity of the LC sensor and turn off if not in the proximity.