none

Dependencies:   mbed

Committer:
qiaolimaxim
Date:
Fri Apr 12 06:56:27 2019 +0000
Revision:
2:7d005ac4146f
max86150

Who changed what in which revision?

UserRevisionLine numberNew contents of line
qiaolimaxim 2:7d005ac4146f 1 #include "mbed.h"
qiaolimaxim 2:7d005ac4146f 2 #include "max86150.h"
qiaolimaxim 2:7d005ac4146f 3 #include "i2c_functions.h"
qiaolimaxim 2:7d005ac4146f 4
qiaolimaxim 2:7d005ac4146f 5 DigitalOut myled(P7_1);
qiaolimaxim 2:7d005ac4146f 6 Serial myPC(USBTX, USBRX); // Initiating Serial Console communication
qiaolimaxim 2:7d005ac4146f 7
qiaolimaxim 2:7d005ac4146f 8 void device_data_read(void);
qiaolimaxim 2:7d005ac4146f 9
qiaolimaxim 2:7d005ac4146f 10
qiaolimaxim 2:7d005ac4146f 11 int main()
qiaolimaxim 2:7d005ac4146f 12 {
qiaolimaxim 2:7d005ac4146f 13 int i=0, j=0;
qiaolimaxim 2:7d005ac4146f 14
qiaolimaxim 2:7d005ac4146f 15 i2c_freq(100000);
qiaolimaxim 2:7d005ac4146f 16 myPC.baud(57600);
qiaolimaxim 2:7d005ac4146f 17
qiaolimaxim 2:7d005ac4146f 18 while (i++<1) {
qiaolimaxim 2:7d005ac4146f 19 myled = !myled;
qiaolimaxim 2:7d005ac4146f 20 wait(1);
qiaolimaxim 2:7d005ac4146f 21
qiaolimaxim 2:7d005ac4146f 22 char data_read[9];
qiaolimaxim 2:7d005ac4146f 23 char data_write[2];
qiaolimaxim 2:7d005ac4146f 24
qiaolimaxim 2:7d005ac4146f 25 // enable()
qiaolimaxim 2:7d005ac4146f 26 data_write[0] = 0x01;
qiaolimaxim 2:7d005ac4146f 27 i2c_write(0xBC, 0x0D, data_write,1); // reset
qiaolimaxim 2:7d005ac4146f 28 data_write[0] = 0x0C;
qiaolimaxim 2:7d005ac4146f 29 i2c_write(0xBC, 0x08, data_write,1); // FIFO roll ON
qiaolimaxim 2:7d005ac4146f 30 data_write[0] = 0x21;
qiaolimaxim 2:7d005ac4146f 31 i2c_write(0xBC, 0x09, data_write,1); // FDx
qiaolimaxim 2:7d005ac4146f 32 data_write[0] = 0x00;
qiaolimaxim 2:7d005ac4146f 33 i2c_write(0xBC, 0x0A, data_write,1); // FDx
qiaolimaxim 2:7d005ac4146f 34 data_write[0] = 0xDB;
qiaolimaxim 2:7d005ac4146f 35 i2c_write(0xBC, 0x0E, data_write,1); // PPG_ADC_RANGE PPG_SR:100SpS, PPG_LED_PW:400uS
qiaolimaxim 2:7d005ac4146f 36 data_write[0] = 0x10;
qiaolimaxim 2:7d005ac4146f 37 i2c_write(0xBC, 0x11, data_write,1); //LED current - IR
qiaolimaxim 2:7d005ac4146f 38 data_write[0] = 0x55;
qiaolimaxim 2:7d005ac4146f 39 i2c_write(0xBC, 0x12, data_write,1); //LED current - Red
qiaolimaxim 2:7d005ac4146f 40 data_write[0] = 0x10;
qiaolimaxim 2:7d005ac4146f 41 i2c_write(0xBC, 0x15, data_write,1); //Pilot_PA
qiaolimaxim 2:7d005ac4146f 42 data_write[0] = 0x04;
qiaolimaxim 2:7d005ac4146f 43 i2c_write(0xBC, 0x10, data_write,1); //PROX_INT_THRESH
qiaolimaxim 2:7d005ac4146f 44
qiaolimaxim 2:7d005ac4146f 45 //data_write[0] = 0x40;
qiaolimaxim 2:7d005ac4146f 46 //i2c_write(0xBC, 0x02, data_write,1); //PPG_RDY_EN
qiaolimaxim 2:7d005ac4146f 47
qiaolimaxim 2:7d005ac4146f 48 data_write[0] = 0x10;
qiaolimaxim 2:7d005ac4146f 49 i2c_write(0xBC, 0x02, data_write,1); //[4] PROX_INT_EN
qiaolimaxim 2:7d005ac4146f 50 data_write[0] = 0x04;
qiaolimaxim 2:7d005ac4146f 51 i2c_write(0xBC, 0x0D, data_write,1); //FIFO start
qiaolimaxim 2:7d005ac4146f 52
qiaolimaxim 2:7d005ac4146f 53 myPC.printf("\r\nStart reading...");
qiaolimaxim 2:7d005ac4146f 54 while (j++<15) {
qiaolimaxim 2:7d005ac4146f 55 myPC.printf("\r\n#%d, ", j);
qiaolimaxim 2:7d005ac4146f 56
qiaolimaxim 2:7d005ac4146f 57 i2c_read(0xBC, 0x00, data_read, 2);
qiaolimaxim 2:7d005ac4146f 58 while(data_read[0]==0x00) {
qiaolimaxim 2:7d005ac4146f 59 myPC.printf("\r\nReg 0x00 = 0x%x, wait for INT", data_read[0]);
qiaolimaxim 2:7d005ac4146f 60 i2c_read(0xBC, 0x00, data_read, 2);
qiaolimaxim 2:7d005ac4146f 61 }
qiaolimaxim 2:7d005ac4146f 62 if (data_read[0] == 0x80) { // FIFO almost full
qiaolimaxim 2:7d005ac4146f 63 myPC.printf("\r\nReg 0x00 = 0x%x, read data", data_read[0]);
qiaolimaxim 2:7d005ac4146f 64 device_data_read();
qiaolimaxim 2:7d005ac4146f 65 }else if(data_read[0] == 0x10) { // Prox_INT
qiaolimaxim 2:7d005ac4146f 66 myPC.printf("\r\nReg 0x00 = 0x%x, Proximity Interrupt", data_read[0]);
qiaolimaxim 2:7d005ac4146f 67 data_write[0] = 0x00;
qiaolimaxim 2:7d005ac4146f 68 i2c_write(0xBC, 0x0D, data_write,1);
qiaolimaxim 2:7d005ac4146f 69 data_write[0] = 0x04;
qiaolimaxim 2:7d005ac4146f 70 i2c_write(0xBC, 0x0D, data_write,1);
qiaolimaxim 2:7d005ac4146f 71 data_write[0] = 0x80;
qiaolimaxim 2:7d005ac4146f 72 i2c_write(0xBC, 0x02, data_write,1);
qiaolimaxim 2:7d005ac4146f 73 }else if(data_read[0] == 0x01) { // Power Ready
qiaolimaxim 2:7d005ac4146f 74 myPC.printf("\r\nReg 0x00 = 0x%x, Power Ready", data_read[0]);
qiaolimaxim 2:7d005ac4146f 75 }else
qiaolimaxim 2:7d005ac4146f 76 myPC.printf("\r\nReg 0x00 = 0x%x, other INT event...", data_read[0]);
qiaolimaxim 2:7d005ac4146f 77 }
qiaolimaxim 2:7d005ac4146f 78 }
qiaolimaxim 2:7d005ac4146f 79 }
qiaolimaxim 2:7d005ac4146f 80
qiaolimaxim 2:7d005ac4146f 81 void device_data_read(void)
qiaolimaxim 2:7d005ac4146f 82 {
qiaolimaxim 2:7d005ac4146f 83 char wr_ptr;
qiaolimaxim 2:7d005ac4146f 84 char rd_ptr;
qiaolimaxim 2:7d005ac4146f 85 char flag = 0;
qiaolimaxim 2:7d005ac4146f 86 char dataBuff[32*2*3];
qiaolimaxim 2:7d005ac4146f 87 int sampleCnt = 0;
qiaolimaxim 2:7d005ac4146f 88 int red[32], ir[32], ecg[32];
qiaolimaxim 2:7d005ac4146f 89 int i;
qiaolimaxim 2:7d005ac4146f 90
qiaolimaxim 2:7d005ac4146f 91 i2c_read(0xBC, 0x04, &wr_ptr, 1);
qiaolimaxim 2:7d005ac4146f 92 i2c_read(0xBC, 0x06, &rd_ptr, 1);
qiaolimaxim 2:7d005ac4146f 93
qiaolimaxim 2:7d005ac4146f 94 if(wr_ptr > rd_ptr)
qiaolimaxim 2:7d005ac4146f 95 sampleCnt = wr_ptr - rd_ptr;
qiaolimaxim 2:7d005ac4146f 96 else
qiaolimaxim 2:7d005ac4146f 97 sampleCnt = 32 + wr_ptr - rd_ptr;
qiaolimaxim 2:7d005ac4146f 98 i2c_read(0xBC, 0x07, dataBuff, sampleCnt*2*3);
qiaolimaxim 2:7d005ac4146f 99
qiaolimaxim 2:7d005ac4146f 100 for(i=0;i<sampleCnt;i++){
qiaolimaxim 2:7d005ac4146f 101 ir[i] = ((dataBuff[i*6+0] << 16) + (dataBuff[i*6+1] << 8) + dataBuff[i*6+2]) & 0x7ffff;
qiaolimaxim 2:7d005ac4146f 102 red[i] = ((dataBuff[i*6+3] << 16) + (dataBuff[i*6+4] << 8) + dataBuff[i*6+5]) & 0x7ffff;
qiaolimaxim 2:7d005ac4146f 103 myPC.printf("\r\nIR = %d, RED = %d", ir[i], red[i]);
qiaolimaxim 2:7d005ac4146f 104 if(ir[i]<0x04*2048*0.8){
qiaolimaxim 2:7d005ac4146f 105 flag = 0x10; // PROX_ON
qiaolimaxim 2:7d005ac4146f 106 myPC.printf("\r\nIR = %d Enter PROX mode", ir[i]);
qiaolimaxim 2:7d005ac4146f 107 }
qiaolimaxim 2:7d005ac4146f 108 }
qiaolimaxim 2:7d005ac4146f 109 if(flag!=0)
qiaolimaxim 2:7d005ac4146f 110 i2c_write(0xBC, 0x02, &flag,1); // PROX_INT_EN
qiaolimaxim 2:7d005ac4146f 111 }