只需一步,快速开始
28
小学生
时序及其示波器显示波形如下所示,黄色为SDA。前面发送的数据没有任何问题,但是当接收数据的时候出现了如下所示问题。试过很多方法无果前来请教各位大佬!
GPIO设置如图所示,SCK和SDA均为推挽输出。
-
代码如下
void i2c_start(void) { BK1080_SDA_DIR_OUT; BK1080_SDA_HIGH; bk1080_delay(1); BK1080_SCK_HIGH; bk1080_delay(1); BK1080_SDA_LOW; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(1); } static void i2c_stop(void) { BK1080_SDA_DIR_OUT; BK1080_SDA_LOW; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(1); BK1080_SCK_HIGH; bk1080_delay(1); BK1080_SDA_HIGH; bk1080_delay(1); } static void i2c_send(uint8_t data) { BK1080_SDA_DIR_OUT; BK1080_SCK_LOW; bk1080_delay(2); for (uint8_t i = 0; i < 8; i++) { if (data & 0x80) BK1080_SDA_HIGH; else BK1080_SDA_LOW; bk1080_delay(1); BK1080_SCK_HIGH; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(2); data <<= 1; } } static uint8_t i2c_read(void) { uint8_t data = 0; BK1080_SDA_DIR_IN; BK1080_SCK_LOW; bk1080_delay(2); for (uint8_t i = 0; i < 8; i++) { data <<= 1; BK1080_SCK_HIGH; bk1080_delay(1); data |= BK1080_SDA_READ; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(2); } return data; } static void i2c_send_ack(iic_ack_t ack) { BK1080_SDA_DIR_OUT; BK1080_SCK_LOW; bk1080_delay(1); if (ack) BK1080_SDA_HIGH; else BK1080_SDA_LOW; bk1080_delay(1); BK1080_SCK_HIGH; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(2); } static uint8_t i2c_get_ack(void) { uint8_t ack; BK1080_SDA_DIR_IN; BK1080_SCK_LOW; bk1080_delay(5); BK1080_SCK_HIGH; bk1080_delay(1); ack = BK1080_SDA_READ; bk1080_delay(1); BK1080_SCK_LOW; bk1080_delay(2); return ack; }
使用道具 举报
357
大学生
本版积分规则 发表回复 回帖后跳转到最后一页
联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 )
GMT+8, 2024-11-23 01:50 , Processed in 0.115847 second(s), 28 queries , Gzip On.
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.