mpu6050详细C语言程序.docx

上传人:b****3 文档编号:5877835 上传时间:2023-05-09 格式:DOCX 页数:24 大小:19.81KB
下载 相关 举报
mpu6050详细C语言程序.docx_第1页
第1页 / 共24页
mpu6050详细C语言程序.docx_第2页
第2页 / 共24页
mpu6050详细C语言程序.docx_第3页
第3页 / 共24页
mpu6050详细C语言程序.docx_第4页
第4页 / 共24页
mpu6050详细C语言程序.docx_第5页
第5页 / 共24页
mpu6050详细C语言程序.docx_第6页
第6页 / 共24页
mpu6050详细C语言程序.docx_第7页
第7页 / 共24页
mpu6050详细C语言程序.docx_第8页
第8页 / 共24页
mpu6050详细C语言程序.docx_第9页
第9页 / 共24页
mpu6050详细C语言程序.docx_第10页
第10页 / 共24页
mpu6050详细C语言程序.docx_第11页
第11页 / 共24页
mpu6050详细C语言程序.docx_第12页
第12页 / 共24页
mpu6050详细C语言程序.docx_第13页
第13页 / 共24页
mpu6050详细C语言程序.docx_第14页
第14页 / 共24页
mpu6050详细C语言程序.docx_第15页
第15页 / 共24页
mpu6050详细C语言程序.docx_第16页
第16页 / 共24页
mpu6050详细C语言程序.docx_第17页
第17页 / 共24页
mpu6050详细C语言程序.docx_第18页
第18页 / 共24页
mpu6050详细C语言程序.docx_第19页
第19页 / 共24页
mpu6050详细C语言程序.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

mpu6050详细C语言程序.docx

《mpu6050详细C语言程序.docx》由会员分享,可在线阅读,更多相关《mpu6050详细C语言程序.docx(24页珍藏版)》请在冰点文库上搜索。

mpu6050详细C语言程序.docx

mpu6050详细C语言程序

/*******************************************************************************

//陀螺仪MPU6050IIC测试程序

*******************************************************************************/

#include"stm32f10x_lib.h"

#include//Keillibrary

GPIO_InitTypeDefGPIO_InitStructure;

ErrorStatusHSEStartUpStatus;

#defineucharunsignedchar

#defineuintunsignedint

//定义MPU6050内部地址

#defineSMPLRT_DIV0x19//陀螺仪采样率,典型值:

0x07(125Hz)

#defineCONFIG0x1A//低通滤波频率,典型值:

0x06(5Hz)

#defineGYRO_CONFIG0x1B//陀螺仪自检及测量范围,典型值:

0x18(不自检,2000deg/s)

#defineACCEL_CONFIG0x1C//加速计自检、测量范围及高通滤波频率,典型值:

0x01(不自检,2G,5Hz)

#defineACCEL_XOUT_H0x3B

#defineACCEL_XOUT_L0x3C

#defineACCEL_YOUT_H0x3D

#defineACCEL_YOUT_L0x3E

#defineACCEL_ZOUT_H0x3F

#defineACCEL_ZOUT_L0x40

//#defineTEMP_OUT_H0x41

//#defineTEMP_OUT_L0x42

//

#defineGYRO_XOUT_H0x43

#defineGYRO_XOUT_L0x44

#defineGYRO_YOUT_H0x45

#defineGYRO_YOUT_L0x46

#defineGYRO_ZOUT_H0x47

#defineGYRO_ZOUT_L0x48

#definePWR_MGMT_10x6B//电源管理,典型值:

0x00(正常启用)

//#defineWHO_AM_I0x75//IIC地址寄存器(默认数值0x68,只读)

//****************************

#defineMPU6050_Addr0xD0//定义器件在IIC总线中的从地址,根据ALTADDRESS地址引脚不同修改

unsignedcharTX_DATA[4];//显示据缓存区

unsignedcharBUF[10];//接收数据缓存区

chartest=0;//IIC用到

shortT_X,T_Y,T_Z,T_T;//X,Y,Z轴,温度

//模拟IIC端口输出输入定义

#defineSCL_HGPIOB->BSRR=GPIO_Pin_6

#defineSCL_LGPIOB->BRR=GPIO_Pin_6

#defineSDA_HGPIOB->BSRR=GPIO_Pin_7

#defineSDA_LGPIOB->BRR=GPIO_Pin_7

#defineSCL_readGPIOB->IDR&GPIO_Pin_6

#defineSDA_readGPIOB->IDR&GPIO_Pin_7

/*函数申明-----------------------------------------------*/

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidNVIC_Configuration(void);

voidUSART1_Configuration(void);

voidWWDG_Configuration(void);

voidDelay(u32nTime);

voidDelayms(vu32m);

/*变量定义----------------------------------------------*/

/*******************************/

voidDATA_printf(uchar*s,shorttemp_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;

}

/*******************************************************************************

*FunctionName:

I2C_GPIO_Config

*Description:

ConfigrationSimulationIICGPIO

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_GPIO_Config(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;

GPIO_Init(GPIOB,&GPIO_InitStructure);

}

/*******************************************************************************

*FunctionName:

I2C_delay

*Description:

SimulationIICTimingseriesdelay

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_delay(void)

{

u8i=30;//这里可以优化速度,经测试最低到5还能写入

while(i)

{

i--;

}

}

voiddelay5ms(void)

{

inti=5000;

while(i)

{

i--;

}

}

/*******************************************************************************

*FunctionName:

I2C_Start

*Description:

MasterStartSimulationIICCommunication

*Input:

None

*Output:

None

*Return:

WheatherStart

*******************************************************************************/

boolI2C_Start(void)

