通过MPU6050测温来进行I2C的验证学习
关于MPU6050寄存器相关可以参考https://blog.csdn.net/he__yuan/article/details/76559569
I2C时序很多,我是直接以原子I2C的程序结合MPU6050来进行测温实验
# include "MPU6050.H"
# include "delay.h"
# include "usart.h"
其中MPU6050.H文件也只是简单的定义了一些我用到的地址
#ifndef __MPU6050_H
#define __MPU6050_H
#include "I2C.H"
# define Slave_Addr 0x68
# define Power_Register_Addr 0x6B
# define Write_Bit 0
# define Read_Bit 1
# define Power_Register_Addr_ResetNum 0x80
函数的声明我没粘贴进去
之后就是整个代码
# include "MPU6050.H"
# include "delay.h"
# include "usart.h"
void MPU6050_Init(void)
{
int temp = 0;
IIC_Init();
MPU6050_WriteOneByte(Power_Register_Addr,Power_Register_Addr_Reset);
delay_ms(100);
MPU6050_WriteOneByte(Power_Register_Addr,0x00);
}
u8 MPU6050_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte((Slave_Addr<<1) | Write_Bit);
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((Slave_Addr<<1) | Read_Bit);
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();
return temp;
}
void MPU6050_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
IIC_Send_Byte((Slave_Addr<<1) | Write_Bit);
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256);
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(10);
}
void MPU6050_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
{
u8 t;
for(t=0;t<Len;t++)
{
MPU6050_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
}
}
u32 MPU6050_ReadLenByte(u16 ReadAddr,u8 Len)
{
u8 t;
u32 temp=0;
for(t=0;t<Len;t++)
{
temp<<=8;
temp+=MPU6050_ReadOneByte(ReadAddr+Len-t-1);
}
return temp;
}
void MPU6050_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
{
while(NumToRead)
{
*pBuffer++=MPU6050_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
void MPU6050_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
while(NumToWrite--)
{
MPU6050_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}
void IIC_Delay(void)
{
delay_us(2);
}
u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
IIC_Start();
IIC_Send_Byte((Slave_Addr<<1) | Write_Bit);
if(IIC_Wait_Ack())
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((Slave_Addr<<1) | Read_Bit);
IIC_Wait_Ack();
while(len)
{
if(len==1)*buf=IIC_Read_Byte(0);
else *buf=IIC_Read_Byte(1);
len--;
buf++;
}
IIC_Stop();
return 0;
}
short MPU_Get_Temperature(void)
{
u8 buf[2];
short raw;
float temp;
MPU_Read_Len(Slave_Addr,0x41,2,buf);
raw=((u16)buf[0]<<8)|buf[1];
temp=36.53+((double)raw)/340;
return temp*100;
}
其中MPU6050初始化时,一定要唤醒MPU6050.温度处理函数是网上的,不知道temp=36.53+((double)raw)/340具体是·什么含义,温度处理函数只是将温度寄存器0x41,0x42读出的值合成一个变量,我尝试用原子编写的读数据函数来进行更改,但数据有问题。再此记录一下,学学之后再来解决这个问题,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)