#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
Uint16 *ExRamStart = (Uint16 *)0x100000;
#define DELAY_US(A) DSP28x_usDelay(((((long double) A * 1000.0L) / (long double)CPU_RATE) - 9.0L) / 5.0L)
void Ini
ti2C(void);
Uint16 ReadData(Uint16 *RamAddr, Uint16 RomAddress, Uint16 number);
Uint16 WriteData(Uint16 *Wdata, Uint16 RomAddress, Uint16 number);
void initmpu();
Uint16 I2C_xrdy();
Uint16 I2C_rrdy();
Uint16 i;
//*************************************************************
#define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)
#define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)
#define GYRO_CONFIG 0x1B //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
#define ACCEL_CONFIG 0x1C //加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B //
电源管理,典型值:0x00(正常启用)
#define WHO_AM_I 0x75 //IIC地址寄存器(默认数值0x68,只读)
#define SlaveAddress 0xD0 //IIC写入时的地址字节数据,+1为读取
//*************************************************************************************
Uint16 a=0x00,b=0x07,c=0x06,d=0x18,e=0x01,f=0x80;
Uint16 dat1[8]={0,0,0,0,0,0,0,0};
//Uint16 databuff[];
void main(void)
{
// Uint16 dat[]={ 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
InitXintf16Gpio(); //zq
InitI2CGpio();
InitI2C();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
LedReg = 0xFF;
DELAY_US(1500);
initmpu();
// WriteData(dat,0x60,8);
DELAY_US(10000);
// ReadData(dat1,0x60,8);
for(; ;)
{
ReadData(dat1,ACCEL_ZOUT_L,2);
}
}
void initmpu()
{
DELAY_US(1500);
WriteData(&e,PWR_MGMT_1,2);
DELAY_US(1500);
WriteData(&a,PWR_MGMT_1,2);
WriteData(&a,PWR_MGMT_1,2);
WriteData(&b,SMPLRT_DIV,2);
WriteData(&c,CONFIG,2);
WriteData(&d,GYRO_CONFIG,2);
}
Uint16 WriteData(Uint16 *Wdata, Uint16 RomAddress, Uint16 number)
{
Uint16 i;
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
while(!I2C_xrdy());
I2caRegs.I2CSAR = SlaveAddress;
I2caRegs.I2CCNT = number + 1;
I2caRegs.I2CDXR = RomAddress;
I2caRegs.I2CMDR.all = 0x6E20;
for (i=0; i<number; i++)
{
while(!I2C_xrdy());
I2caRegs.I2CDXR = *Wdata;
// Wdata++;
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
}
return I2C_SUCCESS;
}
Uint16 ReadData(Uint16 *RamAddr, Uint16 RomAddress, Uint16 number)
{
Uint16 i,Temp;
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
while(!I2C_xrdy());
I2caRegs.I2CSAR = SlaveAddress;
I2caRegs.I2CCNT = 1;
I2caRegs.I2CDXR = RomAddress;
I2caRegs.I2CMDR.all = 0x2620;
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
DELAY_US(500);
while(!I2C_xrdy());
I2caRegs.I2CSAR = SlaveAddress;
I2caRegs.I2CCNT = number;
I2caRegs.I2CMDR.all = 0x2C20;
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
for(i=0;i<number;i++)
{
while(!I2C_rrdy());
Temp = I2caRegs.I2CDRR;
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
*RamAddr = Temp;
RamAddr++;
}
i=0;
return I2C_SUCCESS;
}
Uint16 I2C_xrdy()
{
Uint16 t;
t = I2caRegs.I2CSTR.bit.XRDY;
return t;
}
Uint16 I2C_rrdy()
{
Uint16 t;
t = I2caRegs.I2CSTR.bit.RRDY;
return t;
}
//===========================================================================
// No more.
//===========================================================================
程序如上,为什么我收到的数据 一直是255,求解答
一周热门 更多>