Problem with setting dsPIC Clock Speed to 140Mhz

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Problem with setting dsPIC Clock Speed to 140Mhz

Post 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
Attachments
2.jpg
2.jpg (72.37 KiB) Viewed 11512 times
3.jpg
(72.27 KiB) Downloaded 912 times
4.jpg
4.jpg (64.67 KiB) Viewed 11512 times

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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?

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post by QMESAR »

Hi Benj
This is the macro I am using ,thanks for looking at this
Attachments
Config_OSC_70MIPS.fcm
(535 Bytes) Downloaded 376 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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);

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post by Benj »

Hello,

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

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post by QMESAR »

Thank you BenJ and GTF for your help I appreciate it

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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 */

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post 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 */   
Last edited by GTF on Tue Dec 02, 2014 4:55 pm, edited 4 times in total.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post by QMESAR »

Thank you GTF that helps a lot :D

User avatar
Michael Burger
Posts: 19
Joined: Tue Jul 02, 2019 1:42 pm
Location: South Africa
Has thanked: 4 times
Been thanked: 10 times
Contact:

Re: Problem with setting dsPIC Clock Speed to 140Mhz

Post by Michael Burger »

@ QMESAR
@ Benj
@ GTF

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


Kind Regards

Michael Burger

Post Reply