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

讯为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 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
      • ¥15 prophet运行报错,如何解决?
      • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢
      • ¥15 eclipse无法正常运行
      • ¥15 定义了函数,但是无法根据函数定义触发器
      • ¥20 5变量卡诺图化简得出与非门电路图
      • ¥15 Python爬取交通拥堵指数数据
      • ¥15 使用vba抓取重定向网页问题
      • ¥20 付费需求测试程序(细谈)。
      • ¥15 为什么这段c++代码会报这么多语法错误?