已回答假定已回答

关于ADXL345寄存器的值都为FF,求教原因。

YAY 在 2015-4-3 詢問的問題
最後回覆由neilzhao於2015-4-29提供

float Xa()//计算X轴数值

{

  int a,b;

  a= SPI_Read(0x31);

  b= SPI_Read(0x32);

  if(0xF0==(b&0xf0))

  {

    temp=b&0x0F;

    temp=(temp<<8)+a;

    temp=-temp;

  }

  else

  {

    temp=b;

    temp=(temp<<8)+a;

  }

  return temp * 0.0039;

}

不论怎么改变传感器的位置,a= SPI_Read(0x31);的值都是0x00FF, b= SPI_Read(0x32);的值总是0x32FF,其他的寄存器值也无法读出,硬件确定无错误,求解答。

 

全部程序如下:

 

#include <ioCC2530.h>

#include "ADXL345.h"

#define SDI P1_1

#define SDO P0_0

#define SCLK P0_7

#define CS P2_0

#define uchar unsigned char

#define uint unsigned int

int temp = 0;

float result;

 

 

void SPI_Wrt(uchar addr,uchar data);

int SPI_Read(uchar addr);

 

 

float Xa();

float Ya();

float Za();

void ADXL345_Init ()

{

//  P0SEL = 0x81; //通用IO P0_0 P0_7外设功能

  P0SEL = 0x00;

  P0DIR = 0xFF; //输出IO

  //P0INP = 0xff; //采用默认0x00

 

//  P1SEL = 0x02; //通用IO P1_1外设功能

  P1SEL = 0x00;

  P1DIR = 0xFD; //P1_1为输入

  //P1INP = 0xff; //采用默认0x00

 

 

//  P2SEL = 0x01; //通用IO P2_0外设功能

  P2SEL = 0x00;

  P2DIR = 0xFF; //输出

  //P2INP = 0xff; //采用默认0x00

//****************************ADXL345初始化设置****************************

  SPI_Wrt(0x31,0x2B); //数据通信格式;设置为自检功能禁用,4线制SPI接口,低电平中断输出,13位全分辨率,输出数据右对齐,16g量程

 

 

  SPI_Wrt(0x1E,0x00); //X轴误差补偿; (15.6mg/LSB)

  SPI_Wrt(0x1F,0x00); //Y轴误差补偿; (15.6mg/LSB)

  SPI_Wrt(0x20,0x00); //Z轴误差补偿; (15.6mg/LSB)

 

 

  SPI_Wrt(0x21,0x00); //敲击延时0:禁用; (1.25ms/LSB)

  SPI_Wrt(0x22,0x00); //检测第一次敲击后的延时0:禁用; (1.25ms/LSB)

  SPI_Wrt(0x23,0x00); //敲击窗口0:禁用; (1.25ms/LSB)

 

 

  SPI_Wrt(0x24,0x01); //保存检测活动阀值; (62.5mg/LSB)

  SPI_Wrt(0x25,0x01); //保存检测静止阀值; (62.5mg/LSB)

  SPI_Wrt(0x26,0x2B); //检测活动时间阀值; (1s/LSB)

  SPI_Wrt(0x27,0x00); //

  SPI_Wrt(0x28,0x09); //自由落体检测推荐阀值; (62.5mg/LSB)

  SPI_Wrt(0x29,0xFF); //自由落体检测时间阀值,设置为最大时间; (5ms/LSB)

  SPI_Wrt(0x2A,0x80); //

  //i=SPI_Read(0x2B); //只读寄存器,状态读取

 

 

  SPI_Wrt(0x2C,0x0A); //

  SPI_Wrt(0x2D,0x28); //开启Link,测量功能;关闭自动休眠,休眠,唤醒功能

  SPI_Wrt(0x2E,0x00); //所有均关闭

  SPI_Wrt(0x2F,0x00); //中断功能设定,不使用中断

  //SPI_Read(0x30); //只读寄存器,状态读取

  SPI_Wrt(0x38,0x9F); //FIFO模式设定,Stream模式,触发连接INT1,31级样本缓冲

  //SPI_Read(0x39); //只读寄存器,状态读取

//****************************ADXL345初始化设置****************************

}

 

 

void Delay(int i) // d=100----20us

{

  while(i)

  {

    i--;

  }

}

 

 

void SpiStart()

{

  CS=1;

  SCLK=1;

 

 

  CS=0;

  Delay(30);

}

 

 

void SpiStop()

{

  SCLK=1;

  Delay(30);

  CS=1;

  Delay(1000);

}

 

 

void SPI_WriteByte(uchar data) //地址格式:0xFF 数据格式:0xFF

{

  for(uchar i = 1;i <= 8;i++)

  {

    SCLK = 0;

    Delay(30);

    if(0x80 == (data & 0x80)) //判断0/1

    {

      SDI = 1;

    }

    else

    {

      SDI = 0;

    }

    Delay(30);

    data <<= 1;

    SCLK = 1;

    Delay(30);

  }

}

 

 

void SPI_Wrt(uchar addr,uchar data) //地址格式:0xFF 数据格式:0xFF

{

  SpiStart();

  SPI_WriteByte(addr);

  Delay(1000);

  SPI_WriteByte(data);

  SpiStop();

}

 

 

int SPI_Read(uchar addr) //地址格式:0xFF 数据格式:0xFF

{

  SpiStart();

  int result=0;

  SPI_WriteByte(addr);   // +0x80是什么意思?

  for(uchar i = 1;i <= 8;i++)

  {

    SCLK = 0;

    result <<= 1;

    Delay(30);

    result += SDO;

    Delay(30);

    SCLK = 1;

    Delay(30);

  }

  SpiStop();

  return result;

}

 

 

float Xa()//计算X轴数值

{

  int a,b;

  a= SPI_Read(0x31);

  b= SPI_Read(0x32);

  if(0xF0==(b&0xf0))

  {

    temp=b&0x0F;

    temp=(temp<<8)+a;

    temp=-temp;

  }

  else

  {

    temp=b;

    temp=(temp<<8)+a;

  }

  return temp * 0.0039;

}

 

 

float Ya()//计算Y轴数值

{

  int a,b;

  a= SPI_Read(0x34);

  b= SPI_Read(0x35);

  if(0xF0==(b&0xf0))

  {

    temp=b&0x0F;

    temp=(temp<<8)+a;

    temp=-temp;

  }

  else

  {

    temp=b;

    temp=(temp<<8)+a;

  }

  return temp * 0.0039;

}

 

 

float Za()//计算Z轴数值

{

  int a,b;

  a= SPI_Read(0x36);

  b= SPI_Read(0x37);

  if(0xF0==(b&0xf0))

  {

    temp=b&0x0F;

    temp=(temp<<8)+a;

    temp=-temp;

  }

  else

  {

    temp=b;

    temp=(temp<<8)+a;

  }

  return temp * 0.0039;

}

結果