Working code for pc app 12/01/2018 commit
Dependencies: mbed MS5607 mbed-dsp
Fork of Turrentine_Code by
main.cpp
- Committer:
- AlexStokoe
- Date:
- 2018-02-13
- Revision:
- 6:8f16c432964d
- Parent:
- 2:3d3e21c907e4
File content as of revision 6:8f16c432964d:
#include "mbed.h" #define M_PI 3.14159265358979323846 #define N_SAMPLES 1024 //mbed class def Serial pc(USBTX, USBRX); // tx, rx SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); PwmOut motorOn(p26); Timer t; //variable instaniation unsigned short calData[6]; char buffer16[3]; short int serbuffer[2]; char tempBuffer[4]; unsigned int temp = 0; char pressBuffer[4]; unsigned int press = 0; unsigned int pData[N_SAMPLES]; float spData[N_SAMPLES]; unsigned int tData[N_SAMPLES]; const char cb1 = 0xA2; const char cb2 = 0xA4; const char cb3 = 0xA6; const char cb4 = 0xA8; const char cb5 = 0xAA; const char cb6 = 0xAC; const char * commarr[6] = {&cb1, &cb2, &cb3, &cb4, &cb5, &cb6}; const char D1conv256 = 0x40; const char D1conv512 = 0x42; const char D2conv4096 = 0x58; const char D2conv512 = 0x52; const char readADC = 0x00; float duty = 1; int round(float number) { return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); } //calculate temperature int calcT(unsigned int Tval, unsigned short consts[6]) { int dT = Tval - consts[4]*(2<<7); int T = 2000 + dT*consts[5]/(2<<22); printf("Temp: %d C\n", T); return T; } //calculate 1st order temperature compensated pressure int calcP(unsigned int Tval, unsigned int Pval, unsigned short consts[6]) { int dT = Tval - consts[4]*(2<<7); long long int off = (long long)consts[1]*(2<<16) + ((long long)consts[3] *(long long)dT)/(2<<5); long long int sens = (long long)consts[0]*(2<<15) + ((long long)consts[2] *(long long)dT)/(2<<6); int P = (Pval *(sens/(2<<20)) - off)/(2<<14); //printf("Pressure %d Pa\n\r", P); return P; } int main() { pc.baud(115200); motorOn = 0; motorOn.period_ms(10); printf("Turrentine\n"); // Chip must be deselected cs = 1; // Setup the spi for 8 bit data, high steady state clock, // second edge capture, with a 1MHz clock rate spi.format(8,3); spi.frequency(500000); spi.set_default_write_value(0x00); // Select the device by seting chip select low cs = 0; // Send 0x1E Command to reset the chip spi.write(0x1E); cs = 1; wait_ms(100); cs = 1; printf("Read cal data\n"); //read cal data values from device into program for (char i=0; i<6; i++) { //spi read sequence cs= 0; spi.write(commarr[i], 1, buffer16, 3); //time for SPI to write into data buffers wait_ms(10); cs=1; //Put data into 16bit unsigned int in calData array calData[i] = buffer16[1]<<8 | buffer16[2]; } printf("Pump On\n"); //turn pump on motorOn.write(duty); //dummy samples to wait for sensor response to stabilise int a =0; while(a < 10000) { cs = 0; spi.write(&D1conv256, 1, buffer16, 1); wait_us(500); cs = 1; cs = 0; spi.write(&readADC, 1, pressBuffer, 4); cs = 1; a++; } printf("Start loop\n"); //program loop while(1) { //read temerature value cs = 0; spi.write(&D2conv4096, 1, buffer16, 1); wait_ms(10); cs = 1; cs = 0; spi.write(&readADC, 1, tempBuffer, 4); cs = 1; //write values from buffers to program variables temp = tempBuffer[1]<<16 | tempBuffer[2]<<8 | tempBuffer[3]; int a =0; while(a < 5000) { cs = 0; spi.write(&D1conv256, 1, buffer16, 1); wait_us(500); cs = 1; cs = 0; spi.write(&readADC, 1, pressBuffer, 4); cs = 1; a++; } printf("Start Measurement\n"); t.reset(); t.start(); //loop values for (int x = 0; x < N_SAMPLES; x++) { cs = 0; spi.write(&D1conv256, 1, buffer16, 1); wait_us(500); cs = 1; cs = 0; if (x > 0) { pData[x-1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3]; } spi.write(&readADC, 1, pressBuffer, 4); tData[x] = t.read_us(); cs = 1; } pData[N_SAMPLES - 1] = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3]; //stop timer t.stop(); printf("Stop Measurement\n"); //calc temperature calcT(temp, calData); long long sum = 0; //output data to PC printf("$CAL %hu %hu %hu %hu %hu %hu\n", calData[0], calData[1], calData[2], calData[3], calData[4], calData[5]); for(int i = 0; i < N_SAMPLES; i++) { printf("$%d\n", pData[i]); } printf("$COMPLETE\n"); } }