pic - How to detect that an i2c slave doesn't respond to the master on PIC18f -
i have pic18f i2c master , other devices slaves.
i want detect if slave not on bus or if doesn't responds.
right now, communication master<->slaves works except when slave doesn't responds. when happens, pic stays in waiting state , whole program stopped.
how can detect , fix ? (in software way)
for information, i'm working on pic18f25k22.
i assume using mplab c18. source i2c functions can found in: c:\program files (x86)\microchip\mplabc18\v3.46\src\pmc_common\i2c
next, you'll need figure out functions hanging , write own versions don't go endless loops if slave doesn't respond. readi2c
hanging waiting slave respond. replace myreadi2c
takes timeout parameter. modified version of code in i2c_read.c.
#if defined (i2c_v1) int myreadi2c( long timeout ) { if( ((sspcon1&0x0f)==0x08) || ((sspcon1&0x0f)==0x0b) ) //master mode sspcon2bits.rcen = 1; // enable master 1 byte reception while ( !sspstatbits.bf && timeout > 0) timeout--; // wait until byte received return timeout == 0 ? (-1) : ( sspbuf ); // return read byte } #endif #if defined (i2c_v4) int myreadi2c( long timeout) { while ( !sspstatbits.bf && timeout > 0) timeout--; // wait until byte received return timeout == 0 ? (-1) : ( sspbuf ); // return read byte } #endif
myreadi2c
return -1 on timeout , unsigned char value (0 - 255) on success. need modify other i2c functions use in similar way avoid loops test register status values. value timeout, need find value through experimentation depending on devices clock speed , peripherals response time.
Comments
Post a Comment