ECG_ADS1198

Dependencies:   ADSlib mbed SDFileSystem

This was created for an internship project at IIT hyderabad.

Committer:
joelbandi
Date:
Fri May 01 20:26:20 2015 +0000
Revision:
5:4bcb26d018f8
Parent:
4:dee470044b03
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joelbandi 0:194165295aac 1 #include "mbed.h"
joelbandi 4:dee470044b03 2 #include "ADSlib/ADSlib.h"
joelbandi 3:86eef673aec5 3 #include "SDFileSystem/SDFileSystem.h"
joelbandi 0:194165295aac 4
joelbandi 3:86eef673aec5 5 /***********************PROTOTYPING AND SETTINGS*********************/
joelbandi 2:86845af4bcac 6 DigitalOut CLKSEL(p8);
joelbandi 3:86eef673aec5 7 DigitalOut ADS1CS(p16);
joelbandi 3:86eef673aec5 8 DigitalOut ADS2CS(p17);
joelbandi 5:4bcb26d018f8 9 DigitalIn MULT_ADS(p18);
joelbandi 2:86845af4bcac 10 DigitalOut PWDN(p10);
joelbandi 2:86845af4bcac 11 DigitalOut RESETpin(p9);
joelbandi 3:86eef673aec5 12 DigitalOut STARTpin(p19);
joelbandi 4:dee470044b03 13 DigitalOut waitled(LED1);
joelbandi 2:86845af4bcac 14 DigitalIn DRDY(p15);
joelbandi 2:86845af4bcac 15 SPI spi(p5,p6,p7);
joelbandi 3:86eef673aec5 16 SDFileSystem sd(p11,p12,p13,p14,"sd");
joelbandi 5:4bcb26d018f8 17 signed char Input_Data_Stream[];
joelbandi 5:4bcb26d018f8 18 uint16_t Input_modified[];
joelbandi 3:86eef673aec5 19 LocalFileSystem local("local");
joelbandi 3:86eef673aec5 20 /********************************************************************/
joelbandi 2:86845af4bcac 21
joelbandi 4:dee470044b03 22 int main() // main call
joelbandi 2:86845af4bcac 23 {
joelbandi 2:86845af4bcac 24 /********************POWER ON SEQUENCING *****************************/
joelbandi 4:dee470044b03 25 waitled =1;
joelbandi 4:dee470044b03 26 wait_ms(500); //boot up time
joelbandi 2:86845af4bcac 27
joelbandi 4:dee470044b03 28 ADS1CS=1; //deselect ADS1
joelbandi 2:86845af4bcac 29 spi.format(8,3); //spi setttings
joelbandi 2:86845af4bcac 30 spi.frequency(1000000); //spi settings
joelbandi 2:86845af4bcac 31
joelbandi 2:86845af4bcac 32 CLKSEL = 1; //clock signal (internal) setup Frequency = 2.048 MHz
joelbandi 2:86845af4bcac 33 ADS1CS= 0;
joelbandi 2:86845af4bcac 34 spi.write(W_CONFIG1);
joelbandi 2:86845af4bcac 35 spi.write(0x00);
joelbandi 2:86845af4bcac 36 spi.write(0x64);
joelbandi 2:86845af4bcac 37 ADS1CS =1;
joelbandi 2:86845af4bcac 38
joelbandi 2:86845af4bcac 39 PWDN = 1;
joelbandi 2:86845af4bcac 40 RESETpin = 1;
joelbandi 2:86845af4bcac 41 wait(1); //Delay for Power-On Reset and Oscillator Start-Up
joelbandi 2:86845af4bcac 42
joelbandi 2:86845af4bcac 43 ADS1CS= 0; // intial reset command for device reset and register setting clean up
joelbandi 2:86845af4bcac 44 spi.write(RESET);
joelbandi 2:86845af4bcac 45 pause_clk(18);
joelbandi 2:86845af4bcac 46
joelbandi 2:86845af4bcac 47
joelbandi 2:86845af4bcac 48 spi.write(SDATAC); // device boots up in RDATAC mode set it in SDATAC mode
joelbandi 2:86845af4bcac 49 pause_clk(4);
joelbandi 2:86845af4bcac 50
joelbandi 2:86845af4bcac 51
joelbandi 2:86845af4bcac 52 spi.write(W_CONFIG3); // internal reference stup and enable
joelbandi 2:86845af4bcac 53 spi.write(0x00);
joelbandi 2:86845af4bcac 54 spi.write(0xC0);
joelbandi 2:86845af4bcac 55
joelbandi 2:86845af4bcac 56 /***************POWER ON SEQUENCING COMPLETE**************************/
joelbandi 2:86845af4bcac 57
joelbandi 2:86845af4bcac 58
joelbandi 2:86845af4bcac 59 /**************CHANNEL SETTINGS***************************************/
joelbandi 2:86845af4bcac 60 spi.write(W_CHnSET);
joelbandi 2:86845af4bcac 61 spi.write(0x07);
joelbandi 2:86845af4bcac 62 spi.write((int)ADS_Default_Channel_Settings);
joelbandi 2:86845af4bcac 63 ADS1CS = 1;
joelbandi 2:86845af4bcac 64 /*********************************************************************/
joelbandi 2:86845af4bcac 65
joelbandi 2:86845af4bcac 66 /*****************************CONVERSION BEGINS**********************/
joelbandi 2:86845af4bcac 67 STARTpin= 1;
joelbandi 2:86845af4bcac 68
joelbandi 2:86845af4bcac 69 //write main superloop and rdatac mode and enable dout conversion AND SETUP FILESTORAFGE
joelbandi 4:dee470044b03 70 Timer tensec;
joelbandi 4:dee470044b03 71 tensec.start();
joelbandi 5:4bcb26d018f8 72
joelbandi 5:4bcb26d018f8 73
joelbandi 5:4bcb26d018f8 74
joelbandi 5:4bcb26d018f8 75
joelbandi 5:4bcb26d018f8 76 if (MULT_ADS = 0) {
joelbandi 5:4bcb26d018f8 77 while(tensec.read()<=10) {
joelbandi 5:4bcb26d018f8 78 spi.write(RDATAC);
joelbandi 5:4bcb26d018f8 79 wait_ms(10);
joelbandi 5:4bcb26d018f8 80 while(DRDY) {}
joelbandi 5:4bcb26d018f8 81 ADS1CS= 0;
joelbandi 5:4bcb26d018f8 82 for (int i=0; i<=18; i++) {
joelbandi 5:4bcb26d018f8 83 Input_Data_Stream[i] = spi.write(0x00);
joelbandi 5:4bcb26d018f8 84 }
joelbandi 4:dee470044b03 85 int k= 0;
joelbandi 4:dee470044b03 86 int j= 4;
joelbandi 5:4bcb26d018f8 87 for(int i=3; i<=17; i++) {
joelbandi 5:4bcb26d018f8 88 Input_modified[k]=Input_Data_Stream[i]<<8 + Input_Data_Stream[j];
joelbandi 5:4bcb26d018f8 89 j++;
joelbandi 5:4bcb26d018f8 90 k++;
joelbandi 5:4bcb26d018f8 91 }
joelbandi 5:4bcb26d018f8 92 Input_modified[8]=Input_modified[7]-Input_modified[6];
joelbandi 5:4bcb26d018f8 93 Input_modified[9]=-(Input_modified[7]+Input_modified[6]/2);
joelbandi 5:4bcb26d018f8 94 Input_modified[10]=((2*Input_modified[6])-Input_modified[7])/2;
joelbandi 5:4bcb26d018f8 95 Input_modified[11]=((2*Input_modified[7])-Input_modified[6])/2;
joelbandi 5:4bcb26d018f8 96 spi.write(SDATAC);
joelbandi 2:86845af4bcac 97
joelbandi 5:4bcb26d018f8 98 /***********************FILE IO PROCEDURE ON LOCAL STORAGE******************/
joelbandi 5:4bcb26d018f8 99 FILE* file = fopen("local/logfile.txt","w");
joelbandi 5:4bcb26d018f8 100 for (int k =0; k<12; k++) {
joelbandi 5:4bcb26d018f8 101 fputc(Input_modified[k],file);
joelbandi 5:4bcb26d018f8 102 }
joelbandi 5:4bcb26d018f8 103 //dont forget to
joelbandi 5:4bcb26d018f8 104 //fclose(file);
joelbandi 5:4bcb26d018f8 105 /***************************************************************************/
joelbandi 3:86eef673aec5 106
joelbandi 3:86eef673aec5 107
joelbandi 3:86eef673aec5 108 ///////////////////////////OR///////////////////////////////////////////////////////////////////////
joelbandi 3:86eef673aec5 109
joelbandi 3:86eef673aec5 110
joelbandi 5:4bcb26d018f8 111 /****************************SD I/O OPERATIONS *****************************/
joelbandi 5:4bcb26d018f8 112 mkdir("sd/logfiledir",0777);
joelbandi 5:4bcb26d018f8 113 FILE* file1 = fopen("sd/logfiledir/logfile.txt","w");
joelbandi 5:4bcb26d018f8 114 for (int k =0; k<12; k++) {
joelbandi 5:4bcb26d018f8 115 fputc(Input_modified[k],file1);
joelbandi 5:4bcb26d018f8 116 }
joelbandi 5:4bcb26d018f8 117 //dont forget to
joelbandi 5:4bcb26d018f8 118 //fclose(file1);
joelbandi 5:4bcb26d018f8 119 /***************************************************************************/
joelbandi 4:dee470044b03 120 }
joelbandi 5:4bcb26d018f8 121 }
joelbandi 5:4bcb26d018f8 122
joelbandi 5:4bcb26d018f8 123
joelbandi 5:4bcb26d018f8 124 if (MULT_ADS = 0) {
joelbandi 5:4bcb26d018f8 125 while(tensec.read()<=10) {
joelbandi 5:4bcb26d018f8 126 spi.write(RDATAC);
joelbandi 5:4bcb26d018f8 127 wait_ms(10);
joelbandi 5:4bcb26d018f8 128 while(DRDY) {}
joelbandi 5:4bcb26d018f8 129 ADS1CS= 0;
joelbandi 5:4bcb26d018f8 130 for (int i=0; i<=18; i++) {
joelbandi 5:4bcb26d018f8 131 Input_Data_Stream[i] = spi.write(0x00);
joelbandi 5:4bcb26d018f8 132 }
joelbandi 5:4bcb26d018f8 133 int k= 0;
joelbandi 5:4bcb26d018f8 134 int j= 4;
joelbandi 5:4bcb26d018f8 135 for(int i=3; i<=17; i++) {
joelbandi 5:4bcb26d018f8 136 Input_modified[k]=Input_Data_Stream[i]<<8 + Input_Data_Stream[j];
joelbandi 5:4bcb26d018f8 137 j++;
joelbandi 5:4bcb26d018f8 138 k++;
joelbandi 5:4bcb26d018f8 139 }
joelbandi 5:4bcb26d018f8 140 Input_modified[8]=Input_modified[7]-Input_modified[6];
joelbandi 5:4bcb26d018f8 141 Input_modified[9]=-(Input_modified[7]+Input_modified[6]/2);
joelbandi 5:4bcb26d018f8 142 Input_modified[10]=((2*Input_modified[6])-Input_modified[7])/2;
joelbandi 5:4bcb26d018f8 143 Input_modified[11]=((2*Input_modified[7])-Input_modified[6])/2;
joelbandi 5:4bcb26d018f8 144 spi.write(SDATAC);
joelbandi 5:4bcb26d018f8 145
joelbandi 5:4bcb26d018f8 146 /***********************FILE IO PROCEDURE ON LOCAL STORAGE******************/
joelbandi 5:4bcb26d018f8 147 FILE* file = fopen("local/logfile.txt","w");
joelbandi 5:4bcb26d018f8 148 for (int k =0; k<12; k++) {
joelbandi 5:4bcb26d018f8 149 fputc(Input_modified[k],file);
joelbandi 5:4bcb26d018f8 150 }
joelbandi 5:4bcb26d018f8 151 //dont forget to
joelbandi 5:4bcb26d018f8 152 //fclose(file);
joelbandi 5:4bcb26d018f8 153 /***************************************************************************/
joelbandi 5:4bcb26d018f8 154
joelbandi 5:4bcb26d018f8 155
joelbandi 5:4bcb26d018f8 156 ///////////////////////////OR///////////////////////////////////////////////////////////////////////
joelbandi 5:4bcb26d018f8 157
joelbandi 5:4bcb26d018f8 158
joelbandi 5:4bcb26d018f8 159 /****************************SD I/O OPERATIONS *****************************/
joelbandi 5:4bcb26d018f8 160 mkdir("sd/logfiledir",0777);
joelbandi 5:4bcb26d018f8 161 FILE* file1 = fopen("sd/logfiledir/logfile.txt","w");
joelbandi 5:4bcb26d018f8 162 for (int k =0; k<12; k++) {
joelbandi 5:4bcb26d018f8 163 fputc(Input_modified[k],file1);
joelbandi 5:4bcb26d018f8 164 }
joelbandi 5:4bcb26d018f8 165 //dont forget to
joelbandi 5:4bcb26d018f8 166 //fclose(file1);
joelbandi 5:4bcb26d018f8 167 /***************************************************************************/
joelbandi 5:4bcb26d018f8 168 }
joelbandi 3:86eef673aec5 169 }
joelbandi 5:4bcb26d018f8 170
joelbandi 5:4bcb26d018f8 171
joelbandi 5:4bcb26d018f8 172
joelbandi 5:4bcb26d018f8 173
joelbandi 5:4bcb26d018f8 174
joelbandi 5:4bcb26d018f8 175
joelbandi 5:4bcb26d018f8 176
joelbandi 5:4bcb26d018f8 177
joelbandi 5:4bcb26d018f8 178
joelbandi 5:4bcb26d018f8 179
joelbandi 5:4bcb26d018f8 180
joelbandi 5:4bcb26d018f8 181
joelbandi 5:4bcb26d018f8 182
joelbandi 5:4bcb26d018f8 183
joelbandi 5:4bcb26d018f8 184
joelbandi 5:4bcb26d018f8 185
joelbandi 5:4bcb26d018f8 186
joelbandi 5:4bcb26d018f8 187
joelbandi 5:4bcb26d018f8 188
joelbandi 5:4bcb26d018f8 189
joelbandi 5:4bcb26d018f8 190
joelbandi 5:4bcb26d018f8 191
joelbandi 5:4bcb26d018f8 192
joelbandi 5:4bcb26d018f8 193
joelbandi 5:4bcb26d018f8 194
joelbandi 5:4bcb26d018f8 195
joelbandi 5:4bcb26d018f8 196
joelbandi 5:4bcb26d018f8 197
joelbandi 5:4bcb26d018f8 198
joelbandi 5:4bcb26d018f8 199
joelbandi 5:4bcb26d018f8 200
joelbandi 5:4bcb26d018f8 201
joelbandi 5:4bcb26d018f8 202
joelbandi 5:4bcb26d018f8 203
joelbandi 5:4bcb26d018f8 204
joelbandi 5:4bcb26d018f8 205
joelbandi 5:4bcb26d018f8 206
joelbandi 5:4bcb26d018f8 207
joelbandi 5:4bcb26d018f8 208
joelbandi 5:4bcb26d018f8 209
joelbandi 5:4bcb26d018f8 210
joelbandi 5:4bcb26d018f8 211
joelbandi 5:4bcb26d018f8 212
joelbandi 5:4bcb26d018f8 213
joelbandi 5:4bcb26d018f8 214
joelbandi 5:4bcb26d018f8 215
joelbandi 5:4bcb26d018f8 216
joelbandi 5:4bcb26d018f8 217
joelbandi 5:4bcb26d018f8 218
joelbandi 5:4bcb26d018f8 219
joelbandi 5:4bcb26d018f8 220
joelbandi 5:4bcb26d018f8 221
joelbandi 5:4bcb26d018f8 222
joelbandi 4:dee470044b03 223 tensec.stop();
joelbandi 4:dee470044b03 224 waitled =0;
joelbandi 2:86845af4bcac 225 }
joelbandi 2:86845af4bcac 226
joelbandi 2:86845af4bcac 227
joelbandi 2:86845af4bcac 228