我反反复复试了好多遍用MSP430G2553都没有正确读出MPU6050的数据,代码如下
模拟IIC程序
/*
* IIC.c
*
* Created on: 2017-4-1
* Author: admin
*/
#include"IIC.h"
#include"MSP430G2553.h"
#include"MPU.h"
unsigned char Write_Buf,Write_Addr,Read_Buf,Read_Addr;
//延时函数
void IIC_Delay(void)
{
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
}
//启动IIC
void Start_IIC(void)
{
P1OUT &=0x3f; //设置P1OUT
P1DIR &=0x7f; //SDA=1
IIC_Delay();
P1DIR &=0xbf; //SCL=1
IIC_Delay();
P1DIR |=0x80; //SDA=0
IIC_Delay();
P1DIR |=0x40; //SCL=0
IIC_Delay();
}
//停止IIC
void Stop_IIC(void)
{
P1DIR |=0x80; //SDA=0
IIC_Delay();
P1DIR &=0xbf; //SCL=1
IIC_Delay();
P1DIR &=0x7f; //SDA=1
IIC_Delay();
P1DIR |=0x80; //SDA=0
IIC_Delay();
P1DIR |=0x40; //SCL=0
}
//发送0
void Send_Zero(void)
{
P1DIR |=0x80; //SDA=0
IIC_Delay();
P1DIR &=0xbf; //SCL=1
IIC_Delay();
P1DIR |=0x40; //SCL=0
IIC_Delay();
}
//发送1
void Send_One(void)
{
P1DIR &=0x7f; //SDA=1
IIC_Delay();
P1DIR &=0xbf; //SCL=1
IIC_Delay();
P1DIR |=0x40; //SCL=0
IIC_Delay();
P1DIR |=0x80; //SDA=0
IIC_Delay();
}
//发送一个字节数据
void Send_Char(unsigned char Ctrl_Buf)
{
unsigned char cnt,tmp=0x80;
for(cnt=0;cnt<8;cnt++)
{
if((Ctrl_Buf & tmp)>0)
{
Send_One();
}
else
{
Send_Zero();
}
tmp /=2; //右移一位
}
}
//读一个字节
unsigned char Read_Char(void)
{
unsigned char cnt,tmp=0x80;
unsigned char Read_Buf=0x00;
for(cnt=0;cnt<8;cnt++)
{
P1DIR &=0x7f;
IIC_Delay();
P1DIR &=0xbf;
IIC_Delay();
if((P1IN & 0x80)>0x00)
{
Read_Buf |=tmp;
}
P1DIR |=0x40;
IIC_Delay();
tmp=tmp/2;
}
return(Read_Buf);
}
//应答信号
unsigned char IIC_Ack(void)
{
unsigned char Ack_Flag=0x00;
P1DIR &=0x7f; //SDA=1
IIC_Delay();
P1DIR &=0xbf; //SCL=0
IIC_Delay();
if((P1IN & 0x80)==0x80)
{
Ack_Flag=0x01;
}
P1DIR |=0x40; //SCL=0
IIC_Delay();
return (Ack_Flag);
}
//应答信号响应
void IIC_Nack(void)
{
P1DIR &=0x7f; //SDA=1
IIC_Delay();
P1DIR &=0xbf; //SCL=0
IIC_Delay();
P1DIR |=0x40; //SCL=0
IIC_Delay();
P1DIR |=0x80; //SDA=0
IIC_Delay();
}
//写一个数据
void Write_Byte(unsigned char data,unsigned char addr)
{
unsigned char Step_Flag=0x00;
while(Step_Flag <0x03)
{
if(Step_Flag==0x00)
{
Start_IIC();
Send_Char(SlaveAddress); // ****************************
if(IIC_Ack() ==0) Step_Flag +=1;
}
else if(IIC_Ack() ==1)
{
Send_Char(addr);
if(IIC_Ack()==0)
{
Step_Flag +=1;
}else {Step_Flag=0;}
}
else
{
Send_Char(data);
if(IIC_Ack()==0)
{
Step_Flag +=1;
}else {Step_Flag=0; }
}
}
Stop_IIC();
}
//读数据
unsigned char Read_Byte(unsigned char addr)
{
unsigned char Step_Flag=0;
unsigned char data=0;
while(Step_Flag<0x03)
{
if(Step_Flag==0x00)
{
Start_IIC();
Send_Char(SlaveAddress); //********************************
if(IIC_Ack()==0)
Step_Flag +=1;
}
else if(Step_Flag==1)
{
Send_Char(addr);
if(IIC_Ack()==0)
{
Step_Flag +=1;
}else { Step_Flag =0;}
}
else
{
Start_IIC();
Send_Char(SlaveAddress+1); //*****************
if(IIC_Ack()==0)
{
Step_Flag +=1;
Read_Char();
IIC_Nack();
}else
{
Step_Flag=0;
}
}
}
Stop_IIC();
return (data);
}
MPU6050程序
/*
* MPU.c
*
* Created on: 2017-3-23
* Author: admin
*/
#include"MPU.h"
#include"IIC.H"
void MPU6050Init()
{
Write_Byte(PWR_MGMT_1, 0x00); //唤醒
__delay_cycles(20000);
Write_Byte(SMPLRT_DIV, 0x07);
IIC_Delay();
Write_Byte(CONFIG, 0x06);
IIC_Delay();
Write_Byte(GYRO_CONFIG, 0x00);
IIC_Delay();
Write_Byte(ACCEL_CONFIG, 0x00);
IIC_Delay();
}
int GetData(unsigned char REG_Address)
{
char H,L;
H=Read_Byte(REG_Address);
L=Read_Byte(REG_Address+1);
return (H<<8)+L; //合成数据
}
// X/Y/Z-Axis Accelera
tion
int GetAccelX ()
{
return GetData(WHO_AM_I);
}
int GetAccelY ()
{
return GetData(ACCEL_YOUT_H);
}
int GetAccelZ ()
{
return GetData(ACCEL_ZOUT_H);
}
// X/Y/Z-Axis Angular velocity
int GetAnguX ()
{
return GetData(GYRO_XOUT_H);
}
int GetAnguY ()
{
return GetData(GYRO_YOUT_H);
}
int GetAnguZ ()
{
return GetData(GYRO_ZOUT_H);
}
主函数 程序
#include"MSP430G2553.h"
#include"IIC.h"
#include"MPU.h"
int ax,ay,az,gx,gy,gz;
void main()
{
WDTCTL = WDTPW+WDTHOLD;
if(CALBC1_1MHZ==0xFF||CALDCO_1MHZ==0xFF) while(1);//校准数据是否被擦除,若是则CPU挂起。
BCSCTL1=CALBC1_1MHZ;
DCOCTL=CALDCO_1MHZ;
MPU6050Init();
while(1)
{
ax=GetAccelX ();
ay=GetAccelY ();
az=GetAccelZ ();
gx=GetAnguX ();
gy=GetAnguY ();
gz=GetAnguZ ();
IIC_Delay();
}
}
请求大师指点!!!!!
一周热门 更多>