redboyang
Joined: 17 Aug 2006 Posts: 1
|
I2C problem between 16f873(master) and 16f690(slave) |
Posted: Mon Aug 20, 2007 2:40 am |
|
|
the problem is the i2c bus can't get the right data. after the first i2c_write() , SDA and SCL will both be low, and the routine will be dead.
master code
Code: | #include <16f873a.h>
#use I2C(master, sda=PIN_A2, scl=PIN_A5,slow)
#USE DELAY(CLOCK=4000000)
#FUSES XT,NOWDT,PROTECT,PUT,NOBROWNOUT,NOLVP
#USE RS232(BAUD=19200,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7)
#use FAST_IO(A)
#use FAST_IO(B)
#use FAST_IO(C)
char buffer;
int8 a=0x00;
int8 b=0x00;
void main(void)
{
ENABLE_INTERRUPTS(INT_RDA);
ENABLE_INTERRUPTS(GLOBAL);
while(1)
{
if(buffer=='0')
{
buffer=0x00;
i2c_start();
delay_us(5);
i2c_write(0x9A);
delay_us(5);
i2c_write(0x00);
delay_us(5);
i2c_start();
delay_us(5);
i2c_write(0x9B);
delay_us(5);
a=i2c_read(1);
b=i2c_read(0);
delay_us(5);
i2c_stop();
putc(a);
putc(b);
putc(0x00);
}
}
}
#INT_RDA
void INTRDA(void)
{
buffer=GETC();
} |
slave code
Code: | #include <16F690.h>
#fuses INTRC_IO,NOPROTECT, NOBROWNOUT, NOMCLR, NOCPD, NOWDT, PUT, NOIESO, NOFCMEN
#use delay(clock=4000000)
#use i2c(SLAVE,sda=PIN_B4, scl=PIN_B6, address=0x9a,FORCE_HW)
#byte ANSELH = 0x011F
#bit SMP = 0x94.7
BYTE a;
#INT_SSP
void ssp_interupt ()
{
BYTE incoming, state;
SMP=0;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1)
a = incoming;
}
if(state == 0x80) //Master is requesting data
{
i2c_write(0x01);
delay_us(50);
i2c_write(0x02);
}
}
void main ()
{
ANSELH=0x00;
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
SMP=0;
while (TRUE) {SMP=0;}
} |
|
|