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@6:bf4321ef0330, 2021-08-04 (annotated)
- Committer:
- rgoto
- Date:
- Wed Aug 04 06:41:02 2021 +0000
- Revision:
- 6:bf4321ef0330
- Parent:
- 5:fbeb85ebd47b
UD-GS4-R + 400G sensor + SD card
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hakusan270 | 0:b76e4ba14765 | 1 | #include "mbed.h" |
rgoto | 6:bf4321ef0330 | 2 | #include "SDFileSystem.h" |
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); |
rgoto | 6:bf4321ef0330 | 39 | |
rgoto | 6:bf4321ef0330 | 40 | SDFileSystem *sd = new SDFileSystem(PA_12, PA_11, PB_3, PA_15, "sd"); // mosi, miso, sclk, cs, name |
rgoto | 6:bf4321ef0330 | 41 | |
hakusan270 | 0:b76e4ba14765 | 42 | DigitalOut led(PB_5); |
hakusan270 | 1:025596ffc973 | 43 | int initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 44 | int read3axes(short *tx,short *ty,short *tz); |
hakusan270 | 1:025596ffc973 | 45 | int readTemp(short *tmp); |
rgoto | 6:bf4321ef0330 | 46 | FILE *fp; |
hakusan270 | 0:b76e4ba14765 | 47 | |
hakusan270 | 0:b76e4ba14765 | 48 | int main() |
hakusan270 | 0:b76e4ba14765 | 49 | { |
hakusan270 | 2:42900b8c9a14 | 50 | short x,y,z,tmp; |
hakusan270 | 2:42900b8c9a14 | 51 | |
hakusan270 | 2:42900b8c9a14 | 52 | int i,l; |
hakusan270 | 2:42900b8c9a14 | 53 | int lwaight; |
hakusan270 | 2:42900b8c9a14 | 54 | float wt; |
hakusan270 | 2:42900b8c9a14 | 55 | uint32_t dt; |
hakusan270 | 4:040903da458c | 56 | #if 0 |
hakusan270 | 2:42900b8c9a14 | 57 | pc.printf("\nSTM32 Loadcell example\r\n"); |
hakusan270 | 2:42900b8c9a14 | 58 | while(1) { |
hakusan270 | 2:42900b8c9a14 | 59 | led = !led; |
hakusan270 | 3:43ec9759b4ab | 60 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 61 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 62 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 63 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 64 | dt=0; |
hakusan270 | 2:42900b8c9a14 | 65 | for(i=0;i<24;i++) { |
hakusan270 | 2:42900b8c9a14 | 66 | hx_clk=1; |
hakusan270 | 2:42900b8c9a14 | 67 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 68 | dt <<= 1; |
hakusan270 | 2:42900b8c9a14 | 69 | if (hx_dt==0) dt |= 1; |
hakusan270 | 2:42900b8c9a14 | 70 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 71 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 72 | } |
hakusan270 | 3:43ec9759b4ab | 73 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 74 | wait_us(1); |
hakusan270 | 3:43ec9759b4ab | 75 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 76 | pc.printf("data = %08x %d ",dt,dt); |
hakusan270 | 2:42900b8c9a14 | 77 | wt = (float)(dt) * 0.000038f - 1.2f; |
hakusan270 | 2:42900b8c9a14 | 78 | pc.printf("waight = %3.3f\r\n",wt); |
hakusan270 | 2:42900b8c9a14 | 79 | } |
hakusan270 | 4:040903da458c | 80 | #endif |
hakusan270 | 2:42900b8c9a14 | 81 | |
hakusan270 | 1:025596ffc973 | 82 | initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 83 | |
rgoto | 6:bf4321ef0330 | 84 | h3dacc.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_2);//これで初期化している |
hakusan270 | 5:fbeb85ebd47b | 85 | |
hakusan270 | 0:b76e4ba14765 | 86 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
hakusan270 | 4:040903da458c | 87 | pc.printf("\nSTM32 3D acc sensor example\r\n"); |
rgoto | 6:bf4321ef0330 | 88 | |
rgoto | 6:bf4321ef0330 | 89 | fp = fopen("/sd/testlog.txt", "a"); |
rgoto | 6:bf4321ef0330 | 90 | |
rgoto | 6:bf4321ef0330 | 91 | if (fp == NULL) |
rgoto | 6:bf4321ef0330 | 92 | { |
rgoto | 6:bf4321ef0330 | 93 | pc.printf("open error!!\r\n"); |
rgoto | 6:bf4321ef0330 | 94 | |
rgoto | 6:bf4321ef0330 | 95 | } |
rgoto | 6:bf4321ef0330 | 96 | else |
rgoto | 6:bf4321ef0330 | 97 | { |
rgoto | 6:bf4321ef0330 | 98 | pc.printf("SD opened\r\n"); |
rgoto | 6:bf4321ef0330 | 99 | fclose(fp); |
rgoto | 6:bf4321ef0330 | 100 | wait_ms(10); |
rgoto | 6:bf4321ef0330 | 101 | } |
rgoto | 6:bf4321ef0330 | 102 | |
hakusan270 | 0:b76e4ba14765 | 103 | while(1) { |
hakusan270 | 5:fbeb85ebd47b | 104 | // pc.printf("ADC Temp = %f\r\n", (adc_temp.read()*100)); |
hakusan270 | 5:fbeb85ebd47b | 105 | // pc.printf("ADC VRef = %f\r\n", adc_vref.read()); |
hakusan270 | 1:025596ffc973 | 106 | // pc.printf("\033[2A"); |
hakusan270 | 5:fbeb85ebd47b | 107 | //On board ACC sensor |
hakusan270 | 5:fbeb85ebd47b | 108 | x=y=z=0; |
hakusan270 | 1:025596ffc973 | 109 | read3axes(&x,&y,&z); |
hakusan270 | 1:025596ffc973 | 110 | readTemp(&tmp); |
hakusan270 | 1:025596ffc973 | 111 | pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp); |
hakusan270 | 5:fbeb85ebd47b | 112 | 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 | 113 | // Extra ACC sensor |
hakusan270 | 5:fbeb85ebd47b | 114 | x=y=z=0; |
hakusan270 | 5:fbeb85ebd47b | 115 | h3dacc.readXYZ(&x, &y, &z); |
hakusan270 | 5:fbeb85ebd47b | 116 | pc.printf("High x=%d y=%d z=%d \r\n", x,y,z); |
hakusan270 | 5:fbeb85ebd47b | 117 | |
hakusan270 | 0:b76e4ba14765 | 118 | led = !led; |
hakusan270 | 0:b76e4ba14765 | 119 | wait(1.0); |
hakusan270 | 0:b76e4ba14765 | 120 | } |
hakusan270 | 0:b76e4ba14765 | 121 | } |
hakusan270 | 1:025596ffc973 | 122 | /*********** porting **************/ |
hakusan270 | 1:025596ffc973 | 123 | void spiFormat(int b,int m) { |
hakusan270 | 1:025596ffc973 | 124 | STSPI.format(b,m); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 125 | } |
hakusan270 | 1:025596ffc973 | 126 | void spiFrequency(int f){ |
hakusan270 | 1:025596ffc973 | 127 | STSPI.frequency(f); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 128 | } |
hakusan270 | 1:025596ffc973 | 129 | void spiWriteCS(int cs) { |
hakusan270 | 1:025596ffc973 | 130 | STSPICS=cs; |
hakusan270 | 1:025596ffc973 | 131 | } |
hakusan270 | 1:025596ffc973 | 132 | int spiWrite(int wd) { |
hakusan270 | 1:025596ffc973 | 133 | return ( STSPI.write(wd)); |
hakusan270 | 1:025596ffc973 | 134 | } |