The CTMU is basically a settable current source, with flexible triggering, and a direct connection to the ADC for measuring voltage. It can be used to measure resistance, capacitance, inductance, very short times (think LIDAR), and generate a pulse train which is not synchronized to the CPU clock (entropy source?). From the PIC32 reference manual, section 37: "The CTMU can be used for high resolution time measurement, measure capacitance, measure relative changes in capacitance or generate output pulses with a specific time delay. The CTMU is ideal for interfacing with capacitive-based sensors".
- Measure Resistance.
The first example uses the keypad to choose a curent level and the TFT display to diplay raw ADC values for testing, and a computed resistance. A resistor was connected between the AN9 analog input, pin 26 on the PDIP package, and ground. The current source connects internally to the ADC multiplexer and drives the analog input pin, so that the voltage read by the ADC scales with the resistance. Setting theCTMUCONbits.IRNG
bit field from 0 to 3 sets the current to{550,0.55,5.5,55}
microamps respectively. There is also a trim bit field for fine current adjustment. The image below shows the result of applying current-level 3 (55 microamps) to a 10K resistor. Going to the next higher current saturates the ADC. Going to the next lower current works, but is less accurate because of limited ADC resolution. (project ZIP).
Resistance is calculated as:
R = (float)(raw_adc-adc_zero[key_pressed])/1023. * 3.3 / I_set ;
or converted to the meaning of each term:<br> R = (ADC_reading - zero_offset)/(ADC_full_scale) * Vref/ current
The zero resistance offset was obtained by shorting the ADC pin to ground. This number will probably be different for each chip.
The current was taken as the nominal values from the data sheet TABLE 29-39. Calibration is probably necessary for accurate work.
- Measure Capacitance.
The second example displays the capacitance from AN9 to ground. With no cap attached, the white board, plus ADC sampling cap, adds up to about 18 pf. Uncalibrated accuracy seems good. a 1300 pf, 2% cap reads 1280 pf. The ADC functions have to be manually controlled. (project ZIP)
The steps are:- The ADC sample command,
AcquireADC10()
, closes the sample switch to connect the input pin to the internal ADC sampling cap. - The CTMU discharge switch is closed to short charge to ground from all the capacitance.
- Pause the thread for approximately one millisecond.
- The CTMU discharge switch is opened, then the CTMU charge switch is closed using
CTMUCONbits.EDG1STAT = 1
- Wait for a precise time interval, in this case 2 microseconds, for charge to accumulate on the capacitor under test.
- The CTMU charge switch is opened using
CTMUCONbits.EDG1STAT = 0
to stop charging. - The ADC convert command,
ConvertADC10()
, opens the sampling switch and starts the actual conversion to determine cap voltage. - Wait for the ADC conversion to complete.
- Read the ADC using
ReadADC10(0)
to get the raw ADC conversion. - Compute C=q/V or
C = (Set_current) * (current_time_interval) / ((float)(raw_adc)/ADC_max * Vref)
- The ADC sample command,