Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem_
accel3d.cpp
00001 #include "mbed.h" 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <time.h> 00005 00006 //#include "udgs1.h" 00007 #define ACC_OK 0 00008 //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK); 00009 /******************************************* 00010 int initLIS3DH(); 00011 int read3axes(short *tx,short *ty,short *tz); 00012 int readTemp(short *tmp); 00013 ******************************************/ 00014 00015 unsigned char spiwriteReg(unsigned char ad,unsigned char val); 00016 int spireadRegs(unsigned char ad,unsigned char *p,int bytes); 00017 unsigned char spireadReg(unsigned char ad); 00018 extern void spiFormat(int b,int m); 00019 extern void spiFrequency(int f); 00020 extern void spiWriteCS(int cs); 00021 extern int spiWrite(int wd); 00022 #define LIS2DW12_ID 0x44 00023 /***************************************** 00024 init LIS2DW12 LIS3DH ST micro 3-axes accelerometer 00025 SPI interface 00026 *****************************************/ 00027 int initLIS3DH() 00028 { 00029 // STSPI = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK, D6); 00030 00031 spiFormat(8,3); /* 8bit */ 00032 spiFrequency(1000000); /* 1Mbps */ 00033 spiWriteCS(1); 00034 wait_us(1); 00035 // printf("Who am I %02x\r\n", spireadReg(0x0f)); 00036 spiwriteReg(0x21,0x84); // 0x84(0101 0100) highpass filter/boot on 00037 spiwriteReg(0x1f,0xc0); // 00038 spiwriteReg(0x20,0x35); // 25hz 14bit 00039 spiwriteReg(0x25,0x14); // max 4G 00040 // spiwriteReg(0x23,0x98); 00041 return (spireadReg(0x0f) - LIS2DW12_ID); 00042 } 00043 00044 /************************************************* 00045 read 3-axes from LIS3DH 00046 *************************************************/ 00047 int read3axes(short *tx,short *ty,short *tz) 00048 { 00049 int timeout=5; 00050 unsigned char regs[6]; 00051 wait_us(50); 00052 while((spireadReg(0x07)&7)==0 && timeout--){wait_us(5);}; 00053 spireadRegs(0x28,regs,6);// 14bit signed 1G = 413 9.8m/s**2 00054 // STSPI.frequency(); //1MHz 00055 *tx = (short)(regs[0] | (regs[1]<<8)) >> 3; 00056 *ty = (short)(regs[2] | (regs[3]<<8)) >> 3; 00057 *tz = (short)(regs[4] | (regs[5]<<8)) >> 3; 00058 return(ACC_OK); 00059 } 00060 00061 /************************************************* 00062 read temparture from LIS3DH -105 to +152 degree 00063 *************************************************/ 00064 int readTemp(short *tmp) 00065 { 00066 signed char t; 00067 t = spireadReg(0x26) + 25; 00068 if (tmp) *tmp = t; 00069 return(t); 00070 } 00071 00072 00073 /************************************************* 00074 Read a regster from SPI 00075 *************************************************/ 00076 unsigned char spireadReg(unsigned char ad){ 00077 unsigned char r; 00078 spiWriteCS(0); 00079 wait_us(1); 00080 spiWrite(ad | 0x80); 00081 r = spiWrite(0); 00082 wait_us(1); 00083 spiWriteCS(1); 00084 return(r); 00085 } 00086 00087 /************************************************* 00088 Read multi regsters from SPI 00089 *************************************************/ 00090 int spireadRegs(unsigned char ad,unsigned char *p,int bytes){ 00091 #if 0 00092 while(bytes--) { 00093 *p = spireadReg( ad ); 00094 p++; 00095 ad++; 00096 } 00097 #else 00098 spiWriteCS(0); 00099 wait_us(1); 00100 spiWrite(ad | 0x80 ); 00101 while(bytes--) { 00102 *p++ = spiWrite(0); 00103 } 00104 wait_us(1); 00105 spiWriteCS(1); 00106 #endif 00107 return(ACC_OK); 00108 } 00109 00110 /************************************************* 00111 Write a regster via SPI 00112 *************************************************/ 00113 unsigned char spiwriteReg(unsigned char ad,unsigned char val){ 00114 unsigned char r; 00115 spiWriteCS(0); 00116 wait_us(1); 00117 spiWrite(ad); 00118 r = spiWrite(val); 00119 wait_us(1); 00120 spiWriteCS(1); 00121 return(r); 00122 }
Generated on Fri Aug 26 2022 10:58:25 by
