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 H3LIS331DL
main.cpp@5:fbeb85ebd47b, 2021-08-03 (annotated)
- Committer:
- hakusan270
- Date:
- Tue Aug 03 08:35:52 2021 +0000
- Revision:
- 5:fbeb85ebd47b
- Parent:
- 4:040903da458c
- Child:
- 6:bf4321ef0330
extra ACC sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hakusan270 | 0:b76e4ba14765 | 1 | #include "mbed.h" |
hakusan270 | 0:b76e4ba14765 | 2 | |
hakusan270 | 0:b76e4ba14765 | 3 | /* |
hakusan270 | 0:b76e4ba14765 | 4 | This basic example just shows how to read the ADC internal channels raw values. |
hakusan270 | 0:b76e4ba14765 | 5 | Please look in the corresponding device reference manual for a complete |
hakusan270 | 0:b76e4ba14765 | 6 | description of how to make a temperature sensor, VBat or Vref measurement. |
hakusan270 | 0:b76e4ba14765 | 7 | */ |
hakusan270 | 0:b76e4ba14765 | 8 | |
hakusan270 | 0:b76e4ba14765 | 9 | AnalogIn adc_temp(ADC_TEMP); |
hakusan270 | 0:b76e4ba14765 | 10 | AnalogIn adc_vref(ADC_VREF); |
hakusan270 | 0:b76e4ba14765 | 11 | /* UD-GS2 H/W define |
hakusan270 | 0:b76e4ba14765 | 12 | PIO_SWin PB_4 |
hakusan270 | 0:b76e4ba14765 | 13 | PIO_wkup PA_4 |
hakusan270 | 0:b76e4ba14765 | 14 | PIO_enable PB_0 |
hakusan270 | 0:b76e4ba14765 | 15 | PIO_intout1 PB_2 |
hakusan270 | 0:b76e4ba14765 | 16 | PIO_led PB_5 |
hakusan270 | 0:b76e4ba14765 | 17 | PIO_v20v PC_13 |
hakusan270 | 0:b76e4ba14765 | 18 | PIO_v18v PH_0 |
hakusan270 | 0:b76e4ba14765 | 19 | PIO_intout2 PH_1 |
hakusan270 | 0:b76e4ba14765 | 20 | PIO_spics PB_12 |
hakusan270 | 0:b76e4ba14765 | 21 | PIO_battryMonEn PA_5 |
hakusan270 | 5:fbeb85ebd47b | 22 | PB_7 SDA |
hakusan270 | 5:fbeb85ebd47b | 23 | PB_6 SCL |
hakusan270 | 5:fbeb85ebd47b | 24 | |
hakusan270 | 0:b76e4ba14765 | 25 | */ |
hakusan270 | 5:fbeb85ebd47b | 26 | //DigitalOut hx_clk(PB_7); |
hakusan270 | 5:fbeb85ebd47b | 27 | //DigitalIn hx_dt(PB_6); |
hakusan270 | 5:fbeb85ebd47b | 28 | //I2C i2cacc(p_sda, p_scl) |
hakusan270 | 5:fbeb85ebd47b | 29 | |
hakusan270 | 5:fbeb85ebd47b | 30 | #include "H3LIS331DL.h" |
hakusan270 | 5:fbeb85ebd47b | 31 | |
hakusan270 | 5:fbeb85ebd47b | 32 | H3LIS331DL h3dacc(PB_7,PB_6); |
hakusan270 | 5:fbeb85ebd47b | 33 | |
hakusan270 | 0:b76e4ba14765 | 34 | |
hakusan270 | 0:b76e4ba14765 | 35 | RawSerial pc(PA_9, PA_10,115200); //console UART |
hakusan270 | 0:b76e4ba14765 | 36 | LowPowerTicker interrput; |
hakusan270 | 0:b76e4ba14765 | 37 | SPI STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk |
hakusan270 | 0:b76e4ba14765 | 38 | DigitalOut STSPICS(PB_12); |
hakusan270 | 0:b76e4ba14765 | 39 | DigitalOut led(PB_5); |
hakusan270 | 1:025596ffc973 | 40 | int initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 41 | int read3axes(short *tx,short *ty,short *tz); |
hakusan270 | 1:025596ffc973 | 42 | int readTemp(short *tmp); |
hakusan270 | 0:b76e4ba14765 | 43 | |
hakusan270 | 0:b76e4ba14765 | 44 | int main() |
hakusan270 | 0:b76e4ba14765 | 45 | { |
hakusan270 | 2:42900b8c9a14 | 46 | short x,y,z,tmp; |
hakusan270 | 2:42900b8c9a14 | 47 | |
hakusan270 | 2:42900b8c9a14 | 48 | int i,l; |
hakusan270 | 2:42900b8c9a14 | 49 | int lwaight; |
hakusan270 | 2:42900b8c9a14 | 50 | float wt; |
hakusan270 | 2:42900b8c9a14 | 51 | uint32_t dt; |
hakusan270 | 4:040903da458c | 52 | #if 0 |
hakusan270 | 2:42900b8c9a14 | 53 | pc.printf("\nSTM32 Loadcell example\r\n"); |
hakusan270 | 2:42900b8c9a14 | 54 | while(1) { |
hakusan270 | 2:42900b8c9a14 | 55 | led = !led; |
hakusan270 | 3:43ec9759b4ab | 56 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 57 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 58 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 59 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 60 | dt=0; |
hakusan270 | 2:42900b8c9a14 | 61 | for(i=0;i<24;i++) { |
hakusan270 | 2:42900b8c9a14 | 62 | hx_clk=1; |
hakusan270 | 2:42900b8c9a14 | 63 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 64 | dt <<= 1; |
hakusan270 | 2:42900b8c9a14 | 65 | if (hx_dt==0) dt |= 1; |
hakusan270 | 2:42900b8c9a14 | 66 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 67 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 68 | } |
hakusan270 | 3:43ec9759b4ab | 69 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 70 | wait_us(1); |
hakusan270 | 3:43ec9759b4ab | 71 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 72 | pc.printf("data = %08x %d ",dt,dt); |
hakusan270 | 2:42900b8c9a14 | 73 | wt = (float)(dt) * 0.000038f - 1.2f; |
hakusan270 | 2:42900b8c9a14 | 74 | pc.printf("waight = %3.3f\r\n",wt); |
hakusan270 | 2:42900b8c9a14 | 75 | } |
hakusan270 | 4:040903da458c | 76 | #endif |
hakusan270 | 2:42900b8c9a14 | 77 | |
hakusan270 | 1:025596ffc973 | 78 | initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 79 | |
hakusan270 | 5:fbeb85ebd47b | 80 | h3dacc.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_2); |
hakusan270 | 5:fbeb85ebd47b | 81 | |
hakusan270 | 0:b76e4ba14765 | 82 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
hakusan270 | 4:040903da458c | 83 | pc.printf("\nSTM32 3D acc sensor example\r\n"); |
hakusan270 | 0:b76e4ba14765 | 84 | while(1) { |
hakusan270 | 5:fbeb85ebd47b | 85 | // pc.printf("ADC Temp = %f\r\n", (adc_temp.read()*100)); |
hakusan270 | 5:fbeb85ebd47b | 86 | // pc.printf("ADC VRef = %f\r\n", adc_vref.read()); |
hakusan270 | 1:025596ffc973 | 87 | // pc.printf("\033[2A"); |
hakusan270 | 5:fbeb85ebd47b | 88 | //On board ACC sensor |
hakusan270 | 5:fbeb85ebd47b | 89 | x=y=z=0; |
hakusan270 | 1:025596ffc973 | 90 | read3axes(&x,&y,&z); |
hakusan270 | 1:025596ffc973 | 91 | readTemp(&tmp); |
hakusan270 | 1:025596ffc973 | 92 | pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp); |
hakusan270 | 5:fbeb85ebd47b | 93 | pc.printf("x=%f y=%f z=%f\r\n", (float)x/1024.0f,(float)y/1024.0f,(float)z/1024.0f); |
hakusan270 | 5:fbeb85ebd47b | 94 | // Extra ACC sensor |
hakusan270 | 5:fbeb85ebd47b | 95 | x=y=z=0; |
hakusan270 | 5:fbeb85ebd47b | 96 | h3dacc.readXYZ(&x, &y, &z); |
hakusan270 | 5:fbeb85ebd47b | 97 | pc.printf("High x=%d y=%d z=%d \r\n", x,y,z); |
hakusan270 | 5:fbeb85ebd47b | 98 | |
hakusan270 | 0:b76e4ba14765 | 99 | led = !led; |
hakusan270 | 0:b76e4ba14765 | 100 | wait(1.0); |
hakusan270 | 0:b76e4ba14765 | 101 | } |
hakusan270 | 0:b76e4ba14765 | 102 | } |
hakusan270 | 1:025596ffc973 | 103 | /*********** porting **************/ |
hakusan270 | 1:025596ffc973 | 104 | void spiFormat(int b,int m) { |
hakusan270 | 1:025596ffc973 | 105 | STSPI.format(b,m); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 106 | } |
hakusan270 | 1:025596ffc973 | 107 | void spiFrequency(int f){ |
hakusan270 | 1:025596ffc973 | 108 | STSPI.frequency(f); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 109 | } |
hakusan270 | 1:025596ffc973 | 110 | void spiWriteCS(int cs) { |
hakusan270 | 1:025596ffc973 | 111 | STSPICS=cs; |
hakusan270 | 1:025596ffc973 | 112 | } |
hakusan270 | 1:025596ffc973 | 113 | int spiWrite(int wd) { |
hakusan270 | 1:025596ffc973 | 114 | return ( STSPI.write(wd)); |
hakusan270 | 1:025596ffc973 | 115 | } |