I2C baud rate setting is not the right clock

A forums to allow bugs and problems with Flowcode v6 to be reported and resolved.

Moderator: Benj

patrick schoon
Posts: 51
Joined: Mon Aug 23, 2010 7:07 pm
Location: Amsterdam
Has thanked: 4 times
Been thanked: 9 times
Contact:

I2C baud rate setting is not the right clock

Postby patrick schoon » Thu Apr 06, 2017 10:20 pm

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

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

Re: I2C baud rate setting is not the right clock

Postby Benj » Mon Apr 10, 2017 5:37 pm

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.

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

Re: I2C baud rate setting is not the right clock

Postby Benj » Mon Apr 10, 2017 5:42 pm

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
(25.56 KiB) Downloaded 184 times


Let us know how you get on.

patrick schoon
Posts: 51
Joined: Mon Aug 23, 2010 7:07 pm
Location: Amsterdam
Has thanked: 4 times
Been thanked: 9 times
Contact:

Re: I2C baud rate setting is not the right clock

Postby patrick schoon » Tue Apr 11, 2017 9:25 pm

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

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

Re: I2C baud rate setting is not the right clock

Postby Benj » Wed Apr 12, 2017 11:48 am

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
(25.68 KiB) Downloaded 166 times

patrick schoon
Posts: 51
Joined: Mon Aug 23, 2010 7:07 pm
Location: Amsterdam
Has thanked: 4 times
Been thanked: 9 times
Contact:

Re: I2C baud rate setting is not the right clock

Postby patrick schoon » Wed Apr 12, 2017 10:01 pm

Hi Ben,
:D
This is working well. in total 2% up in baudrate but very acceptable.

Thanks.

Patrick
These users thanked the author patrick schoon for the post:
Benj (Thu Apr 13, 2017 9:54 am)
Rating: 5.26%