{

SDA_H;

SCL_H;

I2C_delay();

if(!

SDA_read)returnFALSE;//SDA线为低电平则总线忙,退出

SDA_L;

I2C_delay();

if(SDA_read)returnFALSE;//SDA线为高电平则总线出错,退出

SDA_L;

I2C_delay();

returnTRUE;

}

/*******************************************************************************

*FunctionName:

I2C_Stop

*Description:

MasterStopSimulationIICCommunication

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_Stop(void)

{

SCL_L;

I2C_delay();

SDA_L;

I2C_delay();

SCL_H;

I2C_delay();

SDA_H;

I2C_delay();

}

/*******************************************************************************

*FunctionName:

I2C_Ack

*Description:

MasterSendAcknowledgeSingle

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_Ack(void)

{

SCL_L;

I2C_delay();

SDA_L;

I2C_delay();

SCL_H;

I2C_delay();

SCL_L;

I2C_delay();

}

/*******************************************************************************

*FunctionName:

I2C_NoAck

*Description:

MasterSendNoAcknowledgeSingle

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_NoAck(void)

{

SCL_L;

I2C_delay();

SDA_H;

I2C_delay();

SCL_H;

I2C_delay();

SCL_L;

I2C_delay();

}

/*******************************************************************************

*FunctionName:

I2C_WaitAck

*Description:

MasterReseriveSlaveAcknowledgeSingle

*Input:

None

*Output:

None

*Return:

WheatherReseriveSlaveAcknowledgeSingle

*******************************************************************************/

boolI2C_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();

returnFALSE;

}

SCL_L;

I2C_delay();

returnTRUE;

}

/*******************************************************************************

*FunctionName:

I2C_SendByte

*Description:

MasterSendaBytetoSlave

*Input:

WillSendDate

*Output:

None

*Return:

None

*******************************************************************************/

voidI2C_SendByte(u8SendByte)//数据从高位到低位//

{

u8i=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;

}

/*******************************************************************************

*FunctionName:

I2C_RadeByte

*Description:

MasterReseriveaByteFromSlave

*Input:

None

*Output:

None

*Return:

DateFromSlave

*******************************************************************************/

unsignedcharI2C_RadeByte(void)//数据从高位到低位//

{

u8i=8;

u8ReceiveByte=0;

SDA_H;

while(i--)

{

ReceiveByte<<=1;

SCL_L;

I2C_delay();

SCL_H;

I2C_delay();

if(SDA_read)

{

ReceiveByte|=0x01;

}

}

SCL_L;

returnReceiveByte;

}

//ZRX

//单字节写入*******************************************

boolSingle_Write(unsignedcharSlaveAddress,unsignedcharREG_Address,unsignedcharREG_data)//void

{

if(!

I2C_Start())returnFALSE;

I2C_SendByte(SlaveAddress);//发送设备地址+写信号//I2C_SendByte(((REG_Address&0x0700)>>7)|SlaveAddress&0xFFFE);//设置高起始地址+器件地址

if(!

I2C_WaitAck()){I2C_Stop();returnFALSE;}

I2C_SendByte(REG_Address);//设置低起始地址

I2C_WaitAck();

I2C_SendByte(REG_data);

I2C_WaitAck();

I2C_Stop();

delay5ms();

returnTRUE;

}

//单字节读取*****************************************

unsignedcharSingle_Read(unsignedcharSlaveAddress,unsignedcharREG_Address)

{unsignedcharREG_data;

if(!

I2C_Start())returnFALSE;

I2C_SendByte(SlaveAddress);//I2C_SendByte(((REG_Address&0x0700)>>7)|REG_Address&0xFFFE);//设置高起始地址+器件地址

if(!

I2C_WaitAck()){I2C_Stop();test=1;returnFALSE;}

I2C_SendByte((u8)REG_Address);//设置低起始地址

I2C_WaitAck();

I2C_Start();

I2C_SendByte(SlaveAddress+1);

I2C_WaitAck();

REG_data=I2C_RadeByte();

I2C_NoAck();

I2C_Stop();

//returnTRUE;

returnREG_data;

}

/*

********************************************************************************

**函数名称:

RCC_Configuration(void)

**函数功能:

时钟初始化

**输入:

**输出:

**返回:

********************************************************************************

*/

voidRCC_Configuration(void)

{

/*RCCsystemreset(fordebugpurpose)*/

RCC_DeInit();

/*EnableHSE*/

RCC_HSEConfig(RCC_HSE_ON);

/*WaittillHSEisready*/

HSEStartUpStatus=RCC_WaitForHSEStartUp();

if(HSEStartUpStatus==SUCCESS)

{

/*HCLK=SYSCLK*/

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/*PCLK2=HCLK*/

RCC_PCLK2Config(RCC_HCLK_Div1);

/*PCLK1=HCLK/2*/

RCC_PCLK1Config(RCC_HCLK_Div2);

/*Flash2waitstate*/

FLASH_SetLatency(FLASH_Latency_2);

/*EnablePrefetchBuffer*/

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/*PLLCLK=8MHz*9=72MHz*/

RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

/*EnablePLL*/

RCC_PLLCmd(ENABLE);

/*WaittillPLLisready*/

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)

{

}

/*SelectPLLassystemclocksource*/

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/*WaittillPLLisusedassystemclocksource*/

while(RCC_GetSYSCLKSource()!

=0x08)

{

}

}

/*EnableGPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOGandAFIOclocks*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_AFIO,ENABLE);

}

/*

********************************************************************************

**函数名称:

GPIO_Configuration(void)

**函数功能:

端口初始化

**输入:

**输出:

**返回:

********************************************************************************

*/

voidGPIO_Configuration(void)

{

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2