- 论坛徽章:
- 0
|
本帖最后由 kenchowcn 于 2012-06-11 17:07 编辑
OS:linux 2.6.28
CPU:三星6410
I2C:HMC5883L数字电子罗盘传感器
我根据文档写运用程序的代码,我在linux下面配置了I2C的驱动,将hmc5883l焊接在i2c0上,能通过芯片在i2c的地址(0x1E)访问,用示波器看了,有信号,对不对没看,可以确定我操作有效果,我就觉得这个地址应该是正确的,DS上也是这样描述的。
我的代码大致是根据这个示例和自带的一个裸机程序写的。代码如下:- int main()
- {
- int fd, opt, i;
- unsigned int slave_address = 0x3C;
- struct i2c_rdwr_ioctl_data compass_buf;
- int x, y, z;
- int x1, y1, z1;
- fd = open(CHIP_DEV, O_RDWR);
- if (0 > fd)
- {
- perror("open");
- return -1;
- }
-
- compass_buf.nmsgs = 3;
- compass_buf.msgs = (struct i2c_msg*)malloc(compass_buf.nmsgs*sizeof(struct i2c_msg));
- if (NULL == compass_buf.msgs)
- {
- perror("malloc error");
- return -1;
- }
- /* 设置超时和重发 */
- ioctl(fd, I2C_TIMEOUT, 1);
- ioctl(fd, I2C_RETRIES, 2);
- /* 设置寄存器的个数 */
- compass_buf.nmsgs = 1;
- compass_buf.msgs[0].len = 3;
- compass_buf.msgs[0].addr = CHIP_ADDR;
- compass_buf.msgs[0].flags = 0;
- compass_buf.msgs[0].buf = (unsigned char*)malloc(3);
- compass_buf.msgs[0].buf[0] = slave_address;
- compass_buf.msgs[0].buf[1] = 0x02;
- compass_buf.msgs[0].buf[2] = 0x00;
- opt = ioctl(fd, I2C_RDWR, (unsigned long)&compass_buf);
- if (0 > opt)
- {
- perror("io write error");
- }
- usleep(6);
-
- while(1)
- {
- compass_buf.nmsgs = 3;
- compass_buf.msgs[0].len = 2;
- compass_buf.msgs[0].addr = CHIP_ADDR;
- compass_buf.msgs[0].flags = 0; /* 标记写 */
- compass_buf.msgs[0].buf = (unsigned char*)malloc(2);
- compass_buf.msgs[0].buf[0] = slave_address;
- compass_buf.msgs[0].buf[1] = 0x03; /* 存储起始地址 */
- compass_buf.msgs[1].len = 2;
- compass_buf.msgs[1].addr = CHIP_ADDR;
- compass_buf.msgs[1].flags = 0;
- compass_buf.msgs[1].buf = (unsigned char*)malloc(2);
- compass_buf.msgs[1].buf[0] = slave_address+1;
- compass_buf.msgs[1].buf[1] = 0x06;
- /* 读取数据存放初始化 */
- compass_buf.msgs[2].len = 6;
- compass_buf.msgs[2].addr = CHIP_ADDR;
- compass_buf.msgs[2].flags = I2C_M_RD; /* 标记读 */
- compass_buf.msgs[2].buf = (unsigned char*)malloc(6);
- compass_buf.msgs[2].buf[0] = 0;
- compass_buf.msgs[2].buf[1] = 0;
- compass_buf.msgs[2].buf[2] = 0;
- compass_buf.msgs[2].buf[3] = 0;
- compass_buf.msgs[2].buf[4] = 0;
- compass_buf.msgs[2].buf[5] = 0;
- opt = ioctl(fd, I2C_RDWR, (unsigned long)&compass_buf);
- if (0 > opt)
- {
- perror("io write error");
- }
-
- for (i=0; i<6; i++)
- {
- printf("%02x ", compass_buf.msgs[2].buf[i]);
- }
- printf("\n");
- #if 0
- /* 补码:高位是1的,原码是负数 */
- x = (compass_buf.msgs[2].buf[0]<<8) | compass_buf.msgs[2].buf[1];
- z = (compass_buf.msgs[2].buf[2]<<8) | compass_buf.msgs[2].buf[3];
- y = (compass_buf.msgs[2].buf[4]<<8) | compass_buf.msgs[2].buf[5];
- if (x1 != x || y1 != y || z1 != z)
- {
- printf(" x=%d\n y=%d\n z=%d\n", (x), (y), (z));
- x1 = x; y1 = y; z1 = z;
- printf("-------------------\n\n");
- }
- #endif
- usleep(100);
- }
- close(fd);
-
- return 0;
- }
复制代码 这个运行结果是:
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里面的代码,继续看芯片文档,最后写的代码。
以前没做过这方面,大家看看我这个程序缺什么? 要怎么调试? 我没主意了。
|
|