Page 1 of 1

Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Fri Nov 28, 2014 12:25 pm
by QMESAR
Hi All

I configure the Clock setting on a dsPIC33EP by including a C file in a macro then call the macro at the beginning of the Code or flow diagram (it is the fisrt function call)
this code get never execute as you will see that my Timing is running 3 time to slow.
I tested this by setting up a T1 interrupt at 100Hz = 10mSec I read in the scope ~28mSec
see screen prints 1&2
The I just deactivate macro and place a C call components with the same code as in the macro then the Timing is 100% (10mSec)correct see screen print 3&4
I did not look into the generated C code to try and see why this is happening .
I guess my question is it not possible to call c code from outside the main Flow diagram ?

Thank you for helping me understand this

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Fri Nov 28, 2014 12:35 pm
by kersing
C code calls are allowed anywhere. I have used C code in macros without any issues.

However, having encountered issues with clock settings in the past my best practice is to always do this in a C code icon in the main program to make sure nothing interferes with it.

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Fri Nov 28, 2014 2:25 pm
by QMESAR
kersing wrote:C code calls are allowed anywhere. I have used C code in macros without any issues.
However, having encountered issues with clock settings in the past my best practice is to always do this in a C code icon in the main program to make sure nothing interferes with it.
Thank you for the reply kersing!
The macro certainly gets not called correctly or something ,I always get worried when a tool does something that I do not know what it is doing ,

@benj
Do you perhaps have an explanation to this ,I will try to investigate the C code generated when I have time for now the customer is waiting for his pro to-type

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Mon Dec 01, 2014 10:45 am
by Benj
Hello,

I can't think why the C code would work in main and not in a macro, I've seen issues before with the PLL not locking but code without dependencies that works in one place should in theory work anywhere.

Could you post your C code and we could have a look to see if it's something that maybe could be improved to be more reliable?

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Mon Dec 01, 2014 3:19 pm
by QMESAR
Hi Benj
This is the macro I am using ,thanks for looking at this

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Mon Dec 01, 2014 6:06 pm
by Benj
Hello,

Please try changing the code from this:

Code: Select all

/***************************************************
* Configure device for 70 Mips with 8 MHZ XTAL
**************************************************/
PLLFBD = 68;              /* M=70 (68 +2)*/
CLKDIVbits.PLLPOST = 0;   /* N1=2 */
CLKDIVbits.PLLPRE = 0;    /* N2=2 */
/***********************************/
To this.

Code: Select all

/***************************************************
* Configure device for 70 Mips with 8 MHZ XTAL
**************************************************/
PLLFBD = 68;              /* M=70 (68 +2)*/
CLKDIVbits.PLLPOST = 0;   /* N1=2 */
CLKDIVbits.PLLPRE = 0;    /* N2=2 */
OSCTUN = 0;			//Tune FRC oscillator, if FRC is used

//Disable Watch Dog Timer
RCONbits.SWDTEN = 0;

// Clock switching to incorporate PLL
__builtin_write_OSCCONH(0x03);	// Initiate Clock Switch to Primary

// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL(0x01);	// Start clock switching
while(OSCCONbits.COSC != 0b011);

// Wait for Clock switch to occur	
while(OSCCONbits.LOCK != 1)
{ };
/***********************************/
Hopefully it should then be more reliable as your waiting for the PLL to lock before continuing.

Also if you are using 8MHz crystal then the PLLFBD value should be 58 not 68 to give you 70MIPs or 140000000 Hz clock speed.

As a final note, we are working on USB support for the dsPIC33EP range. To setup the USB based on a 70MIPs clock you also need this C code.

Code: Select all

//USB H/W initialization for 70 MIPs
ACLKCON3 = 0x24C1;
ACLKDIV3 = 0x7;

// Wait for PLL to lock
ACLKCON3bits.ENAPLL = 1;
while(ACLKCON3bits.APLLCK != 1);

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Mon Dec 01, 2014 6:28 pm
by QMESAR
Ben
Thank you very much I will do as you advise.

I am just not on the same page with you with the 58 setting it depends what you use for N1 and N2 in the PLLPRE and PLLPOST(in my understanding) also M is 2 + the value you write to the PLLFDB ,I use 0 and that is always 2 in each of them then FOSC = XT*M/N1*N2 or do i misunderstand the datasheet :D then I get 8*(68+2)/2*2 = 544/2*2 = 140

I appreciate your help :D

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 3:30 am
by GTF
I just started playing with a dsPIC33EP a few days ago and it seems to me that the examples in the datasheet/reference manual set the value for the PLLFBD register in decimal rather than hex. So for M= 70, PLLFBD should be set as PLLFBD = 0x44 (68 decimal). Other than that, I used C code similar to what Benj has posted here.

See page 14 http://ww1.microchip.com/downloads/en/D ... 05131a.pdf

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 10:40 am
by Benj
Hello,

Sorry your right the correct value is 68, not sure where I got the 58 from :D :roll:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 1:21 pm
by QMESAR
Thank you BenJ and GTF for your help I appreciate it

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 3:25 pm
by QMESAR
Hi Benj
Just a feed back I added the lines of code as you suggested that helped in the sense that I could use the macro call and the controller would run however it did a few times not start to run
after discussions with Microchip Field application engineer I had to include a while loop that checks for the PLL lock to happen with this added the micro start every time no problem at all

does this make sense to you ?

Code: Select all

/***************************************************
* Configure device for 70 Mips with 8 MHZ XTAL
**************************************************/
PLLFBD = 68;              /* M=70 (68 +2)*/
CLKDIVbits.PLLPOST = 0;   /* N1=2 */
CLKDIVbits.PLLPRE = 0;    /* N2=2 */
OSCTUN = 0;         //Tune FRC oscillator, if FRC is used

//Disable Watch Dog Timer
RCONbits.SWDTEN = 0;
//-------------------------------------------------------------------
// Clock switching to incorporate PLL
__builtin_write_OSCCONH(0x03);   // Initiate Clock Switch to Primary

//--------------------------------------------------------------------
//ensure PLL lock before moving on into application code
//--------------------------------------------------------------------
while(OSCCONbits.LOCK != 1);	/* Wait for Pll to Lock */

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 3:39 pm
by GTF
This what I used based on the Microchip examples. It appears that the WDT PLL Lock wait which Benj has included in his C code can also be set in the chip config if the WDT is enabled.

Grant

Code: Select all

// Configure the device PLL to obtain 70 MIPS operation. The crystal frequency is 8 MHz.
// Divide 8 MHz by 2, multiply by 70 and divide by 2. This results in Fosc of 140 MHz.
// The CPU clock frequency is Fcy = Fosc/2 = 70 MHz.
PLLFBD = 0x44; /* M = 70 */
CLKDIVbits.PLLPOST = 0; /* N1 = 2 */
CLKDIVbits.PLLPRE = 0; /* N2 = 2 */
OSCTUN = 0;
/* Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0x3) */
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0x3);
while (_LOCK == 0); /* Wait for PLL lock at 70 MIPS */   

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Tue Dec 02, 2014 3:42 pm
by QMESAR
Thank you GTF that helps a lot :D

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Posted: Sat Jun 20, 2020 10:21 am
by Michael Burger
@ QMESAR
@ Benj
@ GTF

Thank you for this thread and posts it was very helpful! :D :D


Kind Regards

Michael Burger