Blockset described on this wiki is deprecated since 2012.

For Model Based Design (MBD), use the free MPLAB Device Blocks for Simulink, tool from Microchip.
Updated Rapid Control Prototyping (RCP) custom projects are published at:

Hyper Sampling

From - Simulink device driver Blockset for dsPIC / PIC24 / PIC32 Microcontrollers --[[User:LubinKerhuel|LubinKerhuel]] 12:40, 3 September 2009 (UTC)
Revision as of 20:36, 31 August 2010 by LubinKerhuel (talk | contribs) (Related Links:)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
RC filter

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

  • Periodic
  • 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).

Simulink model file

Simulink model file used to programme the PIC32 chip. The ADC and Output Compare HW block are synchronised via the use of Timer 3. Data are sent and logged with matlab via UART.

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.


HyperSampling results. Light blue line is the reconstructed PWM signal generated by the Output Compare peripheral. The deep blue line is the sampled data that superpose perfectly with calculated RC response plotted (red dashed line). The black thin line shows the reconstructed result of the ADC output when use at its fastest sampling rate (without hyper sampling technique).

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.

Looking deeper

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.

Logging Animation

Logging data with the blockst built-in rs232GUI Matlab Interface


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.