Sie sind auf Seite 1von 3

Special Event Trigger

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.

Let’s start with the sequence of events

• Enable Multi-vector interrupts


• Turn off A/D
• Define parameters for the A/D configuration
• Select the A/D channels and reference voltages
• Configure the interrupt for the A/D
• Configure the A/D
• Configure Timer3 and start timer
• Start the A/D sampling

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:

72Mhz/2/256/65535 = 2.14 samples/sec

Or a sample every

1/2.14 = .466 sec

The following is my example code. The ISR routine is where you would put your
code to deal with the A/D data.
/*

This Program demonstrates the use of the


Special Event Trigger

This allows the A/D to sample at a set rate


based on Timer3

*/

// include plib header

#include <plib.h>

// Interrupt Service Routine


//
// Simply toggles bit 2 on port d to
// signify isr call
//

void __ISR(_ADC_VECTOR, ipl2) AdcHandler(void)


{

//
//
// 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;
}

Das könnte Ihnen auch gefallen