|
|
View previous topic :: View next topic |
Author |
Message |
Study
Joined: 09 Apr 2009 Posts: 29
|
Problem with i2c between 2 micros |
Posted: Sun Dec 18, 2011 10:08 am |
|
|
Hi friends, I wrote two programs for two 16f877a to send some numbers through I2C, the fist one is master and sends number 1 to 10 and the other one is slave and should get numbers and show on portb, it works on proteus simulator but it doesn't work on real circuit ( i have pull ups too )
the fist program ( master ) is:
Code: |
//I2C send(Master)
#include "16F877A.h"
#fuses NOWDT,NOPROTECT,PUT,NOBROWNOUT,HS
#use delay(clock=4000000)
#use i2c(master,I2C1,fast=100000)
void main()
{
unsigned char a;
TRISC = 0X00;
delay_ms(500);
a=1;
i2c_start();
i2c_write(0x68);
i2c_stop();
while(1)
{
while(a<11)
{
i2c_start();
i2c_write(a);
a ++;
delay_ms(500);
i2c_stop();
}
a = 1;
}
}
|
and the second one ( slave ) is:
Code: |
//I2C GET(SLAVE)
#include "16F877A.h"
#fuses NOWDT,NOPROTECT,PUT,NOBROWNOUT,HS
#use delay(clock=4000000)
#use i2c(slave,I2C1,fast=100000,address=0x68 )
void main()
{
unsigned char a;
TRISC = 0XFF;
PORTB = 0X00;
TRISB = 0X00;
while(1)
{
a=i2c_read();
PORTB=a;
}
}
|
where is the problem?
Regards _________________ Sorry if i have much mistakes, my english is not good.
Thanks a lot for your helps.
Have a good time |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 18, 2011 3:07 pm |
|
|
Use the CCS example program Ex_Slave.c for the slave PIC:
Quote: |
c:\program files\picc\examples\ex_slave.c
|
Use the code in this post for the Master PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3
Make sure you have pull-up resistors on SDA and SCL (use 4.7K ohms).
Make sure there is a ground connection between the two PICs. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Sun Dec 18, 2011 3:12 pm |
|
|
Because the slave is not addressed.
_Every_ I2C transaction needs an address. As soon as you send I2C_STOP, the slave stops receiving, till it sees it's address.
Look at the I2C slave example. Though you may not want to use interrupts, you basically have to do the same thing, and check the status to see whether an address or a data byte has been received. The first byte sent after an I2C_START, is the address, and the slave hardware looks for this, and checks if it matches it's programmed address. If it does, then the byte becomes available to read, and subsequent bytes keep being received, _until_ you send I2C_STOP. The hardware then switches back to looking for the sequence 'start, address', and won't receive anything till this is seen.
You now see why users here moan about the Proteus simulator. While very good for analog circuitry, none of the PIC peripherals functions as it does in the real chip....
Best Wishes |
|
|
|
|
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
|