代码民工skr 2021-06-01 10:20
浏览 22

讯为4412IIC总线函数使用问题

我的讯为4412板子  在应用层编写代码后,传感器读不出值来,用示波器看了一下I2C_SCL7引脚的输出,波形都不是方波 这是为什么?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
//addr  1101 001  0x69
// reg    0    0x80 0x81 
//
int i2c_read(int fd, unsigned short addr , unsigned int len, unsigned char *buf)
{
	int ret = -1;
	struct i2c_rdwr_ioctl_data am_msgs;
	struct i2c_msg msg;
	memset(&am_msgs, 0, sizeof(am_msgs));
	memset(&msg, 0, sizeof(msg));
	msg.addr = addr;
	msg.flags = I2C_M_RD;
	msg.len = len;
	msg.buf = buf;
	am_msgs.msgs = &msg;
	am_msgs.nmsgs = 1;
	ret = ioctl(fd, I2C_RDWR, &am_msgs);

	return ret;
}

int i2c_write(int fd, unsigned short addr , unsigned int len, unsigned char *buf)
{
	int ret = -1;
	struct i2c_rdwr_ioctl_data am_msgs;
	struct i2c_msg msg;
	memset(&am_msgs, 0, sizeof(am_msgs));
	memset(&msg, 0, sizeof(msg));
	msg.addr = addr;
	msg.flags = 0;
	msg.len = len;
	msg.buf = buf;
	am_msgs.msgs = &msg;
	am_msgs.nmsgs = 1;
	ret = ioctl(fd, I2C_RDWR, &am_msgs);

	return ret;

}

int main(int argc, const char *argv[])
{
	int i = 0;
	int fd = open("/dev/i2c-7", O_RDWR);
	//电源
	char buf[2];
	char temp[128] = {0};
	buf[0] = 0;//寄存器地址
	buf[1] = 0;//写入0号位置的数据
	i2c_write(fd, 0x69, 2, buf);

	sleep(1);
	//0x69  0x80  2
	buf[0] = 0x80;
	i2c_write(fd, 0x69, 1, buf);
	i2c_read(fd, 0x69, 128, temp);
	for (i = 0; i < 64; i++)
	{
		unsigned short tem = (temp[i*2+1]<<8)|(temp[i*2]);
		printf("temp = %f\n", (float)tem/4);
	}
	
	return 0;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题