本帖最后由 perry_peng 于 2014-6-20 09:15 编辑
中断麻烦,我以前用的与pcf8574通信。
非中断模式:
- uint8 i2c_waitIdle()
- {
- uint16 timeout = 0x3ff;
- /**<
- * RW or SEN or RSEN or PEN or RCEN or ACKEN will indicate if the MSSP
- * is in Idle mode.
- */
- while ((SSPSTAT & 0x04 /*RW*/) || (SSPCON2 & 0x1f))
- if (!--timeout) // Check timeout.
- return 1;
- return 0;
- }
- uint8 pcf8574_write_port(uint8 addr, uint8 val)
- {
- uint8 ret = 1;
-
- if (i2c_waitIdle())
- goto __i2c_write_finish;
- delayUsx(5);
- /*SSPCON2bits.*/SEN = 1; // Initiate Start condition on SDA and SCL pins.
- delayUsx(15);
- if (i2c_waitIdle()) // Wait for bus idle
- goto __i2c_write_finish;
- delayUsx(5);
- SSPBUF = 0x40 | (addr << 1); // send address bits with R/W bit
- if (SSPCON2 & 0x40) // ACKSTAT = 0; Acknowledge was received from slave.
- goto __i2c_write_finish;
- delayUsx(15);
- if (i2c_waitIdle()) // Wait for bus idle
- goto __i2c_write_finish;
- delayUsx(5);
- SSPBUF = val; // send data.
- if (SSPCON2 & 0x40) // ACKSTAT = 0; Acknowledge was received from slave.
- goto __i2c_write_finish;
- delayUsx(15);
- ret = 0;
- __i2c_write_finish:
- i2c_waitIdle(); // Wait for bus idle.
- delayUsx(5);
- /*SSPCON2bits.*/PEN = 1; // Initiate STOP condition.
- delayUsx(35);
-
- return ret;
- }
|