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@4:040903da458c, 2021-08-03 (annotated)
- Committer:
- hakusan270
- Date:
- Tue Aug 03 02:54:13 2021 +0000
- Revision:
- 4:040903da458c
- Parent:
- 3:43ec9759b4ab
- Child:
- 5:fbeb85ebd47b
GS2 Mbed test
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 | 0:b76e4ba14765 | 22 | */ |
hakusan270 | 2:42900b8c9a14 | 23 | DigitalOut hx_clk(PB_7); |
hakusan270 | 2:42900b8c9a14 | 24 | DigitalIn hx_dt(PB_6); |
hakusan270 | 0:b76e4ba14765 | 25 | |
hakusan270 | 0:b76e4ba14765 | 26 | RawSerial pc(PA_9, PA_10,115200); //console UART |
hakusan270 | 0:b76e4ba14765 | 27 | LowPowerTicker interrput; |
hakusan270 | 0:b76e4ba14765 | 28 | SPI STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk |
hakusan270 | 0:b76e4ba14765 | 29 | DigitalOut STSPICS(PB_12); |
hakusan270 | 0:b76e4ba14765 | 30 | DigitalOut led(PB_5); |
hakusan270 | 1:025596ffc973 | 31 | int initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 32 | int read3axes(short *tx,short *ty,short *tz); |
hakusan270 | 1:025596ffc973 | 33 | int readTemp(short *tmp); |
hakusan270 | 0:b76e4ba14765 | 34 | |
hakusan270 | 0:b76e4ba14765 | 35 | int main() |
hakusan270 | 0:b76e4ba14765 | 36 | { |
hakusan270 | 2:42900b8c9a14 | 37 | short x,y,z,tmp; |
hakusan270 | 2:42900b8c9a14 | 38 | |
hakusan270 | 2:42900b8c9a14 | 39 | int i,l; |
hakusan270 | 2:42900b8c9a14 | 40 | int lwaight; |
hakusan270 | 2:42900b8c9a14 | 41 | float wt; |
hakusan270 | 2:42900b8c9a14 | 42 | uint32_t dt; |
hakusan270 | 4:040903da458c | 43 | #if 0 |
hakusan270 | 2:42900b8c9a14 | 44 | pc.printf("\nSTM32 Loadcell example\r\n"); |
hakusan270 | 2:42900b8c9a14 | 45 | while(1) { |
hakusan270 | 2:42900b8c9a14 | 46 | led = !led; |
hakusan270 | 3:43ec9759b4ab | 47 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 48 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 49 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 50 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 51 | dt=0; |
hakusan270 | 2:42900b8c9a14 | 52 | for(i=0;i<24;i++) { |
hakusan270 | 2:42900b8c9a14 | 53 | hx_clk=1; |
hakusan270 | 2:42900b8c9a14 | 54 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 55 | dt <<= 1; |
hakusan270 | 2:42900b8c9a14 | 56 | if (hx_dt==0) dt |= 1; |
hakusan270 | 2:42900b8c9a14 | 57 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 58 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 59 | } |
hakusan270 | 3:43ec9759b4ab | 60 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 61 | wait_us(1); |
hakusan270 | 3:43ec9759b4ab | 62 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 63 | pc.printf("data = %08x %d ",dt,dt); |
hakusan270 | 2:42900b8c9a14 | 64 | wt = (float)(dt) * 0.000038f - 1.2f; |
hakusan270 | 2:42900b8c9a14 | 65 | pc.printf("waight = %3.3f\r\n",wt); |
hakusan270 | 2:42900b8c9a14 | 66 | } |
hakusan270 | 4:040903da458c | 67 | #endif |
hakusan270 | 2:42900b8c9a14 | 68 | |
hakusan270 | 1:025596ffc973 | 69 | initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 70 | |
hakusan270 | 0:b76e4ba14765 | 71 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
hakusan270 | 4:040903da458c | 72 | pc.printf("\nSTM32 3D acc sensor example\r\n"); |
hakusan270 | 0:b76e4ba14765 | 73 | while(1) { |
hakusan270 | 0:b76e4ba14765 | 74 | pc.printf("ADC Temp = %f\r\n", (adc_temp.read()*100)); |
hakusan270 | 0:b76e4ba14765 | 75 | pc.printf("ADC VRef = %f\r\n", adc_vref.read()); |
hakusan270 | 1:025596ffc973 | 76 | // pc.printf("\033[2A"); |
hakusan270 | 1:025596ffc973 | 77 | |
hakusan270 | 1:025596ffc973 | 78 | |
hakusan270 | 1:025596ffc973 | 79 | read3axes(&x,&y,&z); |
hakusan270 | 1:025596ffc973 | 80 | readTemp(&tmp); |
hakusan270 | 1:025596ffc973 | 81 | pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp); |
hakusan270 | 1:025596ffc973 | 82 | pc.printf("x=%f y=%f z=%f\r\n", (float)x/1024.0f,(float)y/1024.0f,(float)z/1024.0f); |
hakusan270 | 0:b76e4ba14765 | 83 | led = !led; |
hakusan270 | 0:b76e4ba14765 | 84 | wait(1.0); |
hakusan270 | 0:b76e4ba14765 | 85 | } |
hakusan270 | 0:b76e4ba14765 | 86 | } |
hakusan270 | 1:025596ffc973 | 87 | /*********** porting **************/ |
hakusan270 | 1:025596ffc973 | 88 | void spiFormat(int b,int m) { |
hakusan270 | 1:025596ffc973 | 89 | STSPI.format(b,m); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 90 | } |
hakusan270 | 1:025596ffc973 | 91 | void spiFrequency(int f){ |
hakusan270 | 1:025596ffc973 | 92 | STSPI.frequency(f); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 93 | } |
hakusan270 | 1:025596ffc973 | 94 | void spiWriteCS(int cs) { |
hakusan270 | 1:025596ffc973 | 95 | STSPICS=cs; |
hakusan270 | 1:025596ffc973 | 96 | } |
hakusan270 | 1:025596ffc973 | 97 | int spiWrite(int wd) { |
hakusan270 | 1:025596ffc973 | 98 | return ( STSPI.write(wd)); |
hakusan270 | 1:025596ffc973 | 99 | } |