/////////GLOBAL defined
#define test_periode 364 // Diagnostic frequency = 3 KHz
#define test_duty 160
#define test_sev 155
#define work_periode 54687 // Work frequency = 20 Hz
#define work_duty 4000
#define work_sev 16770
///////GLOBAL FUNCTION////////////////////
void power_up_pwm(void);
void IER_1 (void);
void ADC_initial (void);
/////
static unsigned int ADCValues_1[4] = {0, 0, 0, 0};
static char value[8] ;
//////// ISR //////////////////////////////
void pwm_1() iv IVT_ADDR_PWM1INTERRUPT ics ICS_AUTO {
while( !AD1CON1bits.DONE );
AD1CON1bits.DONE=0;
ADCValues_1[1] = ADC1BUF0; // Read the AN0 conversion result
IntToStr( ADCValues_1[1], value);
IFS5bits.PWM1IF=0;
return;
}
///////MAIN FUNCTION/////////////////////
void main()
{
////////OSCILLATOR CONFIGURATION Fosc=70 MHz
CLKDIV.DOZE_0=0;
CLKDIV.DOZE_1=0;
CLKDIV.DOZE_2=0;
CLKDIV.PLLPRE_0=0; //0+2
CLKDIV.PLLPRE_1=0;
CLKDIV.PLLPRE_2=0;
CLKDIV.PLLPRE_3=0;
CLKDIV.PLLPRE_4=0;
CLKDIV.PLLPOST_0=1; //2(1+1)
CLKDIV.PLLPOST_1=0;
PLLFBD=0x0044; //68+2
/////////PIN CONFIGURATION
RPINR12 = 0x7677; // fault inputs
RPINR18 = 0x0047; // uart1 receive
RPOR3 = 0x0001; // uart1 tx
////////
ANSELB=0x01df;
ANSELC=0x0000;
ANSELD=0x0000;
ANSELE=0x0000;
ANSELG=0x0000;
////////
TRISB=0xf9df;
TRISC=0x1000;
TRISD=0x0088;
TRISE=0x0000;
TRISF.B1=0;
TRISG.B8=0;
TRISG.B9=0;
///////// pwm configuration
UART1_Init(115200);
ADC_initial ();
IER_1 ();
power_up_pwm();
while(1){
UART1_Write(13);
UART1_Write_Text("phase_1:");
UART1_Write_Text(value);
}
}
void power_up_pwm(void){
SPHASE1 = test_periode;
/* Set Duty Cycles */
SDC1 = test_duty;
/* Set Dead Time Values */
DTR1 = DTR2 = DTR3 = 0;
ALTDTR1 = ALTDTR2 = ALTDTR3 = 0;
/* Set PWM Mode to Independent */
IOCON1 = IOCON2 = IOCON3 = 0x4C00;
/* PHASEx/SPHASEx registers provide time base period for this
PWM generator,Edge-Aligned Mode and independent Duty Cycles , enable trigger for interrupt */
PWMCON1 = PWMCON2 = PWMCON3 = 0x0601;
/* Configure Faults */
FCLCON1 = FCLCON2 = FCLCON3 = 0x0003;
/* 64:1 Prescaler */
PTCON2 = 6;
/* ADC Special Event Trigger Configuration */
TRIG1 = test_sev; /* Event Trigger value */
TRGCON1bits.TRGDIV = 0; /* Event Trigger output postscaler set to 1:1 selection */
TRGCON1bits.TRGSTRT =0;
/* Enable PWM Module , event interrupt is enabled */
PTCON = 0x8400;
}
void IER_1 (void){
/* Enable level 1-7 interrupts */
INTCON2bits.GIE=1;
/* Interrupt nesting enabled here */
INTCON1bits.NSTDIS = 0;
/* Set PWM1 Event Match interrupt priority*/
IPC23bits.PWM1IP=7;
/* Reset PWM1 Event Match interrupt flag */
IFS5bits.PWM1IF=0;
/* Enable PWM1 Event Match interrupt */
IEC5bits.PWM1IE=1;
return;
}
void ADC_initial (void){
AD1CON1 = 0x0010; // PWM Generator 1 primary trigger compare ends sampling and starts
AD1CON2 = 0x0000;
AD1CON3 = 0x008B;
AD1CON4 = 0x0000;
AD1CHS0 = 0x0000; // select AN0 for convert
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1; // Turn on the A/D converter
Delay_us(10);
}