我的讯为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;
}