找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1584|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

51單片機(jī)mpu6050庫(kù)分享

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:552316 發(fā)表于 2020-12-18 14:59 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
mpu6050庫(kù)分享

mpu6050.rar (2.03 KB, 下載次數(shù): 27)
  1. #ifndef __MPU6050_H__
  2. #define __MPU6050_H__

  3. //****************************************
  4. // 定義MPU6050內(nèi)部地址
  5. //****************************************
  6. #define        SMPLRT_DIV                0x19        //陀螺儀采樣率,典型值:0x07(125Hz)
  7. #define        CONFIG                        0x1A        //低通濾波頻率,典型值:0x06(5Hz)
  8. #define        GYRO_CONFIG                0x1B        //陀螺儀自檢及測(cè)量范圍,典型值:0x18(不自檢,2000deg/s)
  9. #define        ACCEL_CONFIG        0x1C        //加速計(jì)自檢、測(cè)量范圍及高通濾波頻率,典型值:0x01(不自檢,2G,5Hz)
  10. #define        ACCEL_XOUT_H        0x3B
  11. #define        ACCEL_XOUT_L        0x3C
  12. #define        ACCEL_YOUT_H        0x3D
  13. #define        ACCEL_YOUT_L        0x3E
  14. #define        ACCEL_ZOUT_H        0x3F
  15. #define        ACCEL_ZOUT_L        0x40
  16. #define        TEMP_OUT_H                0x41
  17. #define        TEMP_OUT_L                0x42
  18. #define        GYRO_XOUT_H                0x43
  19. #define        GYRO_XOUT_L                0x44        
  20. #define        GYRO_YOUT_H                0x45
  21. #define        GYRO_YOUT_L                0x46
  22. #define        GYRO_ZOUT_H                0x47
  23. #define        GYRO_ZOUT_L                0x48
  24. #define        PWR_MGMT_1                0x6B        //電源管理,典型值:0x00(正常啟用)
  25. #define        WHO_AM_I                        0x75        //IIC地址寄存器(默認(rèn)數(shù)值0x68,只讀)
  26. #define        SlaveAddress        0xD0        //IIC寫入時(shí)的地址字節(jié)數(shù)據(jù),+1為讀取


  27. typedef unsigned char  uchar;
  28. typedef unsigned short ushort;
  29. typedef unsigned int   uint;


  30. void  InitMPU6050();                                                                                                        //初始化MPU6050
  31. void  Delay5us();
  32. void  I2C_Start();
  33. void  I2C_Stop();
  34. void  I2C_SendACK(bit ack);
  35. bit   I2C_RecvACK();
  36. void  I2C_SendByte(uchar dat);
  37. uchar I2C_RecvByte();
  38. void  I2C_ReadPage();
  39. void  I2C_WritePage();
  40. void  display_ACCEL_x();
  41. void  display_ACCEL_y();
  42. void  display_ACCEL_z();
  43. uchar Single_ReadI2C(uchar REG_Address);                                                //讀取I2C數(shù)據(jù)
  44. void  Single_WriteI2C(uchar REG_Address,uchar REG_data);        //向I2C寫入數(shù)據(jù)
  45. int GetData(uchar REG_Address);

  46. #endif
