Page 1 of 1

I2C baud rate setting is not the right clock

Posted: Thu Apr 06, 2017 10:20 pm
by patrick schoon
Hi,


My project, PIC24EP512 is running at 120MHz clock.
This works with all the used timers and is accurate, also USB is working fine.

Now when setting the I2C (1) to 100k baud rate i get a baud rate arround 750kHz.
I checked this with an analizer. So customizing the baudrate setting to custom and select a value lower than 5000 it is working but is not how it should be.

Latest FC6 version.

Can you check this?


10-04-2017
I did some extra checks.

400k selected baud rate gives approx. 300k
100k selected baud rate gives approx. 760k
Custom selected baud rates:
100k gives 760k
50k gives 240k
25k gives 625k !!!!
20k gives 220k

It seems there is a baudrate calculation error..



Regards,

Patrick Schoon

Re: I2C baud rate setting is not the right clock

Posted: Mon Apr 10, 2017 5:37 pm
by Benj
Hello Patrick,

Thanks for letting us know. I may be having this very same problem on a 16-bit PIC project I'm currently working on. The software channel works fine but the hardware channel locks up.

Looking at the code something doesn't seem right to me.

Currently looks like this which seems very wrong.

Code: Select all

I2C1BRG = (MX_CLK_SPEED/MX_I2C_BAUD_X)-(MX_CLK_SPEED/1111111);	//Set the Baud Rate
I'll investigate and see if I can improve this for you so it works correctly.

Re: I2C baud rate setting is not the right clock

Posted: Mon Apr 10, 2017 5:42 pm
by Benj
Please can you have a go with this and see if this improves things for you.

Simply copy the attached file into your "Flowcode 6/CAL/PIC16BIT" folder.
PIC16BIT_CAL_I2C.c
(24.84 KiB) Downloaded 420 times
Let us know how you get on.

Re: I2C baud rate setting is not the right clock

Posted: Tue Apr 11, 2017 9:25 pm
by patrick schoon
Hi Ben,

No this is not solving the problem, even worse.
Even at very lowt custom baud setting (1000) timing is under 60ns a period.

You are close, I think you have to check here..

#define MX_I2C_HW_BAUD ((MX_CLK_SPEED / MX_CLKS_PER_INST) / MX_I2C_BAUD_X) - 1


I'm using the I2C0 alternative port (ASDA1/ASCL1).

Regards,

Patrick

Re: I2C baud rate setting is not the right clock

Posted: Wed Apr 12, 2017 11:48 am
by Benj
Hello,

I've done some research and the calculation seems to be giving the correct values.

(70MHz / 100KHz) - 2 = 698
(70MHz / 400KHz) - 2 = 173
(70MHz / 1MHz) - 2 = 68

However the I2C baud register is only 9-bit and so the maximum baud delay is 511 so now I try and detect if an overflow is occurring and if it is then I set the value to equal 511.

This brings a 100KHz baud down from measuring 318KHz to 128KHz, the slowest available speed on a device running at 70MIPs.
PIC16BIT_CAL_I2C.c
(24.96 KiB) Downloaded 391 times

Re: I2C baud rate setting is not the right clock

Posted: Wed Apr 12, 2017 10:01 pm
by patrick schoon
Hi Ben,
:D
This is working well. in total 2% up in baudrate but very acceptable.

Thanks.

Patrick