davey
Joined: 04 May 2006 Posts: 17 Location: Sweden
|
i2c-com freezes |
Posted: Wed May 10, 2006 10:49 am |
|
|
Hi!
Below you can see the source-code for a i2c-test-program. I�m sending two bytes of data from one master to a slave. My problem is that I don�t get an acknowledge for the first data-byte that I�m sending....also, after a while the communication freezes and the SLAVE-pic stops working.
Does anyone have an idea where the problem is located?
MASTER-CODE
==========
Code: | #include <i2c_test.H>
#fuses NOWDT
#use i2c(master,sda=PIN_C4,scl=PIN_C3,FORCE_HW)
void send_to_slave() //I2C-funktion
{
port_d ^= 1;
i2c_start();
if(!i2c_write(0x6C)) //address to slave
{
delay_us(20);
if(!i2c_write(0x51)) //data1
{
delay_us(20);
i2c_write(0x1B); //data2
}
}
i2c_stop();
}
main()
{
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_port_a(NO_ANALOGS);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
set_tris_d(0x00);
set_tris_c(0x00);
while(1) //test-loop
{
delay_ms(1000);
send_to_slave();
}
} |
SLAVE-CODE
=========
Code: | #include <i2c_test.H>
#fuses NOWDT
#use i2c(slave,sda=PIN_C4,scl=PIN_C3,address=0x6C,force_hw)
int16 temp;
byte received;
typedef enum {FIRST_READ, LAST_READ} I2C_STATE;
I2C_STATE i2c_status=FIRST_READ;
#int_ssp
receive_isr()
{
port_d ^= 1; //indicator (reaching ISR?)
if(i2c_poll())
{
if(!bit_test(i2c_stat,5)) //check to see if last byte was an address
{
//take care of the address-byte
received=i2c_read();
}
else if(i2c_status==FIRST_READ && bit_test(i2c_stat,5))
{
//first data-byte
received=i2c_read();
temp=received;
i2c_status=LAST_READ;
}
else if(i2c_status==LAST_READ && bit_test(i2c_stat,5))
{
//second data-byte
received=i2c_read();
temp=(temp<<8)&0xFF00;
temp=temp+received;
i2c_status=FIRST_READ;
}
}
bit_clear(i2c_ctrl,3);
}
main()
{
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_port_a(NO_ANALOGS);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
set_tris_d(0x00); // PORT-D: all output
set_tris_c(0xFF); // PORT-C: all input
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(1){}
} |
|
|