Special Event Trigger For PIC32 by Bruce Misner
Special Event Trigger For PIC32 by Bruce Misner
For PIC32
By
Bruce Misner
The special event trigger is a function of the PIC microcontrollers that allows the
controller to sample an analog channel at a certain rate. Timer3 on the PIC32 controls the
rate at which you sample the analog channel. The manner in which you would configure
the A/D and the timer will be discussed in the following text. Also, a point form sequence
of configuration events will be provided as well.
The PIC32 has a more complicated interrupt system than the 16 and 18 families of
microcontroller. The PIC32 can operate with only a single interrupt vector or a multi-
vector set up. Either can be used, but for our example I will use the multi- vector set up.
The key point of the A/D configuration is to enable the trigger source to be
ADC_CLK_TMR the Timer3. There are a multitude of different configurations for the
A/D, whether you want single ended or differential input, what your reference voltages
should be, where your conversion clock comes from, and others. Some of the way you
configure the A/D will depend on the application. The timer must be set to provide the
proper sample rate which will also be dictated by the application. The timer input, if
internal, is dependant on the system clock, the peripheral clock divisor (oscon<20:19>
default is 2), the prescaler, and the count in pr2 register. In my example it would be:
Or a sample every
The following is my example code. The ISR routine is where you would put your
code to deal with the A/D data.
/*
*/
#include <plib.h>
//
//
// this is where you would put your handler code
//
//
mAD1ClearIntFlag();
mPORTDToggleBits(BIT_2);
PORTDbits.RD0 = 1;
int main(void)
{
//
// set bits as output
//
mPORTDSetPinsDigitalOut(BIT_2 | BIT_0);
//
// enable interrupt system
//
INTEnableSystemMultiVectoredInt();
//
// turn off a/d to reprogram
//
CloseADC10();
//
// parameters for a/d configuration
//
#define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG16 | ADC_CLK_TMR
| ADC_AUTO_SAMPLING_ON
#define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE |
ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
#define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
#define PARAM4 SKIP_SCAN_ALL
#define PARAM5 ENABLE_AN0_ANA
//
// select a/d channel and vrefs
//
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF |
ADC_CH0_POS_SAMPLEA_AN0);
//
// configure a/d interrupts
//
ConfigIntADC10(ADC_INT_PRI_2 | ADC_INT_SUB_PRI_3 | ADC_INT_ON);
//
// configure a/d
//
OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );
//
// configure timer3
//
OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_256, 0xFFFF);
//
// start a/d
//
EnableADC10();
//
// infinite loop
//
while(1);
//
// return 0 and end program
//
return 0;
}