免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2875 | 回复: 5
打印 上一主题 下一主题

关于I2C的问题,CU终于不维护了,大家帮我看看。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-11 16:45 |只看该作者 |倒序浏览
本帖最后由 kenchowcn 于 2012-06-11 17:07 编辑

OS:linux 2.6.28
CPU:三星6410
I2C:HMC5883L数字电子罗盘传感器

我根据文档写运用程序的代码,我在linux下面配置了I2C的驱动,将hmc5883l焊接在i2c0上,能通过芯片在i2c的地址(0x1E)访问,用示波器看了,有信号,对不对没看,可以确定我操作有效果,我就觉得这个地址应该是正确的,DS上也是这样描述的。



我的代码大致是根据这个示例和自带的一个裸机程序写的。代码如下:
  1. int main()
  2. {
  3.         int fd, opt, i;
  4.         unsigned int slave_address = 0x3C;
  5.         struct i2c_rdwr_ioctl_data compass_buf;
  6.         int x, y, z;
  7.         int x1, y1, z1;

  8.         fd = open(CHIP_DEV, O_RDWR);
  9.         if (0 > fd)
  10.         {
  11.                 perror("open");
  12.                 return -1;
  13.         }
  14.        
  15.         compass_buf.nmsgs = 3;
  16.         compass_buf.msgs = (struct i2c_msg*)malloc(compass_buf.nmsgs*sizeof(struct i2c_msg));
  17.         if (NULL == compass_buf.msgs)
  18.         {
  19.                 perror("malloc error");
  20.                 return -1;
  21.         }

  22.         /* 设置超时和重发 */
  23.         ioctl(fd, I2C_TIMEOUT, 1);
  24.         ioctl(fd, I2C_RETRIES, 2);

  25.         /* 设置寄存器的个数 */
  26.         compass_buf.nmsgs = 1;

  27.     compass_buf.msgs[0].len = 3;
  28.         compass_buf.msgs[0].addr = CHIP_ADDR;
  29.         compass_buf.msgs[0].flags = 0;
  30.         compass_buf.msgs[0].buf = (unsigned char*)malloc(3);
  31.         compass_buf.msgs[0].buf[0] = slave_address;
  32.         compass_buf.msgs[0].buf[1] = 0x02;
  33.         compass_buf.msgs[0].buf[2] = 0x00;

  34.         opt = ioctl(fd, I2C_RDWR, (unsigned long)&compass_buf);
  35.         if (0 > opt)
  36.         {
  37.                         perror("io write error");
  38.         }

  39.         usleep(6);
  40.        
  41.         while(1)
  42.         {       
  43.                 compass_buf.nmsgs = 3;

  44.                 compass_buf.msgs[0].len = 2;
  45.                 compass_buf.msgs[0].addr = CHIP_ADDR;
  46.                 compass_buf.msgs[0].flags = 0; /* 标记写 */
  47.                 compass_buf.msgs[0].buf = (unsigned char*)malloc(2);
  48.                 compass_buf.msgs[0].buf[0] = slave_address;
  49.                 compass_buf.msgs[0].buf[1] = 0x03; /* 存储起始地址 */

  50.                 compass_buf.msgs[1].len = 2;
  51.                 compass_buf.msgs[1].addr = CHIP_ADDR;
  52.                 compass_buf.msgs[1].flags = 0;
  53.                 compass_buf.msgs[1].buf = (unsigned char*)malloc(2);
  54.                 compass_buf.msgs[1].buf[0] = slave_address+1;
  55.                 compass_buf.msgs[1].buf[1] = 0x06;

  56.                 /* 读取数据存放初始化 */
  57.                 compass_buf.msgs[2].len = 6;
  58.                 compass_buf.msgs[2].addr = CHIP_ADDR;
  59.                 compass_buf.msgs[2].flags = I2C_M_RD; /* 标记读 */
  60.                 compass_buf.msgs[2].buf = (unsigned char*)malloc(6);
  61.                 compass_buf.msgs[2].buf[0] = 0;
  62.                 compass_buf.msgs[2].buf[1] = 0;
  63.                 compass_buf.msgs[2].buf[2] = 0;
  64.                 compass_buf.msgs[2].buf[3] = 0;
  65.                 compass_buf.msgs[2].buf[4] = 0;
  66.                 compass_buf.msgs[2].buf[5] = 0;

  67.                 opt = ioctl(fd, I2C_RDWR, (unsigned long)&compass_buf);
  68.                 if (0 > opt)
  69.                 {
  70.                                 perror("io write error");
  71.                 }
  72.                
  73.                 for (i=0; i<6; i++)
  74.                 {
  75.                         printf("%02x ", compass_buf.msgs[2].buf[i]);
  76.                 }
  77.                 printf("\n");

  78. #if 0       
  79.             /* 补码:高位是1的,原码是负数 */
  80.                 x = (compass_buf.msgs[2].buf[0]<<8) | compass_buf.msgs[2].buf[1];
  81.                 z = (compass_buf.msgs[2].buf[2]<<8) | compass_buf.msgs[2].buf[3];
  82.                 y = (compass_buf.msgs[2].buf[4]<<8) | compass_buf.msgs[2].buf[5];

  83.                 if (x1 != x || y1 != y || z1 != z)
  84.                 {
  85.                         printf(" x=%d\n y=%d\n z=%d\n", (x), (y), (z));
  86.                         x1 = x; y1 = y; z1 = z;
  87.                         printf("-------------------\n\n");
  88.                 }
  89. #endif

  90.                 usleep(100);

  91.         }

  92.         close(fd);
  93.        
  94.         return 0;
  95. }
复制代码
这个运行结果是:
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00

没有取到数据,我开始从i2c协议,再到linux里面的代码,继续看芯片文档,最后写的代码。
以前没做过这方面,大家看看我这个程序缺什么? 要怎么调试? 我没主意了。

论坛徽章:
0
2 [报告]
发表于 2012-06-14 17:32 |只看该作者
楼主CHIP_ADDR是多少?
你的slave_address是0x3C,我的理解是0x3C是芯片的I2C写地址,0x3D是读地址,所以CHIP_ADDR应该是0x1E,而slave_address指的应该是芯片内部的寄存器地址,你是不是弄错了..

论坛徽章:
0
3 [报告]
发表于 2012-06-15 09:27 |只看该作者
女人一定要对自己好一点。一旦累死了,就会有别的女人化你的钱,住你的房,睡你的老公,还打你的娃!

知道了 不错~~~

论坛徽章:
0
4 [报告]
发表于 2012-07-04 16:02 |只看该作者

对,我搞错了,现在好了,谢谢了
回复 2# ricky221006


   

论坛徽章:
0
5 [报告]
发表于 2013-05-10 21:00 |只看该作者
楼主,我想知道,你说的linux下配置了I2C驱动,配置了哪里呢?

论坛徽章:
0
6 [报告]
发表于 2013-05-10 22:24 |只看该作者
我怎么觉得楼主的程序是对的?到底错在哪?求解。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP