欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
MPU6050-51單片機(jī)參考程序
[打印本頁(yè)]
作者:
stm8s520
時(shí)間:
2019-7-17 21:36
標(biāo)題:
MPU6050-51單片機(jī)參考程序
//****************************************
// Update to MPU6050 by shinetop
// MCU: STC89C52
// 2012.3.1
// 功能: 顯示加速度計(jì)和陀螺儀的10位原始數(shù)據(jù)
//****************************************
// 使用單片機(jī)STC89C52
// 晶振:11.0592M
// 顯示:串口
// 編譯環(huán)境 Keil uVision2
//****************************************
#include <REG52.H>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#include <INTRINS.H>
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
//****************************************
// 定義51單片機(jī)端口
//****************************************
sbit SCL=P1^5; //IIC時(shí)鐘引腳定義
sbit SDA=P1^4; //IIC數(shù)據(jù)引腳定義
//****************************************
// 定義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寫(xiě)入時(shí)的地址字節(jié)數(shù)據(jù),+1為讀取
//**************************************************************************************************
//定義類型及變量
//**************************************************************************************************
uchar dis[6]; //顯示數(shù)字(-511至512)的字符數(shù)組
int dis_data; //變量
//**************************************************************************************************
//函數(shù)聲明
//**************************************************************************************************
void Delay5us();
void delay(unsigned int k); //延時(shí)
void lcd_printf(uchar *s,int temp_data);
//********************************MPU6050操作函數(shù)***************************************************
void InitMPU6050(); //初始化MPU6050
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寫(xiě)入數(shù)據(jù)
//********************************************************************************
//整數(shù)轉(zhuǎn)字符串
//********************************************************************************
void lcd_printf(uchar *s,int temp_data)
{
if(temp_data<0)
{
temp_data=-temp_data;
*s='-';
}
else *s=' ';
*++s =temp_data/10000+0x30;
temp_data=temp_data%10000; //取余運(yùn)算
*++s =temp_data/1000+0x30;
temp_data=temp_data%1000; //取余運(yùn)算
*++s =temp_data/100+0x30;
temp_data=temp_data%100; //取余運(yùn)算
*++s =temp_data/10+0x30;
temp_data=temp_data%10; //取余運(yùn)算
*++s =temp_data+0x30;
}
//******************************************************************************************************
//串口初始化
//*******************************************************************************************************
void init_uart()
{
TMOD=0x21;
TH1=0xfd; //實(shí)現(xiàn)波特率9600(系統(tǒng)時(shí)鐘11.0592MHZ)
TL1=0xfd;
SCON=0x50;
PS=1; //串口中斷設(shè)為高優(yōu)先級(jí)別
TR0=1; //啟動(dòng)定時(shí)器
TR1=1;
ET0=1; //打開(kāi)定時(shí)器0中斷
ES=1;
EA=1;
}
//*************************************************************************************************
//串口發(fā)送函數(shù)
//*************************************************************************************************
void SeriPushSend(uchar send_data)
{
SBUF=send_data;
while(!TI);TI=0;
}
//*************************************************************************************************
//************************************延時(shí)*********************************************************
//*************************************************************************************************
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;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; //寫(xiě)應(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è)備寫(xiě)入一個(gè)字節(jié)數(shù)據(jù)
//*****************************************************************************************************
void Single_WriteI2C(uchar REG_Address,uchar REG_data)
{
I2C_Start(); //起始信號(hào)
I2C_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(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è)備地址+寫(xiě)信號(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)
{
uchar H,L;
H=Single_ReadI2C(REG_Address);
L=Single_ReadI2C(REG_Address+1);
return ((H<<8)+L); //合成數(shù)據(jù)
}
//******************************************************************************************************
//超級(jí)終端(串口調(diào)試助手)上顯示10位數(shù)據(jù)
//******************************************************************************************************
void Display10BitData(int value)
{ uchar i;
// value/=64; //轉(zhuǎn)換為10位數(shù)據(jù)
lcd_printf(dis, value); //轉(zhuǎn)換數(shù)據(jù)顯示
for(i=0;i<6;i++)
{
SeriPushSend(dis[i]);
}
// DisplayListChar(x,y,dis,4); //啟始列,行,顯示數(shù)組,顯示長(zhǎng)度
}
//*******************************************************************************************************
//主程序
//*******************************************************************************************************
void main()
{
delay(500); //上電延時(shí)
init_uart();
InitMPU6050(); //初始化MPU6050
delay(150);
while(1)
{
Display10BitData(GetData(ACCEL_XOUT_H)); //顯示X軸加速度
Display10BitData(GetData(ACCEL_YOUT_H)); //顯示Y軸加速度
Display10BitData(GetData(ACCEL_ZOUT_H)); //顯示Z軸加速度
Display10BitData(GetData(GYRO_XOUT_H)); //顯示X軸角速度
Display10BitData(GetData(GYRO_YOUT_H)); //顯示Y軸角速度
Display10BitData(GetData(GYRO_ZOUT_H)); //顯示Z軸角速度
SeriPushSend(0x0d);
SeriPushSend(0x0a);//換行,回車
delay(2000);
}
}
復(fù)制代碼
MPU-6050-C51參考程序.rar
2019-7-17 21:36 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
31.96 KB, 下載次數(shù): 21, 下載積分: 黑幣 -5
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1