我已经帮楼主在我们这边的板子上做了测试,ADXL345+ADuC7026,测试+/-16G,串口使用9600。主要代码如下:
void
IRQ_Handler() __irq
{
if(0 != (IRQSTA & SPM5_IO_BIT)) //External
IRQ3 (IRQ3->INT2 pin of ADXL345)
{
Calculate_Sign = 1; //Set
flag
xl345Read(6, XL345_DATAX0, buf); //Read DATAX/Y/Z
registers
DataX_Low = buf[0];
DataX_High = buf[1];
DataX_High
&= 0x1F;
DataY_Low = buf[2];
DataY_High = buf[3];
DataY_High
&= 0x1F;
DataZ_Low = buf[4];
DataZ_High = buf[5];
DataZ_High
&= 0x1F;
}
else if(0 != (IRQSTA & UART_BIT)) //UART
Interrupt
{
UartInterrupt = COMIID0 ;
if(0x04 ==
UartInterrupt) //Has Received a Data
{
UartDataReceived =
COMRX; //Get Received Data
if(0xAA == UartDataReceived) //Test
Communication By Sending Device ID
{
while(!(0x020==(COMSTA0
& 0x020))){}
COMTX = Dev_ID; //COMTX is an 8-bit Transmit
Register.
}
if(0x55 == UartDataReceived) //Start
Command
{
IsStart = 0x01;
}
if(0xA5 ==
UartDataReceived) //Stop Command
{
IsStart =
0x00;
}
}
}
}
void ADuC7026_Initiate(void)
{
//Clock Initial
POWKEY1 = 0x01; //Start PLL Setting
POWCON =
0x00; //Set PLL Active Mode With CD = 0 CPU CLOCK DIVIDER = 41.78MHz
POWKEY2 = 0xF4; //Finish PLL Setting
GP1CON = 0x00002211; //PIN
set up for I2C and UART
GP2DAT = GP2DAT | 0x04040000; //Disable
LCD;
GP0DAT = GP0DAT | 0x02020000; //Disable LED;
GP4DAT = GP4DAT |
0x01000000; //Grouding the SDO, I2C address for writing and reading is 0xA6 and
0xA7
//UART Initial,Baud Rate = 9600
COMCON0 = 0x080;
COMDIV0 = 0x088;
COMDIV1 = 0x000;
COMCON0 = 0x007;
//I2C Initial
I2C1CFG = 0x00000082; // Master Enable &
Enable Generation of Master Clock
I2C1DIV = 0x3232; // 0x3232 =
400kHz 0xCFCF = 100kHz
//Interrupt Set Up
FIQEN =
SM_MASTER1_BIT; //I2C1 Master Interrupt
}
//ADXL345 initialization,
register configuration
void ADXL345_Initiate()
{
buf[0] =
XL345_RATE_50; //Output Data Rate: 50Hz
xl345Write(1, XL345_BW_RATE,
buf);
buf[0] = XL345_FULL_RESOLUTION | XL345_RANGE_16G;//Data Format: +/-16g
range, right justified, 256->1g
xl345Write(1, XL345_DATA_FORMAT,
buf);
buf[0] = XL345_DATAREADY; //INT_Enable: Data Ready
xl345Write(1,
XL345_INT_ENABLE, buf);
buf[0] = XL345_DATAREADY; //INT_Map: Data Ready
interrupt to the INT2 pin
xl345Write(1, XL345_INT_MAP, buf);
}
void
main(void)
{
ADuC7026_Initiate(); //ADuC7026
Initialization
ADXL345_Initiate(); //ADXL345 Initialization
IsStart
= 0x00; //Variables initialization
xl345Read(1,XL345_DEVID,buf);
//Get Device ID
Dev_ID = buf[0];
COMIEN0 = 0x01; //Enable
Receive Buffer Full Interrupt
IRQEN = UART_BIT | SPM5_IO_BIT; //Enable
UART and IRQ3 Interrupt (IRQ3->INT2 pin of ADXL345)
buf[0] =
XL345_MEASURE; //Power CTL: Measure mode
xl345Write(1, XL345_POWER_CTL,
buf);
while(1)
{
if(0x01 == IsStart) //
Start
{
if(1 == Calculate_Sign)
{
Calculate_Sign =
0;
Acceleration[X_CHANNEL] =
DataX_High;
Acceleration[X_CHANNEL] = (Acceleration[X_CHANNEL]<<8)
| DataX_Low;
Acceleration[Y_CHANNEL] =
DataY_High;
Acceleration[Y_CHANNEL] = (Acceleration[Y_CHANNEL]<<8)
| DataY_Low;
Acceleration[Z_CHANNEL] =
DataZ_High;
Acceleration[Z_CHANNEL] = (Acceleration[Z_CHANNEL]<<8)
| DataZ_Low;
//Translate the twos complement to true binary
code (256+4096)->1g, (4096-256)->(-1)g
for(iTemp=X_CHANNEL;iTemp<=Z_CHANNEL;iTemp++)
{
if(Acceleration[iTemp]
< 4096)
{
Acceleration[iTemp] = Acceleration[iTemp] +
4096;
}
else if(Acceleration[iTemp] >=
4096)
{
Acceleration[iTemp] = Acceleration[iTemp] -
4096;
}
un.ui =
Acceleration[iTemp];
putchar(un.uc[1]);//High
Bit
putchar(un.uc[0]);//Low
bit
}
}
}
}
}
得到的原始数据如下所示:
其中每两列为一轴的高低8位数据,结果与数据手册指标相符。以下三图分别为静止、沿X、Z轴晃动在PC端通过串口接收与显示的测试结果。
所以如果芯片确定是从正规渠道购买的,芯片本身是没有问题的。请再检查一下软件是否正确或SPI通信是否正常。
我已经帮楼主在我们这边的板子上做了测试,ADXL345+ADuC7026,测试+/-16G,串口使用9600。主要代码如下:
void
IRQ_Handler() __irq
{
if(0 != (IRQSTA & SPM5_IO_BIT)) //External
IRQ3 (IRQ3->INT2 pin of ADXL345)
{
Calculate_Sign = 1; //Set
flag
xl345Read(6, XL345_DATAX0, buf); //Read DATAX/Y/Z
registers
DataX_Low = buf[0];
DataX_High = buf[1];
DataX_High
&= 0x1F;
DataY_Low = buf[2];
DataY_High = buf[3];
DataY_High
&= 0x1F;
DataZ_Low = buf[4];
DataZ_High = buf[5];
DataZ_High
&= 0x1F;
}
else if(0 != (IRQSTA & UART_BIT)) //UART
Interrupt
{
UartInterrupt = COMIID0 ;
if(0x04 ==
UartInterrupt) //Has Received a Data
{
UartDataReceived =
COMRX; //Get Received Data
if(0xAA == UartDataReceived) //Test
Communication By Sending Device ID
{
while(!(0x020==(COMSTA0
& 0x020))){}
COMTX = Dev_ID; //COMTX is an 8-bit Transmit
Register.
}
if(0x55 == UartDataReceived) //Start
Command
{
IsStart = 0x01;
}
if(0xA5 ==
UartDataReceived) //Stop Command
{
IsStart =
0x00;
}
}
}
}
void ADuC7026_Initiate(void)
{
//Clock Initial
POWKEY1 = 0x01; //Start PLL Setting
POWCON =
0x00; //Set PLL Active Mode With CD = 0 CPU CLOCK DIVIDER = 41.78MHz
POWKEY2 = 0xF4; //Finish PLL Setting
GP1CON = 0x00002211; //PIN
set up for I2C and UART
GP2DAT = GP2DAT | 0x04040000; //Disable
LCD;
GP0DAT = GP0DAT | 0x02020000; //Disable LED;
GP4DAT = GP4DAT |
0x01000000; //Grouding the SDO, I2C address for writing and reading is 0xA6 and
0xA7
//UART Initial,Baud Rate = 9600
COMCON0 = 0x080;
COMDIV0 = 0x088;
COMDIV1 = 0x000;
COMCON0 = 0x007;
//I2C Initial
I2C1CFG = 0x00000082; // Master Enable &
Enable Generation of Master Clock
I2C1DIV = 0x3232; // 0x3232 =
400kHz 0xCFCF = 100kHz
//Interrupt Set Up
FIQEN =
SM_MASTER1_BIT; //I2C1 Master Interrupt
}
//ADXL345 initialization,
register configuration
void ADXL345_Initiate()
{
buf[0] =
XL345_RATE_50; //Output Data Rate: 50Hz
xl345Write(1, XL345_BW_RATE,
buf);
buf[0] = XL345_FULL_RESOLUTION | XL345_RANGE_16G;//Data Format: +/-16g
range, right justified, 256->1g
xl345Write(1, XL345_DATA_FORMAT,
buf);
buf[0] = XL345_DATAREADY; //INT_Enable: Data Ready
xl345Write(1,
XL345_INT_ENABLE, buf);
buf[0] = XL345_DATAREADY; //INT_Map: Data Ready
interrupt to the INT2 pin
xl345Write(1, XL345_INT_MAP, buf);
}
void
main(void)
{
ADuC7026_Initiate(); //ADuC7026
Initialization
ADXL345_Initiate(); //ADXL345 Initialization
IsStart
= 0x00; //Variables initialization
xl345Read(1,XL345_DEVID,buf);
//Get Device ID
Dev_ID = buf[0];
COMIEN0 = 0x01; //Enable
Receive Buffer Full Interrupt
IRQEN = UART_BIT | SPM5_IO_BIT; //Enable
UART and IRQ3 Interrupt (IRQ3->INT2 pin of ADXL345)
buf[0] =
XL345_MEASURE; //Power CTL: Measure mode
xl345Write(1, XL345_POWER_CTL,
buf);
while(1)
{
if(0x01 == IsStart) //
Start
{
if(1 == Calculate_Sign)
{
Calculate_Sign =
0;
Acceleration[X_CHANNEL] =
DataX_High;
Acceleration[X_CHANNEL] = (Acceleration[X_CHANNEL]<<8)
| DataX_Low;
Acceleration[Y_CHANNEL] =
DataY_High;
Acceleration[Y_CHANNEL] = (Acceleration[Y_CHANNEL]<<8)
| DataY_Low;
Acceleration[Z_CHANNEL] =
DataZ_High;
Acceleration[Z_CHANNEL] = (Acceleration[Z_CHANNEL]<<8)
| DataZ_Low;
//Translate the twos complement to true binary
code (256+4096)->1g, (4096-256)->(-1)g
for(iTemp=X_CHANNEL;iTemp<=Z_CHANNEL;iTemp++)
{
if(Acceleration[iTemp]
< 4096)
{
Acceleration[iTemp] = Acceleration[iTemp] +
4096;
}
else if(Acceleration[iTemp] >=
4096)
{
Acceleration[iTemp] = Acceleration[iTemp] -
4096;
}
un.ui =
Acceleration[iTemp];
putchar(un.uc[1]);//High
Bit
putchar(un.uc[0]);//Low
bit
}
}
}
}
}
得到的原始数据如下所示:
其中每两列为一轴的高低8位数据,结果与数据手册指标相符。以下三图分别为静止、沿X、Z轴晃动在PC端通过串口接收与显示的测试结果。
所以如果芯片确定是从正规渠道购买的,芯片本身是没有问题的。请再检查一下软件是否正确或SPI通信是否正常。
举报