復(fù)制代碼
  1. #include <reg52.h>
  2. #include "mpu6050.h"
  3. #include <INTRINS.H>

  4. sbit    SCL=P1^0;                        //IIC時(shí)鐘引腳定義
  5. sbit    SDA=P1^1;                        //IIC數(shù)據(jù)引腳定義


  6. //**************************************
  7. //延時(shí)5微秒(STC90C52RC@12M)
  8. //不同的工作環(huán)境,需要調(diào)整此函數(shù)
  9. //當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
  10. //**************************************
  11. void Delay5us()
  12. {
  13.         _nop_();_nop_();_nop_();_nop_();
  14.         _nop_();_nop_();_nop_();_nop_();
  15.         _nop_();_nop_();_nop_();_nop_();
  16.         _nop_();_nop_();_nop_();_nop_();
  17.         _nop_();_nop_();_nop_();_nop_();
  18.         _nop_();_nop_();_nop_();_nop_();
  19. }
  20. //**************************************
  21. //I2C起始信號(hào)
  22. //**************************************
  23. void I2C_Start()
  24. {
  25.     SDA = 1;                    //拉高數(shù)據(jù)線
  26.     SCL = 1;                    //拉高時(shí)鐘線
  27.     Delay5us();                 //延時(shí)
  28.     SDA = 0;                    //產(chǎn)生下降沿
  29.     Delay5us();                 //延時(shí)
  30.     SCL = 0;                    //拉低時(shí)鐘線
  31. }
  32. //**************************************
  33. //I2C停止信號(hào)
  34. //**************************************
  35. void I2C_Stop()
  36. {
  37.     SDA = 0;                    //拉低數(shù)據(jù)線
  38.     SCL = 1;                    //拉高時(shí)鐘線
  39.     Delay5us();                 //延時(shí)
  40.     SDA = 1;                    //產(chǎn)生上升沿
  41.     Delay5us();                 //延時(shí)
  42. }
  43. //**************************************
  44. //I2C發(fā)送應(yīng)答信號(hào)
  45. //入口參數(shù):ack (0:ACK 1:NAK)
  46. //**************************************
  47. void I2C_SendACK(bit ack)
  48. {
  49.     SDA = ack;                  //寫應(yīng)答信號(hào)
  50.     SCL = 1;                    //拉高時(shí)鐘線
  51.     Delay5us();                 //延時(shí)
  52.     SCL = 0;                    //拉低時(shí)鐘線
  53.     Delay5us();                 //延時(shí)
  54. }
  55. //**************************************
  56. //I2C接收應(yīng)答信號(hào)
  57. //**************************************
  58. bit I2C_RecvACK()
  59. {
  60.     SCL = 1;                    //拉高時(shí)鐘線
  61.     Delay5us();                 //延時(shí)
  62.     CY = SDA;                   //讀應(yīng)答信號(hào)
  63.     SCL = 0;                    //拉低時(shí)鐘線
  64.     Delay5us();                 //延時(shí)
  65.     return CY;
  66. }
  67. //**************************************
  68. //向I2C總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)
  69. //**************************************
  70. void I2C_SendByte(uchar dat)
  71. {
  72.     uchar i;
  73.     for (i=0; i<8; i++)         //8位計(jì)數(shù)器
  74.     {
  75.         dat <<= 1;              //移出數(shù)據(jù)的最高位
  76.         SDA = CY;               //送數(shù)據(jù)口
  77.         SCL = 1;                //拉高時(shí)鐘線
  78.         Delay5us();             //延時(shí)
  79.         SCL = 0;                //拉低時(shí)鐘線
  80.         Delay5us();             //延時(shí)
  81.     }
  82.     I2C_RecvACK();
  83. }
  84. //**************************************
  85. //從I2C總線接收一個(gè)字節(jié)數(shù)據(jù)
  86. //**************************************
  87. uchar I2C_RecvByte()
  88. {
  89.     uchar i;
  90.     uchar dat = 0;
  91.     SDA = 1;                    //使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
  92.     for (i=0; i<8; i++)         //8位計(jì)數(shù)器
  93.     {
  94.         dat <<= 1;
  95.         SCL = 1;                //拉高時(shí)鐘線
  96.         Delay5us();             //延時(shí)
  97.         dat |= SDA;             //讀數(shù)據(jù)               
  98.         SCL = 0;                //拉低時(shí)鐘線
  99.         Delay5us();             //延時(shí)
  100.     }
  101.     return dat;
  102. }
  103. //**************************************
  104. //向I2C設(shè)備寫入一個(gè)字節(jié)數(shù)據(jù)
  105. //**************************************
  106. void Single_WriteI2C(uchar REG_Address,uchar REG_data)
  107. {
  108.     I2C_Start();                  //起始信號(hào)
  109.     I2C_SendByte(SlaveAddress);   //發(fā)送設(shè)備地址+寫信號(hào)
  110.     I2C_SendByte(REG_Address);    //內(nèi)部寄存器地址,
  111.     I2C_SendByte(REG_data);       //內(nèi)部寄存器數(shù)據(jù),
  112.     I2C_Stop();                   //發(fā)送停止信號(hào)
  113. }
  114. //**************************************
  115. //從I2C設(shè)備讀取一個(gè)字節(jié)數(shù)據(jù)
  116. //**************************************
  117. uchar Single_ReadI2C(uchar REG_Address)
  118. {
  119.         uchar REG_data;
  120.         I2C_Start();                   //起始信號(hào)
  121.         I2C_SendByte(SlaveAddress);    //發(fā)送設(shè)備地址+寫信號(hào)
  122.         I2C_SendByte(REG_Address);     //發(fā)送存儲(chǔ)單元地址,從0開(kāi)始        
  123.         I2C_Start();                   //起始信號(hào)
  124.         I2C_SendByte(SlaveAddress+1);  //發(fā)送設(shè)備地址+讀信號(hào)
  125.         REG_data=I2C_RecvByte();       //讀出寄存器數(shù)據(jù)
  126.         I2C_SendACK(1);                //接收應(yīng)答信號(hào)
  127.         I2C_Stop();                    //停止信號(hào)
  128.         return REG_data;
  129. }
  130. //**************************************
  131. //初始化MPU6050
  132. //**************************************
  133. void InitMPU6050()
  134. {
  135.         Single_WriteI2C(PWR_MGMT_1, 0x00);        //解除休眠狀態(tài)
  136.         Single_WriteI2C(SMPLRT_DIV, 0x07);
  137.         Single_WriteI2C(CONFIG, 0x06);
  138.         Single_WriteI2C(GYRO_CONFIG, 0x18);
  139.         Single_WriteI2C(ACCEL_CONFIG, 0x01);
  140. }
  141. //**************************************
  142. //合成數(shù)據(jù)
  143. //**************************************
  144. int GetData(uchar REG_Address)
  145. {
  146.         char H,L;
  147.         H=Single_ReadI2C(REG_Address);
  148.         L=Single_ReadI2C(REG_Address+1);
  149.         return (H<<8)+L;   //合成數(shù)據(jù)
  150. }
復(fù)制代碼




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表