/**< * \fn i2c_write_byte(c) * \brief Writes a byte to device. * \param c data */ void i2c_write_byte(uint8 c) { uint8 i; for (i = 0; i < 8; i++) { set_sda(c & 0x80); set_scl_high(); delay_4us(); c <<= 1; set_scl_low(); delay_4us(); } } /**< * \fn i2c_read_byte() * \brief Reads a byte from device. * \return Returned data. */ uint8 i2c_read_byte(void) { uint8 i, c = 0; for (i = 0; i < 8; i++) { set_scl_high(); delay_4us(); c <<= 1; c |= get_sda(); set_scl_low(); delay_4us(); } return c; } uint8 pcf8574_write_port(uint8 slave, uint8 bits) { uint8 ret = 1; /* START condition. */ set_sda_high(); set_scl_high(); delay_4us(); set_sda_low(); delay_4us(); set_scl_low(); delay_xms(5); ///< SLAVE address. i2c_write_byte(0x40 | (slave & 0x07) << 1); /**< ACK condition. */ set_sda_high(); delay_4us(); set_scl_high(); delay_4us(); ACC = get_sda(); set_scl_low(); delay_4us(); if (ACC) goto __i2c_write_finish; delay_xms(5); i2c_write_byte(bits); /**< ACK condition. */ set_sda_high(); delay_4us(); set_scl_high(); delay_4us(); ACC = get_sda(); set_scl_low(); delay_4us(); if (ACC) goto __i2c_write_finish; ret = 0; __i2c_write_finish: delay_xms(5); /**< STOP condition. */ set_sda_low(); delay_4us(); set_scl_high(); delay_4us(); set_sda_high(); delay_xms(30); return ret; } uint8 pcf8574_read_port(uint8 slave, uint8 *out) { uint8 ret = 1; /**< START condition. */ set_sda_high(); set_scl_high(); delay_4us(); set_sda_low(); delay_4us(); set_scl_low(); delay_xms(5); ///< SLAVE address. i2c_write_byte(0x41 | (slave & 0x07) << 1); /**< ACK condition. */ set_sda_high(); delay_4us(); set_scl_high(); delay_4us(); ACC = get_sda(); set_scl_low(); delay_4us(); if (ACC) goto __i2c_read_finish; delay_xms(5); *out = i2c_read_byte(); /**< NAK condition. */ set_sda_high(); set_scl_high(); delay_4us(); set_scl_low(); set_sda_low(); delay_4us(); ret = 0; __i2c_read_finish: delay_xms(5); /**< STOP condition. */ set_sda_low(); delay_4us(); set_scl_high(); delay_4us(); set_sda_high(); delay_xms(30); return ret; }
收藏0 举报
yewuyi 发表于 2014-5-28 11:28 读比写稍微复杂一点而已,多了一个‘写’的过程。 读步骤一般如下:
ningling_21 发表于 2014-5-28 12:49 LZ流程好像没搞清楚...
loliweive 发表于 2014-5-29 09:16 uint8 i2c_read_byte(void),这个函数不应该带有地址形参吗?
vkic007 发表于 2014-5-28 22:47 模拟IIC需要注意一些情况,首先要注意数据线的输入输出切换。其次是否应答。再者注意好读写控制。对于时序 ...
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
时间类勋章
发帖类勋章
等级类勋章
5
640
2
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号