Matlab-Simulink device driver Blockset for Microchip dsPIC / PIC24 / PIC32 Microcontrollers.
The objective of this example is to demonstrate the synchronization of the ADC sampling time with PWM generated signal. The rise and fall response of a RC filter are sampled at an unusual fast frequency for a microcontroller. The time constant of the RC filter is 100us. Usual sampling time would not allow sampling the rising curve details of this filter. The artificial sampling time obtained is 10ns. Experiments are done using a PIC32 but can be ported very easily on any dsPIC.
Please use the forum or edit this page for any remarks/precision or others...
The Electronics – RC filter
The RC system is presented on the first figure. A resistor of 1Kohm is connected to a capacity of 100nz. The input of the RC filter is the Output Compare peripheral of the PIC32 used. PWM signal of 500us were generated using the Output Compare Hardware block. The ADC (channel 5) is connected at the filter output.
The filter output signal is
- Time invariant
Thus, one unique sample is taken at each period. The end sampling time is moved at each period by an amount of 10.418ns which is very small. Note however than the sampling time has duration of 4.84us and the conversion time takes 1.875us (thus can be done easily within one PWM period of 500us).
The ADC block is set to synchronise the ADC sampling time with Timer 3 end of Period. This timer 3 is used to generate the PWM signal (see Output Compare HW block). The PWM signal is shifted at each simulink time step by an amount of 10ns (block Offset Sampling from 0 to OCmax). Thus, the ADC sampling time, which occurs at the end of the timer 3 period is virtually shifted within the generated PWM output signal.
The figure with the result show the reconstructed PWM signal (which was not logged!). The predicted response of the RC filter is plotted in red dashed line. The sampled result is plotted in deep blue. The black line is a reconstruction of the fastest sampling time of the PIC32 using classical method. The ADC sampling delay line is the value of the Sampling Offset within PWM (see Simulink file)
Download this example, with simulink model file, script and logged data.
Although if not exactly similar, a method called RIS (Random Interleaved Sampling) is sometime used on oscilloscope to improve sampling time resolution of periodic signal.
Thanks to Marc Boyron from CNRS (Biorobotics lab, Marseille) for fruitful discussion about this example. Thanks to Microchip, for providing the PIC32 PIM, the Explorer16 board and the Real Ice programmer.