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@3:a8c38c3fe967, 2018-03-16 (annotated)
- Committer:
- summers
- Date:
- Fri Mar 16 18:35:06 2018 +0000
- Revision:
- 3:a8c38c3fe967
- Parent:
- 2:b6a0d4ba24a1
- Child:
- 4:e0dbf2bdb967
This reads D1 and D2 correctly and does the conversion
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 | 3:a8c38c3fe967 | 23 | |
summers | 3:a8c38c3fe967 | 24 | while (1) { |
summers | 2:b6a0d4ba24a1 | 25 | // the third paramater gives over samping |
summers | 2:b6a0d4ba24a1 | 26 | // 0 - 256 |
summers | 2:b6a0d4ba24a1 | 27 | // 1 - 512 |
summers | 2:b6a0d4ba24a1 | 28 | // 2 - 1024 |
summers | 2:b6a0d4ba24a1 | 29 | // 3 - 2048 |
summers | 2:b6a0d4ba24a1 | 30 | // 4 - 4096 |
summers | 2:b6a0d4ba24a1 | 31 | MS5611(&D1,&D2,4); |
summers | 2:b6a0d4ba24a1 | 32 | pc.printf("D1 = %i\r\nD2 = %i\r\n",D1,D2); |
summers | 2:b6a0d4ba24a1 | 33 | |
summers | 2:b6a0d4ba24a1 | 34 | MS5611_phys(D1,D2,PROM,&T,&P); |
summers | 2:b6a0d4ba24a1 | 35 | |
summers | 2:b6a0d4ba24a1 | 36 | pc.printf("Temperature = %f deg C\r\n",T/100.0); |
summers | 2:b6a0d4ba24a1 | 37 | pc.printf("Pressure = %f mbar\r\n",P/100.0); |
summers | 2:b6a0d4ba24a1 | 38 | |
summers | 3:a8c38c3fe967 | 39 | wait(10.0); |
summers | 3:a8c38c3fe967 | 40 | } |
summers | 1:3295382ddc81 | 41 | } |
summers | 1:3295382ddc81 | 42 | |
summers | 1:3295382ddc81 | 43 | void MS5611_init(int *PROM) |
summers | 1:3295382ddc81 | 44 | { |
summers | 3:a8c38c3fe967 | 45 | uint32_t crc=0; |
summers | 1:3295382ddc81 | 46 | spi.format(8,0); // 8 bit mode 0 - this is default anyway but good to code. |
summers | 1:3295382ddc81 | 47 | spi.frequency(1000000); // 1MHz is this the minimum? |
summers | 1:3295382ddc81 | 48 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 49 | if (spi.write(0x1E)!=0xfe) { |
summers | 2:b6a0d4ba24a1 | 50 | pc.printf("Error reseting the device\r\n"); |
summers | 2:b6a0d4ba24a1 | 51 | } |
summers | 1:3295382ddc81 | 52 | wait_ms(3); // give time for ROM to reload 2.8ms by the spec |
summers | 2:b6a0d4ba24a1 | 53 | cs.write(1); // close the connection - does it finish the command? |
summers | 2:b6a0d4ba24a1 | 54 | wait_us(10); // Pause after putting CSB high |
summers | 1:3295382ddc81 | 55 | for (int i=0; i<8; i++) { |
summers | 2:b6a0d4ba24a1 | 56 | cs.write(0); // enable the SC to start a command. |
summers | 2:b6a0d4ba24a1 | 57 | if (spi.write(0xA0|(i<<1))!=0xfe) { |
summers | 2:b6a0d4ba24a1 | 58 | pc.printf("Error reading prom(%i)\r\n",i); |
summers | 2:b6a0d4ba24a1 | 59 | } |
summers | 1:3295382ddc81 | 60 | register uint8_t promh=spi.write(0x00); |
summers | 1:3295382ddc81 | 61 | register uint8_t proml=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 62 | cs.write(1); // disable CS to finish the command |
summers | 1:3295382ddc81 | 63 | PROM[i]=((promh<<8)|(proml)); |
summers | 3:a8c38c3fe967 | 64 | crc|=(i==7)?(PROM[i]&0xff00):PROM[i]; // Note AN520 gives this bit mask |
summers | 2:b6a0d4ba24a1 | 65 | for (int j=0; j<16; j++) { |
summers | 2:b6a0d4ba24a1 | 66 | crc=crc<<1; |
summers | 2:b6a0d4ba24a1 | 67 | if (crc&0x1000000) { // implimt the CRC4 algorithm 0x13 - |
summers | 3:a8c38c3fe967 | 68 | crc^=0x1300000; // This won't do the last 4 bits of PROM7 |
summers | 2:b6a0d4ba24a1 | 69 | } |
summers | 2:b6a0d4ba24a1 | 70 | } |
summers | 2:b6a0d4ba24a1 | 71 | wait_us(10); // Pause probably not stricly needed, but after switching CSB high give a bit of time. |
summers | 1:3295382ddc81 | 72 | } |
summers | 2:b6a0d4ba24a1 | 73 | if ((crc>>20)!=(PROM[7]&0xf)) |
summers | 2:b6a0d4ba24a1 | 74 | pc.printf("CRC check sum: %x vs recorded %x\r\n",crc>>20,PROM[7]&0xf); |
summers | 1:3295382ddc81 | 75 | } |
summers | 1:3295382ddc81 | 76 | |
summers | 2:b6a0d4ba24a1 | 77 | void MS5611(uint32_t *D1, uint32_t *D2, int os) |
summers | 1:3295382ddc81 | 78 | { |
summers | 1:3295382ddc81 | 79 | // cs.write(0); |
summers | 1:3295382ddc81 | 80 | spi.format(8,0); |
summers | 1:3295382ddc81 | 81 | spi.frequency(1000000); |
summers | 2:b6a0d4ba24a1 | 82 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 83 | if (spi.write(0x1E)!=0xfe) { // and reset the device |
summers | 2:b6a0d4ba24a1 | 84 | pc.printf("Error reseting the device\r\n"); |
summers | 2:b6a0d4ba24a1 | 85 | } |
summers | 2:b6a0d4ba24a1 | 86 | wait_ms(3); // give time for ROM to reload 2.8ms by the spec |
summers | 2:b6a0d4ba24a1 | 87 | cs.write(1); // close the connection - does it finish the command? |
summers | 2:b6a0d4ba24a1 | 88 | wait_us(10); // Pause after putting CSB high |
summers | 2:b6a0d4ba24a1 | 89 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 90 | if (spi.write(0x40|(os<<1))!=0xfe) { // D1 please |
summers | 2:b6a0d4ba24a1 | 91 | pc.printf("Error asking for D1\r\n"); |
summers | 2:b6a0d4ba24a1 | 92 | } |
summers | 2:b6a0d4ba24a1 | 93 | cs.write(1); // Disable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 94 | wait_us(600*(1<<os)); // pause for read, longer when oversampling |
summers | 2:b6a0d4ba24a1 | 95 | cs.write(0); // Enable the MS5611 intercae |
summers | 3:a8c38c3fe967 | 96 | if(spi.write(0x00)!=0xfe) { // can I have the result?; |
summers | 3:a8c38c3fe967 | 97 | pc.printf("Error reading D1\r\n"); |
summers | 3:a8c38c3fe967 | 98 | } |
summers | 2:b6a0d4ba24a1 | 99 | register uint8_t d1h=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 100 | register uint8_t d1m=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 101 | register uint8_t d1l=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 102 | cs.write(1); // and terminate the command |
summers | 2:b6a0d4ba24a1 | 103 | wait_us(10); // Pause after putting CSB high |
summers | 2:b6a0d4ba24a1 | 104 | cs.write(0); // Enable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 105 | if (spi.write(0x50|(os<<1))!=0xfe) { // D2 please |
summers | 2:b6a0d4ba24a1 | 106 | pc.printf("Error asking for D2\r\n"); |
summers | 2:b6a0d4ba24a1 | 107 | } |
summers | 2:b6a0d4ba24a1 | 108 | cs.write(1); // Disable the MS5611 intercae |
summers | 2:b6a0d4ba24a1 | 109 | wait_us(600*(1<<os)); // pause for read, longer when oversampling |
summers | 2:b6a0d4ba24a1 | 110 | cs.write(0); // Enable the MS5611 intercae |
summers | 3:a8c38c3fe967 | 111 | if(spi.write(0x00)!=0xfe) { // can I have the result?; |
summers | 3:a8c38c3fe967 | 112 | pc.printf("Error reading D2\r\n"); |
summers | 3:a8c38c3fe967 | 113 | } |
summers | 2:b6a0d4ba24a1 | 114 | register uint8_t d2h=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 115 | register uint8_t d2m=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 116 | register uint8_t d2l=spi.write(0x00); |
summers | 2:b6a0d4ba24a1 | 117 | cs.write(1); // and terminate the command |
summers | 2:b6a0d4ba24a1 | 118 | *D1=((uint32_t) d1h<<16)|((uint32_t) d1m<<8)|((uint32_t) d1l); |
summers | 2:b6a0d4ba24a1 | 119 | *D2=((uint32_t) d2h<<16)|((uint32_t) d2m<<8)|((uint32_t) d2l); |
summers | 2:b6a0d4ba24a1 | 120 | } |
summers | 2:b6a0d4ba24a1 | 121 | |
summers | 2:b6a0d4ba24a1 | 122 | void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P) |
summers | 2:b6a0d4ba24a1 | 123 | { |
summers | 3:a8c38c3fe967 | 124 | int64_t dt=((int64_t) D2)-(((int64_t) PROM[5])<<8); |
summers | 2:b6a0d4ba24a1 | 125 | *T=2000.0+((double) dt*PROM[6])/8388608.0; |
summers | 3:a8c38c3fe967 | 126 | int64_t off=(((int64_t) PROM[2])<<16)+((((int64_t) PROM[4])*dt)>>7); |
summers | 3:a8c38c3fe967 | 127 | int64_t sens=(((int64_t) PROM[1])<<15)+((((int64_t) PROM[3])*dt)>>8); |
summers | 3:a8c38c3fe967 | 128 | *P=(((double) D1)*((double) sens)/2097152.0-((double) off))/32768.0; |
summers | 0:9146d1e52be0 | 129 | } |