1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| /******************************************************************************/
/* Bits configuration */
/******************************************************************************/
_FOSCSEL(FNOSC_PRI & IESO_ON); //Select Internal FRC at POR
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON); //Only clock Switching enabled
//(Fail Safe Clock Monitor is disabled)
_FWDT(WDTEN_OFF);
_FICD(ICS_PGD2 & JTAGEN_OFF);
_FDEVOPT(PWMLOCK_OFF);
/******************************************************************************/
/* Main */
/******************************************************************************/
int main(void){
/*-------------------------------Initialisation-------------------------------*/
initOscillator();
initPWM();
initComparator ();
/*-------------------------------Infinity Loop--------------------------------*/
while(1){
}
return 0;
}
/******************************************************************************/
/* Function */
/******************************************************************************/
/*-----------------------------Configure Oscillator---------------------------*/
void initOscillator (void){
RCONbits.SWDTEN=0; //Disable Watch Dog Timer
//Configure Oscillator to operate the device at 68 MHz
//Fosc = Fin*M/(N1*N2), Fcy = Fosc/2
//Fosc = 7.37*(76)/(2*2)= ~140Mhz for Fosc, Fcy = 70 MHz
//Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 74; //M = PLLFBD + 2
CLKDIVbits.PLLPOST = 0; //N1 = 2
CLKDIVbits.PLLPRE = 0; //N2 = 2
__builtin_write_OSCCONH(0x01); //New Oscillator selection FRC w/ PLL
__builtin_write_OSCCONL(0x01); //Enable Switch
while(OSCCONbits.COSC != 0b001); //Wait for Osc. to switch to FRC w/ PLL
while(OSCCONbits.LOCK != 1); //Wait for PLL to Lock
//Setup the ADC and PWM clock for 120MHz
//((FRC * 16) / APSTSCLR ) = (7.37MHz * 16) / 1 = 117.9MHz
ACLKCONbits.FRCSEL = 1; //FRC provides input for Auxiliary PLL (x16)
ACLKCONbits.SELACLK = 1; //Aux Osc. provides clock source for PWM & ADC
ACLKCONbits.APSTSCLR = 7; //Divide Auxiliary clock by 1
ACLKCONbits.ENAPLL = 1; //Enable Auxiliary PLL
while(ACLKCONbits.APLLCK != 1); //Wait for Auxiliary PLL to Lock
RCONbits.SWDTEN=1; //Enable Watch Dog Timer
}
void initPWM (void){
/* Initializing PWM1 Generator for controlling MOSFET */
PWMCON1bits.ITB = 1; /* Select independent time base for enabling XPRES */
PWMCON1bits.XPRES = 1; /* Select Current Reset mode */
IOCON1bits.PMOD = 1; /* Select Redundant mode since only PWM1H is being used for MOSFET */
IOCON1bits.FLTDAT = 0; /* To make the PWM signals low during Fault condition */
FCLCON1bits.FLTSRC = 0b01101; /* Select Analog Comparator1 as Fault Source for PWM1 */
FCLCON1bits.FLTMOD = 1; /* Select Cycle-by-cycle Fault mode for upper limit cut-off */
FCLCON1bits.CLSRC = 0b01110; /* Select Analog Comparator2 as Current Limit Source for Current Reset of PWM1 */
}
void initComparator (void){
/* Configuring ACMP1 for Upper Current Limit and ACMP2 for Lower Current Limit */
CMP1CONbits.RANGE = 1; /* Set Maximum DAC output voltage to AVDD */
CMP1DAC = 3600; /* Configure to turn OFF MOSFET at 2.9V of comparator input (upper
current reference) */
CMP1CONbits.CMPON = 1; /* Turn ON Analog Comparator1 */
CMP2CONbits.RANGE = 1; /* Set Maximum DAC output voltage to AVDD */
CMP2CONbits.CMPPOL = 1; /* Invert output polarity of Analog Comparator 2 for lower limit
current detection and PWM Reset */
CMP2DAC = 1489; /* Configure to reset PWM at 1.2V of comparator input (lower current
reference */
CMP2CONbits.CMPON = 1; /* Turn ON Analog Comparator2 */
} |
Partager