// Center aligned PWM.
// Note: The PWM period is set to dLoopInTcy/2 but since it counts up and
// and then down =› the interrupt flag is set to 1 at zero =› actual
// interrupt period is dLoopInTcy
PHASE1 = LOOPTIME_TCY;
PHASE2 = LOOPTIME_TCY;
PHASE3 = LOOPTIME_TCY;
PTPER = 2*LOOPTIME_TCY+1;
PWMCON1 = 0x0204; // Enable PWM output pins and configure them as
PWMCON2 = 0x0204; // complementary mode
PWMCON3 = 0x0204;
//I/O pins initially controlled by GPIO
IOCON1 = 0;
IOCON2 = 0;
IOCON3 = 0;
//configure deadtime
DTR1 = 0x0000;
DTR2 = 0x0000;
DTR3 = 0x0000;
ALTDTR1 = DDEADTIME; // 700 ns of dead time
ALTDTR2 = DDEADTIME; // 700 ns of dead time
ALTDTR3 = DDEADTIME; // 700 ns of dead time
FCLCON1 = 0x3; //Fault disabled
FCLCON2 = 0x3; //Fault disabled
FCLCON3 = 0x3; //Fault disabled
PTCON2 = 0x0000; // Divide by 1 to generate PWM
PDC1 = MIN_DUTY; // Initialise the duty registers
PDC2 = MIN_DUTY;
PDC3 = MIN_DUTY;
IPC23bits.PWM1IP = 4; // PWM Interrupt Priority 4
IPC23bits.PWM2IP = 4; // PWM Interrupt Priority 4
IPC24bits.PWM3IP = 4; // PWM Interrupt Priority 4
IFS5bits.PWM1IF=0; // Clearing the PWM Interrupt Flag
IEC5bits.PWM1IE=0; // Enabling the PWM interrupt
// PTCON = 0x8000; // Enable PWM for center aligned operation
//I/O pins controlled by PWM
IOCON1 = 0xC000;
Delay(100); // Adding this delay allows enough time for high-ESR-type C61 to recover
IOCON2 = 0xC000;
Delay(100); // Adding this delay allows enough time for high-ESR-type C61 to recover
IOCON3 = 0xC000;
// SEVTCMP: ??????????? ??????????? ?????????? ??????? ?????
//???? ????????? ADC ????????????? ???????????? ????? PWM: 0 ???????? ? ???????
SEVTCMP = 0;
// ============= ADC - Measure Current & Pot ======================
// ADC setup for simultanous sampling on
// CH0=AN13, CH1=AN0, CH2=AN1, CH3=AN2.
// Sampling triggered by PWM and stored in signed fractional form.
// Signed fractional (DOUT = sddd dddd dd00 0000)
AD1CON1bits.FORM = 3;
AD1CON1bits.SSRC = 3;
AD1CON1bits.SSRCG = 0;
// Simultaneous Sample Select bit (only applicable when CHPS = 01 or 1x)
// Samples CH0, CH1, CH2, CH3 simultaneously (when CHPS = 1x)
// Samples CH0 and CH1 simultaneously (when CHPS = 01)
AD1CON1bits.SIMSAM = 1;
// Sampling begins immediately after last conversion completes.
// SAMP bit is auto set.
AD1CON1bits.ASAM = 1;
AD1CON2 = 0;
// Samples CH0, CH1, CH2, CH3 simultaneously (when CHPS = 1x)
AD1CON2bits.CHPS = 2;
AD1CON3 = 0;
// A/D Conversion Clock Select bits = 6 * Tcy
AD1CON3bits.ADCS = 6;
/* ADCHS: ADC Input Channel Select Register */
AD1CHS0 = 0;
// CH0 is AN13 for POT
AD1CHS0bits.CH0SA = 3;
// CH1 positive input is AN0, CH2 positive input is AN1, CH3 positive input is AN2
AD1CHS123bits.CH123SA = 0;
/* ADCSSL: ADC Input Scan Select Register */
AD1CSSL = 0;
// Turn on A/D module
AD1CON1bits.ADON = 1;
// Wait until first conversion takes place to measure offsets.
Delay(100);
//Fault enabled
FCLCON1 = 0x00FD; //Fault enabled Fault SRC - Fault 32
FCLCON2 = 0x00FD; //Fault enabled Fault SRC - Fault 32
FCLCON3 = 0x00FD; //Fault enabled Fault SRC - Fault 32
return;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++*/
/************************************************** ****************************/
/* Function name: ResetPeriph */
/* Function parameters: None */
/* Function return: None */
/* Description: Reset the peripherals during runtime/reset configurations */
/************************************************** ****************************/
/*+++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++*/
void ResetPeriph(void)
{
// clear and Enable ADC interrupt
IFS0bits.AD1IF = 0;
IEC0bits.AD1IE = 1;
/* Initialise duty regs */
PDC1 = MIN_DUTY;
PDC2 = MIN_DUTY;
PDC3 = MIN_DUTY;
return; |