-
.
/*****************
**************************************************
***********
*
//
陀螺仪
MPU6050
IIC
测试程序
*********
**************************************************
********************
/
#include
#include
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
#define uchar unsigned
char
#define uint unsigned int
//
定
义
MPU6050
内部地址
#define SMPLRT_DIV
0x19
//
陀螺仪采样率,典型值:
0x07(125Hz)
#define CONFIG
0x1A
//
低通滤波频率,典
型值:
0x06(5Hz)
#define
GYRO_CONFIG
0x1B
//
< br>陀螺仪自检及测量范围,典型值:
0x18(
不自检,<
/p>
2000deg/s)
#define
ACCEL_CONFIG
0x1C
//
< br>加速计自检、测量范围及高通滤波频率,典型值:
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
//
电源管理,典型值:<
/p>
0x00(
正常启用
)
//#define
WHO_AM_I
0x75
//IIC
地址寄存器
(
默认数值
0x
68
,只读
)
.
.
//****************************
#define MPU6050_Addr 0xD0
//
定义器件在
IIC
总线中的从地址
,
根据
ALT
ADDRESS
地
址引脚不同修改
unsigned char TX_DATA[4];
//
显示据缓存区
unsigned
char BUF[10];
//
接收数据缓存区
char
test=0;
//IIC
用到
short T_X,T_Y,T_Z,T_T;
//X,Y,Z
轴,温度
//
模拟
IIC
端口输出输入定义
#define SCL_H GPIOB->BSRR =
GPIO_Pin_6
#define SCL_L
GPIOB->BRR = GPIO_Pin_6
#define SDA_H GPIOB->BSRR =
GPIO_Pin_7
#define SDA_L
GPIOB->BRR = GPIO_Pin_7
#define SCL_read GPIOB->IDR &
GPIO_Pin_6
#define SDA_read
GPIOB->IDR & GPIO_Pin_7
/*
函数申明
-----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART1_Configuration(void);
void WWDG_Configuration(void);
void Delay(u32 nTime);
void
Delayms(vu32 m);
/*
变量定义
----------------------------------------------*/
/*******************************/
void
DATA_printf(uchar *s,short temp_data)
{
if(temp_data<0){
temp_data=-temp_data;
*s='-';
}
else *s=' ';
*++s =temp_data/100+0x30;
temp_data=temp_data%100;
//
取余运算
*++s
=temp_data/10+0x30;
temp_data=temp_data%10;
//
取余运算
.
.
*++s =temp_data+0x30;
}
/*
**************************************************
***************************
*
* Function Name : I2C_GPIO_Config
* Description : Configration
Simulation IIC GPIO
* Input :
None
* Output : None
* Return : None
****
**************************************************
************************
*/
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO__Pin = GPIO_Pin_6;
GPIO__Speed = GPIO_Speed_50MHz;
GPIO__Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB,
&GPIO_InitStructure);
GPIO__Pin = GPIO_Pin_7;
GPIO__Speed
= GPIO_Speed_50MHz;
GPIO__Mode =
GPIO_Mode_Out_OD;
GPIO_Init(GPIOB,
&GPIO_InitStructure);
}
/*****************************************
*************************************
*
* Function Name : I2C_delay
* Description : Simulation IIC
Timing series delay
* Input :
None
* Output : None
* Return : None
****
**************************************************
************************
*/
void I2C_delay(void)
{
u8 i=30; //
这里可以优化速度
< br>,经测试最低到
5
还能写入
while(i)
{
i--;
}
.
.
}
void delay5ms(void)
{
int i=5000;
while(i)
{
i--;
}
}
/*************************
**************************************************
***
*
* Function Name :
I2C_Start
* Description : Master
Start Simulation IIC Communication
*
Input : None
* Output
: None
* Return : Wheather
Start
*********************************
*********************************************
*/
bool I2C_Start(void)
{
SDA_H;
SCL_H;
I2C_delay();
if(!SDA_read)return FALSE;
/
/SDA
线为低电平则总线忙
,
退出<
/p>
SDA_L;
I2C_delay();
if(SDA_read) return FALSE;
/
/SDA
线为高电平则总线出错
,
退出
SDA_L;
I2C_delay();
return TRUE;
}
/*
**************************************************
***************************
*
* Function Name : I2C_Stop
* Description : Master Stop
Simulation IIC Communication
* Input
: None
* Output : None
* Return : None
****
**************************************************
************************
*/
void I2C_Stop(void)
{
.
.
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}
/
**************************************************
****************************
*
* Function Name : I2C_Ack
*
Description : Master Send Acknowledge Single
* Input : None
*
Output : None
* Return
: None
********************************
**********************************************
*/
void I2C_Ack(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/************************************************
******************************
*
* Function Name : I2C_NoAck
* Description : Master Send No
Acknowledge Single
* Input :
None
* Output : None
* Return : None
****
**************************************************
************************
*/
void I2C_NoAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
.
.
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/
**************************************************
****************************
*
* Function Name : I2C_WaitAck
* Description : Master Reserive
Slave Acknowledge Single
* Input
: None
* Output : None
* Return : Wheather Reserive
Slave Acknowledge Single
**************
**************************************************
**************
*/
bool
I2C_WaitAck(void) //
返回为
:=1
有
ACK,=0
无
ACK
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
I2C_delay();
return FALSE;
}
SCL_L;
I2C_delay();
return TRUE;
}
/*
**************************************************
***************************
*
* Function Name : I2C_SendByte
* Description : Master Send a Byte
to Slave
* Input : Will Send
Date
* Output : None
* Return : None
****
**************************************************
************************
*/
void I2C_SendByte(u8 SendByte)
//
数据从高位到低位
//
{
u8 i=8;
.
.
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80)
SDA_H;
else
SDA_L;
SendByte<<=1;
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}
/*
**************************************************
***************************
*
* Function Name : I2C_RadeByte
* Description : Master Reserive a
Byte From Slave
* Input : None
* Output : None
*
Return : Date From Slave
*****
**************************************************
***********************
*/
unsigned char I2C_RadeByte(void)
//
数据从高位到低位
//
{
u8 i=8;
u8
ReceiveByte=0;
SDA_H;
while(i--)
{
ReceiveByte<<=1;
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte|=0x01;
}
}
SCL_L;
return ReceiveByte;
.
.
}
//ZRX
//
单字节写入
**********
*********************************
bool Single_Write(unsigned char
SlaveAddress,unsigned char REG_Address,unsigned
char REG_data)
//void
{
if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress);
//
发<
/p>
送
设
备
地
址
+
写
信
号
//I2C_SendByte(((REG_Address &
0x0700) >>7) | SlaveAddress & 0xFFFE);//
设置高起
始地址
+
器件地址
if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
I2C_SendByte(REG_Address );
//
设置低起始地址
I2C_WaitAck();
I2C_SendByte(REG_data);
I2C_WaitAck();
I2C_Stop();
delay5ms();
return
TRUE;
}
//
单字节读取
*****************************
************
unsigned char
Single_Read(unsigned char SlaveAddress,unsigned
char REG_Address)
{ unsigned char
REG_data;
if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress);
//I2C_SendByte(((REG_Address
&
0x0700)
>>7)
|
REG_Address & 0xFFFE);//
p>
设置高起始地址
+
器件地址
if(!I2C_WaitAck()){I2C_Stop();test=1; return
FALSE;}
I2C_SendByte((u8)
REG_Address); //
设置低起始地址
I2C_WaitAck();
I2C_Start();
I2C_SendByte(SlaveAddress+1);
I2C_WaitAck();
REG_data= I2C_RadeByte();
I2C_NoAck();
I2C_Stop();
//return TRUE;
return REG_data;
}
/*
*************
**************************************************
****************
.