TA的每日心情 | 衰 2017-11-27 16:33 |
---|
签到天数: 7 天 连续签到: 1 天 [LV.3]偶尔看看II
|
先上效果图:
MPU6050 由于其性价比高,被广泛由于各种需要检测姿态角和惯性导航的系统中。其读取也是很方便,使用的模拟IIC,移植性较强,但要注意设置为开漏输出,废话不多说,上代码。
- #include "include.h"
- int main(void)
- {
- systick_config();
-
- UART_Init(Debug_UART,115200);
-
- LED_Init();
- MPU6050_Init();
- while(1)
- {
- LED_Troggle();
- delay_1ms(50);
- printf("%d %d %d %d %d %d\r\n",getAccX(),getAccY(),getAccZ(),getGyroX(),getGyroY(),getGyroZ());
- }
- }
复制代码
- #include "IIC_Analog_6050.h"
- int read_buff[6];
- void I2C_Start(void);
- void I2C_Stop(void);
- void I2C_Ack(void);
- void I2C_NoAck(void);
- void I2C_SendByte(uint8);
- uint8 I2C_ReceiveByte(void);
- void I2C_Delay(uint16);
- void I2C_SDA_OUT(void)
- {
- //使能时钟
- rcu_periph_clock_enable(MPU6050_PORT_CLK);
- //设置模式
- gpio_mode_set(MPU6050_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,SDA_Pin);
- gpio_output_options_set(MPU6050_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,SDA_Pin);
- }
- void I2C_SDA_IN(void)
- {
- rcu_periph_clock_enable(MPU6050_PORT_CLK);
- rcu_periph_clock_enable(RCU_SYSCFG);
- gpio_mode_set(MPU6050_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE,SDA_Pin);
- }
- /*
- * I2C_Init
- * 初始化I2C所需引脚
- */
- void I2C_Init(void)
- {
- //使能时钟
- rcu_periph_clock_enable(MPU6050_PORT_CLK);
- //设置模式
- gpio_mode_set(MPU6050_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,SDA_Pin|SCL_Pin);
- gpio_output_options_set(MPU6050_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,SDA_Pin|SCL_Pin);
- //清零
- GPIO_BOP(MPU6050_PORT) = SDA_Pin|SCL_Pin;
- }
- /*
- * I2C_WriteReg
- * 写I2C设备寄存器
- */
- void I2C_WriteReg(uint8 dev_addr,uint8 reg_addr , uint8 data)
- {
- I2C_Start();
- I2C_SendByte(dev_addr<<1);
- I2C_SendByte(reg_addr );
- I2C_SendByte(data);
- I2C_Stop();
- }
- //读寄存器
- uint8 I2C_ReadByte(uint8 dev_addr,uint8 reg_addr)
- {
- uint8 data;
- I2C_Start();
- I2C_SendByte(dev_addr<<1);
- I2C_SendByte(reg_addr);
- I2C_Start();
- I2C_SendByte((dev_addr<<1)+1);
- data= I2C_ReceiveByte();
- I2C_NoAck();
- I2C_Stop();
- return data;
- }
- //读寄存器
- int16 I2C_ReadWord(uint8 dev_addr,uint8 reg_addr)
- {
- char h,l;
- I2C_Start();
- I2C_SendByte( dev_addr<<1);
- I2C_SendByte( reg_addr);
- I2C_Start();
- I2C_SendByte((dev_addr<<1)+1);
- h= I2C_ReceiveByte();
- I2C_Ack();
- l= I2C_ReceiveByte();
- I2C_NoAck();
- I2C_Stop();
- return (h<<8)+l;
- }
- void I2C_ReadGryo(uint8 dev_addr,uint8 reg_addr,int16 *x,int16 *y)
- {
- char h,l;
- I2C_Start();
- I2C_SendByte( dev_addr<<1);
- I2C_SendByte( reg_addr);
- I2C_Start();
- I2C_SendByte((dev_addr<<1)+1);
- h= I2C_ReceiveByte();
- I2C_Ack();
- l= I2C_ReceiveByte();
- I2C_Ack();
- *x=(h<<8)+l;
- h= I2C_ReceiveByte();
- I2C_Ack();
- l= I2C_ReceiveByte();
- I2C_Ack();
- h= I2C_ReceiveByte();
- I2C_Ack();
- l= I2C_ReceiveByte();
- I2C_NoAck();
- *y=(h<<8)+l;
- I2C_Stop();
- }
- /*
- * I2C_Start
- * I2C起始信号,内部调用
- */
- void I2C_Start(void)
- {
- I2C_SDA_OUT();
- I2C_DELAY();
- I2C_DELAY();
- I2C_SDA_H;
- I2C_SCL_H;
- I2C_DELAY();
- I2C_DELAY();
- I2C_SDA_L;
- I2C_DELAY();
- I2C_DELAY();
- I2C_SCL_L;
- I2C_DELAY();
- I2C_DELAY();
- }
- /*
- * I2C_Stop
- * I2C停止信号,内部调用
- */
- void I2C_Stop(void)
- {
- I2C_SDA_L;
- I2C_SCL_L;
- I2C_DELAY();
- I2C_DELAY();
- I2C_SCL_H;
- I2C_DELAY();
- I2C_DELAY();
- I2C_SDA_H;
- I2C_DELAY();
- I2C_DELAY();
- I2C_SCL_L;
- }
- /*
- * I2C_Stop
- * I2C应答信号,内部调用
- */
- void I2C_Ack(void)
- {
- I2C_SCL_L;
- I2C_DELAY();
-
- I2C_SDA_L;
- I2C_DELAY();
-
- I2C_SCL_H;
- I2C_DELAY();
-
- I2C_SCL_L;
- I2C_DELAY();
- }
- /*
- * I2C_NoAck
- * I2C无应答信号,内部调用
- */
- void I2C_NoAck(void)
- {
- I2C_SCL_L;
- I2C_DELAY();
- I2C_SDA_H;
- I2C_DELAY();
- I2C_SCL_H;
- I2C_DELAY();
- I2C_SCL_L;
- I2C_DELAY();
- }
- /*
- * I2C_SendByte
- * I2C发送数据,内部调用
- */
- void I2C_SendByte(uint8 data)
- {
- uint8 i=8;
- while(i--)
- {
- I2C_SCL_L;
- if(data&0x80)
- I2C_SDA_H;
- else
- I2C_SDA_L;
- data<<=1;
- I2C_DELAY();
- I2C_SCL_H;
- I2C_DELAY();
- }
- I2C_SCL_L;
- I2C_DELAY();
- I2C_SDA_H;
- I2C_DELAY();
- I2C_SCL_H;
- I2C_DELAY();
- I2C_SCL_L;
- }
- /*
- * I2C_SendByte
- * I2C接收数据,内部调用
- */
- uint8 I2C_ReceiveByte(void)
- {
- uint8 i=8;
- uint8 ReceiveByte=0;
-
- I2C_SDA_H;
- I2C_SDA_IN();
-
- while(i--)
- {
- ReceiveByte<<=1;
- I2C_SCL_L;
- I2C_DELAY();
- I2C_SCL_H;
- I2C_DELAY();
- if(I2C_SDA_I)
- {
- ReceiveByte|=0x01;
- }
-
- }
- I2C_SDA_OUT();
- I2C_SCL_L;
-
- return ReceiveByte;
- }
- /*
- * I2C_SendByte
- * I2C延时函数,内部调用
- */
- void I2C_Delay(uint16 i)
- {
- while(i)
- i--;
- }
- uint8 MPU6050_Init(void)
- {
- I2C_Init();
- delay_1ms(100);
- //解除休眠
- I2C_WriteReg( SLAVEADRESS , PWR_MGMT_1 , 0x00 );//电源管理
- I2C_WriteReg( SLAVEADRESS , SMPLRT_DIV , 0x07 );//采样率分频
- I2C_WriteReg( SLAVEADRESS , MPU_CONFIG , 0x07 );//低通滤波频率
- I2C_WriteReg( SLAVEADRESS , GYRO_CONFIG , 0x18 );//陀螺仪
- I2C_WriteReg( SLAVEADRESS , ACCEL_CONFIG ,0x00 );//加速度计
- if(I2C_ReadByte(SLAVEADRESS , WHO_AM_I)==0x68 ) return 1;
- else return 0;
- }
- int16 getAccX(void)
- {
- short AccX = 0;
- char AccXH = 0 , AccXL = 0;
- AccXH = I2C_ReadByte( SLAVEADRESS , ACCEL_XOUT_H );
- AccXL = I2C_ReadByte( SLAVEADRESS , ACCEL_XOUT_L );
- AccX = (AccXH<<8)|AccXL;
- return AccX;
- }
- int16 getAccY(void)
- {
- int16 AccY = 0;
- char AccYH = 0 , AccYL = 0;
- AccYH = I2C_ReadByte( SLAVEADRESS , ACCEL_YOUT_H );
- AccYL = I2C_ReadByte( SLAVEADRESS , ACCEL_YOUT_L );
- AccY = (AccYH<<8)|AccYL;
- return AccY;
- }
- int16 getAccZ(void)
- {
- int16 AccZ = 0;
- char AccZH = 0 , AccZL = 0;
- AccZH = I2C_ReadByte( SLAVEADRESS , ACCEL_ZOUT_H );
- AccZL = I2C_ReadByte( SLAVEADRESS , ACCEL_ZOUT_L );
- AccZ = (AccZH<<8)|AccZL;
- return AccZ;
- }
- int16 getGyroX(void)
- {
- int16 GyroX = 0;
- char GyroXH = 0 , GyroXL = 0;
-
- GyroXH = I2C_ReadByte( SLAVEADRESS , GYRO_XOUT_H );
- GyroXL = I2C_ReadByte( SLAVEADRESS , GYRO_XOUT_H );
-
- GyroX = (GyroXH<<8)|GyroXL;
-
- return GyroX;
- }
- int16 getGyroY(void)
- {
- int16 GyroY = 0;
- char GyroYH = 0 , GyroYL = 0;
-
- GyroYH = I2C_ReadByte( SLAVEADRESS , GYRO_YOUT_H );
- GyroYL = I2C_ReadByte( SLAVEADRESS , GYRO_YOUT_H );
-
- GyroY = (GyroYH<<8)|GyroYL;
-
- return GyroY;
- }
- int16 getGyroZ(void)
- {
- int16 GyroZ = 0;
- char GyroZH = 0 , GyroZL = 0;
-
- GyroZH = I2C_ReadByte( SLAVEADRESS , GYRO_ZOUT_H );
- GyroZL = I2C_ReadByte( SLAVEADRESS , GYRO_ZOUT_H );
-
- GyroZ = (GyroZH<<8)|GyroZL;
-
- return GyroZ;
- }
- short getTemperature(void)
- {
- short temperature = 0;
- char temperatureH = 0 , temperatureL = 0;
- temperatureH = I2C_ReadByte( SLAVEADRESS , TEMP_OUT_H );
- temperatureL = I2C_ReadByte( SLAVEADRESS , TEMP_OUT_L );
- temperature = (temperatureH<<8)|temperatureL;
- return temperature;
- }
-
复制代码 有需求的自行下载附件
myGD32 - 副本.rar
(7.04 MB, 下载次数: 45)
|
|