中级会员
- 积分
- 314
- 金钱
- 314
- 注册时间
- 2015-11-20
- 在线时间
- 76 小时
|
3金钱
最近想用MSP430G2553做平衡小车,现在被MPU6050严重阻碍,我在使用模拟IIC读取MPU6050数据时,无法获得正确数据,程序如下,请大师指点迷津:
/*
* IIC.c
*
* Created on: 2017-4-1
* Author: admin
*/
MSP430G2553模拟IIC程序
#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 Acceleration
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);
}
请大师看看
|
|