Hi,
I paste the code here:
BR
XiangJun Rong
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#define PE_MCUINIT
#include <stdio.h>
#include "derivative.h" /* include peripheral declarations */
#define SOFTWARE_TRIGGER 0
#define PIT_TRIGGER 0
#define PDB_TRIGGER 1
void MCU_init(void); /* Device initialization function declaration */
void ADC_ISR(void);
unsigned int sample[10];
//ADC1_DM1 is connected to adjustable resistor, so the code can be used to
//test the output of the adjustable resistor
int main(void)
{
int counter = 0;
MCU_init(); /* call device initialization */
//toggle LED E1 by toggling PCT7 pin
//set the PCT7 as GPIO output pin mode
SIM_SCGC5=SIM_SCGC5|0x3E00; //enable port A/B/C/D/E clock
asm(nop);
PORTC_PCR7=0x100; //PTC7 in GPIO mode
GPIOC_PDDR=0x80; //PTC direction register, PTC7 is in output mode
NVICIP58=0x0F; //set ADC0 interrupt priority
NVICICPR1|=1<<26; //clear the pending register of interrupt source of ADC(57)
NVICISER1|=1<<26; //set the interrupt source of ADC0, 57%32=25
#if SOFTWARE_TRIGGER
//Conclusion: ADC_ISR(void) can be entered
//enable ADC0 clock, FTM1 and FTM0 clock, PIT clock, PDB clock
SIM_SCGC6|=0x0BC00000;
SIM_SCGC3|=0x08000000; //enable ADC1 clock
//ADICLK=00 Bus Clock, MODE=01 12 bits conversion
//ADLSMP=0 short sample time
//ADIV 00 divide ratio is 1
ADC1_CFG1=0x04;
ADC1_CFG2=0x00;
ADC1_SC2=0x00; //software triggered,
ADC1_SC3=0x00; //one conversion
//initiate ADC conversion by software triggering
ADC1_SC1A=0x54; //interrupt enable and select ADC1_DM1 channel
asm(cpsie i); //interrupt enable
#endif
#if PIT1_TRIGGER
//enable ADC0 clock, FTM1 and FTM0 clock, PIT clock, PDB clock
//Conclusion: ADC_ISR(void) can be entered
SIM_SCGC6|=0x0BC00000;
SIM_SCGC3|=0x08000000; //enable ADC1 clock
//ADICLK=00 Bus Clock, MODE=01 12 bits conversion
//ADLSMP=0 short sample time
//ADIV 00 divide ratio is 1
//PIT module configuration
PIT_MCR = 0x00;
PIT_LDVAL1 = 0x003FFFFF; // setup timer so that the LED have enough time to flash
//select PIT1 trigger via SIM_SOP7
SIM_SOPT7=0x8500; //PIT1 trigger
ADC1_CFG1=0x04;
ADC1_CFG2=0x00;
ADC1_SC2=0x40; //hardware triggered,
ADC1_SC3=0x00; //one conversion
//initiate ADC conversion by software triggering
ADC1_SC1A=0x54; //interrupt enable and select ADC1_DM1 channel
PIT_TCTRL1 |= 0x01; // start Timer 1
asm(cpsie i); //interrupt enable
#endif
#if PDB_TRIGGER
//software trigger PDB, PDB triggers ADC
//enable ADC0 clock, FTM1 and FTM0 clock, PIT clock, PDB clock
//Conclusion: ADC_ISR(void) can be entered
SIM_SCGC6|=0x0BC00000;
SIM_SCGC3|=0x08000000; //enable ADC1 clock
//ADICLK=00 Bus Clock, MODE=01 12 bits conversion
//ADLSMP=0 short sample time
//ADIV 00 divide ratio is 1
//PDB module configuration
PDB0_CH0S=0x0000;
PDB0_SC=0x00F80; //PDB enable, PDB interrupt enable
PDB0_MOD=0x8000;
PDB0_IDLY=0x00;
PDB0_CH0C1=0x0303; //Enable CH0
PDB0_CH0DLY0=0x1000;
PDB0_CH0DLY1=0x5000;
PDB0_SC|=0x02; //write the register
PDB0_SC|=0x01; //write the register
//select PIT1 trigger via SIM_SOP7
SIM_SOPT7=0x00; //PDB0 trigger ADC1
ADC1_CFG1=0x04;
ADC1_CFG2=0x00;
ADC1_SC2=0x40; //hardware triggered,
ADC1_SC3=0x00; //one conversion
//initiate ADC conversion by software triggering
ADC1_SC1A=0x54; //interrupt enable and select ADC1_DM1 channel
ADC1_SC1B=0x53; //interrupt enable and select ADC1_DM0 channel
PDB0_SC|=0x10000; //start trigger
asm(cpsie i); //interrupt enable
//The ADC_ISR() can not be entered
#endif
for(;;) {
counter++;
}
return 0;
}
#pragma interrupt on
void ADC_ISR(void)
{
//LED indicator
GPIOC_PTOR=0x80;
sample[0]=ADC1_RA;
asm(nop);
#if SOFTWARE_TRIGGER
ADC1_SC1A=0x54; //software triggering interrupt enable
#endif
}
#pragma interrupt on