新手入门
- 积分
- 14
- 金钱
- 14
- 注册时间
- 2020-10-3
- 在线时间
- 3 小时
|
5金钱
问题描述有点长,我已经献上我的所有币,希望能有大佬解决,谢谢
目的:使用中国移动的M5311的硬件I2C接口和MPU6050通信,将获得的运动数据发送到串口调试助手
问题:现在问题是:可以读出除了加速度数据寄存器(Accelerometer Measurements)和温度数据寄存器(Temperature Measurement)还有角速度数据寄存器(Gyroscope Measurements)之外的全部寄存器的数据,也就是说,除了这几个寄存器的数据不能通过I2C总线读取,其他的MPU6050寄存器都可以读取,当然也都可以写入,我使用能成功读取其他寄存器数据的函数去读这三个数据寄存器的值,得到的全都是0。下面是我的代码
- #include"mpu6050.h"
- //下面为移植程序,移植自正点原子的ATK-MPU6050资料
- //从串口上传数据给匿名四轴上位机
- void uart_niming_report(u8 fun, u8 *data, u8 len)
- {
- u8 send_buf[32];
- u8 i;
- if(len>28) return;
- send_buf[len+3] = 0;
- send_buf[0] = 0x88;
- send_buf[1] = fun;
- send_buf[2] = len;
- for(i=0;i<len;i++) send_buf[3+i] = data[i];
- for(i=0;i<len+3;i++) send_buf[len+3] += send_buf[i];
- opencpu_uart_send(HAL_UART_1,send_buf,len+4);
- }
- //发送加速度和陀螺仪原始数据
- void mpu6050_send_data(short aacx, short aacy, short aacz, short gyrox, short gyroy, short gyroz)
- {
- u8 tbuf[12];
- tbuf[0] = (aacx>>8)&0xff;
- tbuf[1] = aacx&0xff;
- tbuf[2] = (aacy>>8)&0xff;
- tbuf[3] = aacy&0xff;
- tbuf[4] = (aacz>>8)&0xff;
- tbuf[5] = aacz&0xff;
- tbuf[6] = (gyrox>>8)&0xff;
- tbuf[7] = gyrox&0xff;
- tbuf[8] = (gyroy>>8)&0xff;
- tbuf[9] = gyroy&0xff;
- tbuf[10] = (gyroz>>8)&0xff;
- tbuf[11] = gyroz&0xff;
- // opencpu_printf(tbuf);
- uart_niming_report(0xa1,tbuf,12);
- }
- u8 mpu_init(void)
- {
- u8 res, ack_flag, reg_flag;
- opencpu_i2c_init();
- opencpu_printf("1");
- opencpu_i2c_set_freq(HAL_I2C_FREQUENCY_50K);
- opencpu_printf("2");
- ack_flag = mpu_write_byte(MPU_PWR_MGMT1_REG,0x80);
- opencpu_printf("3");
- vTaskDelay(10);
- reg_flag = mpu_read_byte(MPU_PWR_MGMT1_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- opencpu_uart_send(HAL_UART_1,&ack_flag,1);
- ack_flag = mpu_write_byte(MPU_PWR_MGMT1_REG, 0x00);
- opencpu_uart_send(HAL_UART_1,&ack_flag,1);
- opencpu_printf("4");
- ack_flag = mpu_set_gyro_fsr(3);
- opencpu_uart_send(HAL_UART_1, &ack_flag, 1);
- opencpu_printf("5");
- mpu_set_accel_fsr(1);
- opencpu_printf("6");
- mpu_set_rate(50);
- opencpu_printf("7");
- mpu_write_byte(MPU_INT_EN_REG, 0x00);
- opencpu_printf("8");
- mpu_write_byte(MPU_USER_CTRL_REG, 0x00);
- opencpu_printf("9");
- mpu_write_byte(MPU_FIFO_EN_REG, 0x80);
- reg_flag = mpu_read_byte(MPU_FIFO_EN_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- opencpu_printf("10");
- res = mpu_read_byte(MPU_DEVICE_ID_REG);
- opencpu_printf("11");
- if(res == MPU_ADDR)
- {
- opencpu_printf("12");
- opencpu_uart_send(HAL_UART_1, &res, 1);
- mpu_write_byte(MPU_PWR_MGMT1_REG, 0x01);
- reg_flag = mpu_read_byte(MPU_PWR_MGMT1_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- mpu_write_byte(MPU_PWR_MGMT2_REG, 0x00);
- mpu_set_rate(50);
- }
- else return 1;
- return 0;
- }
- u8 mpu_write_byte(u8 reg,u8 data)
- {
- // opencpu_i2c_init();
- // reg = (reg << 1)|0;
- u8 buf[2] = {reg,data};
- u8 ack_flag;
- ack_flag = opencpu_i2c_write(MPU_ADDR, buf, 2);
- return ack_flag;
- // int ack_flag;
- // if(ack_flag = opencpu_i2c_write(MPU_ADDR, ®, 1))
- // {
- // opencpu_i2c_deinit();
- // return 1;
- // }
- // opencpu
- }
- u8 mpu_read_byte(u8 reg)
- {
- u8 res;
- opencpu_i2c_write_read(MPU_ADDR, reg, &res, 1);
- return res;
- }
- u8 mpu_read_len(u8 reg, u8 len, u8 *buf)
- {
- u8 i;
- for(i=0;i<len;i++)
- {
- *(buf+i) = mpu_read_byte(reg+i);
- }
- return 0;
- }
- u8 mpu_write_len(u8 addr, u8 reg, u8 len, u8 *buf)
- {
- u8 i;
- u8 ack_flag;
- u8 *new_buf;
- new_buf = (u8*)malloc(sizeof(u8)*(len+1));
- for(i=0; i<len; i++)
- {
- new_buf[len-i] = buf[len-1-i];
- }
- new_buf[0] = reg;
- ack_flag = opencpu_i2c_write(addr, new_buf, len+1);
- return ack_flag;
- }
- u8 mpu_get_accelerometer(short *ax, short *ay, short *az)
- {
- u8 buf[6], res;
- res = mpu_read_len(MPU_ACCEL_XOUTH_REG, 6, buf);
- opencpu_printf("a");
- if(res==0)
- {
- opencpu_printf("b");
- *ax = ((u16_t)buf[0]<<8)|buf[1];
- *ay = ((u16_t)buf[2]<<8)|buf[3];
- *az = ((u16_t)buf[4]<<8)|buf[5];
- opencpu_uart_send(HAL_UART_1, ax, 2);
- }
- return res;
- }
- u8 mpu_get_gyroscope(short *gx,short *gy,short *gz)
- {
- u8 buf[6],res;
- res = mpu_read_len(MPU_GYRO_XOUTH_REG,6,buf);
- opencpu_printf("c");
- if(res==0)
- {
- opencpu_printf("d");
- *gx=((u16_t)buf[0]<<8)|buf[1];
- *gy=((u16_t)buf[2]<<8)|buf[3];
- *gz=((u16_t)buf[4]<<8)|buf[5];
- opencpu_uart_send(HAL_UART_1, gx, 2);
- }
- return res;
- }
- short mpu_get_temperature(void)
- {
- u8 buf[2];
- short raw;
- float temp;
- mpu_read_len(MPU_TEMP_OUTH_REG,2,buf);
- raw=((u16_t)buf[0]<<8)|buf[1];
- temp=36.53+((double)raw)/340;
- return temp*100;
- }
- u8 mpu_set_lpf(u16_t lpf)
- {
- u8 data=0;
- if(lpf>=188)data=1;
- else if(lpf>=98)data=2;
- else if(lpf>=42)data=3;
- else if(lpf>=20)data=4;
- else if(lpf>=10)data=5;
- else data=6;
- return mpu_write_byte(MPU_CFG_REG,data);//设置数字低通滤波器
- }
- u8 mpu_set_rate(u16_t rate)
- {
- u8 data, reg_flag;
- if(rate>1000)rate=1000;
- if(rate<4)rate=4;
- data=1000/rate-1;
- data=mpu_write_byte(MPU_SAMPLE_RATE_REG,data); //设置数字低通滤波器
- reg_flag = mpu_read_byte(MPU_SAMPLE_RATE_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- return mpu_set_lpf(rate/2); //自动设置lpf为采样率的一半
- }
- u8 mpu_set_accel_fsr(u8 fsr)
- {
- u8 reg_flag;
- mpu_write_byte(MPU_ACCEL_CFG_REG,fsr<<3);//设置加速度传感器满量程范围
- reg_flag = mpu_read_byte(MPU_ACCEL_CFG_REG);
- opencpu_uart_send(HAL_UART_1, reg_flag, 1);
- return 0;
- }
- u8 mpu_set_gyro_fsr(u8 fsr)
- {
- u8 reg_flag;
- mpu_write_byte(MPU_GYRO_CFG_REG,fsr<<3);//设置陀螺仪满量程范围
- reg_flag = mpu_read_byte(MPU_GYRO_CFG_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- return 0;
- }
复制代码 然后是我的main.c 函数
- short aacx, aacy, aacz;
- short gyrox, gyroy, gyroz;
- short temp, init_flag;
- custom_uart_init();
- init_flag = mpu_init();
- opencpu_uart_send(HAL_UART_1, &init_flag, 1);
- opencpu_printf("4");
- while(1)
- {
- opencpu_printf("5");
- temp = mpu_read_byte(MPU_ACCEL_XOUTH_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_ACCEL_XOUTL_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_ACCEL_YOUTH_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_ACCEL_YOUTL_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_ACCEL_ZOUTH_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_ACCEL_ZOUTL_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_TEMP_OUTH_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- temp = mpu_read_byte(MPU_TEMP_OUTL_REG);
- opencpu_uart_send(HAL_UART_1, &temp, 1);
- vTaskDelay(200);
- }
复制代码 说明:上面的<opencpu_printf>函数和<opencpu_uart_send>函数都是通过串口向上位机传递数据,实际上是我通过串口数据调试我的程序,现在测试的结果如下
- [17:22:46.386]收←◆31 32 33
- [17:22:46.480]收←◆00 00 00 34 18 00 35 08 36 13 37 38 39 80 31 30 31 31 31 32 68 01 13 00 34 35 00 00 00 00 00 00 00 00
复制代码 上面的输出中31 32 33是调用<mpu_init>函数时opencpu_printf打印的;
然后延时了100ms <vTaskDelay(10)>
接下来的00 00 00可以不用管,34是打印的"4"
18是调用了<mpu_set_gyro_fsr>时获取的陀螺仪设置寄存器的值,也就是如下所示
- u8 mpu_set_gyro_fsr(u8 fsr)
- {
- u8 reg_flag;
- mpu_write_byte(MPU_GYRO_CFG_REG,fsr<<3);//设置陀螺仪满量程范围
- reg_flag = mpu_read_byte(MPU_GYRO_CFG_REG);
- opencpu_uart_send(HAL_UART_1, ®_flag, 1);
- return 0;
- }
复制代码 通过<mpu_read_byte>读取陀螺仪设置寄存器获取它的值,可以打印出18,证明写入设置和读取是成功的
到这一步其实可以证明我的<mpu_read_byte>函数,和<mpu_write_byte>函数都是没有问题的,可以通过I2C写入和读取
但是
- 34 35 00 00 00 00 00 00 00 00
复制代码 到最后这几位,就到了main函数,可以看出,串口先打印了“4”,“5”,然后通过<mpu_read_byte>获取加速度数据寄存器的值,发现全部都为0。
当然,在获取值的时候我在不停让传感器运动,但是输出的全都是00,没有任何变化。
如果我的<mpu_read_byte>可以读取其他寄存器的值,按理来说应该也可以读取加速度数据寄存器的值,但是读不出来,我也很困扰。
设置寄存器的初始化都是移植自正点原子的程序,应该没什么问题。先在不知道问题出在哪。如果能解决,十分感谢
|
|