Working code for pc app 12/01/2018 commit

Dependencies:   mbed MS5607 mbed-dsp

Fork of Turrentine_Code by Alex Stokoe

main.cpp

Committer:
AlexStokoe
Date:
2018-01-12
Revision:
0:2c6d81be69d8
Child:
1:ab3dacbfcde6

File content as of revision 0:2c6d81be69d8:

#include "mbed.h"

#define M_PI 3.14159265358979323846

//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[1000];
float spData[1000];
unsigned int tData[1000];

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\r", 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);
    
    // 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;
    
    //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];        
        }
        
    //display calibration values to check against datasheet
    printf("C1: %hu\n\rC2: %hu\n\rC3: %hu\n\rC4: %hu\n\rC5: %hu\n\rC6: %hu\n\r", calData[0], calData[1], calData[2], calData[3], calData[4], calData[5]);
    
    //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;
        
        //read pressure value
        cs = 0;
        spi.write(&D1conv512, 1, buffer16, 1);
        wait_ms(2);
        cs = 1; cs = 0;
        
        spi.write(&readADC, 1, pressBuffer, 4);
        cs = 1;
        
        //write values from buffers to program variables
        temp = tempBuffer[1]<<16 | tempBuffer[2]<<8 | tempBuffer[3];
        press = pressBuffer[1]<<16 | pressBuffer[2]<<8 | pressBuffer[3];
        
        //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++;
            }
            
        
        int nsample = 1000;
        double cumsum = 0;
        
        t.reset();
        t.start();
        //loop values
        for (int x=0; x <nsample; 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];
                cumsum = cumsum + pData[x-1];
                }
            spi.write(&readADC, 1, pressBuffer, 4);
            tData[x] = t.read_us();
            cs = 1;
        
            }
            
        //stop motor and timer
        t.stop();
        motorOn.write(0);
        
        //calc temperature
        calcT(temp, calData);
        long long sum = 0;
        
        float avg = calcP(temp, cumsum/(nsample-1), calData);
        
        printf("avg %f\n\r", avg); 
        
        //for crossing detection
        bool cflag1 = (avg > pData[0]);
        bool cflag2 = (avg > pData[1]);
        int numcross = 0;
        int lastcrosspoint = 0;
        unsigned int crosstart;
        unsigned int crossend;
        float freq = 0;
        
        //smooth data array 20 sample size
        for (int y=0; y< nsample-1; y++){
            if (9< y && y <nsample-10){
                sum = 0;
                for (int z=0; z<20; z++){
                    sum = sum + pData[y-10+z];
                    }
                    
                spData[y] = (float) calcP(temp, sum/20, calData);
                
                //detect avg crossings for frequency calc
                if (y>10){
                    cflag2 = (avg > spData[y]);
                    cflag1 = (avg > spData[y-1]);
                    
                    if (cflag1 != cflag2){
                        if(lastcrosspoint<=(y-10)){
                            numcross++;
                            lastcrosspoint = y;
                            
                            if (numcross == 1){
                                crosstart = tData[y];
                                }
                                
                            crossend = tData[y];
                            }
                        }   
                    }      
                //calc pressure & display list
                //printf("%d\t%d\t%d\n\r", tData[y], calcP(temp, spData[y], calData), calcP(temp, pData[y], calData));
                printf("%d\t%f\n\r", tData[y],spData[y]);
                } else if (10> y || y >nsample-10) {
                    spData[y] = (float) calcP(temp, pData[y], calData);
                    }
            
            
            }
        
        printf("numcross = %d\n\r", numcross);
        printf("crosstart = %d\n\r", crosstart);
        printf("crossend = %d\n\r", crossend);
            
        freq = (1e6 *numcross)/(2*(crossend - crosstart));
        printf("frequency = %f\n\r", freq);
        
        for (int z=0; z< nsample-1; z++){
            
            spData[z] = spData[z] -avg;
            }
        
        printf("Repeat Blockage Test?\n\rPress Enter to continue\n\r");
        
        while(1){             
            if (pc.getc() != 0) {
                break;
                }
            }
            
        }
}