Clock Settings for Pic32

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

Moderator: Benj

Post Reply
Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Clock Settings for Pic32

Post by Alkaline »

Hi everyone

I have a problem with the configuration of the clocks of the pic 32 and the relative timings.
As a reference I use the excel table released by microchip that I have attached.

This is the problem; if I report the same values ​​inside the program configurator the timings and the various clocks (spi, uart etc) are wrong.
In the example to have the correct timings I had to indicate as system clock 80Mhz while in reality it is working at 40 MHz. Only in this way the delays are correct.

For the uart communication I had to set a baud rate 38400 instead of 9600 (9600 is the speed with which I have to work)
With this configuration I was able to correctly communicate.

Where am I doing wrong?
Attachments
project_clock.fcfx
(23.71 KiB) Downloaded 207 times
PIC32_470f512H.xlsx
(452.77 KiB) Downloaded 218 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: Clock Settings for Pic32

Post by Benj »

Hello,

80MHz in the project options sounds correct for 40MHz operation and for the delays. Getting the delays correct is a good starting point.

In the configuration settings try changing the "Peripheral Clock Divisor" setting to Sys_Clk/1 and hopefully this will be closer to what you are expecting re. the SPI and UART.

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Hi Benji

if I understand correctly, there is no direct connection between the program's system clock and the microchip excel file.

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Hi Benj

I'm back to the topic again because I still have the same problems.

I can't get the correct delays and peripheral speeds (even software mode).

the configuration is as follows

