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@1:025596ffc973, 2021-02-12 (annotated)
- Committer:
- hakusan270
- Date:
- Fri Feb 12 05:52:56 2021 +0000
- Revision:
- 1:025596ffc973
- Parent:
- 0:b76e4ba14765
- Child:
- 2:42900b8c9a14
porting 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 | 0:b76e4ba14765 | 22 | */ |
hakusan270 | 0:b76e4ba14765 | 23 | |
hakusan270 | 0:b76e4ba14765 | 24 | RawSerial pc(PA_9, PA_10,115200); //console UART |
hakusan270 | 0:b76e4ba14765 | 25 | LowPowerTicker interrput; |
hakusan270 | 0:b76e4ba14765 | 26 | SPI STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk |
hakusan270 | 0:b76e4ba14765 | 27 | DigitalOut STSPICS(PB_12); |
hakusan270 | 0:b76e4ba14765 | 28 | DigitalOut led(PB_5); |
hakusan270 | 1:025596ffc973 | 29 | int initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 30 | int read3axes(short *tx,short *ty,short *tz); |
hakusan270 | 1:025596ffc973 | 31 | int readTemp(short *tmp); |
hakusan270 | 0:b76e4ba14765 | 32 | |
hakusan270 | 0:b76e4ba14765 | 33 | int main() |
hakusan270 | 0:b76e4ba14765 | 34 | { |
hakusan270 | 1:025596ffc973 | 35 | initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 36 | short x,y,z,tmp; |
hakusan270 | 1:025596ffc973 | 37 | |
hakusan270 | 0:b76e4ba14765 | 38 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
hakusan270 | 0:b76e4ba14765 | 39 | while(1) { |
hakusan270 | 0:b76e4ba14765 | 40 | pc.printf("ADC Temp = %f\r\n", (adc_temp.read()*100)); |
hakusan270 | 0:b76e4ba14765 | 41 | pc.printf("ADC VRef = %f\r\n", adc_vref.read()); |
hakusan270 | 1:025596ffc973 | 42 | // pc.printf("\033[2A"); |
hakusan270 | 1:025596ffc973 | 43 | |
hakusan270 | 1:025596ffc973 | 44 | |
hakusan270 | 1:025596ffc973 | 45 | read3axes(&x,&y,&z); |
hakusan270 | 1:025596ffc973 | 46 | readTemp(&tmp); |
hakusan270 | 1:025596ffc973 | 47 | pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp); |
hakusan270 | 1:025596ffc973 | 48 | 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 | 49 | led = !led; |
hakusan270 | 0:b76e4ba14765 | 50 | wait(1.0); |
hakusan270 | 0:b76e4ba14765 | 51 | } |
hakusan270 | 0:b76e4ba14765 | 52 | } |
hakusan270 | 1:025596ffc973 | 53 | /*********** porting **************/ |
hakusan270 | 1:025596ffc973 | 54 | void spiFormat(int b,int m) { |
hakusan270 | 1:025596ffc973 | 55 | STSPI.format(b,m); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 56 | } |
hakusan270 | 1:025596ffc973 | 57 | void spiFrequency(int f){ |
hakusan270 | 1:025596ffc973 | 58 | STSPI.frequency(f); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 59 | } |
hakusan270 | 1:025596ffc973 | 60 | void spiWriteCS(int cs) { |
hakusan270 | 1:025596ffc973 | 61 | STSPICS=cs; |
hakusan270 | 1:025596ffc973 | 62 | } |
hakusan270 | 1:025596ffc973 | 63 | int spiWrite(int wd) { |
hakusan270 | 1:025596ffc973 | 64 | return ( STSPI.write(wd)); |
hakusan270 | 1:025596ffc973 | 65 | } |