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
- Committer:
- summers
- Date:
- 2018-03-17
- Revision:
- 4:e0dbf2bdb967
- Parent:
- 3:a8c38c3fe967
- Child:
- 5:630aefee388b
File content as of revision 4:e0dbf2bdb967:
#include "mbed.h" #include "main.h" Serial pc(SERIAL_TX, SERIAL_RX, 115200); // tx, rx //DigitalOut led(LED_RED); //SPI spi(PTC6,PTC7,PTD1,PTC0); //SPI spi(PTC6,PTC7,PTD1); // my prefered, but mixed SPI spi(PTC6,PTC7,PTC5); // PTC5 - the LED //SPI spi(PTD2,PTD3,PTD1); DigitalOut cs(PTC0); int main() { int PROM[8]; uint32_t D1,D2; double T,P; cs.write(1); // disable all SPI pc.printf("Hello World!\r\n"); MS5611_init(PROM); for (int i=0; i<8; i++) { pc.printf("Prom(%i): %i\r\n",i,PROM[i]); } while (1) { // the third paramater gives over samping // 0 - 256 // 1 - 512 // 2 - 1024 // 3 - 2048 // 4 - 4096 MS5611(&D1,&D2,4); pc.printf("%i\t%i\t",D1,D2); MS5611_phys(D1,D2,PROM,&T,&P); pc.printf("%f\t",T/100.0); pc.printf("%f\r\n",P/100.0); wait(1.0); } } void MS5611_init(int *PROM) { uint32_t crc=0; spi.format(8,0); // 8 bit mode 0 - this is default anyway but good to code. spi.frequency(1000000); // 1MHz is this the minimum? cs.write(0); // Enable the MS5611 intercae if (spi.write(0x1E)!=0xfe) { pc.printf("Error reseting the device\r\n"); } wait_ms(3); // give time for ROM to reload 2.8ms by the spec cs.write(1); // close the connection - does it finish the command? wait_us(10); // Pause after putting CSB high for (int i=0; i<8; i++) { cs.write(0); // enable the SC to start a command. if (spi.write(0xA0|(i<<1))!=0xfe) { pc.printf("Error reading prom(%i)\r\n",i); } register uint8_t promh=spi.write(0x00); register uint8_t proml=spi.write(0x00); cs.write(1); // disable CS to finish the command PROM[i]=((promh<<8)|(proml)); crc|=(i==7)?(PROM[i]&0xff00):PROM[i]; // Note AN520 gives this bit mask for (int j=0; j<16; j++) { crc=crc<<1; if (crc&0x1000000) { // implimt the CRC4 algorithm 0x13 - crc^=0x1300000; // This won't do the last 4 bits of PROM7 } } wait_us(10); // Pause probably not stricly needed, but after switching CSB high give a bit of time. } if ((crc>>20)!=(PROM[7]&0xf)) pc.printf("CRC check sum: %x vs recorded %x\r\n",crc>>20,PROM[7]&0xf); } void MS5611(uint32_t *D1, uint32_t *D2, int os) { // cs.write(0); spi.format(8,0); spi.frequency(1000000); cs.write(0); // Enable the MS5611 intercae if (spi.write(0x1E)!=0xfe) { // and reset the device pc.printf("Error reseting the device\r\n"); } wait_ms(3); // give time for ROM to reload 2.8ms by the spec cs.write(1); // close the connection - does it finish the command? wait_us(10); // Pause after putting CSB high cs.write(0); // Enable the MS5611 intercae if (spi.write(0x40|(os<<1))!=0xfe) { // D1 please pc.printf("Error asking for D1\r\n"); } cs.write(1); // Disable the MS5611 intercae wait_us(600*(1<<os)); // pause for read, longer when oversampling cs.write(0); // Enable the MS5611 intercae if(spi.write(0x00)!=0xfe) { // can I have the result?; pc.printf("Error reading D1\r\n"); } *D1=((uint32_t) spi.write(0x00))<<16; *D1|=((uint32_t) spi.write(0x00))<<8; *D1|=((uint32_t) spi.write(0x00)); cs.write(1); // and terminate the command wait_us(10); // Pause after putting CSB high cs.write(0); // Enable the MS5611 intercae if (spi.write(0x50|(os<<1))!=0xfe) { // D2 please pc.printf("Error asking for D2\r\n"); } cs.write(1); // Disable the MS5611 intercae wait_us(600*(1<<os)); // pause for read, longer when oversampling cs.write(0); // Enable the MS5611 intercae if(spi.write(0x00)!=0xfe) { // can I have the result?; pc.printf("Error reading D2\r\n"); } *D2=((uint32_t) spi.write(0x00))<<16; *D2|=((uint32_t) spi.write(0x00))<<8; *D2|=((uint32_t) spi.write(0x00)); cs.write(1); // and terminate the command } void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P) { int64_t dt=((int64_t) D2)-(((int64_t) PROM[5])<<8); *T=2000.0+((double) dt*PROM[6])/8388608.0; int64_t off128=(((int64_t) PROM[2])<<23)+(((int64_t) PROM[4])*dt); int64_t sens256=(((int64_t) PROM[1])<<23)+(((int64_t) PROM[3])*dt); *P=(((double) D1)*((double) sens256)/4194304.0-((double) off128))/4194304.0; }