nairitb
Junior Member level 1
- Joined
- Mar 25, 2014
- Messages
- 18
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 154
// Crytal is 24MHz.
// Configure the oscillator to operate the device at 51 MIPS using PLL
#include <stdio.h>
#include <stdlib.h>
#include <p33EP512MU810.h>
#include "clockSwitch.h"
// DSPIC33EP512MU810 Configuration Bit Settings
#include <xc.h>
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC & IESO_OFF);
// Enable Clock Switching and Configure Primary Oscillator in HS mode
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_HS);
_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
int main()
{
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD= 32; // M=34
CLKDIVbits.PLLPOST= 0; // N2=2
CLKDIVbits.PLLPRE= 4; // N1=6
CLKDIVbits.DOZE= 0;
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
//OSCCON=0x301; //Oscillator Control Register
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 3);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
TRISF=0;
while(1)
{
PORTF = 0x01;
}
}
// oscillator - note
// • Medium Speed Oscillator (XT Mode) is a medium gain, medium frequency mode used to work
// with crystal frequencies of 3.5 MHz to 10 MHz.
//• High-Speed Oscillator (HS Mode) is a high-gain, high-frequency mode used to work
// with crystal frequencies of 10 MHz to 25 MHz.
// do oscillator calculations given crystal frequency and required instruction frequency
//The settings below set up the oscillator and PLL
//follows:
// Crystal Frequency * (PLLDIV+2)
// Fosc = ---------------------------------
// (PLLPOST+2) * (PLLPRE+2)
// FCY = Fosc/2
//
// configuration must be _FOSCSEL(FNOSC_PRIPLL);
#define Fin 8000000UL // crystal frequency input
#if((Fin < 3500000) || (Fin > 25000000))
# error "Fin outside range 3.5MHz to 25MHz"
#endif
#define FCY 60000000 // required instruction frequency MIPs
#if(FCY > 70000000)
# error "required instruction frequency FCY > 70MIPs"
#endif
#define PLLpre 2ul // PLL prescale
//#define PLLdiv 60ul // PLL VCO divisor
#define PLLpost 0ul // PLL post divisor
#define PLLdiv ((FCY *2* (PLLpost+2)*(PLLpre+2))/Fin)-2
// now calculate PLL input and output
#define VCOinput (Fin/(PLLpre+2))
#if((VCOinput < 800000ul) || (VCOinput > 8000000ul))
#error "error VCOinput range must be .8MHz to 8MHz"
#endif
#define VCOoutput (VCOinput*(PLLdiv+2))
#if((VCOoutput < 120000000ul) || (VCOoutput > 340000000ul))
#error "error VCOoutput range must be 100MHz to 340MHz"
#endif
#define Fosc (VCOoutput/(PLLpost+2))
#define SYSCLK Fosc
// Setup configuration bits
_FOSCSEL(FNOSC_PRIPLL);
#if(Fin < 10000000)
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_XT); // crystal frequencies of 3.5 MHz to 10 MHz.
#else
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_HS); // crystal frequencies of 10 MHz to 25 MHz.
#endif
_FWDT(FWDTEN_OFF);
_FPOR( ALTI2C1_ON); // select aletrnate I2C1 pins for MCP79411
PLLFBDbits.PLLDIV=PLLdiv;
CLKDIVbits.PLLPOST = PLLpost;
CLKDIVbits.PLLPRE = PLLpre;
OSCTUN = 0;
/* Initiate Clock Switch to Primary
* Oscillator with PLL (NOSC= 0x3)*/
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0x3);
// Configuring the auxiliary PLL, since the primary
// oscillator provides the source clock to the auxiliary
// PLL, the auxiliary oscillator is disabled. Note that
// the AUX PLL is enabled. The input 8MHz clock is divided
// by 2, multiplied by 24 and then divided by 2. Wait till
// the AUX PLL locks.
ACLKCON3 = 0x24C1;
ACLKDIV3 = 0x7;
ACLKCON3bits.ENAPLL = 1;
while(ACLKCON3bits.APLLCK != 1);
Its not working. I think my oscillator is not working. Do you know how to check whether its good or bad? I am using a 24MHz crystal oscillator with two 27 pF capacitors.
Oh by the way, I saw in the design for dsPIC33E USB Starter Kit of Microchip (which uses dsPIC33EP512MU810) that a 1M resistor is connected in parallel to the crystal. Why is it so? Image of schematic attached.
_FOSCSEL(FNOSC_PRIPLL);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_HS); // crystal frequencies of 10 MHz to 25 MHz.
// Crytal is 24MHz.
// Configure the oscillator to operate the device at 51 MIPS using PLL
#include <stdio.h>
#include <stdlib.h>
#include <p33EP512MU810.h>
// DSPIC33EP512MU810 Configuration Bit Settings
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
// FOSCSEL
#pragma config FNOSC = FRC // Initial Oscillator Source Selection Bits (Internal Fast RC (FRC))
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Mode Select bits (HS Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = ON // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled and Fail-safe Clock Monitor is disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = OFF // Brown-out Reset (BOR) Detection Enable bit (BOR is disabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
int main(void)
{
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD= 32; // M=34
CLKDIVbits.PLLPOST= 0; // N2=2
CLKDIVbits.PLLPRE= 2; // N1=6
CLKDIVbits.DOZE= 0;
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
//OSCCON=0x301; //Oscillator Control Register
//OSCCONH Unlock Sequence
__builtin_write_OSCCONH(0x78);
__builtin_write_OSCCONH(0x9A);
//Oscillator select
__builtin_write_OSCCONH(0x03);
//OSCCONL Unlock Sequence
__builtin_write_OSCCONL(0x46);
__builtin_write_OSCCONL(0x57);
__builtin_write_OSCCONL(OSCCON | 0x01); //turn on OSWEN
while (OSCCONbits.OSWEN); //wait until OSWEN to low
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b011);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
TRISF=0;
while(1)
{
PORTF = 0x01;
}
}
If the PLL of the target device is enabled and configured for the device start-up oscillator, the maximum oscillator source frequency must be limited to 3 MHz < FIN < 5.5 MHz to comply with device PLL start-up conditions. This means that if the external oscillator frequency is outside this range, the application must start-up in the FRC mode first. The default PLL settings after a POR with an oscillator frequency outside this range will violate the device operating speed.
Once the device powers up, the application firmware can initialize the PLL SFRs, CLKDIV and PLLDBF to a suitable value, and then perform a clock switch to the Oscillator + PLL clock source. Note that clock switching must be enabled in the device Configuration Word.
(DS70616G-page 33)
I loaded the following configuration in my dsPIC33EP256MU806 boardOkay. I checked the whole hardware again and found that there was no continuity between the crystal oscillator and the microcontroller OSC pins. Resoldered the crystal. Now there is continuity.
Tried to run the program now WITHOUT PLL. Works both for FRC and Primary Oscillator modes. However, when I try to switch to PLL, code again gets stuck in while (OSCCONbits.COSC!= 0b001); loop.
Currently using 11.0592MHz crystal. Changed the CLKDIV/PLLFBD accordingly.
#
_FOSCSEL(FNOSC_PRIPLL);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_HS); // crystal frequencies of 10 MHz to 25 MHz.
define Fin 11059200UL // crystal frequency input
#if((Fin < 3500000) || (Fin > 25000000))
# error "Fin outside range 3.5MHz to 25MHz"
#endif
#define FCY 60000000 // required instruction frequency MIPs
#if(FCY > 70000000)
# error "required instruction frequency FCY > 70MIPs"
#endif
#define PLLpre 2ul // PLL prescale
//#define PLLdiv 60ul // PLL VCO divisor
#define PLLpost 0ul // PLL post divisor
#define PLLdiv ((FCY *2* (PLLpost+2)*(PLLpre+2))/Fin)-2
// now calculate PLL input and output
#define VCOinput (Fin/(PLLpre+2))
#if((VCOinput < 800000ul) || (VCOinput > 8000000ul))
#error "error VCOinput range must be .8MHz to 8MHz"
#endif
#define VCOoutput (VCOinput*(PLLdiv+2))
#if((VCOoutput < 120000000ul) || (VCOoutput > 340000000ul))
#error "error VCOoutput range must be 100MHz to 340MHz"
#endif
#define Fosc (VCOoutput/(PLLpost+2))
#define SYSCLK Fosc
PLLFBDbits.PLLDIV=PLLdiv;
CLKDIVbits.PLLPOST = PLLpost;
CLKDIVbits.PLLPRE = PLLpre;
OSCTUN = 0;
/* Initiate Clock Switch to Primary
* Oscillator with PLL (NOSC= 0x3)*/
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0x3);
// Configuring the auxiliary PLL, since the primary
// oscillator provides the source clock to the auxiliary
// PLL, the auxiliary oscillator is disabled. Note that
// the AUX PLL is enabled. The input 8MHz clock is divided
// by 2, multiplied by 24 and then divided by 2. Wait till
// the AUX PLL locks.
ACLKCON3 = 0x24C1;
ACLKDIV3 = 0x7;
ACLKCON3bits.ENAPLL = 1;
while(ACLKCON3bits.APLLCK != 1);
in the code I entered in my first post it states that _FOSCSEL(FNOSC_PRIPLL) Primary Oscillator (XT, HS, EC) with PLL is required (to enable two speed oscillator start up), e.g.Hey horace. I didn't understand one thing. Why did you write _FOSCSEL(FNOSC_PRIPLL); whereas what we are actually doing is running board from PRI at first and then switching to PLL.
I was trying _FOSCSEL(FNOSC_PRI);. Did not work. But when I tried _FOSCSEL(FNOSC_PRIPLL);, it worked!! :thinker:
Please enlighten me :smile:
// oscillator - note
// • Medium Speed Oscillator (XT Mode) is a medium gain, medium frequency mode used to work
// with crystal frequencies of 3.5 MHz to 10 MHz.
//• High-Speed Oscillator (HS Mode) is a high-gain, high-frequency mode used to work
// with crystal frequencies of 10 MHz to 25 MHz.
// do oscillator calculations given crystal frequency and required instruction frequency
//The settings below set up the oscillator and PLL
//follows:
// Crystal Frequency * (PLLDIV+2)
// Fosc = ---------------------------------
// (PLLPOST+2) * (PLLPRE+2)
// FCY = Fosc/2
//
// configuration must be _FOSCSEL(FNOSC_PRIPLL);
// Crytal is 24MHz.
// Configure the oscillator to operate the device at 51 MIPS using PLL
#include <stdio.h>
#include <stdlib.h>
#include <p33EP512MU810.h>
#define Fin 24000000 // crystal frequency input
#if((Fin < 3500000) || (Fin > 25000000))
# error "Fin outside range 3.5MHz to 25MHz"
#endif
#define FCY 51000000 // required instruction frequency MIPs
#if(FCY > 70000000)
# error "required instruction frequency FCY > 70MIPs"
#endif
#define PLLpre 2 // PLL prescale ; N1=4
#define PLLpost 0 // PLL post divisor ; N2=2
#define PLLdiv ((FCY *2* (PLLpost+2)*(PLLpre+2))/Fin)-2 // PLL VCO divisor
// now calculate PLL input and output
#define VCOinput (Fin/(PLLpre+2))
#if((VCOinput < 800000) || (VCOinput > 8000000))
#error "error VCOinput range must be .8MHz to 8MHz"
#endif
#define VCOoutput (VCOinput*(PLLdiv+2))
#if((VCOoutput < 120000000) || (VCOoutput > 340000000))
#error "error VCOoutput range must be 120MHz to 340MHz"
#endif
#define Fosc (VCOoutput/(PLLpost+2))
#define SYSCLK Fosc
// DSPIC33EP512MU810 Configuration Bit Settings
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = OFF // General Segment Code-Protect bit (General Segment Code protect is disabled)
#pragma config GSSK = OFF // General Segment Key bits (General Segment Write Protection and Code Protection is Disabled)
// FOSCSEL
#pragma config FNOSC = PRIPLL // Initial Oscillator Source Selection Bits (Primary Oscillator (XT, HS, EC) with PLL)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)
// FOSC
#pragma config POSCMD = HS // Primary Oscillator Mode Select bits (HS Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler Bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Wait Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FPOR
#pragma config FPWRT = PWR128 // Power-on Reset Timer Value Select bits (128ms)
#pragma config BOREN = OFF // Brown-out Reset (BOR) Detection Enable bit (BOR is disabled)
#pragma config ALTI2C1 = OFF // Alternate I2C pins for I2C1 (SDA1/SCK1 pins are selected as the I/O pins for I2C1)
#pragma config ALTI2C2 = OFF // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)
// FICD
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config RSTPRI = PF // Reset Target Vector Select bit (Device will obtain reset instruction from Primary flash)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FAS
#pragma config AWRP = OFF // Auxiliary Segment Write-protect bit (Auxiliary program memory is not write-protected)
#pragma config APL = OFF // Auxiliary Segment Code-protect bit (Aux Flash Code protect is disabled)
#pragma config APLK = OFF // Auxiliary Segment Key bits (Aux Flash Write Protection and Code Protection is Disabled)
int main(void)
{
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
TRISF=0;
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBDbits.PLLDIV=PLLdiv;
CLKDIVbits.PLLPOST = PLLpost;
CLKDIVbits.PLLPRE = PLLpre;
CLKDIVbits.DOZE= 0;
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
//Oscillator select
__builtin_write_OSCCONH(0x03);
// Clock Switch
__builtin_write_OSCCONL(OSCCON | 0x01); //turn on OSWEN
while (OSCCONbits.OSWEN); //wait until OSWEN to low
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b011);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
/* Set PWM Period on Primary Time Base */
PTPER = 4000;
/* Set Phase Shift */
PHASE1 = 4000;
SPHASE1 = 2000;
PHASE2 = 1000;
SPHASE2 = 3000;
/* Set Duty Cycles */
MDC = 2000;
/* Set Dead Time Values */
DTR1 = DTR2 = 0;
ALTDTR1 = ALTDTR2 = 0;
/* Set PWM Mode to Independent */
IOCON1 = IOCON2 = 0xCC00;
/* Set Primary Time Base, Edge-Aligned Mode and Master Duty Cycles */
PWMCON1 = PWMCON2 = 0x0100;
/* Configure Faults */
FCLCON1 = FCLCON2 = 0x0003;
/* 1:1 Prescaler */
PTCON2 = 0x0000;
while(1)
{
/* Enable PWM Module */
PTCON = 0x8000;
PORTFbits.RF0 = 1;
}
}
good to hear it is working - I seem to remember that I required the auxiliary oscillator to drive the USB interface to talk to the host PCThe present code which is working for me is:
LED connected at RF0 is ON and I get PWM signal at PWM1H, 1L, 2H, 2L.
By the way, I have disabled the Auxiliary oscillator.
Thanks a lot.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?