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
main.cpp@3:43ec9759b4ab, 2021-03-29 (annotated)
- Committer:
- hakusan270
- Date:
- Mon Mar 29 07:11:52 2021 +0000
- Revision:
- 3:43ec9759b4ab
- Parent:
- 2:42900b8c9a14
- Child:
- 4:040903da458c
fix pwdown
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 | 2:42900b8c9a14 | 43 | pc.printf("\nSTM32 Loadcell example\r\n"); |
hakusan270 | 2:42900b8c9a14 | 44 | while(1) { |
hakusan270 | 2:42900b8c9a14 | 45 | led = !led; |
hakusan270 | 3:43ec9759b4ab | 46 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 47 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 48 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 49 | wait(1.0); |
hakusan270 | 2:42900b8c9a14 | 50 | dt=0; |
hakusan270 | 2:42900b8c9a14 | 51 | for(i=0;i<24;i++) { |
hakusan270 | 2:42900b8c9a14 | 52 | hx_clk=1; |
hakusan270 | 2:42900b8c9a14 | 53 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 54 | dt <<= 1; |
hakusan270 | 2:42900b8c9a14 | 55 | if (hx_dt==0) dt |= 1; |
hakusan270 | 2:42900b8c9a14 | 56 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 57 | wait_us(1); |
hakusan270 | 2:42900b8c9a14 | 58 | } |
hakusan270 | 3:43ec9759b4ab | 59 | hx_clk=1; |
hakusan270 | 3:43ec9759b4ab | 60 | wait_us(1); |
hakusan270 | 3:43ec9759b4ab | 61 | hx_clk=0; |
hakusan270 | 2:42900b8c9a14 | 62 | pc.printf("data = %08x %d ",dt,dt); |
hakusan270 | 2:42900b8c9a14 | 63 | wt = (float)(dt) * 0.000038f - 1.2f; |
hakusan270 | 2:42900b8c9a14 | 64 | pc.printf("waight = %3.3f\r\n",wt); |
hakusan270 | 2:42900b8c9a14 | 65 | } |
hakusan270 | 2:42900b8c9a14 | 66 | |
hakusan270 | 2:42900b8c9a14 | 67 | |
hakusan270 | 1:025596ffc973 | 68 | initLIS3DH(); |
hakusan270 | 1:025596ffc973 | 69 | |
hakusan270 | 0:b76e4ba14765 | 70 | pc.printf("\nSTM32 ADC internal channels reading example\r\n"); |
hakusan270 | 0:b76e4ba14765 | 71 | while(1) { |
hakusan270 | 0:b76e4ba14765 | 72 | pc.printf("ADC Temp = %f\r\n", (adc_temp.read()*100)); |
hakusan270 | 0:b76e4ba14765 | 73 | pc.printf("ADC VRef = %f\r\n", adc_vref.read()); |
hakusan270 | 1:025596ffc973 | 74 | // pc.printf("\033[2A"); |
hakusan270 | 1:025596ffc973 | 75 | |
hakusan270 | 1:025596ffc973 | 76 | |
hakusan270 | 1:025596ffc973 | 77 | read3axes(&x,&y,&z); |
hakusan270 | 1:025596ffc973 | 78 | readTemp(&tmp); |
hakusan270 | 1:025596ffc973 | 79 | pc.printf("x=%d y=%d z=%d tmperature=%d\r\n", x,y,z,tmp); |
hakusan270 | 1:025596ffc973 | 80 | 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 | 81 | led = !led; |
hakusan270 | 0:b76e4ba14765 | 82 | wait(1.0); |
hakusan270 | 0:b76e4ba14765 | 83 | } |
hakusan270 | 0:b76e4ba14765 | 84 | } |
hakusan270 | 1:025596ffc973 | 85 | /*********** porting **************/ |
hakusan270 | 1:025596ffc973 | 86 | void spiFormat(int b,int m) { |
hakusan270 | 1:025596ffc973 | 87 | STSPI.format(b,m); /* 8bit */ |
hakusan270 | 1:025596ffc973 | 88 | } |
hakusan270 | 1:025596ffc973 | 89 | void spiFrequency(int f){ |
hakusan270 | 1:025596ffc973 | 90 | STSPI.frequency(f); /* 1Mbps */ |
hakusan270 | 1:025596ffc973 | 91 | } |
hakusan270 | 1:025596ffc973 | 92 | void spiWriteCS(int cs) { |
hakusan270 | 1:025596ffc973 | 93 | STSPICS=cs; |
hakusan270 | 1:025596ffc973 | 94 | } |
hakusan270 | 1:025596ffc973 | 95 | int spiWrite(int wd) { |
hakusan270 | 1:025596ffc973 | 96 | return ( STSPI.write(wd)); |
hakusan270 | 1:025596ffc973 | 97 | } |