主题 : mini2440上你用内核自带的i2c-dev写的HMC5883L的应用程序,求各位大神看看问题出现在哪 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 124338
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2016-04-12
最后登录: 2017-11-02
楼主  发表于: 2016-05-11 12:15

 mini2440上你用内核自带的i2c-dev写的HMC5883L的应用程序,求各位大神看看问题出现在哪

下面是代码,编译后,放到开发板 ,运行时出现: 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>


//*******************È«&frac34;Ö±äÁ¿*****************
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707


int ret, fd;

struct i2c_rdwr_ioctl_data HMC5883L_data;

//************Ê&sup1;Óõ&frac12;µÄº¯Êý¶¨ÒåºÍÉùÃ÷***************
struct i2c_msg

{

unsigned short addr; //´Ó»úµÄµØÖ·

unsigned short flags; //ÉèÖöÁд

#define I2C_M_TEN 0x0010

#define I2C_M_RD 0x0001

unsigned short len; //Ò»¸öÏûÏ¢×Ö·û´®µÄ×Ö&frac12;ÚÊý

unsigned char *buf; //ÏûÏ¢×Ö·û´®

};

struct i2c_rdwr_ioctl_data

{

struct i2c_msg *msgs;

int nmsgs; //´«ÊäÏûÏ¢µÄ¸öÊý

};

//дi2cÊý&frac34;Ý
void write_i2c_data(int slave_addr, int reg_addr, int value)
{
HMC5883L_data.nmsgs = 1; //´«Êä1ÌõÐÅÏ¢

(HMC5883L_data.msgs[0]).len = 2; //ÐÅÏ¢&sup3;¤¶ÈΪ2¸ö×Ö&frac12;Ú

(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ· 0x50

(HMC5883L_data.msgs[0]).flags = 0; // flag=0±íÊ&frac34;д

(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(2); //ÉêÇë¿Õ&frac14;ä

(HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý&frac34;ÝÊÇAT24C08´æ´¢¿Õ&frac14;äµÄµØÖ·

(HMC5883L_data.msgs[0]).buf[1] = value;//µÚ¶þ¸ö´«ÊäµÄÊý&frac34;ÝÊÇÏòAT24C08µØÖ·¿Õ&frac14;äдµÄÊý

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; //ÐÅÏ¢&sup3;¤¶ÈΪ2¸ö×Ö&frac12;Ú

(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ· 0x50

(HMC5883L_data.msgs[0]).flags = 0; // flag=0±íÊ&frac34;д

(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(1); //ÉêÇë¿Õ&frac14;ä

(HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý&frac34;ÝÊÇAT24C08´æ´¢¿Õ&frac14;äµÄµØÖ·

// (e2prom_data.msgs[0]).buf[1] = value;//µÚ¶þ¸ö´«ÊäµÄÊý&frac34;ÝÊÇÏòAT24C08µØÖ·¿Õ&frac14;äдµÄÊý

ret = ioctl(fd, I2C_RDWR, (unsigned long)&HMC5883L_data); //µ÷ÓÃioctl

if (ret < 0)
{

perror("ioctl error1");
}
}

//¶Ái2cÊý&frac34;Ý
void read_i2c_data(int slave_addr)
{
HMC5883L_data.nmsgs = 1; //´«Êä2ÌõÐÅÏ¢

// (HMC5883L_data.msgs[0]).len = 1; //ÐÅÏ¢µÄ&sup3;¤¶ÈΪ1¸ö×Ö&frac12;Ú

// (HMC5883L_data.msgs[0]).addr = slave_addr; //´Ó»úµÄµØÖ·

// (HMC5883L_data.msgs[0]).flags = 0;// flag=0±íÊ&frac34;д

// (HMC5883L_data.msgs[0]).buf[0] = reg_addr;//µÚÒ»¸ö´«ÊäµÄÊý&frac34;ÝÊÇAT24C08´æ´¢¿Õ&frac14;äµÄµØÖ·

(HMC5883L_data.msgs[0]).len = 6;//ÐÅÏ¢µÄ&sup3;¤¶ÈΪ1¸ö×Ö&frac12;Ú

(HMC5883L_data.msgs[0]).addr = slave_addr;//´Ó»úµÄµØÖ·

(HMC5883L_data.msgs[0]).flags = I2C_M_RD;// flag= I2C_M_RD±íÊ&frac34;¶Á

(HMC5883L_data.msgs[0]).buf = (unsigned char*)malloc(6); //ÉêÇë¿Õ&frac14;ä

(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);//&sup1;ÒÆðÒ»ºÁÃë
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ÊÇÔÚ×¢&sup2;ái2c-dev.cºó&sup2;úÉúµÄ£¬´ú±íÒ»¸ö¿É&sup2;Ù×÷µÄÊÊÅäÆ÷¡£ÀûÓÃÁËÄÚºËÖеÄ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); //&sup3;¬Ê±Ê±&frac14;ä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); //´òÓ¡¶Áµ&frac12;µÄÊý&frac34;Ý
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;
级别: 新手上路
UID: 124338
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2016-04-12
最后登录: 2017-11-02
1楼  发表于: 2016-05-18 19:19
自己顶一下,各位兄弟看看啊