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
h3lis331spi.cpp
00001 #include "mbed.h" 00002 #include <stdio.h> 00003 00004 #define H3LIS331DL_CTRL_REG1 0x20 00005 #define H3LIS331DL_CTRL_REG2 0x21 00006 #define H3LIS331DL_CTRL_REG3 0x22 00007 #define H3LIS331DL_CTRL_REG4 0x23 00008 #define H3LIS331DL_CTRL_REG5 0x24 00009 //STATUS_REG_AXIES 00010 #define H3LIS331DL_STATUS_REG 0x27 // 00011 00012 ///i2c or SPI 00013 //#define USESPI 00014 00015 //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK); 00016 /******************************************* 00017 int initLIS331(); 00018 int read3axes(short *tx,short *ty,short *tz); 00019 int readTemp(short *tmp); 00020 ******************************************/ 00021 00022 00023 //mbed Class 00024 //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK); 00025 //SPI STSPI(D11, D12, D13); 00026 //\\DigitalOut STSPICS(D10); 00027 00028 #ifdef USESPI 00029 ///// SPI - 00030 extern SPI STSPI; 00031 extern DigitalOut STSPICS; 00032 /*********** porting **************/ 00033 void spiFormat(int b,int m) { 00034 STSPI.format(b,m); /* 8bit */ 00035 } 00036 void spiFrequency(int f){ 00037 STSPI.frequency(f ); /* 1Mbps */ 00038 } 00039 void spiWriteCS(int cs) { 00040 STSPICS=cs; 00041 } 00042 int spiWrite(int wd) { 00043 return ( STSPI.write(wd)); 00044 } 00045 //////// 00046 #else 00047 //i2c ///// 00048 extern I2C i2c; 00049 int deviceAddress = 0x18; 00050 /////////// 00051 #endif 00052 extern RawSerial pc; 00053 static unsigned char spireadReg(unsigned char ad); 00054 static int spireadRegs(unsigned char ad,unsigned char *p,int bytes); 00055 static unsigned char spiwriteReg(unsigned char ad,unsigned char val); 00056 #define LIS331 0x32 00057 /***************************************** 00058 init h3LIS331DL ST micro 3-axes accelerometer 00059 SPI interface 00060 *****************************************/ 00061 int initLIS331() 00062 { 00063 // STSPI = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK, D6); 00064 #ifdef USESPI 00065 spiFormat(8,3); /* 8bit */ 00066 spiFrequency(8000000); /* 8Mbps */ 00067 spiWriteCS(1); 00068 #else 00069 #endif 00070 wait_us(10); 00071 pc.printf("Who am I %02x\r\n", spireadReg(0x0f)); 00072 spiwriteReg(H3LIS331DL_CTRL_REG1,0x27); // power normal xyz enable out 50Hz 00073 spiwriteReg(H3LIS331DL_CTRL_REG2,0x00); // normal, no filter 00074 spiwriteReg(H3LIS331DL_CTRL_REG3,0x00); // no interrut 00075 spiwriteReg(H3LIS331DL_CTRL_REG4,0x30); // BDU 1 BLE 0 fs 3 400g 00076 spiwriteReg(H3LIS331DL_CTRL_REG5,0x00); // max 400G 00077 // spiwriteReg(0x23,0x98); 00078 //spireadReg(0x25); 00079 return (spireadReg(0x0f) - LIS331); 00080 } 00081 00082 00083 /************************************************* 00084 read 3-axes from LIS3DH 00085 *************************************************/ 00086 int read3axes331(short *tx,short *ty,short *tz) 00087 { 00088 int timeout=5; 00089 unsigned char regs[6]; 00090 wait_us(5); 00091 while((spireadReg(0x27)&7)!=7 && timeout--){wait_us(5);}; 00092 for (int i=0;i<6;i++) regs[i]= spireadReg(0x28 + i );// 14bit signed 1G = 413 9.8m/s**2 00093 // STSPI.frequency(); //1MHz 00094 *tx = (short)(regs[0] | (regs[1]<<8)) >> 3; 00095 *ty = (short)(regs[2] | (regs[3]<<8)) >> 3; 00096 *tz = (short)(regs[4] | (regs[5]<<8)) >> 3; 00097 return(0); 00098 } 00099 00100 00101 00102 /************************************************* 00103 Read a regster from SPI 00104 *************************************************/ 00105 static unsigned char spireadReg(unsigned char ad){ 00106 unsigned char r; 00107 #ifdef USESPI 00108 spiWriteCS(0); 00109 wait_us(1); 00110 spiWrite(ad | 0x80); 00111 r = spiWrite(0); 00112 wait_us(1); 00113 spiWriteCS(1); 00114 #else 00115 char Reg=ad; 00116 char Data[2]; 00117 00118 i2c.write((deviceAddress << 1) & 0xFE, (char *)&Reg, 1); 00119 i2c.read((deviceAddress << 1) | 0x01, (char *)Data, 2); 00120 r = Data[1]; 00121 // pc.printf("read regs %02x %02x\r\n", Data[0],Data[1]); 00122 #endif 00123 return(r); 00124 } 00125 00126 /************************************************* 00127 Read multi regsters from SPI 00128 *************************************************/ 00129 static int spireadRegs(unsigned char ad,unsigned char *p,int bytes){ 00130 #if 1 00131 while(bytes--) { 00132 *p = spireadReg( ad ); 00133 p++; 00134 ad++; 00135 } 00136 #else 00137 spiWriteCS(0); 00138 wait_us(1); 00139 spiWrite(ad | 0x80 ); 00140 while(bytes--) { 00141 *p++ = spiWrite(0); 00142 } 00143 wait_us(1); 00144 spiWriteCS(1); 00145 #endif 00146 return(0); 00147 } 00148 00149 /************************************************* 00150 Write a regster via SPI 00151 *************************************************/ 00152 static unsigned char spiwriteReg(unsigned char ad,unsigned char val){ 00153 unsigned char r; 00154 #ifdef USESPI 00155 spiWriteCS(0); 00156 wait_us(1); 00157 spiWrite(ad); 00158 r = spiWrite(val); 00159 wait_us(1); 00160 spiWriteCS(1); 00161 #else 00162 //i2c 00163 uint8_t buf[2]; 00164 00165 buf[0] = ad; 00166 buf[1] = val; 00167 i2c.write((deviceAddress << 1) & 0xFE, (char *)buf, 2); 00168 #endif 00169 return(r); 00170 } 00171 00172 00173 static unsigned char i2cReadReg(unsigned char ad,unsigned char val){ 00174 unsigned char r; 00175 #ifdef USESPI 00176 #else 00177 //i2c 00178 uint8_t buf[2]; 00179 00180 buf[0] = ad; 00181 buf[1] = val; 00182 i2c.write((deviceAddress << 1) & 0xFE, (char *)buf, 2); 00183 00184 #endif 00185 return(r); 00186 } 00187 00188
Generated on Sat Sep 16 2023 03:59:10 by
1.7.2