|
mpu6050庫(kù)分享
mpu6050.rar
(2.03 KB, 下載次數(shù): 27)
2020-12-18 14:58 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
- #ifndef __MPU6050_H__
- #define __MPU6050_H__
- //****************************************
- // 定義MPU6050內(nèi)部地址
- //****************************************
- #define SMPLRT_DIV 0x19 //陀螺儀采樣率,典型值:0x07(125Hz)
- #define CONFIG 0x1A //低通濾波頻率,典型值:0x06(5Hz)
- #define GYRO_CONFIG 0x1B //陀螺儀自檢及測(cè)量范圍,典型值:0x18(不自檢,2000deg/s)
- #define ACCEL_CONFIG 0x1C //加速計(jì)自檢、測(cè)量范圍及高通濾波頻率,典型值:0x01(不自檢,2G,5Hz)
- #define ACCEL_XOUT_H 0x3B
- #define ACCEL_XOUT_L 0x3C
- #define ACCEL_YOUT_H 0x3D
- #define ACCEL_YOUT_L 0x3E
- #define ACCEL_ZOUT_H 0x3F
- #define ACCEL_ZOUT_L 0x40
- #define TEMP_OUT_H 0x41
- #define TEMP_OUT_L 0x42
- #define GYRO_XOUT_H 0x43
- #define GYRO_XOUT_L 0x44
- #define GYRO_YOUT_H 0x45
- #define GYRO_YOUT_L 0x46
- #define GYRO_ZOUT_H 0x47
- #define GYRO_ZOUT_L 0x48
- #define PWR_MGMT_1 0x6B //電源管理,典型值:0x00(正常啟用)
- #define WHO_AM_I 0x75 //IIC地址寄存器(默認(rèn)數(shù)值0x68,只讀)
- #define SlaveAddress 0xD0 //IIC寫入時(shí)的地址字節(jié)數(shù)據(jù),+1為讀取
- typedef unsigned char uchar;
- typedef unsigned short ushort;
- typedef unsigned int uint;
- void InitMPU6050(); //初始化MPU6050
- void Delay5us();
- void I2C_Start();
- void I2C_Stop();
- void I2C_SendACK(bit ack);
- bit I2C_RecvACK();
- void I2C_SendByte(uchar dat);
- uchar I2C_RecvByte();
- void I2C_ReadPage();
- void I2C_WritePage();
- void display_ACCEL_x();
- void display_ACCEL_y();
- void display_ACCEL_z();
- uchar Single_ReadI2C(uchar REG_Address); //讀取I2C數(shù)據(jù)
- void Single_WriteI2C(uchar REG_Address,uchar REG_data); //向I2C寫入數(shù)據(jù)
- int GetData(uchar REG_Address);
- #endif
復(fù)制代碼- #include <reg52.h>
- #include "mpu6050.h"
- #include <INTRINS.H>
- sbit SCL=P1^0; //IIC時(shí)鐘引腳定義
- sbit SDA=P1^1; //IIC數(shù)據(jù)引腳定義
- //**************************************
- //延時(shí)5微秒(STC90C52RC@12M)
- //不同的工作環(huán)境,需要調(diào)整此函數(shù)
- //當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
- //**************************************
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- //**************************************
- //I2C起始信號(hào)
- //**************************************
- void I2C_Start()
- {
- SDA = 1; //拉高數(shù)據(jù)線
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SDA = 0; //產(chǎn)生下降沿
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- }
- //**************************************
- //I2C停止信號(hào)
- //**************************************
- void I2C_Stop()
- {
- SDA = 0; //拉低數(shù)據(jù)線
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SDA = 1; //產(chǎn)生上升沿
- Delay5us(); //延時(shí)
- }
- //**************************************
- //I2C發(fā)送應(yīng)答信號(hào)
- //入口參數(shù):ack (0:ACK 1:NAK)
- //**************************************
- void I2C_SendACK(bit ack)
- {
- SDA = ack; //寫應(yīng)答信號(hào)
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- //**************************************
- //I2C接收應(yīng)答信號(hào)
- //**************************************
- bit I2C_RecvACK()
- {
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- CY = SDA; //讀應(yīng)答信號(hào)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- return CY;
- }
- //**************************************
- //向I2C總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)
- //**************************************
- void I2C_SendByte(uchar dat)
- {
- uchar i;
- for (i=0; i<8; i++) //8位計(jì)數(shù)器
- {
- dat <<= 1; //移出數(shù)據(jù)的最高位
- SDA = CY; //送數(shù)據(jù)口
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- I2C_RecvACK();
- }
- //**************************************
- //從I2C總線接收一個(gè)字節(jié)數(shù)據(jù)
- //**************************************
- uchar I2C_RecvByte()
- {
- uchar i;
- uchar dat = 0;
- SDA = 1; //使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
- for (i=0; i<8; i++) //8位計(jì)數(shù)器
- {
- dat <<= 1;
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- dat |= SDA; //讀數(shù)據(jù)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- return dat;
- }
- //**************************************
- //向I2C設(shè)備寫入一個(gè)字節(jié)數(shù)據(jù)
- //**************************************
- void Single_WriteI2C(uchar REG_Address,uchar REG_data)
- {
- I2C_Start(); //起始信號(hào)
- I2C_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫信號(hào)
- I2C_SendByte(REG_Address); //內(nèi)部寄存器地址,
- I2C_SendByte(REG_data); //內(nèi)部寄存器數(shù)據(jù),
- I2C_Stop(); //發(fā)送停止信號(hào)
- }
- //**************************************
- //從I2C設(shè)備讀取一個(gè)字節(jié)數(shù)據(jù)
- //**************************************
- uchar Single_ReadI2C(uchar REG_Address)
- {
- uchar REG_data;
- I2C_Start(); //起始信號(hào)
- I2C_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫信號(hào)
- I2C_SendByte(REG_Address); //發(fā)送存儲(chǔ)單元地址,從0開(kāi)始
- I2C_Start(); //起始信號(hào)
- I2C_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號(hào)
- REG_data=I2C_RecvByte(); //讀出寄存器數(shù)據(jù)
- I2C_SendACK(1); //接收應(yīng)答信號(hào)
- I2C_Stop(); //停止信號(hào)
- return REG_data;
- }
- //**************************************
- //初始化MPU6050
- //**************************************
- void InitMPU6050()
- {
- Single_WriteI2C(PWR_MGMT_1, 0x00); //解除休眠狀態(tài)
- Single_WriteI2C(SMPLRT_DIV, 0x07);
- Single_WriteI2C(CONFIG, 0x06);
- Single_WriteI2C(GYRO_CONFIG, 0x18);
- Single_WriteI2C(ACCEL_CONFIG, 0x01);
- }
- //**************************************
- //合成數(shù)據(jù)
- //**************************************
- int GetData(uchar REG_Address)
- {
- char H,L;
- H=Single_ReadI2C(REG_Address);
- L=Single_ReadI2C(REG_Address+1);
- return (H<<8)+L; //合成數(shù)據(jù)
- }
復(fù)制代碼
|
|