CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C Baud Rate Calculation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tmuir



Joined: 04 Apr 2010
Posts: 3

View user's profile Send private message

I2C Baud Rate Calculation
PostPosted: Sun Apr 04, 2010 4:27 pm     Reply with quote

I'm doing 400kHz I2C on a dsPIC30F4011. I've got everything working, but I still dont understand the baud rate calculation. I realize that CCS takes care of that for you, but I'm trying to understand exactly whats going on in every register.

According to the dspic30F family reference manual: http://ww1.microchip.com/downloads/en/DeviceDoc/70046D.pdf page 550, The equation for I2CBRG is (Fcy/Fscl - Fcy/1,111,111) - 1. I'm using a 7.3728 MHz internal oscillator with a 8X PLL, making Fcy = 14.7456MHz. If Fscl is 400kHz, I2CBRG ends up being 22. Using this value, the I2C does not work.

Looking at the .lst file generated by CCS, using the same values above for Fcy and Fscl, I2CBRG is set to 0x5D, or 93. Using this value, I2C works.

Is the FRM wrong? What is the right calculation?
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Mon Apr 05, 2010 3:00 am     Reply with quote

What CCS setup line are you using for the I2C?.
What compiler version?.
Post your fuse line, clock line, and I2C setup line.
Are you _sure_ all your I2C hardware does work OK at 400K (pull up's low enough, bus capacitance low enough, etc. etc..).

I'd say that CCS is probably getting the calculation wrong, but your hardware is not successfully running at 400K. It just 'happens' that the wrong value from CCS, brings the bus rate low enough to make it work....

Best Wishes
tmuir



Joined: 04 Apr 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon Apr 05, 2010 1:10 pm     Reply with quote

PCWHD v. 4.079

Code:


#include <30F4011.h>
#device *=16   //use 16 bit pointers
#device ADC=10  //use 10 bit ADC

#FUSES NOWDT                   
#FUSES WPSA64
#FUSES FRC_PLL8               
#FUSES NOCKSFSM                 
#FUSES PUT64                   
#FUSES NOPWMPIN                 
#FUSES LPOL_HIGH               
#FUSES HPOL_HIGH
#FUSES MCLR                     
#FUSES NOPROTECT               
#FUSES NOWRT                   
#FUSES NODEBUG                 
#FUSES NOCOE                   
#FUSES ICS0                     
#FUSES RESERVED               

#use delay(clock=58982400)   
#use rs232(UART1A,baud=38400,parity=N,bits=8,DISABLE_INTS)
#use i2c(MASTER, FAST=400000, I2C1)



And in the .lst file, just under main():

Code:

031A:  MOV.B   #5D,W0L
031C:  MOV.B   W0L,204


I was able to look at the clock line this morning on a scope, and I came up with an I2C clock frequency of around 150kHz using the 0x5D I2CBRG. This also fits the equation in the FRM. So it looks like CCS is calculating the BRG value incorrectly.

I'm using the DSPIC2DEM board, so its not a handwired/hacked together setup. I'm reading the bytes off of the wii nunchuck and classic controller, with the wiichuck breakout board from sparkfun. The wires from the breakout board to the dspic2dem board are 22 gauge and about 6" long.

Will shortening those wire to about 2" be enough?
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Mon Apr 05, 2010 2:09 pm     Reply with quote

As far as I know, the nunchuck, uses I2C, at 100KHz, not 400KHz....

Best Wishes
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Mon Apr 05, 2010 2:16 pm     Reply with quote

Update on that.
Have just done a search, and you will find in the Nunchuck forums, people saying it does run at 400KHz, _but_ you have to add delays between the transmitted bytes, in the order of 8uSec, before it works reliably at this rate. Apparently the wireless units, have to use 400K (won't work at 100K), but the timings between the bytes, need to stay at the 100K spacing, or it won't work...
A search on Google, will find a _lot_ about this.

Best Wishes
tmuir



Joined: 04 Apr 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon Apr 05, 2010 4:54 pm     Reply with quote

Everything I had read said 400kHz, but I'll believe my oscilloscope over the internet any day of the week. You're right, there is a ton of wii nunchuck info on google, mostly for arduinos. The best site I found breaks down just about every wii peripheral's data format: http://wiibrew.org/wiki/Wiimote/Extension_Controllers#Data_Format_.28Classic_Controller.29

For future reference though, if I truly need 400kHz I2C, where in my code do I need to set I2CBRG to the correct value, to make sure that it is not overwritten by everything that comes along with #use_i2c()?
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 2:08 am     Reply with quote

First, I'd report the baud rate error to CCS.
With luck, they should give you an update for this, and that way you can go back to using the standard functions. Smile
At the start of the main. Things like baud rate setups, are done just once, in the 'pre-header', that the compiler generates just above the main. Only when you have things that change settings (like 'set_uart_speed' on the normal serial), are changes made 'later'.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group