下面是代码,编译后,放到开发板 ,运行时出现: ioctl error1: No such device or address
ioctl error1: No such device or address ,求各位看看是什么问题
#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <math.h>
//*******************È«¾Ö±äÁ¿*****************
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707
int ret, fd;
struct i2c_rdwr_ioctl_data HMC5883L_data;
//************ʹÓõ½µÄº¯Êý¶¨ÒåºÍÉùÃ÷***************
struct i2c_msg
{
unsigned short addr; //´Ó»úµÄµØÖ·
unsigned short flags; //ÉèÖöÁд
#define I2C_M_TEN 0x0010
#define I2C_M_RD 0x0001
unsigned short len; //Ò»¸öÏûÏ¢×Ö·û´®µÄ×Ö½ÚÊý
unsigned char *buf; //ÏûÏ¢×Ö·û´®
};
struct i2c_rdwr_ioctl_data
{
struct i2c_msg *msgs;
int nmsgs; //´«ÊäÏûÏ¢µÄ¸öÊý
};
//дi2cÊý¾Ý
void write_i2c_data(int slave_addr, int reg_addr, int value)
{
HMC5883L_data.nmsgs = 1; //´«Êä1ÌõÐÅÏ¢
(HMC5883L_data.msgs[0]).len = 2; //ÐÅÏ¢³¤¶ÈΪ2¸ö×Ö½Ú
(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ· 0x50
(HMC5883L_data.msgs[0]).flags = 0; // flag=0±íʾд
(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(2); //ÉêÇë¿Õ¼ä
(HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý¾ÝÊÇAT24C08´æ´¢¿Õ¼äµÄµØÖ·
(HMC5883L_data.msgs[0]).buf[1] = value;//µÚ¶þ¸ö´«ÊäµÄÊý¾ÝÊÇÏòAT24C08µØÖ·¿Õ¼äдµÄÊý
ret = ioctl(fd, I2C_RDWR, (unsigned long)&HMC5883L_data); //µ÷ÓÃioctl
if (ret < 0) {
perror("ioctl error1");
}
}
//send one byte data
void write_i2c_byte(int slave_addr, int reg_addr)
{
HMC5883L_data.nmsgs = 1; //´«Êä1ÌõÐÅÏ¢
(HMC5883L_data.msgs[0]).len = 1; //ÐÅÏ¢³¤¶ÈΪ2¸ö×Ö½Ú
(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ· 0x50
(HMC5883L_data.msgs[0]).flags = 0; // flag=0±íʾд
(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(1); //ÉêÇë¿Õ¼ä
(HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý¾ÝÊÇAT24C08´æ´¢¿Õ¼äµÄµØÖ·
// (e2prom_data.msgs[0]).buf[1] = value;//µÚ¶þ¸ö´«ÊäµÄÊý¾ÝÊÇÏòAT24C08µØÖ·¿Õ¼äдµÄÊý
ret = ioctl(fd, I2C_RDWR, (unsigned long)&HMC5883L_data); //µ÷ÓÃioctl
if (ret < 0)
{
perror("ioctl error1");
}
}
//¶Ái2cÊý¾Ý
void read_i2c_data(int slave_addr)
{
HMC5883L_data.nmsgs = 1; //´«Êä2ÌõÐÅÏ¢
// (HMC5883L_data.msgs[0]).len = 1; //ÐÅÏ¢µÄ³¤¶ÈΪ1¸ö×Ö½Ú
// (HMC5883L_data.msgs[0]).addr = slave_addr; //´Ó»úµÄµØÖ·
// (HMC5883L_data.msgs[0]).flags = 0;// flag=0±íʾд
// (HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý¾ÝÊÇAT24C08´æ´¢¿Õ¼äµÄµØÖ·
(HMC5883L_data.msgs[0]).len = 6;//ÐÅÏ¢µÄ³¤¶ÈΪ1¸ö×Ö½Ú
(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ·
(HMC5883L_data.msgs[0]).flags = I2C_M_RD;// flag= I2C_M_RD±íʾ¶Á
(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(6); //ÉêÇë¿Õ¼ä
(HMC5883L_data.msgs[0]).buf[0] = 0; //Çå¿Õ
(HMC5883L_data.msgs[0]).buf[1] = 0;
(HMC5883L_data.msgs[0]).buf[2] = 0;
(HMC5883L_data.msgs[0]).buf[3] = 0;
(HMC5883L_data.msgs[0]).buf[4] = 0;
(HMC5883L_data.msgs[0]).buf[5] = 0;
ret = ioctl(fd, I2C_RDWR, (unsigned long)&HMC5883L_data); //µ÷ÓÃioctl
if (ret < 0) {
perror("ioctl error2");
}
}
//Initial the HMC5883L
void HMC5883l_init()
{
write_i2c_data(0x3C, 0x02, 0x00);
}
//read HMC5883L X,Y,Z data addr is 0x03-0x08
void HMC5883L_read_data()
{
write_i2c_byte(0x3C,0x03);
sleep(1);//¹ÒÆðÒ»ºÁÃë
read_i2c_data(0x3D);
sleep(5);
}
int main()
{
int i = 0;
int x, y, z;
double angle;
fd = open("/dev/i2c/0", O_RDWR);
//dev/i2c/0ÊÇÔÚ×¢²ái2c-dev.cºó²úÉúµÄ£¬´ú±íÒ»¸ö¿É²Ù×÷µÄÊÊÅäÆ÷¡£ÀûÓÃÁËÄÚºËÖеÄi2c-dev.c
if (fd<0)
{
perror("open error");
}
HMC5883L_data.nmsgs = 2; //´«ÊäµÄÐÅÏ¢¸öÊý
HMC5883L_data.msgs = (struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
if (!HMC5883L_data.msgs) {
perror("malloc error");
exit(1);
}
ioctl(fd, I2C_TIMEOUT, 100); //³¬Ê±Ê±¼ä100ms
ioctl(fd, I2C_RETRIES, 2); //ÖØÊÔ´ÎÊý2´Î
sleep(5);
HMC5883l_init();
while (1)
{
i++;
HMC5883L_read_data();
x = (HMC5883L_data.msgs[0]).buf[0] << 8 | (HMC5883L_data.msgs[0]).buf[1]; //Combine MSB and LSB of X Data output register
y = (HMC5883L_data.msgs[0]).buf[2] << 8 | (HMC5883L_data.msgs[0]).buf[3]; //Combine MSB and LSB of Y Data output register
z = (HMC5883L_data.msgs[0]).buf[4] << 8 | (HMC5883L_data.msgs[0]).buf[5]; //Combine MSB and LSB of Z Data output register
printf("buff[x]=%d\n", x); //´òÓ¡¶Áµ½µÄÊý¾Ý
printf("buff[y]=%d\n", y);
printf("buff[z]=%d\n", z);
angle = atan2((double)y, (double)x) * (180 / 3.14159265) + 180; // angle in degrees
angle *= 10;
printf("buff[z]=%d\n", angle);
if (i == 10)
{
close(fd);
}
sleep(67);
}
return 0;