Whenever you're doing I2C, download/compile/run PCM P's "I2C Scanner" program from the code library. It's a 'must run' as it'll show IF the slave is really there, and where.
Have you got the correct I2C bus pullup resistors ? 3 factors, speed, distance, voltage... all play a part in determining the proper value.
Confirm the I2C wires aren't crossed ! Silly mistake like that can spoil your day...

Do the PICs run at their proper speed ?
2nd program to cut/compile/run is a '1Hz LED' program. This will comfirm the hardware is running at the proper speed.

You'll need to confirm these BEFORE trying to debug your code.
Jay

PCM programmer

Joined: 06 Sep 2003Posts: 20172

Posted: Mon Feb 12, 2018 12:05 am

Your i2c slave program has some hidden interrupt overhead. I calculate
that at 32 MHz, the total time to execute the interrupt routine is about
6 usec.

Your master code is a tight loop. You need to put a delay in there to give
the slave's interrupt routine enough time to finish, before you start a new
transmission. Let's be really conservative and stick a 100 usec delay in
there. Try this and see if it works better:

Also, as written, there is a problem, since the first I2C_read, will return the device address, which is the reason for the I2C_isr_state test, which allows you to know 'where' you are in each transaction....

Last edited by Ttelmah on Mon Feb 12, 2018 7:37 am; edited 1 time in total

temtronic

Joined: 01 Jul 2010Posts: 5878Location: Greensville,Ontario

Posted: Mon Feb 12, 2018 5:54 am

As a follow up, I'd make the master main 'delay loop' to be slower, say 1 second. If you've got an LED on the slave's pin A5 then you'll SEE the on/off response. Now if you have a scope, this won't matter as much.

Well that's good news !!
Works with real PICs so get rid of the 'simulator' as it is defective !!

Ttelmah

Joined: 11 Mar 2010Posts: 12736

Posted: Tue Feb 27, 2018 1:03 pm

Unfortunately, I2C is one of the things Proteus handles badly.
As a master it is reasonable. As a slave, poor.
It handles simple serial OK.
Basic SPI also usually OK.
Any more complex peripheral it has problems.
Getting people to understand that it doesn't work, seems surprisingly hard...

newguy

Joined: 24 Jun 2004Posts: 1456Location: Edmonton, Alberta

Posted: Tue Feb 27, 2018 1:09 pm

Ttelmah wrote:

Getting people to understand that it doesn't work, seems surprisingly hard...

I'm struck by what the populace will believe or choose not to believe. Like convincing some people that vaccinating their kids is a good thing = hard, but convincing them that it's a bad thing = easy.