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.
main.cpp@2:b6a0d4ba24a1, 2018-03-15 (annotated)
- Committer:
- summers
- Date:
- Thu Mar 15 17:07:42 2018 +0000
- Revision:
- 2:b6a0d4ba24a1
- Parent:
- 1:3295382ddc81
- Child:
- 3:a8c38c3fe967
First Draft of SPI interface to MS5611 pressure sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
summers | 0:9146d1e52be0 | 1 | #include "mbed.h" |
summers | 1:3295382ddc81 | 2 | #include "main.h" |
summers | 0:9146d1e52be0 | 3 | |
summers | 1:3295382ddc81 | 4 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); // tx, rx |
summers | 1:3295382ddc81 | 5 | //DigitalOut led(LED_RED); |
summers | 1:3295382ddc81 | 6 | //SPI spi(PTC6,PTC7,PTD1,PTC0); |
summers | 1:3295382ddc81 | 7 | //SPI spi(PTC6,PTC7,PTD1); // my prefered, but mixed |
summers | 1:3295382ddc81 | 8 | SPI spi(PTC6,PTC7,PTC5); // PTC5 - the LED |
summers | 1:3295382ddc81 | 9 | //SPI spi(PTD2,PTD3,PTD1); |
summers | 1:3295382ddc81 | 10 | DigitalOut cs(PTC0); |
summers | 0:9146d1e52be0 | 11 | |
summers | 0:9146d1e52be0 | 12 | int main() |
summers | 0:9146d1e52be0 | 13 | { |
summers | 1:3295382ddc81 | 14 | int PROM[8]; |
summers | 2:b6a0d4ba24a1 | 15 | uint32_t D1,D2; |
summers | 2:b6a0d4ba24a1 | 16 | double T,P; |
summers | 1:3295382ddc81 | 17 | cs.write(1); // disable all SPI |
summers | 2:b6a0d4ba24a1 | 18 | pc.printf("Hello World!\r\n"); |
summers | 1:3295382ddc81 | 19 | MS5611_init(PROM); |
summers | 1:3295382ddc81 | 20 | for (int i=0; i<8; i++) { |
summers | 2:b6a0d4ba24a1 | 21 | pc.printf("Prom(%i): %i\r\n",i,PROM[i]); |
summers | 1:3295382ddc81 | 22 | } |
summers | 2:b6a0d4ba24a1 | 23 | // the third paramater gives over samping |
summers | 2:b6a0d4ba24a1 | 24 | // 0 - 256 |
summers | 2:b6a0d4ba24a1 | 25 | // 1 - 512 |
summers | 2:b6a0d4ba24a1 | 26 | // 2 - 1024 |
summers | 2:b6a0d4ba24a1 | 27 | // 3 - 2048 |
summers | 2:b6a0d4ba24a1 | 28 | // 4 - 4096 |
summers | 2:b6a0d4ba24a1 | 29 | MS5611(&D1,&D2,4); |
summers | 2:b6a0d4ba24a1 | 30 | pc.printf("D1 = %i\r\nD2 = %i\r\n",D1,D2); |
summers | 2:b6a0d4ba24a1 | 31 | |
summers | 2:b6a0d4ba24a1 | 32 | MS5611_phys(D1,D2,PROM,&T,&P); |
summers | 2:b6a0d4ba24a1 | 33 | |
summers | 2:b6a0d4ba24a1 | 34 | pc.printf("Temperature = %f deg C\r\n",T/100.0); |
summers | 2:b6a0d4ba24a1 | 35 | pc.printf("Pressure = %f mbar\r\n",P/100.0); |
summers | 2:b6a0d4ba24a1 | 36 | |
summers | 1:3295382ddc81 | 37 | } |
summers | 1:3295382ddc81 | 38 | |
summers | 1:3295382ddc81 | 39 | void MS5611_init(int *PROM) |
summers | 1:3295382ddc81 | 40 | { |
summers | 2:b6a0d4ba24a1 | 41 | long crc=0; |
summers | 1:3295382ddc81 | 42 | spi.format(8,0); // 8 bit mode 0 - this is default anyway but good to code. |
summers | 1:3295382ddc81 | 43 | spi.frequency(1000000); // 1MHz is this the minimum? |
summers | 1:3295382ddc81 | 44 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 45 | if (spi.write(0x1E)!=0xfe) { |
summers | 2:b6a0d4ba24a1 | 46 | pc.printf("Error reseting the device\r\n"); |
summers | 2:b6a0d4ba24a1 | 47 | } |
summers | 1:3295382ddc81 | 48 | wait_ms(3); // give time for ROM to reload 2.8ms by the spec |
summers | 2:b6a0d4ba24a1 | 49 | cs.write(1); // close the connection - does it finish the command? |
summers | 2:b6a0d4ba24a1 | 50 | wait_us(10); // Pause after putting CSB high |
summers | 1:3295382ddc81 | 51 | for (int i=0; i<8; i++) { |
summers | 2:b6a0d4ba24a1 | 52 | cs.write(0); // enable the SC to start a command. |
summers | 2:b6a0d4ba24a1 | 53 | if (spi.write(0xA0|(i<<1))!=0xfe) { |
summers | 2:b6a0d4ba24a1 | 54 | pc.printf("Error reading prom(%i)\r\n",i); |
summers | 2:b6a0d4ba24a1 | 55 | } |
summers | 1:3295382ddc81 | 56 | register uint8_t promh=spi.write(0x00); |
summers | 1:3295382ddc81 | 57 | register uint8_t proml=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 58 | cs.write(1); // disable CS to finish the command |
summers | 1:3295382ddc81 | 59 | PROM[i]=((promh<<8)|(proml)); |
summers | 2:b6a0d4ba24a1 | 60 | crc|=(i==7)?(PROM[i]&0xff00):PROM[i]; |
summers | 2:b6a0d4ba24a1 | 61 | for (int j=0; j<16; j++) { |
summers | 2:b6a0d4ba24a1 | 62 | crc=crc<<1; |
summers | 2:b6a0d4ba24a1 | 63 | if (crc&0x1000000) { // implimt the CRC4 algorithm 0x13 - |
summers | 2:b6a0d4ba24a1 | 64 | crc^=0x1300000; // to my mind this is implimented wrong - but its how the MS5611 does it |
summers | 2:b6a0d4ba24a1 | 65 | } |
summers | 2:b6a0d4ba24a1 | 66 | } |
summers | 2:b6a0d4ba24a1 | 67 | wait_us(10); // Pause probably not stricly needed, but after switching CSB high give a bit of time. |
summers | 1:3295382ddc81 | 68 | } |
summers | 2:b6a0d4ba24a1 | 69 | if ((crc>>20)!=(PROM[7]&0xf)) |
summers | 2:b6a0d4ba24a1 | 70 | pc.printf("CRC check sum: %x vs recorded %x\r\n",crc>>20,PROM[7]&0xf); |
summers | 1:3295382ddc81 | 71 | } |
summers | 1:3295382ddc81 | 72 | |
summers | 2:b6a0d4ba24a1 | 73 | void MS5611(uint32_t *D1, uint32_t *D2, int os) |
summers | 1:3295382ddc81 | 74 | { |
summers | 1:3295382ddc81 | 75 | // cs.write(0); |
summers | 1:3295382ddc81 | 76 | spi.format(8,0); |
summers | 1:3295382ddc81 | 77 | spi.frequency(1000000); |
summers | 2:b6a0d4ba24a1 | 78 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 79 | if (spi.write(0x1E)!=0xfe) { // and reset the device |
summers | 2:b6a0d4ba24a1 | 80 | pc.printf("Error reseting the device\r\n"); |
summers | 2:b6a0d4ba24a1 | 81 | } |
summers | 2:b6a0d4ba24a1 | 82 | wait_ms(3); // give time for ROM to reload 2.8ms by the spec |
summers | 2:b6a0d4ba24a1 | 83 | cs.write(1); // close the connection - does it finish the command? |
summers | 2:b6a0d4ba24a1 | 84 | wait_us(10); // Pause after putting CSB high |
summers | 2:b6a0d4ba24a1 | 85 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 86 | if (spi.write(0x40|(os<<1))!=0xfe) { // D1 please |
summers | 2:b6a0d4ba24a1 | 87 | pc.printf("Error asking for D1\r\n"); |
summers | 2:b6a0d4ba24a1 | 88 | } |
summers | 2:b6a0d4ba24a1 | 89 | cs.write(1); // Disable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 90 | wait_us(600*(1<<os)); // pause for read, longer when oversampling |
summers | 2:b6a0d4ba24a1 | 91 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 92 | register uint8_t d1h=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 93 | register uint8_t d1m=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 94 | register uint8_t d1l=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 95 | cs.write(1); // and terminate the command |
summers | 2:b6a0d4ba24a1 | 96 | wait_us(10); // Pause after putting CSB high |
summers | 2:b6a0d4ba24a1 | 97 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 98 | if (spi.write(0x50|(os<<1))!=0xfe) { // D2 please |
summers | 2:b6a0d4ba24a1 | 99 | pc.printf("Error asking for D2\r\n"); |
summers | 2:b6a0d4ba24a1 | 100 | } |
summers | 2:b6a0d4ba24a1 | 101 | cs.write(1); // Disable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 102 | wait_us(600*(1<<os)); // pause for read, longer when oversampling |
summers | 2:b6a0d4ba24a1 | 103 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 104 | register uint8_t d2h=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 105 | register uint8_t d2m=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 106 | register uint8_t d2l=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 107 | cs.write(1); // and terminate the command |
summers | 2:b6a0d4ba24a1 | 108 | *D1=((uint32_t) d1h<<16)|((uint32_t) d1m<<8)|((uint32_t) d1l); |
summers | 2:b6a0d4ba24a1 | 109 | *D2=((uint32_t) d2h<<16)|((uint32_t) d2m<<8)|((uint32_t) d2l); |
summers | 2:b6a0d4ba24a1 | 110 | } |
summers | 2:b6a0d4ba24a1 | 111 | |
summers | 2:b6a0d4ba24a1 | 112 | void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P) |
summers | 2:b6a0d4ba24a1 | 113 | { |
summers | 2:b6a0d4ba24a1 | 114 | int64_t dt=D2-(PROM[5]<<8); |
summers | 2:b6a0d4ba24a1 | 115 | *T=2000.0+((double) dt*PROM[6])/8388608.0; |
summers | 2:b6a0d4ba24a1 | 116 | int64_t off128=(PROM[2]<<23)+PROM[4]*dt; |
summers | 2:b6a0d4ba24a1 | 117 | int64_t sens256=(PROM[1]<<23)+PROM[3]*dt; |
summers | 2:b6a0d4ba24a1 | 118 | *P=(((double) D1*sens256)/536870912.0-((double) off128)/128.0)/ 4194304.0; |
summers | 0:9146d1e52be0 | 119 | } |