QMI8610 QMC5883调试记录

news/2024/11/27 8:47:42/

这个芯片复位跟一般芯片不一样,高电平复位,低电平有效。

下面代码接收显示,得到的应该是xyz的磁感强度,还需要自己算法做方位算法校准算法等

qmi8610

//main.cpp
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <ctype.h>
#include <math.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <memory.h>
#include "main.h"
// unsigned int Linux_IICNum = 0;//使用海思的那个iic
int Rd_I2C(int Linux_IICNum, unsigned int chipAddr, unsigned int regAddr)
{int retval = 0;int fd = -1, tmp, i;char file_name[0x10];unsigned char buf[4];unsigned int i2c_num, dev_addr, reg_addr, reg_addr_end;unsigned int reg_width = 1, data_width = 1, reg_step = 1;int cur_addr;static struct i2c_rdwr_ioctl_data rdwr;static struct i2c_msg msg[2];unsigned int data;memset(buf, 0x0, 4);i2c_num = Linux_IICNum;dev_addr = chipAddr;reg_addr = regAddr;reg_addr_end = regAddr;dev_addr = dev_addr >> 1;
#ifdef DEBUGprintf("i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; reg_addr_end:0x%x; \reg_width: %d; data_width: %d; reg_step: %d. \n\n",i2c_num, dev_addr << 1, reg_addr, reg_addr_end,reg_width, data_width, reg_step);
#endif // DEBUGsprintf(file_name, "/dev/i2c-%u", i2c_num);fd = open(file_name, O_RDWR);if (fd < 0){printf("Open %s error!\n", file_name);retval = -1;goto end0;}retval = ioctl(fd, I2C_SLAVE_FORCE, dev_addr);if (retval < 0){printf("CMD_SET_I2C_SLAVE error!\n");retval = -1;goto end1;}msg[0].addr = dev_addr;msg[0].flags = 0;msg[0].len = reg_width;msg[0].buf = buf;msg[1].addr = dev_addr;msg[1].flags = 0;msg[1].flags |= I2C_M_RD;msg[1].len = data_width;msg[1].buf = buf;rdwr.msgs = &msg[0];rdwr.nmsgs = (__u32)2;for (cur_addr = reg_addr; cur_addr <= reg_addr_end; cur_addr += reg_step){if (reg_width == 2){buf[0] = (cur_addr >> 8) & 0xff;buf[1] = cur_addr & 0xff;}elsebuf[0] = cur_addr & 0xff;retval = ioctl(fd, I2C_RDWR, &rdwr);if (retval != 2){printf("CMD_I2C_READ error!\n");retval = -1;goto end1;}if (data_width == 2){data = buf[1] | (buf[0] << 8);}elsedata = buf[0];//        printf("0x%x: 0x%x\n", cur_addr, data);}retval = data;end1:close(fd);
end0:return retval;
}int Wr_I2C(int Linux_IICNum, unsigned int chipAddr, unsigned int regAddr, unsigned int pData)
{int retval = 0;int fd = -1;int index = 0, tmp, i;char file_name[0x10];unsigned char buf[4];unsigned char i2c_num, dev_addr, reg_addr, data;unsigned int reg_width = 1, data_width = 1;i2c_num = Linux_IICNum;dev_addr = chipAddr;reg_addr = regAddr;data = pData;dev_addr = dev_addr >> 1;
#ifdef DEBUGprintf("i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; data:0x%x; reg_width: %d; data_width: %d.\n",i2c_num, dev_addr << 1, reg_addr, data, reg_width, data_width);
#endifsprintf(file_name, "/dev/i2c-%u", i2c_num);fd = open(file_name, O_RDWR);if (fd < 0){printf("Open %s error!\n", file_name);retval = -1;goto end0;}retval = ioctl(fd, I2C_SLAVE_FORCE, dev_addr);if (retval < 0){printf("set i2c device address error!\n");retval = -1;goto end1;}if (reg_width == 2){buf[index] = (reg_addr >> 8) & 0xff;index++;buf[index] = reg_addr & 0xff;index++;}else{buf[index] = reg_addr & 0xff;index++;}if (data_width == 2){buf[index] = (data >> 8) & 0xff;index++;buf[index] = data & 0xff;index++;}else{buf[index] = data & 0xff;index++;}for (int i = 0; i < 2; i++){retval = write(fd, buf, (reg_width + data_width));if (retval < 0){printf("i2c write error! i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; data:0x%x; reg_width: %d; data_width: %d.\n",i2c_num, dev_addr << 1, reg_addr, data, reg_width, data_width);retval = -1;}else{retval = 0;break;}}
end1:close(fd);
end0:return retval;
}int main(int argc, char *argv[])
{while (1){    unsigned int mxyz[10][3] = {0};unsigned int avermxyz[3] = {0};unsigned int buffer[6] = {0};static float magGaussData[3] = {0.0};static unsigned char cnt = 0;buffer[0]=Rd_I2C(4,0x96,0x19);buffer[1]=Rd_I2C(4,0x96,0x1A);buffer[2]=Rd_I2C(4,0x96,0x1B);buffer[3]=Rd_I2C(4,0x96,0x1C);buffer[4]=Rd_I2C(4,0x96,0x1D);buffer[5]=Rd_I2C(4,0x96,0x1E);printf("Xl=%d,Xh=%d,Yl=%d,Yh=%d,Zl=%d,Zh=%d\r\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);mxyz[cnt][0] = (int16_t)(buffer[1] << 8 | buffer[0]);mxyz[cnt][1] = (int16_t)(buffer[3] << 8 | buffer[2]);mxyz[cnt][2] = (int16_t)(buffer[5] << 8 | buffer[4]);usleep(1000 * 300);}return 0;
}

qmc5883

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <ctype.h>
#include <math.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <memory.h>
#include "main.h"
// unsigned int Linux_IICNum = 0;//使用海思的那个iic
int Rd_I2C(int Linux_IICNum, unsigned int chipAddr, unsigned int regAddr)
{int retval = 0;int fd = -1, tmp, i;char file_name[0x10];unsigned char buf[4];unsigned int i2c_num, dev_addr, reg_addr, reg_addr_end;unsigned int reg_width = 1, data_width = 1, reg_step = 1;int cur_addr;static struct i2c_rdwr_ioctl_data rdwr;static struct i2c_msg msg[2];unsigned int data;memset(buf, 0x0, 4);i2c_num = Linux_IICNum;dev_addr = chipAddr;reg_addr = regAddr;reg_addr_end = regAddr;dev_addr = dev_addr >> 1;
#ifdef DEBUGprintf("i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; reg_addr_end:0x%x; \reg_width: %d; data_width: %d; reg_step: %d. \n\n",i2c_num, dev_addr << 1, reg_addr, reg_addr_end,reg_width, data_width, reg_step);
#endif // DEBUGsprintf(file_name, "/dev/i2c-%u", i2c_num);fd = open(file_name, O_RDWR);if (fd < 0){printf("Open %s error!\n", file_name);retval = -1;goto end0;}retval = ioctl(fd, I2C_SLAVE_FORCE, dev_addr);if (retval < 0){printf("CMD_SET_I2C_SLAVE error!\n");retval = -1;goto end1;}msg[0].addr = dev_addr;msg[0].flags = 0;msg[0].len = reg_width;msg[0].buf = buf;msg[1].addr = dev_addr;msg[1].flags = 0;msg[1].flags |= I2C_M_RD;msg[1].len = data_width;msg[1].buf = buf;rdwr.msgs = &msg[0];rdwr.nmsgs = (__u32)2;for (cur_addr = reg_addr; cur_addr <= reg_addr_end; cur_addr += reg_step){if (reg_width == 2){buf[0] = (cur_addr >> 8) & 0xff;buf[1] = cur_addr & 0xff;}elsebuf[0] = cur_addr & 0xff;retval = ioctl(fd, I2C_RDWR, &rdwr);if (retval != 2){printf("CMD_I2C_READ error!\n");retval = -1;goto end1;}if (data_width == 2){data = buf[1] | (buf[0] << 8);}elsedata = buf[0];//        printf("0x%x: 0x%x\n", cur_addr, data);}retval = data;end1:close(fd);
end0:return retval;
}int Wr_I2C(int Linux_IICNum, unsigned int chipAddr, unsigned int regAddr, unsigned int pData)
{int retval = 0;int fd = -1;int index = 0, tmp, i;char file_name[0x10];unsigned char buf[4];unsigned char i2c_num, dev_addr, reg_addr, data;unsigned int reg_width = 1, data_width = 1;i2c_num = Linux_IICNum;dev_addr = chipAddr;reg_addr = regAddr;data = pData;dev_addr = dev_addr >> 1;
#ifdef DEBUGprintf("i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; data:0x%x; reg_width: %d; data_width: %d.\n",i2c_num, dev_addr << 1, reg_addr, data, reg_width, data_width);
#endifsprintf(file_name, "/dev/i2c-%u", i2c_num);fd = open(file_name, O_RDWR);if (fd < 0){printf("Open %s error!\n", file_name);retval = -1;goto end0;}retval = ioctl(fd, I2C_SLAVE_FORCE, dev_addr);if (retval < 0){printf("set i2c device address error!\n");retval = -1;goto end1;}if (reg_width == 2){buf[index] = (reg_addr >> 8) & 0xff;index++;buf[index] = reg_addr & 0xff;index++;}else{buf[index] = reg_addr & 0xff;index++;}if (data_width == 2){buf[index] = (data >> 8) & 0xff;index++;buf[index] = data & 0xff;index++;}else{buf[index] = data & 0xff;index++;}for (int i = 0; i < 2; i++){retval = write(fd, buf, (reg_width + data_width));if (retval < 0){printf("i2c write error! i2c_num:0x%x, dev_addr:0x%x; reg_addr:0x%x; data:0x%x; reg_width: %d; data_width: %d.\n",i2c_num, dev_addr << 1, reg_addr, data, reg_width, data_width);retval = -1;}else{retval = 0;break;}}
end1:close(fd);
end0:return retval;
}int main(int argc, char *argv[])
{Wr_I2C(3, 0x58, 0x0b, 0x00);Wr_I2C(3, 0x58, 0x0a, 0xC3);Wr_I2C(3, 0x58, 0x0d, 0x40);while (1){    unsigned int mxyz[10][3] = {0};unsigned int avermxyz[3] = {0};unsigned int buffer[6] = {0};static float magGaussData[3] = {0.0};static unsigned char cnt = 0;buffer[0]=Rd_I2C(3,0x58,0x01);buffer[1]=Rd_I2C(3,0x58,0x02);buffer[2]=Rd_I2C(3,0x58,0x03);buffer[3]=Rd_I2C(3,0x58,0x04);buffer[4]=Rd_I2C(3,0x58,0x05);buffer[5]=Rd_I2C(3,0x58,0x06);printf("Xl=%d,Xh=%d,Yl=%d,Yh=%d,Zl=%d,Zh=%d\r\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);mxyz[cnt][0] = (int16_t)(buffer[1] << 8 | buffer[0]);mxyz[cnt][1] = (int16_t)(buffer[3] << 8 | buffer[2]);mxyz[cnt][2] = (int16_t)(buffer[5] << 8 | buffer[4]);cnt++;if (cnt >= 10){cnt = 0;for (unsigned char i = 0, j = 0; i < 3; i++){for (j = 0; j < 10; j++){avermxyz[i] += mxyz[j][i];}avermxyz[i] = avermxyz[i] / 10;}printf("average original data :x=%d,y=%d,z=%d\r\n", avermxyz[0], avermxyz[1], avermxyz[2]);// for (unsigned char k = 0; k < 3; k++)// {//     magGaussData[k] = magGaussData[k] - 32768; // 32768:Null Field Output//     magGaussData[k] = avermxyz[k] / 3000;      // 3000:sensitivity, 3000 counts/G// }// printf("convert the raw data :x=%f,y=%f,z=%f\r\n", magGaussData[0], magGaussData[1], magGaussData[2]);// printf("\r\n");}usleep(1000 * 300);}return 0;
}


http://www.ppmy.cn/news/701789.html

相关文章

[SAM 代码解读 1] class Sam(nn.Module)

代码解读都写在注释了 目录 sam.py image_encoder.py 重点(Calculate decomposed Relative Positional Embeddings) GitHub 官方代码 sam.py # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved.# This source code is licensed under the lice…

糖糖班趣事之棒棒糖篇

以下故事从东方幼儿园网站上&#xff08;2006年上半年&#xff09;摘录而来&#xff0c;感谢朱老师、干老师对橙橙的辛勤培养和细心记录。 冰糖小苹果最近&#xff0c;橙橙又有了一个新的小名“小苹果”&#xff0c;班上的孩子们都知道她是“小苹果”。一天&#xff0c;吃午饭…

谁抢走你的棒棒糖?精彩的创意让你得到的不仅仅是震撼 值得一看(图)

吉尔&#xff0e;格林伯格&#xff08;Jill Greenberg&#xff09;把包括自己女儿在内的27个两至三岁小屁娃弄到自己的摄影棚&#xff0c;塞给他们棒棒糖&#xff0c;趁他们舔得开心&#xff0c;突然把棒棒糖抢走&#xff0c;然后抓起相机抓拍下小屁娃们此时的反应。 我看着这组…

turtle 彩虹棒棒糖

turtle 彩虹棒棒糖 import turtl as t import random t.colormode(255) for i in range(400):a,b,c,d random.randint(0,255),random.randint(0,255),random.randint(0,255),random.randint(0,180)t.color(a,b,c)t.circle(i,d) t.done()

turtle库画棒棒糖

import turtle as t #初始化 t.title("棒棒糖") t.bgcolor("light blue") t.speed(0) t.pensize(25) t.shape("turtle") #棒子描边 t.penup() t.goto(0,100) t.pendown() t.color("#eff686") t.goto(-100,-100)#棒子 t.pensize(15) t.…

Tableau图表 • 棒棒糖图

棒棒糖图&#xff0c;是一种常见的可视化图形&#xff0c;它实际上是条形图和点图的组合&#xff0c;能够同时将长度&#xff08;条形&#xff09;和精度&#xff08;点&#xff09;表现出来。 图&#xff1a;棒棒糖图示例(图片来源于网络) 这里使用tableau自带的超市数据源&am…

R语言基于ggpubr包绘制棒棒糖图

棒棒糖图有些类似我们传统的柱状图&#xff0c;由一根线条和一个圆形组成&#xff0c;比传统柱状图能表达更多信息。特别是在分类数目比较多的时候&#xff0c;如基因表达等&#xff0c;棒棒糖图更能很好的展示数据。今天我们使用R语言ggpubr包来绘制棒棒糖图&#xff0c;ggpub…

复现SCI论文图表:棒棒糖图|渐变|legeng设置

有小伙伴在群里咨询了一个图的做法&#xff0c;如下&#xff1a; &#xff08;Reference&#xff1a;GRB10 and E2F3 as Diagnostic Markers of Osteoarthritis and Their Correlation with Immune Infiltration&#xff09; 是一个棒棒糖图&#xff0c;图的特点是大小表示相关…