新手上路
- 积分
- 42
- 金钱
- 42
- 注册时间
- 2016-4-25
- 在线时间
- 12 小时
|

楼主 |
发表于 2016-4-28 09:30:23
|
显示全部楼层
原子哥,现在我直接用的模块还是出现相同的问题(只有az和gy是随着变化的,其他轴都不变还是508),我把程序贴上来,请老师帮忙看看是不是底层的驱动有问题?
#include "hardware.h"
///////////////////////////////////////////////////////////////////////////////////////////
这里是模拟iic的程序
/*
function: iic_init
input:
return:
*/
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_I2C, ENABLE);
GPIO_InitStructure.GPIO_Pin = I2C_SCL | I2C_SDA ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_SDA_H;
I2C_SCL_H;
}
/*
function: set sda out
input:
return:
*/
void IIC_SDA_OUT (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = I2C_SDA ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*
function: set sda in
input:
return:
*/
void IIC_SDA_IN (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = I2C_SDA ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*
function: set iic start
input:
return:
*/
void IIC_Start(void)
{
IIC_SDA_OUT();
I2C_SDA_H;
I2C_SCL_H;
delay_us(5);
I2C_SDA_L;
delay_us(5);
I2C_SCL_L; //钳住SCL总线准备发送数据
}
/*
function: set iic stop
input:
return:
*/
void IIC_Stop(void)
{
IIC_SDA_OUT();
I2C_SCL_L;
I2C_SDA_L;
delay_us(5);
I2C_SCL_H;
I2C_SDA_H;
delay_us(5);
}
/*
function: set iic response
input:
return:
*/
void IIC_ACK(void)
{
I2C_SCL_L;
IIC_SDA_OUT();
I2C_SDA_L;
delay_us(2);
I2C_SCL_H;
delay_us(4);
I2C_SCL_L;
}
/*
function: set iic ont response
input:
return:
*/
void IIC_NACK(void)
{
I2C_SCL_L;
IIC_SDA_OUT();
I2C_SDA_H;
delay_us(2);
I2C_SCL_H;
delay_us(4);
I2C_SCL_L;
}
/*
function: set iic wait response
input:
return: 0 success
1 fail
*/
u8 IIC_Wait_Ack(void)
{
u8 waittime;
IIC_SDA_IN();
I2C_SCL_H;
I2C_SDA_H;
delay_us(1);
while( GPIO_ReadInputDataBit(GPIO_I2C,I2C_SDA))
{
waittime++;
if(waittime>250)
{
IIC_Stop();
return(1);
}
}
I2C_SCL_L;
return 0;
}
//
//
/*
function: set iic send a bye
input: date
return:
*/
void IIC_Send_Byte(u8 data)
{
u8 i;
IIC_SDA_OUT();
I2C_SCL_L; //
delay_us(1);
for(i=0;i<8;i++)
{
if((data&0x80) == 0x80)
I2C_SDA_H;
else
I2C_SDA_L;
I2C_SCL_H;
delay_us(2); //
I2C_SCL_L;
delay_us(2);
data<<=1;//
}
}
/*
function: set iic read a byte
input: ack 0/1
return: u8 the byte
*/
u8 IIC_Read_Byte(u8 ack)
{
u8 i,receive=0;
IIC_SDA_IN();
for(i=0;i<8;i++)
{
I2C_SCL_L;
delay_us(2);
receive<<=1;
I2C_SCL_H;
delay_us(1);
if(GPIO_ReadInputDataBit(GPIO_I2C,I2C_SDA))
receive++;
}
//
if(ack == 0)
IIC_NACK();
else
IIC_ACK();
return receive;
}
#include "hardware.h"
////这里是MPU6050的驱动程序,参考了原子的源码,没有做改动//////////////////////////////////////////////////////////////////////////////
u8 MPU_Init(void)
{
u8 res;
IIC_Init();//3õê¼»ˉIIC×üÏß
delay_ms(800);
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //
delay_ms(200);
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //
MPU_Set_Gyro_Fsr(3); //
MPU_Set_Accel_Fsr(0); //
MPU_Set_Rate(50); //
MPU_Write_Byte(MPU_INT_EN_REG,0X00); //
MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //
MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //
MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //
res=MPU_Read_Byte(MPU_DEVICE_ID_REG);
//printf("%x",res);
if(res==MPU_ADDR)//Æ÷¼tIDÕyè·
{
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //
MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //
MPU_Set_Rate(50); //
}else return 1;
return 0;
}
u8 MPU_Set_Gyro_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_GYRO_CFG_REG,fsr<<3);//
}
u8 MPU_Set_Accel_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_ACCEL_CFG_REG,fsr<<3);//
}
u8 MPU_Set_LPF(u16 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 rate)
{
u8 data;
if(rate>1000)rate=1000;
if(rate<4)rate=4;
data=1000/rate-1;
data=MPU_Write_Byte(MPU_SAMPLE_RATE_REG,data); //
return MPU_Set_LPF(rate/2); //
}
short MPU_Get_Temperature(void)
{
u8 buf[2];
short raw;
float temp;
MPU_Read_Len(MPU_ADDR,MPU_TEMP_OUTH_REG,2,buf);
raw=((u16)buf[0]<<8)|buf[1];
temp=36.53+((double)raw)/340;
return temp*100;;
}
u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz)
{
u8 buf[6],res;
res=MPU_Read_Len(MPU_ADDR,MPU_GYRO_XOUTH_REG,6,buf);
if(res==0)
{
*gx=((u16)buf[0]<<8)|buf[1];
*gy=((u16)buf[2]<<8)|buf[3];
*gz=((u16)buf[4]<<8)|buf[5];
}
return res;;
}
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az)
{
u8 buf[6],res;
res=MPU_Read_Len(MPU_ADDR,MPU_ACCEL_XOUTH_REG,6,buf);
if(res==0)
{
*ax=((u16)buf[0]<<8)|buf[1];
*ay=((u16)buf[2]<<8)|buf[3];
*az=((u16)buf[4]<<8)|buf[5];
}
return res;
}
u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
u8 i;
IIC_Start();
IIC_Send_Byte((addr<<1)|0);//·¢ËíÆ÷¼tμØÖ·+D′Ãüáî
if(IIC_Wait_Ack()) //μè′yó|′e
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //
IIC_Wait_Ack(); //
for(i=0;i<len;i++)
{
IIC_Send_Byte(buf); //
if(IIC_Wait_Ack()) //
{
IIC_Stop();
return 1;
}
}
IIC_Stop();
return 0;
}
u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
IIC_Start();
IIC_Send_Byte((addr<<1)|0);//
if(IIC_Wait_Ack()) //
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //
IIC_Wait_Ack(); //
IIC_Start();
IIC_Send_Byte((addr<<1)|1);//
IIC_Wait_Ack(); //
while(len)
{
if(len==1)*buf=IIC_Read_Byte(0);//
else *buf=IIC_Read_Byte(1); //
len--;
buf++;
}
IIC_Stop(); //2úéúò»¸öí£Ö1ìõ¼t
return 0;
}
u8 MPU_Write_Byte(u8 reg,u8 data)
{
IIC_Start();
IIC_Send_Byte((MPU_ADDR<<1)|0);//
if(IIC_Wait_Ack()) //
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //
IIC_Wait_Ack(); / /
IIC_Send_Byte(data);//
if(IIC_Wait_Ack()) //
{
IIC_Stop();
return 1;
}
IIC_Stop();
return 0;
}
u8 MPU_Read_Byte(u8 reg)
{
u8 res;
IIC_Start();
IIC_Send_Byte((MPU_ADDR<<1)|0);//
IIC_Wait_Ack(); //
IIC_Send_Byte(reg); //
IIC_Wait_Ack(); //
IIC_Start();
IIC_Send_Byte((MPU_ADDR<<1)|1);//
IIC_Wait_Ack(); //
res=IIC_Read_Byte(0);//
IIC_Stop(); //
return res;
}
主程序中初始化之后只做两件事,读传感器和串口显示
res=MPU_Get_Accelerometer(ax,ay,az);
IMU_off_arrc.ax=*ax;
IMU_off_arrc.ay=*ay;
IMU_off_arrc.az=*az;
printf("%x",res);
printf("\r\n");
delay_ms(500);
res=MPU_Get_Gyroscope(gx,gy,gz);
printf("%x",res);
printf("\r\n");
printf("off_ax=%d\r\n",IMU_off_arrc.ax);
printf("off_ay=%d\r\n",IMU_off_arrc.ay);
printf("off_az=%d\r\n",IMU_off_arrc.az);
|
|