problem in i2c communication using pic 16f886

problem in i2c communication using pic 16f886

Postby ash1994 » Wed May 27, 2015 7:16 am

Hey guys ....

I am using 1 pic16f886 as a master and another pic16f886 as a slave communicating each other by means of an i2c bus..

The idea is to send a simple text from 1 master to many slaves but I have limited my code to test for just a single slave only.
The message is first recieved by a data reg through UART from the PC.From this data register the data is sent bit by bit via a i2c bus

The Problem i think is with the write function of i2c in my master code but I am not able to debug properly.

Also I am using UART along with i2c for message printing etc.

Pls check in my master and my slave code disregarding the functionality of UART since it works fine,but my i2c is a no-go.(also forget the switch case used to send to many slaves).

The 2 master and slave C files are attached .

Also the code is written using xc8 C compiler of microchip mplabX IDE.

Pls check the slave code too .

Thanks
Attachments
codes.zip
Contains 2 codes named newmaster and newslave
(5.86 KiB) Downloaded 53 times
ash1994
 
Posts: 3
Joined: Wed May 27, 2015 7:12 am

Re: problem in i2c communication using pic 16f886

Postby Olin Lathrop » Wed May 27, 2015 9:55 pm

You need to do what's called "debugging". A good way to strategize debugging is to do experiments that tell you which part of the system the bug is in, eventually getting down to a small enough section you can see what the problem is.

The obvious thing to do here is to look at the IIC bus. That will tell you if the master is sending garbage, or the master is sending what it's supposed to but the slave isn't interpreting it right.

It is also important to properly note the symptoms. Think of each symptom as a clue that tells you something. Simply saying "it doesn't work" is useless for debugging. What specifically doesn't work? You never get any bytes to show up at the slave. OK, so start breaking it down. Is the master sending the right address byte? Is the slave ACKing it? Is the rest of the message being sent correctly, or does it hang after the first byte, etc.

Yes, we can see "I am not able to debug properly", but that's no excuse nor does it absolve you of your obligation. It appears you haven't even tried to do the obvious things. Grow up, stop making exuses, and start collecing some real data on your problem.
User avatar
Olin Lathrop
Verified identity
 
Posts: 48
Joined: Fri May 30, 2014 3:38 pm
Location: Littleton, MA USA
PIC experience: Professional 5+ years with MCHP products

Re: problem in i2c communication using pic 16f886

Postby ash1994 » Thu May 28, 2015 8:16 am

yes sir,great words really

I broke down to smaller levels and found the mistakes in my code ....now its working well ,the only problem now is -
if master is sending say - "ASH1994" then slave recieves and outputs on UART this way - "SH1994H"

i am not able to find out the error there or as to why "A" is not sent :(
ash1994
 
Posts: 3
Joined: Wed May 27, 2015 7:12 am

Re: problem in i2c communication using pic 16f886

Postby Olin Lathrop » Thu May 28, 2015 12:52 pm

ash1994 wrote:if master is sending say - "ASH1994" then slave recieves and outputs on UART this way - "SH1994H"
i am not able to find out the error there or as to why "A" is not sent :(


Again, see what is actually being sent on the bus. That will tell you if it's the master or the slave that's messing up.
User avatar
Olin Lathrop
Verified identity
 
Posts: 48
Joined: Fri May 30, 2014 3:38 pm
Location: Littleton, MA USA
PIC experience: Professional 5+ years with MCHP products

Re: problem in i2c communication using pic 16f886

Postby Ian.M » Fri May 29, 2015 11:48 am

+1 for looking at the data on the bus.

Its quite possible to do this successfully even on an old analog scope. Simply add code in the master's main loop to toggle a spare pin before sending the I2C start bit to give you something to trigger on, and run a script on the PC to send the same commands to the master PIC over and over again. The scope needs to be dual channel (in chopped mode), with a delay timebase so you can zoom in on the I2C clock and data waveform and transcribe them onto paper one bit at a time.

The extra pin toggle is also useful for triggering a logic analyser, but in that case, if its any good, you can use its I2C protocol decoder.

If you have both, check the basic waveforms on the scope and the data on the logic analyser.

Of course, if you don't have a scope or a logic analyser, you are FUBAR.
Ian.M
Verified identity
 
Posts: 94
Joined: Wed May 28, 2014 12:47 am
PIC experience: Professional 1+ years with MCHP products

Re: problem in i2c communication using pic 16f886

Postby Olin Lathrop » Fri May 29, 2015 9:19 pm

Ian.M wrote:Its quite possible to do this successfully even on an old analog scope. Simply add code in the master's main loop to toggle a spare pin before sending the I2C start bit to give you something to trigger on, and run a script on the PC to send the same commands to the master PIC over and over again.

You can probably get away with using the falling edge of SCL as the trigger, with enough trigger holdoff so that it won't re-trigger in the middle of the message.
User avatar
Olin Lathrop
Verified identity
 
Posts: 48
Joined: Fri May 30, 2014 3:38 pm
Location: Littleton, MA USA
PIC experience: Professional 5+ years with MCHP products

Re: problem in i2c communication using pic 16f886

Postby ash1994 » Tue Jun 02, 2015 8:12 am

sure i got my code right ...since the data needs to be available before the falling edge of SCL(hold time),i used a small delay to get it right !!!ill post the code soon ..thanx sir Olin..i did as u told me by breaking down the problems indivitually and got here finally!!
I am still an engineering student and have just started doing my hobby project in my 3rd year of engineering :).thanx for the help
ash1994
 
Posts: 3
Joined: Wed May 27, 2015 7:12 am


Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 1 guest