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
accel3d.cpp@1:025596ffc973, 2021-02-12 (annotated)
- Committer:
- hakusan270
- Date:
- Fri Feb 12 05:52:56 2021 +0000
- Revision:
- 1:025596ffc973
porting acc sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hakusan270 | 1:025596ffc973 | 1 | #include "mbed.h" |
hakusan270 | 1:025596ffc973 | 2 | #include <stdio.h> |
hakusan270 | 1:025596ffc973 | 3 | #include <stdlib.h> |
hakusan270 | 1:025596ffc973 | 4 | #include <time.h> |
hakusan270 | 1:025596ffc973 | 5 | |
hakusan270 | 1:025596ffc973 | 6 | //#include "udgs1.h" |
hakusan270 | 1:025596ffc973 | 7 | #define ACC_OK 0 |
hakusan270 | 1:025596ffc973 | 8 | //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK); |
hakusan270 | 1:025596ffc973 | 9 | /******************************************* |
hakusan270 | 1:025596ffc973 | 10 | int initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 11 | int read3axes(short *tx,short *ty,short *tz); |
hakusan270 | 1:025596ffc973 | 12 | int readTemp(short *tmp); |
hakusan270 | 1:025596ffc973 | 13 | ******************************************/ |
hakusan270 | 1:025596ffc973 | 14 | |
hakusan270 | 1:025596ffc973 | 15 | unsigned char spiwriteReg(unsigned char ad,unsigned char val); |
hakusan270 | 1:025596ffc973 | 16 | int spireadRegs(unsigned char ad,unsigned char *p,int bytes); |
hakusan270 | 1:025596ffc973 | 17 | unsigned char spireadReg(unsigned char ad); |
hakusan270 | 1:025596ffc973 | 18 | extern void spiFormat(int b,int m); |
hakusan270 | 1:025596ffc973 | 19 | extern void spiFrequency(int f); |
hakusan270 | 1:025596ffc973 | 20 | extern void spiWriteCS(int cs); |
hakusan270 | 1:025596ffc973 | 21 | extern int spiWrite(int wd); |
hakusan270 | 1:025596ffc973 | 22 | #define LIS2DW12_ID 0x44 |
hakusan270 | 1:025596ffc973 | 23 | /***************************************** |
hakusan270 | 1:025596ffc973 | 24 | init LIS2DW12 LIS3DH ST micro 3-axes accelerometer |
hakusan270 | 1:025596ffc973 | 25 | SPI interface |
hakusan270 | 1:025596ffc973 | 26 | *****************************************/ |
hakusan270 | 1:025596ffc973 | 27 | int initLIS3DH() |
hakusan270 | 1:025596ffc973 | 28 | { |
hakusan270 | 1:025596ffc973 | 29 | // STSPI = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK, D6); |
hakusan270 | 1:025596ffc973 | 30 | |
hakusan270 | 1:025596ffc973 | 31 | spiFormat(8,3); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 32 | spiFrequency(1000000); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 33 | spiWriteCS(1); |
hakusan270 | 1:025596ffc973 | 34 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 35 | // printf("Who am I %02x\r\n", spireadReg(0x0f)); |
hakusan270 | 1:025596ffc973 | 36 | spiwriteReg(0x21,0x84); // boot on |
hakusan270 | 1:025596ffc973 | 37 | spiwriteReg(0x1f,0xc0); // |
hakusan270 | 1:025596ffc973 | 38 | spiwriteReg(0x20,0x35); // 25hz 14bit |
hakusan270 | 1:025596ffc973 | 39 | spiwriteReg(0x25,0x14); // max 4G |
hakusan270 | 1:025596ffc973 | 40 | // spiwriteReg(0x23,0x98); |
hakusan270 | 1:025596ffc973 | 41 | return (spireadReg(0x0f) - LIS2DW12_ID); |
hakusan270 | 1:025596ffc973 | 42 | } |
hakusan270 | 1:025596ffc973 | 43 | |
hakusan270 | 1:025596ffc973 | 44 | /************************************************* |
hakusan270 | 1:025596ffc973 | 45 | read 3-axes from LIS3DH |
hakusan270 | 1:025596ffc973 | 46 | *************************************************/ |
hakusan270 | 1:025596ffc973 | 47 | int read3axes(short *tx,short *ty,short *tz) |
hakusan270 | 1:025596ffc973 | 48 | { |
hakusan270 | 1:025596ffc973 | 49 | int timeout=5; |
hakusan270 | 1:025596ffc973 | 50 | unsigned char regs[6]; |
hakusan270 | 1:025596ffc973 | 51 | wait_us(50); |
hakusan270 | 1:025596ffc973 | 52 | while((spireadReg(0x07)&7)==0 && timeout--){wait_us(5);}; |
hakusan270 | 1:025596ffc973 | 53 | spireadRegs(0x28,regs,6);// 14bit signed 1G = 413 9.8m/s**2 |
hakusan270 | 1:025596ffc973 | 54 | // STSPI.frequency(); //1MHz |
hakusan270 | 1:025596ffc973 | 55 | *tx = (short)(regs[0] | (regs[1]<<8)) >> 3; |
hakusan270 | 1:025596ffc973 | 56 | *ty = (short)(regs[2] | (regs[3]<<8)) >> 3; |
hakusan270 | 1:025596ffc973 | 57 | *tz = (short)(regs[4] | (regs[5]<<8)) >> 3; |
hakusan270 | 1:025596ffc973 | 58 | return(ACC_OK); |
hakusan270 | 1:025596ffc973 | 59 | } |
hakusan270 | 1:025596ffc973 | 60 | |
hakusan270 | 1:025596ffc973 | 61 | /************************************************* |
hakusan270 | 1:025596ffc973 | 62 | read temparture from LIS3DH -105 to +152 degree |
hakusan270 | 1:025596ffc973 | 63 | *************************************************/ |
hakusan270 | 1:025596ffc973 | 64 | int readTemp(short *tmp) |
hakusan270 | 1:025596ffc973 | 65 | { |
hakusan270 | 1:025596ffc973 | 66 | signed char t; |
hakusan270 | 1:025596ffc973 | 67 | t = spireadReg(0x26) + 25; |
hakusan270 | 1:025596ffc973 | 68 | if (tmp) *tmp = t; |
hakusan270 | 1:025596ffc973 | 69 | return(t); |
hakusan270 | 1:025596ffc973 | 70 | } |
hakusan270 | 1:025596ffc973 | 71 | |
hakusan270 | 1:025596ffc973 | 72 | |
hakusan270 | 1:025596ffc973 | 73 | /************************************************* |
hakusan270 | 1:025596ffc973 | 74 | Read a regster from SPI |
hakusan270 | 1:025596ffc973 | 75 | *************************************************/ |
hakusan270 | 1:025596ffc973 | 76 | unsigned char spireadReg(unsigned char ad){ |
hakusan270 | 1:025596ffc973 | 77 | unsigned char r; |
hakusan270 | 1:025596ffc973 | 78 | spiWriteCS(0); |
hakusan270 | 1:025596ffc973 | 79 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 80 | spiWrite(ad | 0x80); |
hakusan270 | 1:025596ffc973 | 81 | r = spiWrite(0); |
hakusan270 | 1:025596ffc973 | 82 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 83 | spiWriteCS(1); |
hakusan270 | 1:025596ffc973 | 84 | return(r); |
hakusan270 | 1:025596ffc973 | 85 | } |
hakusan270 | 1:025596ffc973 | 86 | |
hakusan270 | 1:025596ffc973 | 87 | /************************************************* |
hakusan270 | 1:025596ffc973 | 88 | Read multi regsters from SPI |
hakusan270 | 1:025596ffc973 | 89 | *************************************************/ |
hakusan270 | 1:025596ffc973 | 90 | int spireadRegs(unsigned char ad,unsigned char *p,int bytes){ |
hakusan270 | 1:025596ffc973 | 91 | #if 0 |
hakusan270 | 1:025596ffc973 | 92 | while(bytes--) { |
hakusan270 | 1:025596ffc973 | 93 | *p = spireadReg( ad ); |
hakusan270 | 1:025596ffc973 | 94 | p++; |
hakusan270 | 1:025596ffc973 | 95 | ad++; |
hakusan270 | 1:025596ffc973 | 96 | } |
hakusan270 | 1:025596ffc973 | 97 | #else |
hakusan270 | 1:025596ffc973 | 98 | spiWriteCS(0); |
hakusan270 | 1:025596ffc973 | 99 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 100 | spiWrite(ad | 0x80 ); |
hakusan270 | 1:025596ffc973 | 101 | while(bytes--) { |
hakusan270 | 1:025596ffc973 | 102 | *p++ = spiWrite(0); |
hakusan270 | 1:025596ffc973 | 103 | } |
hakusan270 | 1:025596ffc973 | 104 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 105 | spiWriteCS(1); |
hakusan270 | 1:025596ffc973 | 106 | #endif |
hakusan270 | 1:025596ffc973 | 107 | return(ACC_OK); |
hakusan270 | 1:025596ffc973 | 108 | } |
hakusan270 | 1:025596ffc973 | 109 | |
hakusan270 | 1:025596ffc973 | 110 | /************************************************* |
hakusan270 | 1:025596ffc973 | 111 | Write a regster via SPI |
hakusan270 | 1:025596ffc973 | 112 | *************************************************/ |
hakusan270 | 1:025596ffc973 | 113 | unsigned char spiwriteReg(unsigned char ad,unsigned char val){ |
hakusan270 | 1:025596ffc973 | 114 | unsigned char r; |
hakusan270 | 1:025596ffc973 | 115 | spiWriteCS(0); |
hakusan270 | 1:025596ffc973 | 116 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 117 | spiWrite(ad); |
hakusan270 | 1:025596ffc973 | 118 | r = spiWrite(val); |
hakusan270 | 1:025596ffc973 | 119 | wait_us(1); |
hakusan270 | 1:025596ffc973 | 120 | spiWriteCS(1); |
hakusan270 | 1:025596ffc973 | 121 | return(r); |
hakusan270 | 1:025596ffc973 | 122 | } |
hakusan270 | 1:025596ffc973 | 123 | |
hakusan270 | 1:025596ffc973 | 124 | |
hakusan270 | 1:025596ffc973 | 125 | |
hakusan270 | 1:025596ffc973 | 126 |