40Mhz external clock (I don't use PLL)
if I set the system clock on flowocode at 40 mhz these are the results.
The delay is half and i2c (only software mode because hw doesn't work it goes to 83Khz (???))
If on flowocode I set 80 Mhz (but I don't understand why I should set twice the speed at which the MCU actually goes) the results are the following:

Perfect Delay (250ms)
I2s software 55Khz (???)

I can't understand where I'm wrong. :( :(
A simple test program is attached
Attachments
test timing.fcfx
(9.76 KiB) Downloaded 186 times
40Mhz setting.jpg
40Mhz setting.jpg (67.57 KiB) Viewed 6530 times
80Mhz setting.jpg
80Mhz setting.jpg (62.77 KiB) Viewed 6530 times

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Alkaline

Sometimes PIC gives a litle problem with the speed. For my PIC I did it with a c-code for the hardware uart and I2C
Unfortunately I have to change by hand when I'm switching from 200 Mega to 100 Mega.
But I do not do that often.
Maybe Ben knows how to read the selected Clock Speed (Hz) setting in the FC8 program as a variable....

regards

Stefan
Init.PNG
(21.35 KiB) Downloaded 2034 times
delay_timing.PNG
(6.29 KiB) Downloaded 2034 times

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Hello

I'm trying to analyze the clock in an attempt to help

I set a timer and checked that the interrupt occurs at the right time.

Both using Timer 1 and the remaining Timers the results are correct.

Regards

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Update: If I insert the System.h library and the SYSTEM_CONFIG code

the baud rate of the uart port is correct but the timings always remain half

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: Clock Settings for Pic32

Post by Benj »

Hello,

These PIC32 devices are very odd, some work fine but others do not and the rules seem to change mid family so rolling out a divider is very hard. I've tried multiple times to get this right.

Which PIC32 device(s) are you currently testing.

I could maybe change the UART baud calulation for these specific device(s) so it's correct.

It doesn't help that the peripheral clock is configurable but in most cases users are not touching this so it's hard to blame the problems on this.

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben

I'm using the PICC32MZ2048EFG100 and PICC32MZ2048EFG144.

You are right the peripheral clock is not the only problem or solution.

I have found that with the existing values in the baud rate register can never be achieved the speed. So I have set this by hand and let the peripheral clock untouched. But that is not the solution yet.

These PIC32 devices are very odd, some work fine but others do not and the rules seem to change mid family so rolling out a divider is very hard
Yes I think so but we can fix it....

I test new values with my oscilloscopes if you want

And one question, how can I read the Value from the clockspeed I choosed in the project options in my program?

regards

Stefan

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: Clock Settings for Pic32

Post by Benj »

Hi Stefan,

The baud calc is done here in this file which also references the project options clock speed.

C:\ProgramData\MatrixTSL\FlowcodeV8\CAL\PIC32BIT\PIC32BIT_CAL_UART.c

If no dividor is specified in the device definition file then we currently default to 4.

Code: Select all

#ifndef MX_UART_BAUD_DIV
	#define MX_UART_BAUD_DIV 4
#endif
The various baud rates are then calculated like this.

Code: Select all

#define MX_HARD_BAUD_1200	((MX_CLK_SPEED / 1200) / MX_UART_BAUD_DIV)
PICC32MZ2048EFG100 = #define MX_UART_BAUD_DIV 2
PICC32MZ2048EFG144 = #define MX_UART_BAUD_DIV 2

so they are both defined currently as /2 instead of the default /4

You could override the definition file setting in the PIC32BIT_CAL_UART.c file by adding this at the top of the file.

Code: Select all

#undef MX_UART_BAUD_DIV 
#define MX_UART_BAUD_DIV xxx
Where xxx is your new divider. Sounds like it's either going to be 4 or 1 to get the correct calculation.

There is also this setting to be aware of in the chip configuration, this is currently /2 and maybe should instead be /1.
Config.jpg
Config.jpg (29.65 KiB) Viewed 6383 times

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Hi benj

I am currently using a pic32mx470f512h

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben, Hi Alkaline

It's a little confusing or I'm doing something wrong.
I can not found FPBDIV PeripheralClock Divisor in the PIC32Mz2048EFG100 or 144 in the config menu.
But this chip has 6 different ones or not?
In my project I used 100Mhz Clock but the problem was with 200Mhz the same.
There is this high Baudrate Bit BRGH what I read as 1.
that would also be a 4 times speed factor.
I'll go on.....
I testet the values from the #def. and show it on the display.

result are following:

100000000
2
434


Show.PNG
(20.33 KiB) Downloaded 1877 times
BRGH.PNG
(123.34 KiB) Downloaded 1877 times

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben, Hi Alkaline

Here I do not understand how this is working. #if (MX_HARD_BAUD_115200 > 65535)


PIC32BIT_CAL_UART.c

Code: Select all

	#define MX_HARD_BAUD_115200	((MX_CLK_SPEED / 115200) / MX_UART_BAUD_DIV)
	#define MX_HARD_SLOW_115200	1
	#if (MX_HARD_BAUD_115200 > 65535)
		#undef MX_HARD_BAUD_115200
		#undef MX_HARD_SLOW_115200
		#define MX_HARD_SLOW_115200 	0
		#define MX_HARD_BAUD_115200	(( MX_CLK_SPEED / 115200 ) / (MX_UART_BAUD_DIV * 4))

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: Clock Settings for Pic32

Post by Benj »

Hello,

First we try the calculation using the high speed baud rate, this gives higher resolution so will be able to acheive faster baud rates with less error.

If the calculation results in a value that is too big for the baud rate register (> 65535) e.g. a fairly low baud rate then we switch to the slow mode and do the calculation again.

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben, Hi Alkaline

OK.
Just a question. When you use MX_CLK_SPEED instead of Peripheral Bus clock, you can use the MX_UART_BAUD_DIV (2) to get 100Mhz
and then if the
BRGH=1 use the 4
BRGH=0 use the 16

like this?

Code: Select all

 #define MX_HARD_BAUD_115200   (((MX_CLK_SPEED/MX_UART_BAUD_DIV) / 115200) / 4)
   #define MX_HARD_SLOW_115200   1
   #if (MX_HARD_BAUD_115200 > 65535)
      #undef MX_HARD_BAUD_115200
      #undef MX_HARD_SLOW_115200
      #define MX_HARD_SLOW_115200    0
      #define MX_HARD_BAUD_115200   ((( MX_CLK_SPEED/MX_UART_BAUD_DIV) / 115200 ) /  16)

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben, Hi Alkaline

Actually, I need a divider with 8 and one with 32. This would simply be because the slow baud rate calculation already has a factor of 4


So I will test with a divider in the PIC32BIT_CAL_UART.c file by adding this at the top of the file .

maybe you can do this even more elegant But to test it should work

Code: Select all

#ifdef MX_PIC32_MZ
     #ifndef MX_UART_BAUD_DIV
         #define MX_UART_BAUD_DIV 8
      #endif
      #undef MX_UART_BAUD_DIV 
      #define MX_UART_BAUD_DIV 8
#endif


regards

Stefan

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Clock Settings for Pic32

Post by stefan.erni »

Hi Ben, Hi Alkaline

It's working with the additional Code in the PIC32BIT_CAL_UART.c file
The Uart is connecting to the PC.

And for you Alkaline remove the _MZ in the first line (MX_PIC32_MZ) and choose your divider
like this code

Code: Select all

#ifdef MX_PIC32
     #ifndef MX_UART_BAUD_DIV
         #define MX_UART_BAUD_DIV 8
      #endif
      #undef MX_UART_BAUD_DIV 
      #define MX_UART_BAUD_DIV 8
#endif
regards

Stefan

Alkaline
Posts: 143
Joined: Mon Aug 20, 2018 11:04 am
Has thanked: 42 times
Been thanked: 41 times
Contact:

Re: Clock Settings for Pic32

Post by Alkaline »

Hi Stefan

Thanks for the info, I'll try the code in the next few days and I'll update you on the situation

Regards

Post Reply