View previous topic :: View next topic |
Author |
Message |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
Communication between two 12f1822 via I2c |
Posted: Sun Feb 11, 2018 4:37 pm |
|
|
slave does not receive data
Master
Code: |
#include <12F1822.h>
#use delay(internal=32000000)
#use i2c(Master,Fast,sda=PIN_A2,scl=PIN_A1,force_hw)
void main()
{
while(TRUE)
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x01);
i2c_stop();
}
}
|
and Slave
Code: |
#include <12F1822.h>
#fuses NOPUT,NOWDT,NOBROWNOUT
#use delay(internal=32000000)
#use i2c(Slave,Fast,sda=PIN_A2,scl=PIN_A1,force_hw,address=0xa0)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A3,bits=8,stream=PORT1)
int8 z=0;
#INT_SSP
void ssp_interrupt()
{
output_high(pin_A5);
// int state = i2c_isr_state();
// if(state < 0x80) // Master is sending data
// {
z=i2c_read();
// }
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(TRUE)
{
printf("%u\r",z);
if (z==1) output_high(pin_A5);
if (z==0) output_low(pin_A5);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sun Feb 11, 2018 5:11 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
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:
Quote: | while(TRUE)
{
i2c_start();
i2c_write(0xa0);
i2c_write(0x01);
i2c_stop();
delay_us(100);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
Posted: Mon Feb 12, 2018 4:42 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: 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. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
Posted: Mon Feb 12, 2018 7:55 am |
|
|
and (of course) to actually show anything:
Code: |
#include <12F1822.h>
#use delay(internal=32000000)
#use i2c(Master,Fast,sda=PIN_A2,scl=PIN_A1,force_hw)
void main()
{
int8 toggle=0;
while(TRUE)
{
i2c_start();
i2c_write(0xa0);
i2c_write(toggle);
i2c_stop();
delay_ms(1000);
toggle ^=1; //change the toggle
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
|
Posted: Mon Feb 19, 2018 11:18 am |
|
|
does i2c code work properly in proteus simulation? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
Posted: Mon Feb 19, 2018 12:00 pm |
|
|
On _some_ chips it does. However not all. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Mon Feb 19, 2018 1:03 pm |
|
|
probably 'works' without the pullups on the bus ..sigh... ![Laughing](images/smiles/icon_lol.gif) |
|
![](templates/subSilver/images/spacer.gif) |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
|
Posted: Sun Feb 25, 2018 6:54 am |
|
|
Ttelmah wrote: | On _some_ chips it does. However not all. |
test 12f1829 and 16f877a and 16f873 but it was not successful ![Question](images/smiles/icon_question.gif) |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sun Feb 25, 2018 7:01 am |
|
|
re:
Quote: |
test 12f1829 and 16f877a and 16f873 but it was not successful |
on REAL PICs or just the 'simulation' ?
we need to know that information ! |
|
![](templates/subSilver/images/spacer.gif) |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
|
Posted: Tue Feb 27, 2018 9:05 am |
|
|
temtronic wrote: | re:
Quote: |
test 12f1829 and 16f877a and 16f873 but it was not successful |
on REAL PICs or just the 'simulation' ?
we need to know that information ! |
Not working in simulation! Work in real pic. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Tue Feb 27, 2018 9:12 am |
|
|
Well that's good news !!
Works with real PICs so get rid of the 'simulator' as it is defective !! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
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... ![Sad](images/smiles/icon_sad.gif) |
|
![](templates/subSilver/images/spacer.gif) |
newguy
Joined: 24 Jun 2004 Posts: 1903
|
|
Posted: Tue Feb 27, 2018 1:09 pm |
|
|
Ttelmah wrote: | Getting people to understand that it doesn't work, seems surprisingly hard... ![Sad](images/smiles/icon_sad.gif) |
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. ![Sad](images/smiles/icon_sad.gif) |
|
![](templates/subSilver/images/spacer.gif) |
|