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

Post Reply
patrick schoon
Posts: 74
Joined: Mon Aug 23, 2010 7:07 pm
Location: Amsterdam
Has thanked: 7 times
Been thanked: 13 times
Contact:

I2C baud rate setting is not the right clock

Post 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

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: I2C baud rate setting is not the right clock

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

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: I2C baud rate setting is not the right clock

Post 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 416 times
Let us know how you get on.

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

Re: I2C baud rate setting is not the right clock

Post 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

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: I2C baud rate setting is not the right clock

Post 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 388 times

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

Re: I2C baud rate setting is not the right clock

Post by patrick schoon »

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

Thanks.

Patrick

Post Reply