本帖最后由 allen87171 于 2013-8-5 17:33 编辑
debug模式會卡在 temp=GetData(TEMP_OUT_H); 這裡
我已經卡好久了 請大家幫幫忙
#include <p18f67k22.h>
#include "Delay.h"
#include "MPU6050.h"
#include "LCM.h"
#include "i2c.h"
void main(void)
{
int temp=0;
TRISDbits.TRISD0=0;
LATDbits.LATD0=1;
DelayMs(5);
Init_LCM();
DelayMs(5);
I2C_Init();
DelayMs(5);
while(1)
{
SetCursor(0,2);
InitMPU6050();
temp=GetData(TEMP_OUT_H);
Dsp4Hex(temp);//4位 十六值
}
}
}
///////////////////////////////////////
void Initi2c()
{
unsigned char sync_mode;
unsigned char slew;
sync_mode = MASTER;
slew = SLEW_ON;
//20MHz,?生400k IIC?率,SSPADDR = Fosc/4/baud-1=20M/4/400k-1=11
SSPADD = 0x09; //400khz
OpenI2C( sync_mode, slew );
}
//**************************************
//向MPU6050???入一?字??据
//**************************************
void Single_WriteI2C(unsigned char REG_Address,unsigned char REG_data)
{
IdleI2C(); // initiate START condition
StartI2C();
WriteI2C(Write_MPU6050); //?送??地址+?信?
WriteI2C(REG_Address); //?部寄存器地址,
WriteI2C(REG_data); //?部寄存器?据,
StopI2C(); // send STOP condition
}
//**************************************
//?MPU6050???取一?字??据
//**************************************
unsigned char Single_ReadI2C(unsigned char REG_Address)
{
unsigned char REG_data;
IdleI2C();
StartI2C(); //send start condition
WriteI2C(Write_MPU6050); //?送??地址+?信?
WriteI2C(REG_Address); //?送存??元地址,?0?始
IdleI2C();
StartI2C(); //起始信?
WriteI2C(Read_MPU6050); //?送??地址+?信?
REG_data = ReadI2C(); //read in single byte
NotAckI2C(); // send ACK condition
StopI2C(); //send stop condition
return REG_data;
}
void InitMPU6050()
{
Single_WriteI2C(PWR_MGMT_1, 0x00); //解除休眠??
Single_WriteI2C(SMPLRT_DIV, 0x07); //陀螺?采?率,典型值:0x07(125Hz)
Single_WriteI2C(CONFIG, 0x06); //低通?波?率,典型值:0x06(5Hz)
Single_WriteI2C(GYRO_CONFIG, 0x18); //陀螺?自?及?量范?,典型值:0x18(不自?,2000deg/s) 0x00 250
Single_WriteI2C(ACCEL_CONFIG, 0x01); //加速?自?、?量范?及高通?波?率,典型值:0x01(不自?,2G,5Hz) ±4g 8192LSB/g
}
int GetData(unsigned char REG_Address)
{
char H,L;
H=Single_ReadI2C(REG_Address);
L=Single_ReadI2C(REG_Address+1);
return (H<<8)+L; //合成?据
}
|