ADXL345数据读出主程序
0赞
发表于 2/25/2012 9:02:46 AM
阅读(26703)
通过I2C方式对数据读出,硬件连接好之后,把调试好的程序给大家吧!这里有两个不同的程序,先给大家一个吧!
#include#define CR 0x0D void UART_Initiate() { GP1CON |= 0x011; // Setting up UART at 9600 (CD=0) COMCON0 = 0x80; // Setting DLAB COMDIV0 = 0x88; // COMDIV1 = 0x00; COMCON0 = 0x07; // Clearing DLAB } void sendfloatdata(float *data) { char buffer[4]; char *p; int i = 0; p = (char*)data; while(! (0x020 == (COMSTA0 & 0x020))) {} COMTX =0x0A; for (i = 0;i<4;i++) { buffer[i] = *p; p++; while(! (0x020 == (COMSTA0 & 0x020))) {} COMTX =buffer[i]; } } void senddata(int to_send) { while(!(0x020==(COMSTA0 & 0x020))){} COMTX = 0x0A; // output LF while(!(0x020==(COMSTA0 & 0x020))){} COMTX = 0x0D; // output CR while(!(0x020==(COMSTA0 & 0x020))){} COMTX = ((to_send >> 8) & 0xFF); while(!(0x020==(COMSTA0 & 0x020))){} COMTX = ((to_send) & 0x0FF); } void delay (int timer) { while(timer >= 0) timer--; } int putchar(int ch) { /* Write character to Serial Port */ if (ch == '\n') { while(!(0x020==(COMSTA0 & 0x020))) {} COMTX = CR; /* output CR */ } while(!(0x020==(COMSTA0 & 0x020))) {} return (COMTX = ch); }
/* Project: ADXL345 Demo Functions: Page rotation, shake detection and single / double tap detection Hardware platform: ADuC7026EVB and ADXL345EVB Development tool: Keil UV3 Designed by: Nicolle Jia, CAST team, ADI Version: V1.0 Data: 2008.5.29 */ #include "ADuC7026.h" #include "I2C_Master.h" #include#define CR 0x0D //Reigster of ADXL345 char DataX1, DataX2, DataY1, DataY2, DataZ1, DataZ2; //High byte and Low byte of Data register of X, Y, Z int DataX=0, DataY=0, DataZ=0; //Word of Data register of X, Y, Z float XINCL=0.0, YINCL=0.0, ZINCL=0.0; unsigned char DevID; //Device ID unsigned char Interrupt; unsigned char IsStart; //Uart put char, send Data directly /*void putchar( char Data) { COMTX=Data; while(!(0x020==(COMSTA0 & 0x020))) {;} } */ void sendfloatdata(float *data) { char buffer[4]; char *p; int i = 0; p = (char*)data; while(! (0x020 == (COMSTA0 & 0x020))) {} COMTX =0x0A; for (i = 0;i<4;i++) { buffer[i] = *p; p++; while(! (0x020 == (COMSTA0 & 0x020))) {} COMTX =buffer[i]; } } void senddata(int to_send) { while(!(0x020==(COMSTA0 & 0x020))){} COMTX = 0x0A; // output LF while(!(0x020==(COMSTA0 & 0x020))){} COMTX = 0x0D; // output CR while(!(0x020==(COMSTA0 & 0x020))){} COMTX = ((to_send >> 8) & 0xFF); while(!(0x020==(COMSTA0 & 0x020))){} COMTX = ((to_send) & 0x0FF); } void delay (int timer) { while(timer >= 0) timer--; } int putchar(int ch) { /* Write character to Serial Port */ if (ch == '\n') { while(!(0x020==(COMSTA0 & 0x020))) {} COMTX = CR; /* output CR */ } while(!(0x020==(COMSTA0 & 0x020))) {} return (COMTX = ch); } //Uart interrupt service function, implement the communication with PC demo software void IRQ_Handler() __irq { unsigned char UartDataReceived; // Received Data unsigned char UartInterrupt; // Interrupt status UartInterrupt =COMIID0 ; if(UartInterrupt==0x04)//Has Received a Data { UartDataReceived=COMRX; //Get received data if(UartDataReceived==0xAA) //Test connection by sending out device ID { COMTX=DevID; } if(UartDataReceived==0x55) //Start command { IsStart=0x01; } if(UartDataReceived==0xA5) //Stop command { IsStart=0x00; } } /* else if(UartInterrupt==0x02) // Has Send a Data { // Nothing to do } */ } //ADuC7026 UART initialization void UART_Initiate() { // Setup tx & rx pins on P1.0 and P1.1 GP1CON = 0x2211; // I2C on P1.2 and P1.3 Setup tx & rx pins on P1.0 and P1.1 for UART //Initiate the UART Port to 9600bps POWKEY1 = 0x01; //Start PLL setting,changeless POWCON = 0x00; POWKEY2 = 0xF4; //Finish PLL setting,changeless COMCON0 = 0x80; // Setting DLAB COMDIV0 = 0x0B; // Setting DIV0 and DIV1 to DL calculated COMDIV1 = 0x00; COMCON0 = 0x07; // Clearing DLAB // fractional divider COMDIV2 = 0x883E; //Enable UART interrupt COMIEN0=0x03; IRQEN = 0x4000; } //ADuC7026 I2C1 initialization void I2C1_Initiate() { //Initiate the I2C1 Port to 400kbps GP1CON = 0x2211; // I2C on P1.2 and P1.3 Setup tx & rx pins on P1.0 and P1.1 for UART I2C1CFG = 0x82; // Master Enable & Enable Generation of Master Clock // I2C-Master setup I2C1DIV = 0x3232; // 0x3232 = 400kHz // 0xCFCF = 100kHz //Enable I2C1 Master Interupt FIQEN |= SM_MASTER1_BIT; } //ADuc7026 initialization, UART and I2C1 void ADuC7026_Initiate(void) { UART_Initiate(); I2C1_Initiate(); } //ADXL345 initialization, register configuration void ADXL345_Initiate() { I2C_WRITE_REGISTER(0x2D,0x08); //Power CTL: Measure mode I2C_WRITE_REGISTER(0x2C,0x0C); //Rate: 400Hz I2C_WRITE_REGISTER(0x31,0x01); //Data Format: 4g right justified 128=1g I2C_WRITE_REGISTER(0x2E,0x80); //Int En: Data Rdy, Single Tap, Doulbe Tap,Free fall } //Delay void Delay(unsigned int Time1, unsigned int Timer2) { unsigned int i, j, k=0xFFFF; for(i=0;i 0) k--; } } void main(void) { ADuC7026_Initiate(); //ADuC7026 Initialization ADXL345_Initiate(); //ADXL345 Initialization //Variables initialization IsStart=0x00; GP4DAT=0x04000000; DevID=(unsigned char)I2C_READ_REGISTER(0x00); //get device ID first I2C_READ_REGISTER(0x30); //clear interrupt; while(1) //Endless loop { //GP4DAT |= 0x02000000; // if(IsStart==0x01) // Start // { Interrupt=(unsigned char)I2C_READ_REGISTER(0x30); // get interrupt status GP4DAT |= 0x02000000; if((Interrupt&0x80)==0x80) // Data Rdy interrupt, get X Y Z data for shake and rotate function { // Get high byte and low byte data of X Y Z, and combine into Word DataX1=(unsigned char)I2C_READ_REGISTER(0x32); DataX2=(unsigned char)I2C_READ_REGISTER(0x33); DataY1=(unsigned char)I2C_READ_REGISTER(0x34); DataY2=(unsigned char)I2C_READ_REGISTER(0x35); DataZ1=(unsigned char)I2C_READ_REGISTER(0x36); DataZ2=(unsigned char)I2C_READ_REGISTER(0x37); /* putchar(0x0A); putchar(0x0D); putchar(DataX2&0x07); putchar(DataX1); putchar(DataY2&0x07); putchar(DataY1); putchar(DataZ2&0x07); putchar(DataZ1); */ DataX = ((DataX2&0x07)<<8) | DataX1; if(DataX & 0x0400) { DataX = DataX - 0x07FF - 1; } DataY = ((DataY2&0x07)<<8) | DataY1; if(DataY & 0x0400) { DataY = DataY - 0x07FF - 1; } DataZ = ((DataZ2&0x07)<<8) | DataZ1; if(DataZ & 0x0400) { DataZ = DataZ - 0x07FF - 1; } // senddata(DataZ); XINCL = DataX *4* 180/1024; // GP4DAT ^= 0x00010000; printf("%0.1f\n",XINCL); YINCL = DataY *4* 180/1024; // GP4DAT ^= 0x00020000; printf("%f\n",YINCL); ZINCL = DataZ *4* 180/1024; // GP4DAT ^= 0x00040000; printf("%f\n\n\n",ZINCL); } Delay(100,20000); // delay a short time for next read for interrupt register // } } }
