View previous topic :: View next topic |
Author |
Message |
falleaf
Joined: 23 May 2004 Posts: 48
|
|
Posted: Mon Jun 21, 2004 4:05 pm |
|
|
Yes mike,
I can send to the slave and test good, but I cannot read from it.
Thanks muchie. |
|
|
falleaf
Joined: 23 May 2004 Posts: 48
|
|
Posted: Wed Jun 23, 2004 11:40 am |
|
|
Quote: | For receiving, you will need to determine what you want to put in the outgoing bytes. The global array I provided is just an example taken from the application note. However, it is pretty much useless by itself.
So, you said that you tried it and it works fine for master sending bytes to the slave, and you confirmed that the slave received the bytes? But when the master tries to read from the slave, then you get errors?
I'll try to answer your questions later this evening when I go home.
-Mike |
Clearer:
I make i2c 5 byte buffer.
I send 05, 06, 07 to slave. Read from the slave. It's good
I send a read command to the slave, I receive only the first byte (value = 05) all other byte = 0xff.!! I don't know why. |
|
|
valemike Guest
|
|
Posted: Wed Jun 23, 2004 1:32 pm |
|
|
Post your code on the master side. How many i2c_read() operations did you do? Remember that your last i2c_read() operation should have a '0' in its argument (i.e. i2c_read(0);), followed by a i2c_stop();
-Mike |
|
|
falleaf
Joined: 23 May 2004 Posts: 48
|
|
Posted: Thu Jun 24, 2004 3:52 pm |
|
|
Oh, I write all read function in mode (0).
byte1 = i2c_read(0);
byte2=i2c_read(0);
...
well, it's wrong??
I'll test and tell you the results tomorrow. Thanks muchie valemike |
|
|
snibbe
Joined: 06 Jan 2005 Posts: 4
|
WORKING I2C MASTER AND SLAVE LOGGER : MULTI-BYTE |
Posted: Sun Jan 09, 2005 2:07 pm |
|
|
Complete working code examples for multi-byte write and read can be found here:
http://www.ccsinfo.com/forum/viewtopic.php?p=37808#37808
Thanks very much to valemike for the slave code.
Best wishes,
Scott |
|
|
UFAnders
Joined: 13 Apr 2005 Posts: 36 Location: Michigan
|
I2C Errors with getting byte from slave! |
Posted: Tue Jun 28, 2005 8:48 pm |
|
|
Snibbe/Valmike, I have no idea why I can't get your routine to work on my chip...
I have re-defined the SSP control registers to match that of the 18F2520, but for some reason the SSP interrupt always goes to the default case once I try to read data back from the slave. I can send it values fine as hyperterminal shows the correct hex numbers contained in the slave (as entered on the master side). Once I try to read anything from the slave, however, I get the "I2C ERROR".
Here are my slave's SSP registers upon trying to read from the slave:
Quote: | I2C ERROR!
SSPBUF=11
SSPADD=10
SSPSTAT=0C
SSPCON1=66
SSPCON2=00 |
Anyone ever run into this? Thanks for your help! |
|
|
valemike Guest
|
|
Posted: Tue Jun 28, 2005 10:11 pm |
|
|
I'm going to have to sample the PIC18F2520 and try it. |
|
|
UFAnders
Joined: 13 Apr 2005 Posts: 36 Location: Michigan
|
I2C Errors with getting byte from slave! |
Posted: Tue Jun 28, 2005 10:16 pm |
|
|
Thanks for the quick reply!
Have you had luck using this routine with any other 28-pin 18Fxxxx PICs? I also have a few 18F252s that I could try...
Thanks! |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Tue Jun 28, 2005 10:41 pm |
|
|
Hi UFAnders,
First thing that you can do to rule out slave overruns as being the culprit is to sprinkle a few 10ms delays between the sending of different i2c functions. If it works when you inject ms delays in between, then overruns are the reason. It seems a bit kludgy to inject millisecond delays, but you have no choice but to put those in especially if the slave processor is slow. Otherwise, you'll get an overrun error which will then require you to do some overrun error handling, which i don't know how to do.
Okay, another thing is that there was a bug in the version before the 3.223 compiler that caused i2c_start() to fail for some devices. I asked CCS which devices they were, but haven't gotten an answer back for weeks. I'll probably e-mail them again.
So, not realizing that 3.223 fixed i2c_start(), I gave up hope and decided to implement the master code myself, based on the Microchip app note.
WARNING: This only works for writing ONE byte and reading ONE byte. You'll then have to issue an i2c_stop(). For my purposes this was enough.
And, you'll notice that i have clumsy while-loops that might never exit the function and cause it to hang. I still have to fix that.
Code: |
void setup_i2c(void)
{
// Configure for Master I2C
PIC_SSPCON1 = 0x28;
PIC_SSPSTAT = 0x80;
PIC_SSPADD = 0x1D; // set for 33000 bps
}
unsigned int i2c_start(void)
{
// Send the START condition
PIC_SSPCON2 |= PIC_SSPCON2_SEN;
// Wait for the START condition to complete.
while (PIC_SSPCON2 & PIC_SSPCON2_SEN)
{
}
return 0;
}
unsigned int i2c_restart(void)
{
// Send the START condition
PIC_SSPCON2 |= PIC_SSPCON2_RSEN;
// Wait for the START condition to complete.
while (PIC_SSPCON2 & PIC_SSPCON2_RSEN)
{
}
return 0;
}
unsigned int i2c_stop(void)
{
// Send the STOP condition
PIC_SSPCON2 |= PIC_SSPCON2_PEN;
// Wait for the START condition to complete.
while (PIC_SSPCON2 & PIC_SSPCON2_PEN)
{
}
return 0;
}
unsigned int i2c_write(unsigned char this_char)
{
unsigned int return_value = 1;
PIC_SSPBUF = this_char;
while (PIC_SSPSTAT & PIC_SSPSTAT_RW)
{
}
// Now check if there is an ACK sent back
if (PIC_SSPCON2 & PIC_SSPCON2_ACKSTAT)
{
return_value = 1;
}
else
{
return_value = 0;
}
return return_value;
}
unsigned int i2c_read(void)
{
unsigned int return_value;
PIC_SSPCON2 |= PIC_SSPCON2_RCEN; // Enable receive mode.
// Wait for the RCEN bit to clear
while (PIC_SSPCON2 & PIC_SSPCON2_RCEN)
{
}
// Send NACK bit for ACKnowledge sequence.
PIC_SSPCON2 |= PIC_SSPCON2_ACKDT;
PIC_SSPCON2 &= ~PIC_SSPCON2_ACKEN; // Send ACK data now
return_value = PIC_SSPBUF;
return return_value;
}
|
You can try it out and see if you still have the same problem. if not, then go to SPI. In fact, i might go over to SPI one day. I heard from my former colleague that they were getting wierd values using i2c when the PIC16F818 (?) was the slave, but it got better when the PIC18F252 was put in - with the same code
I've also read in comp.arch.embedded or comp.arch.piclist that the PIC's MSSP modules samples ACKs at a certain edge such that if your capacitor slowed things down, or if your board's traces had capacitance, then you'd miss that ACK altogether. A workaround back then would have been to do software bit banging on the Master side.
Anyways, my past experience with I2C with a PIC master is pretty good. With a PIC being a slave, I don't know if my past problems were due to the pre-3.223 compiler version or the implementation of my slave code. But with my Master implementation above, i have gotten no more errors. No guarantees it won't have problems in the field though. oh well.
-valemike
Mike |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
Re: I2C Errors with getting byte from slave! |
Posted: Tue Jun 28, 2005 10:51 pm |
|
|
UFAnders wrote: | Thanks for the quick reply!
Have you had luck using this routine with any other 28-pin 18Fxxxx PICs? I also have a few 18F252s that I could try...
Thanks! |
I've tried the routine on a pair of PIC18F458 chips using the following combinations:
1. CCS library functions for the Master PIC using pre-3.223;
SLAVE PIC: Falleaf's posted code
Result: Occasional ack error after sending i2c_start(). Don't know if it's due to the compiler bug.
2. Master: My own simple master i2c implementation (posted above);
Slave: Falleaf's posted code.
Result: Got rid of the occasional ACK error.
3. Soon to try:
Master: 3.223+ version i2c library
See if i don't get anymore ACK errors. |
|
|
UFAnders
Joined: 13 Apr 2005 Posts: 36 Location: Michigan
|
I2C Errors with getting byte from slave! |
Posted: Tue Jun 28, 2005 11:16 pm |
|
|
Durr, I forgot to mention that I'm using PCH 3.217.
I'm bloody tired at the moment, but I'll be sure to try your suggestions tomorrow. |
|
|
Guest
|
Re: I2C Errors with getting byte from slave! |
Posted: Wed Jun 29, 2005 12:34 am |
|
|
UFAnders wrote: | Durr, I forgot to mention that I'm using PCH 3.217.
I'm bloody tired at the moment, but I'll be sure to try your suggestions tomorrow. |
By the way,
My occasional i2c_start() ACK failure in pre 3.223 code would only happen once in a few hundred tries.
Anyhow, i've run my home-made code at 33kbps and 100kbps with no problems, not even an error in a whole afternoon, where i'm averaging 75 start/write address/restart/write_address/read/stop sequences per minute. I am also using the same value pullup resistors as on the picdem-2plus demo board (I forgot what value that was, but i'll look at my schematic tomorrow.
Do you have a demo board such as the PICDEM-2 Plus? I really wish everyone on this forum had a common prototype board.
-Mike |
|
|
|