Interface between the mbed lpc1768 and the ads1298

Dependencies:   USBDevice mbed

Committer:
faithcerebral
Date:
Mon Apr 14 16:27:56 2014 +0000
Revision:
1:26b8b0e4d836
Parent:
0:bd3a560e245e
uncommented the test info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faithcerebral 0:bd3a560e245e 1 /**********************************************************************************************************************************
faithcerebral 0:bd3a560e245e 2 *changed the ISR function
faithcerebral 0:bd3a560e245e 3 *incuded 0x02 to make sure that 24 buts are sent
faithcerebral 0:bd3a560e245e 4 ****************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 5 // include files
faithcerebral 0:bd3a560e245e 6 #include "mbed.h"
faithcerebral 0:bd3a560e245e 7 #include "ADS1x9x.h"
faithcerebral 0:bd3a560e245e 8 #include "stdef.h"
faithcerebral 0:bd3a560e245e 9 #pragma diag_suppress 177 //suppress unused declared entities
faithcerebral 0:bd3a560e245e 10
faithcerebral 0:bd3a560e245e 11 /*******************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 12 Definitions
faithcerebral 0:bd3a560e245e 13 ********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 14 #define ADS1298_DATA_LENGTH 27
faithcerebral 0:bd3a560e245e 15 #define STAND_BY_MODE 0x04
faithcerebral 0:bd3a560e245e 16 #define REGISTER_DEFAULT_SETTINGS 0x00
faithcerebral 0:bd3a560e245e 17 #define WAIT_CCSSC 0.000000007 //wait 7ns
faithcerebral 0:bd3a560e245e 18 #define WAIT_CSH 0.000001 //wait 2 clock cycles
faithcerebral 0:bd3a560e245e 19 #define WAIT_DSHD 0.00008 //wait 16 clock cycles
faithcerebral 0:bd3a560e245e 20 #define WAIT_RST 0.000009//wait 18 clock cycles
faithcerebral 0:bd3a560e245e 21 #define wait_time 0.000004 //8th sclk falling edge
faithcerebral 0:bd3a560e245e 22 #define wait_time2 0.000012 //wait 24clock cycles
faithcerebral 0:bd3a560e245e 23 #define buffer_size 100 // size of data buffers
faithcerebral 0:bd3a560e245e 24
faithcerebral 0:bd3a560e245e 25 /***********************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 26 Pin Allocations
faithcerebral 0:bd3a560e245e 27 ***********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 28 DigitalOut myled(LED1);
faithcerebral 0:bd3a560e245e 29 Timer timer1;
faithcerebral 0:bd3a560e245e 30 SPI spi(p5,p6,p7); // Initialisng variables mosi, miso,sclk,chip select
faithcerebral 0:bd3a560e245e 31 DigitalOut cs(p8); //Chip select
faithcerebral 0:bd3a560e245e 32 DigitalOut out(p18); // To Oscillioscope
faithcerebral 0:bd3a560e245e 33 InterruptIn DRDY(p22); //Interrupt signal
faithcerebral 0:bd3a560e245e 34 DigitalOut Start(p23);
faithcerebral 0:bd3a560e245e 35 DigitalOut PWDN (p24);
faithcerebral 0:bd3a560e245e 36 DigitalOut CLKSEL(p25);
faithcerebral 0:bd3a560e245e 37 DigitalOut Reset(p26);
faithcerebral 0:bd3a560e245e 38 DigitalOut CS_START(p27);
faithcerebral 0:bd3a560e245e 39
faithcerebral 0:bd3a560e245e 40
faithcerebral 0:bd3a560e245e 41 Serial pc(USBTX, USBRX); //tx,rx
faithcerebral 0:bd3a560e245e 42 PwmOut pw(p21);
faithcerebral 0:bd3a560e245e 43
faithcerebral 0:bd3a560e245e 44 /***********************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 45 STRUCTURES
faithcerebral 0:bd3a560e245e 46 *************************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 47 // info from pc
faithcerebral 0:bd3a560e245e 48 typedef struct {
faithcerebral 0:bd3a560e245e 49 int resolution; //resolution of adc
faithcerebral 0:bd3a560e245e 50 volatile char data_out1[9]; //msb
faithcerebral 0:bd3a560e245e 51 volatile char data_out2[9];//middle byte
faithcerebral 0:bd3a560e245e 52 volatile char data_out3[9];//lsb
faithcerebral 0:bd3a560e245e 53 int ttl_freq;
faithcerebral 0:bd3a560e245e 54 int gain; //duty cycle of pulse generation.
faithcerebral 0:bd3a560e245e 55 int RLD; //right leg drive
faithcerebral 0:bd3a560e245e 56 int test_duration;
faithcerebral 0:bd3a560e245e 57 int num_channels;
faithcerebral 0:bd3a560e245e 58 int count;
faithcerebral 0:bd3a560e245e 59 int lpf_cutoff;
faithcerebral 0:bd3a560e245e 60 int mfs;// value either 1 or 0 representing if a ganzfeld or multifocal system(mfs) is used
faithcerebral 0:bd3a560e245e 61 int mfs_frequency; //frequency of mfs simulator
faithcerebral 0:bd3a560e245e 62 int ganzfeld_frequency; //frequency of ganzfeld simulator
faithcerebral 0:bd3a560e245e 63 int freq_s;
faithcerebral 0:bd3a560e245e 64
faithcerebral 0:bd3a560e245e 65 }data;
faithcerebral 0:bd3a560e245e 66
faithcerebral 0:bd3a560e245e 67 data test;
faithcerebral 0:bd3a560e245e 68
faithcerebral 0:bd3a560e245e 69 /**************************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 70 * Global Variables *
faithcerebral 0:bd3a560e245e 71 **************************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 72 unsigned char ADS1x9x_SPI_data;
faithcerebral 0:bd3a560e245e 73 signed char ADS1x9x_Data [ADS1298_DATA_LENGTH];
faithcerebral 0:bd3a560e245e 74 float buffer_1[buffer_size] = {0};
faithcerebral 0:bd3a560e245e 75 float buffer_2[buffer_size] = {0};
faithcerebral 0:bd3a560e245e 76 unsigned char device_slot;
faithcerebral 0:bd3a560e245e 77
faithcerebral 0:bd3a560e245e 78 /************************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 79 Function prototypes
faithcerebral 0:bd3a560e245e 80 **********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 81 void pulse_generation(int mfs, int ganzfeld_frequency, int mfs_frequency );
faithcerebral 0:bd3a560e245e 82 void Initialize_ADS1x9x_Mode(void);
faithcerebral 0:bd3a560e245e 83 void spi_initialise(void);
faithcerebral 0:bd3a560e245e 84 void file_system(void);
faithcerebral 0:bd3a560e245e 85 void read_data(void);
faithcerebral 0:bd3a560e245e 86 void test_info(void);
faithcerebral 0:bd3a560e245e 87 int Initialize_ADS1x9x_Channel (void);
faithcerebral 0:bd3a560e245e 88 void lpf_coef(void);
faithcerebral 0:bd3a560e245e 89 float lpf( float coeff[5], float);
faithcerebral 0:bd3a560e245e 90 void ISR1();
faithcerebral 0:bd3a560e245e 91 void read(void);
faithcerebral 0:bd3a560e245e 92
faithcerebral 0:bd3a560e245e 93
faithcerebral 0:bd3a560e245e 94 /**************************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 95 MAIN PROGRAM
faithcerebral 0:bd3a560e245e 96 ***************************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 97 int main() {
faithcerebral 0:bd3a560e245e 98
faithcerebral 0:bd3a560e245e 99 pc.baud(14400);
faithcerebral 0:bd3a560e245e 100 int system_pause = 1;
faithcerebral 0:bd3a560e245e 101 int device_slot = 1;
faithcerebral 0:bd3a560e245e 102
faithcerebral 0:bd3a560e245e 103 DRDY.fall(&ISR1);
faithcerebral 0:bd3a560e245e 104
faithcerebral 0:bd3a560e245e 105 test_info();
faithcerebral 0:bd3a560e245e 106 // calculate the low pass filter coeficeints
faithcerebral 0:bd3a560e245e 107 lpf_coef();
faithcerebral 0:bd3a560e245e 108 wait(0.1);
faithcerebral 0:bd3a560e245e 109
faithcerebral 0:bd3a560e245e 110 cs = LOW;
faithcerebral 0:bd3a560e245e 111
faithcerebral 0:bd3a560e245e 112 CS_START =LOW;
faithcerebral 0:bd3a560e245e 113 wait(WAIT_CCSSC);
faithcerebral 0:bd3a560e245e 114
faithcerebral 0:bd3a560e245e 115 pc.printf("Initialising");
faithcerebral 0:bd3a560e245e 116 spi_initialise();
faithcerebral 0:bd3a560e245e 117 cs = LOW;
faithcerebral 0:bd3a560e245e 118 wait(0.1);
faithcerebral 0:bd3a560e245e 119
faithcerebral 0:bd3a560e245e 120 Reset = 1; //introduced this
faithcerebral 0:bd3a560e245e 121 wait(wait_time);
faithcerebral 0:bd3a560e245e 122 Reset = 0;
faithcerebral 0:bd3a560e245e 123
faithcerebral 0:bd3a560e245e 124 pc.printf("Waking up adc from sleep ");
faithcerebral 0:bd3a560e245e 125 spi.write(WAKE_CONVERTER_FROM_SLEEP); //power up adc
faithcerebral 0:bd3a560e245e 126 wait(1);
faithcerebral 0:bd3a560e245e 127
faithcerebral 0:bd3a560e245e 128
faithcerebral 0:bd3a560e245e 129 spi.write(RESET_CONVERTER); //reset
faithcerebral 0:bd3a560e245e 130 wait(WAIT_RST);
faithcerebral 0:bd3a560e245e 131
faithcerebral 0:bd3a560e245e 132 CLKSEL = 1; //use internal clock
faithcerebral 0:bd3a560e245e 133 wait(wait_time);
faithcerebral 0:bd3a560e245e 134
faithcerebral 0:bd3a560e245e 135 PWDN = 1;
faithcerebral 0:bd3a560e245e 136 Reset = 1;
faithcerebral 0:bd3a560e245e 137 wait(1.5);
faithcerebral 0:bd3a560e245e 138 // Reset = 0; //not sure about this bit.
faithcerebral 0:bd3a560e245e 139 // wait(WAIT_RST);
faithcerebral 0:bd3a560e245e 140
faithcerebral 0:bd3a560e245e 141 spi.write(RESET_CONVERTER); //reset
faithcerebral 0:bd3a560e245e 142 wait(WAIT_RST);
faithcerebral 0:bd3a560e245e 143 pc.printf("Converter has been reset\n");
faithcerebral 0:bd3a560e245e 144
faithcerebral 0:bd3a560e245e 145 read(); //read config registers
faithcerebral 0:bd3a560e245e 146 wait(wait_time);
faithcerebral 0:bd3a560e245e 147
faithcerebral 0:bd3a560e245e 148 spi.write(WRITE_CONFIG_3_REGISTER);
faithcerebral 0:bd3a560e245e 149 wait(wait_time);
faithcerebral 0:bd3a560e245e 150 spi.write(SINGLE_BYTE_READ_WRITE);
faithcerebral 0:bd3a560e245e 151 wait(wait_time);
faithcerebral 0:bd3a560e245e 152 spi.write(INTERNAL_REF);
faithcerebral 0:bd3a560e245e 153
faithcerebral 0:bd3a560e245e 154
faithcerebral 0:bd3a560e245e 155 //setup config registers
faithcerebral 0:bd3a560e245e 156 Initialize_ADS1x9x_Mode(); //config 1,2,3,4
faithcerebral 0:bd3a560e245e 157
faithcerebral 0:bd3a560e245e 158 test.num_channels = Initialize_ADS1x9x_Channel();
faithcerebral 0:bd3a560e245e 159
faithcerebral 0:bd3a560e245e 160 //initialise gpio
faithcerebral 0:bd3a560e245e 161 init_ADS1x9x_IO (device_slot);
faithcerebral 0:bd3a560e245e 162 wait(wait_time);
faithcerebral 0:bd3a560e245e 163
faithcerebral 0:bd3a560e245e 164 pc.printf("Reading the config settings");
faithcerebral 0:bd3a560e245e 165
faithcerebral 0:bd3a560e245e 166 read();
faithcerebral 0:bd3a560e245e 167 wait(0.0001);
faithcerebral 0:bd3a560e245e 168
faithcerebral 0:bd3a560e245e 169
faithcerebral 0:bd3a560e245e 170 Start = LOW;
faithcerebral 0:bd3a560e245e 171 wait(WAIT_CSH);
faithcerebral 0:bd3a560e245e 172
faithcerebral 0:bd3a560e245e 173 spi.write(START_RESTART_CONVERSION);
faithcerebral 0:bd3a560e245e 174 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 175
faithcerebral 0:bd3a560e245e 176 spi.write(SET_READ_DATA_CONTINUOUSLY );
faithcerebral 0:bd3a560e245e 177 wait(wait_time);
faithcerebral 0:bd3a560e245e 178
faithcerebral 0:bd3a560e245e 179
faithcerebral 0:bd3a560e245e 180
faithcerebral 0:bd3a560e245e 181 system_pause = pc.scanf("%d\n",&system_pause); //wait for start command
faithcerebral 0:bd3a560e245e 182 timer1.start();
faithcerebral 0:bd3a560e245e 183
faithcerebral 0:bd3a560e245e 184 while(timer1.read() < 200){ //Ensure that the test runs for 2 minute only
faithcerebral 0:bd3a560e245e 185
faithcerebral 0:bd3a560e245e 186 //check for pause before continuing
faithcerebral 0:bd3a560e245e 187 system_pause = 1;
faithcerebral 0:bd3a560e245e 188 if (system_pause == 0)
faithcerebral 0:bd3a560e245e 189 {
faithcerebral 0:bd3a560e245e 190 while(system_pause == 0)
faithcerebral 0:bd3a560e245e 191 {
faithcerebral 0:bd3a560e245e 192 system_pause = pc.scanf("%d\n",&system_pause);
faithcerebral 0:bd3a560e245e 193
faithcerebral 0:bd3a560e245e 194 }
faithcerebral 0:bd3a560e245e 195 }
faithcerebral 0:bd3a560e245e 196
faithcerebral 0:bd3a560e245e 197 else
faithcerebral 0:bd3a560e245e 198 {
faithcerebral 0:bd3a560e245e 199 pulse_generation(test.mfs, test.ganzfeld_frequency,test.mfs_frequency); //5 is just an arbitrary value selected so that the code runs, to use adc speed
faithcerebral 0:bd3a560e245e 200
faithcerebral 0:bd3a560e245e 201 int a = test.count - 1;
faithcerebral 0:bd3a560e245e 202
faithcerebral 0:bd3a560e245e 203 __disable_irq();
faithcerebral 0:bd3a560e245e 204 if (test.count > a) //prevents the same data being repeatedly sent since the adc is slower than the mcu
faithcerebral 0:bd3a560e245e 205 {
faithcerebral 0:bd3a560e245e 206 //include lpf
faithcerebral 0:bd3a560e245e 207 // test.data_out = lpf(test.data_out)
faithcerebral 0:bd3a560e245e 208 // read2();
faithcerebral 0:bd3a560e245e 209 for (int i = 0; i <= (test.num_channels +1);i++){
faithcerebral 0:bd3a560e245e 210
faithcerebral 0:bd3a560e245e 211 //pc.printf("\n");
faithcerebral 0:bd3a560e245e 212 pc.printf("%02x%02x%02x\n",test.data_out1[i], test.data_out2[i],test.data_out3[i]);
faithcerebral 0:bd3a560e245e 213 }
faithcerebral 0:bd3a560e245e 214
faithcerebral 0:bd3a560e245e 215 }
faithcerebral 0:bd3a560e245e 216
faithcerebral 0:bd3a560e245e 217
faithcerebral 0:bd3a560e245e 218 else
faithcerebral 0:bd3a560e245e 219 {
faithcerebral 0:bd3a560e245e 220 wait(0.000000000001); //momentary pause
faithcerebral 0:bd3a560e245e 221
faithcerebral 0:bd3a560e245e 222 }
faithcerebral 0:bd3a560e245e 223 __enable_irq ();
faithcerebral 0:bd3a560e245e 224
faithcerebral 0:bd3a560e245e 225 }
faithcerebral 0:bd3a560e245e 226 }
faithcerebral 0:bd3a560e245e 227 Start = LOW; //stop conversion
faithcerebral 0:bd3a560e245e 228 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 229 // spi.write(0x0A);
faithcerebral 0:bd3a560e245e 230 spi.write( STOP_READ_DATA_CONTINUOUSLY );
faithcerebral 0:bd3a560e245e 231 wait(wait_time );
faithcerebral 0:bd3a560e245e 232 spi.write(STAND_BY_MODE); //Enter standby mode
faithcerebral 0:bd3a560e245e 233 timer1.stop();
faithcerebral 0:bd3a560e245e 234 wait(WAIT_CSH);
faithcerebral 0:bd3a560e245e 235 cs = HIGH;
faithcerebral 0:bd3a560e245e 236 return 1;
faithcerebral 0:bd3a560e245e 237 }
faithcerebral 0:bd3a560e245e 238 /**********************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 239 TEST INFORMATION
faithcerebral 0:bd3a560e245e 240 **********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 241 void test_info(void){
faithcerebral 1:26b8b0e4d836 242 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
faithcerebral 0:bd3a560e245e 243 //open text file
faithcerebral 1:26b8b0e4d836 244 FILE *fp = fopen("/local/out.txt", "r"); // Open "out.txt" on the local file system for reading
faithcerebral 0:bd3a560e245e 245
faithcerebral 1:26b8b0e4d836 246 if (fp == NULL){
faithcerebral 1:26b8b0e4d836 247 myled = 1;
faithcerebral 1:26b8b0e4d836 248 }
faithcerebral 1:26b8b0e4d836 249 else {
faithcerebral 1:26b8b0e4d836 250 while (!feof(fp)){
faithcerebral 0:bd3a560e245e 251
faithcerebral 1:26b8b0e4d836 252 fscanf(fp,"%d\n",&test.num_channels);
faithcerebral 1:26b8b0e4d836 253 fscanf(fp,"%d\n",&test.gain);
faithcerebral 1:26b8b0e4d836 254 fscanf(fp,"%d\n",&test.ttl_freq); //specify frequency of test
faithcerebral 1:26b8b0e4d836 255 fscanf(fp,"%d\n",&test.mfs);//specify if multifocal or erg test
faithcerebral 1:26b8b0e4d836 256 }
faithcerebral 1:26b8b0e4d836 257 }
faithcerebral 0:bd3a560e245e 258
faithcerebral 0:bd3a560e245e 259
faithcerebral 0:bd3a560e245e 260 }
faithcerebral 0:bd3a560e245e 261
faithcerebral 0:bd3a560e245e 262 /***********************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 263 PUSLE GENERATION
faithcerebral 0:bd3a560e245e 264 ***********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 265 void pulse_generation(int mfs, int ganzfeld_frequency, int mfs_frequency ){
faithcerebral 0:bd3a560e245e 266
faithcerebral 0:bd3a560e245e 267 PwmOut pw(p21);
faithcerebral 0:bd3a560e245e 268 float duty_cycle;
faithcerebral 0:bd3a560e245e 269
faithcerebral 0:bd3a560e245e 270 static double period;
faithcerebral 0:bd3a560e245e 271 //Enable ganzfeld synchronising pulse
faithcerebral 0:bd3a560e245e 272 // pc.printf("Using a Ganzfeld(0) or Mfs(1)?"); //mfs=>multifocal system
faithcerebral 0:bd3a560e245e 273 switch(test.mfs)
faithcerebral 0:bd3a560e245e 274 {
faithcerebral 0:bd3a560e245e 275 case 0:
faithcerebral 0:bd3a560e245e 276 { //ganzfeld
faithcerebral 0:bd3a560e245e 277 period = 1/test.ttl_freq;
faithcerebral 0:bd3a560e245e 278 pw.period(period);
faithcerebral 0:bd3a560e245e 279 pw = 0.5;
faithcerebral 0:bd3a560e245e 280 }
faithcerebral 0:bd3a560e245e 281 case 1: //mf case sends pulse at the operating frequency of the adc
faithcerebral 0:bd3a560e245e 282 {
faithcerebral 0:bd3a560e245e 283 pw.period(0.0010);
faithcerebral 0:bd3a560e245e 284 pw = 0.5; //duty cycle of 50% , a perfect square wave
faithcerebral 0:bd3a560e245e 285
faithcerebral 0:bd3a560e245e 286 }
faithcerebral 0:bd3a560e245e 287 }
faithcerebral 0:bd3a560e245e 288
faithcerebral 0:bd3a560e245e 289 }
faithcerebral 0:bd3a560e245e 290 /************************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 291 SPI INTERFACE
faithcerebral 0:bd3a560e245e 292 *************************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 293
faithcerebral 0:bd3a560e245e 294 void spi_initialise(void){
faithcerebral 0:bd3a560e245e 295
faithcerebral 0:bd3a560e245e 296 spi.format(8,1); //configuring the transmission of data and mode 1
faithcerebral 0:bd3a560e245e 297 spi.frequency(2000000); //spi clock frequency to 2Mhz
faithcerebral 0:bd3a560e245e 298 return;
faithcerebral 0:bd3a560e245e 299 }
faithcerebral 0:bd3a560e245e 300
faithcerebral 0:bd3a560e245e 301
faithcerebral 0:bd3a560e245e 302 /**********************************************************************************************************************************
faithcerebral 0:bd3a560e245e 303 INITIALISE ADS I/O
faithcerebral 0:bd3a560e245e 304 **********************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 305 void init_ADS1x9x_IO (unsigned char){
faithcerebral 0:bd3a560e245e 306 //right now, not using any input or output pins
faithcerebral 0:bd3a560e245e 307 //setting all GPIO to output mode and connecting them to ground
faithcerebral 0:bd3a560e245e 308
faithcerebral 0:bd3a560e245e 309 spi.write(WRITE_GENERAL_PORT_IO); //write the command to the GPIO register
faithcerebral 0:bd3a560e245e 310 wait(wait_time);
faithcerebral 0:bd3a560e245e 311 spi.write(SINGLE_BYTE_READ_WRITE);
faithcerebral 0:bd3a560e245e 312 wait(wait_time);
faithcerebral 0:bd3a560e245e 313 spi.write(REGISTER_DEFAULT_SETTINGS); //Turn all general purpose inputs and outputs off
faithcerebral 0:bd3a560e245e 314 wait(wait_time);
faithcerebral 0:bd3a560e245e 315 //pc.printf("GPIO initialisation complete\n");
faithcerebral 0:bd3a560e245e 316 return;
faithcerebral 0:bd3a560e245e 317 }
faithcerebral 0:bd3a560e245e 318
faithcerebral 0:bd3a560e245e 319 /*************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 320 INITIALISE ADS MODE
faithcerebral 0:bd3a560e245e 321 **************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 322 //Data rate configured
faithcerebral 0:bd3a560e245e 323 void Initialize_ADS1x9x_Mode() {
faithcerebral 0:bd3a560e245e 324
faithcerebral 0:bd3a560e245e 325 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 326 spi.write(WRITE_CONFIG_1_REGISTER); //Write to config1
faithcerebral 0:bd3a560e245e 327 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 328 spi.write(SINGLE_BYTE_READ_WRITE); //Second byte of WReg
faithcerebral 0:bd3a560e245e 329 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 330 switch(test.freq_s)
faithcerebral 0:bd3a560e245e 331 {
faithcerebral 0:bd3a560e245e 332 case 0:
faithcerebral 0:bd3a560e245e 333 spi.write(THIRTY_TWO_KSPS_SAMPLING_FREQ); //use command 0xC0 to use external clock
faithcerebral 0:bd3a560e245e 334 test.freq_s = 32000;
faithcerebral 0:bd3a560e245e 335 case 1:
faithcerebral 0:bd3a560e245e 336 spi.write(SIXTEEN_KSPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 337 test.freq_s = 16000;
faithcerebral 0:bd3a560e245e 338 case 2:
faithcerebral 0:bd3a560e245e 339 spi.write(EIGHT_KSPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 340 test.freq_s = 8000;
faithcerebral 0:bd3a560e245e 341 case 3:
faithcerebral 0:bd3a560e245e 342 spi.write(FOUR_KSPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 343 test.freq_s = 4000;
faithcerebral 0:bd3a560e245e 344 case 4:
faithcerebral 0:bd3a560e245e 345 spi.write(TWO_KSPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 346 test.freq_s = 2000;
faithcerebral 0:bd3a560e245e 347 case 5:
faithcerebral 0:bd3a560e245e 348 spi.write(ONE_KSPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 349 pc.printf("\n1k sampling rate\n");
faithcerebral 0:bd3a560e245e 350 test.freq_s = 1000;
faithcerebral 0:bd3a560e245e 351 case 6:
faithcerebral 0:bd3a560e245e 352 spi.write(FIVE_SPS_SAMPLING_FREQ);
faithcerebral 0:bd3a560e245e 353 test.freq_s = 500;
faithcerebral 0:bd3a560e245e 354 }
faithcerebral 0:bd3a560e245e 355 wait(0.00002);
faithcerebral 0:bd3a560e245e 356
faithcerebral 0:bd3a560e245e 357 pc.printf("Config 1 value\n");
faithcerebral 0:bd3a560e245e 358 volatile char d = spi.write(FETCH_DATA);
faithcerebral 0:bd3a560e245e 359 wait(wait_time2);
faithcerebral 0:bd3a560e245e 360 pc.printf("%X",d);
faithcerebral 0:bd3a560e245e 361
faithcerebral 0:bd3a560e245e 362
faithcerebral 0:bd3a560e245e 363 spi.write(WRITE_CONFIG_2_REGISTER ); //Write to CONFIG 2
faithcerebral 0:bd3a560e245e 364 wait(wait_time);
faithcerebral 0:bd3a560e245e 365 spi.write(SINGLE_BYTE_READ_WRITE);
faithcerebral 0:bd3a560e245e 366 wait(wait_time);
faithcerebral 0:bd3a560e245e 367 spi.write(REGISTER_DEFAULT_SETTINGS); //configures the test signal generation
faithcerebral 0:bd3a560e245e 368
faithcerebral 0:bd3a560e245e 369 wait(wait_time);
faithcerebral 0:bd3a560e245e 370 spi.write(WRITE_CONFIG_3_REGISTER); //Write to CONFIG 3
faithcerebral 0:bd3a560e245e 371 wait(wait_time);
faithcerebral 0:bd3a560e245e 372 spi.write(SINGLE_BYTE_READ_WRITE);
faithcerebral 0:bd3a560e245e 373 wait(wait_time);
faithcerebral 0:bd3a560e245e 374 spi.write(INTERNAL_REF); //Configures multireference and enables internal reference buffer
faithcerebral 0:bd3a560e245e 375 wait(0.01);
faithcerebral 0:bd3a560e245e 376
faithcerebral 0:bd3a560e245e 377 //CONFIG4
faithcerebral 0:bd3a560e245e 378
faithcerebral 0:bd3a560e245e 379 spi.write(WRITE_CONFIG_4_REGISTER );
faithcerebral 0:bd3a560e245e 380 wait(wait_time);
faithcerebral 0:bd3a560e245e 381 spi.write(SINGLE_BYTE_READ_WRITE);
faithcerebral 0:bd3a560e245e 382 wait(wait_time);
faithcerebral 0:bd3a560e245e 383 spi.write(REGISTER_DEFAULT_SETTINGS); //set adc in continous mode and respiration modulatiion frequency at 64khz
faithcerebral 0:bd3a560e245e 384 wait(0.00001);
faithcerebral 0:bd3a560e245e 385
faithcerebral 0:bd3a560e245e 386 //pc.printf("Configuration complete");
faithcerebral 0:bd3a560e245e 387 return ;
faithcerebral 0:bd3a560e245e 388
faithcerebral 0:bd3a560e245e 389 }
faithcerebral 0:bd3a560e245e 390 /*****************************************************************************************************************************
faithcerebral 0:bd3a560e245e 391 INITIALISE ADS CHANNEL
faithcerebral 0:bd3a560e245e 392 ******************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 393 int Initialize_ADS1x9x_Channel (void){
faithcerebral 0:bd3a560e245e 394
faithcerebral 0:bd3a560e245e 395 int i,j;
faithcerebral 0:bd3a560e245e 396
faithcerebral 0:bd3a560e245e 397 spi.write( WRITE_CHANNEL_1_SET_REGISTER); //Start at channel register 1
faithcerebral 0:bd3a560e245e 398 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 399 spi.write(EIGHT_BYTE_READ_WRITE); //write 8 channels
faithcerebral 0:bd3a560e245e 400 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 401 switch(test.gain)
faithcerebral 0:bd3a560e245e 402 {
faithcerebral 0:bd3a560e245e 403 case 1:
faithcerebral 0:bd3a560e245e 404 for ( i = 0; i <= test.num_channels; i++)
faithcerebral 0:bd3a560e245e 405 {
faithcerebral 0:bd3a560e245e 406 spi.write(GAIN_ONE); //turn on channels
faithcerebral 0:bd3a560e245e 407 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 408 }
faithcerebral 0:bd3a560e245e 409 case 2:
faithcerebral 0:bd3a560e245e 410 for ( i = 0;i <= test.num_channels ;i++)
faithcerebral 0:bd3a560e245e 411 {
faithcerebral 0:bd3a560e245e 412 spi.write(GAIN_TWO); //turn on channels
faithcerebral 0:bd3a560e245e 413 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 414 }
faithcerebral 0:bd3a560e245e 415 case 3:
faithcerebral 0:bd3a560e245e 416 for ( i = 1; i <= test.num_channels ;i++)
faithcerebral 0:bd3a560e245e 417 {
faithcerebral 0:bd3a560e245e 418 spi.write(GAIN_THREE); //turn on channels
faithcerebral 0:bd3a560e245e 419 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 420 }
faithcerebral 0:bd3a560e245e 421 case 4:
faithcerebral 0:bd3a560e245e 422 for (i = 1; i <= test.num_channels; i++)
faithcerebral 0:bd3a560e245e 423 {
faithcerebral 0:bd3a560e245e 424 spi.write(GAIN_FOUR); //turn on channels
faithcerebral 0:bd3a560e245e 425
faithcerebral 0:bd3a560e245e 426 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 427 }
faithcerebral 0:bd3a560e245e 428 case 6:
faithcerebral 0:bd3a560e245e 429 for (i = 1; i <= test.num_channels; i++)
faithcerebral 0:bd3a560e245e 430 {
faithcerebral 0:bd3a560e245e 431 spi.write(GAIN_SIX); //turn on channels
faithcerebral 0:bd3a560e245e 432 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 433 pc.printf("gain of 6");
faithcerebral 0:bd3a560e245e 434 }
faithcerebral 0:bd3a560e245e 435 case 8:
faithcerebral 0:bd3a560e245e 436 for ( i = 0; i <= test.num_channels ; i++)
faithcerebral 0:bd3a560e245e 437 {
faithcerebral 0:bd3a560e245e 438 spi.write(GAIN_EIGHT); //turn on channels
faithcerebral 0:bd3a560e245e 439 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 440 }
faithcerebral 0:bd3a560e245e 441 case 12:
faithcerebral 0:bd3a560e245e 442 for (i = 0; i <= test.num_channels ;i++)
faithcerebral 0:bd3a560e245e 443 {
faithcerebral 0:bd3a560e245e 444 spi.write(GAIN_TWELVE); //turn on channels
faithcerebral 0:bd3a560e245e 445 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 446 }
faithcerebral 0:bd3a560e245e 447
faithcerebral 0:bd3a560e245e 448 }//switch case bracket
faithcerebral 0:bd3a560e245e 449
faithcerebral 0:bd3a560e245e 450 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 451 for (j = 1; j <= (8 - test.num_channels); j++)
faithcerebral 0:bd3a560e245e 452 {
faithcerebral 0:bd3a560e245e 453 spi.write(INPUT_SHORT); // turn off channels
faithcerebral 0:bd3a560e245e 454 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 455 pc.printf("turning off channels\n") ;
faithcerebral 0:bd3a560e245e 456 }
faithcerebral 0:bd3a560e245e 457 wait(wait_time);
faithcerebral 0:bd3a560e245e 458
faithcerebral 0:bd3a560e245e 459 //pc.printf("ADC channels initiaslise\n");
faithcerebral 0:bd3a560e245e 460 return test.num_channels;
faithcerebral 0:bd3a560e245e 461 //Configure right leg drive
faithcerebral 0:bd3a560e245e 462 }
faithcerebral 0:bd3a560e245e 463
faithcerebral 0:bd3a560e245e 464 /*******************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 465 Low pass filter_coefficents
faithcerebral 0:bd3a560e245e 466 The low pass filter uses a 3rd Order butterworth filter to have a 40db attenuation
faithcerebral 0:bd3a560e245e 467 This function calculates the coeffients to be used for the low pass filter
faithcerebral 0:bd3a560e245e 468 inputs:sampling frequency, cut off frequency
faithcerebral 0:bd3a560e245e 469 outputs:a pointer to an array with all the coeffiecents.
faithcerebral 0:bd3a560e245e 470 ********************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 471 void lpf_coef(void){
faithcerebral 0:bd3a560e245e 472 //calculate prewarped coeficients
faithcerebral 0:bd3a560e245e 473 static float wp; //the prewarped frequencies
faithcerebral 0:bd3a560e245e 474 static float coeff[5] = {0};
faithcerebral 0:bd3a560e245e 475 float pi = 3.14159265359;
faithcerebral 0:bd3a560e245e 476 static float a;
faithcerebral 0:bd3a560e245e 477 static float b;
faithcerebral 0:bd3a560e245e 478 static float* coeff_pointer;
faithcerebral 0:bd3a560e245e 479
faithcerebral 0:bd3a560e245e 480 a = 0.5*wp*wp;
faithcerebral 0:bd3a560e245e 481 b = 0.5*wp*wp*wp;
faithcerebral 0:bd3a560e245e 482
faithcerebral 0:bd3a560e245e 483 //user inputs the desired cut off frequency
faithcerebral 0:bd3a560e245e 484 //pc.printf("enter the desired cut off frequency:");
faithcerebral 0:bd3a560e245e 485
faithcerebral 0:bd3a560e245e 486 wp = tan((2*pi *test.lpf_cutoff)/2*test.freq_s); //prewarped frequency
faithcerebral 0:bd3a560e245e 487
faithcerebral 0:bd3a560e245e 488 coeff[0] = (-1 + wp - a + b)/wp;
faithcerebral 0:bd3a560e245e 489 coeff[1] = (3 -wp -a+3*b)/wp;
faithcerebral 0:bd3a560e245e 490 coeff[2] = (-3 -wp +3*a +3*b)/wp;
faithcerebral 0:bd3a560e245e 491 coeff[3] = (1 + wp + a + b);
faithcerebral 0:bd3a560e245e 492 coeff[4] = wp;
faithcerebral 0:bd3a560e245e 493 }
faithcerebral 0:bd3a560e245e 494
faithcerebral 0:bd3a560e245e 495 /*******************************************************************************************************************************************
faithcerebral 0:bd3a560e245e 496 Low pass filter
faithcerebral 0:bd3a560e245e 497 ******************************************************************************************************************************************/
faithcerebral 0:bd3a560e245e 498 float lpf( float coeff[5], float lpf_in){
faithcerebral 0:bd3a560e245e 499
faithcerebral 0:bd3a560e245e 500 static float lpf_out;
faithcerebral 0:bd3a560e245e 501 float y[4];
faithcerebral 0:bd3a560e245e 502 float x[4];
faithcerebral 0:bd3a560e245e 503
faithcerebral 0:bd3a560e245e 504
faithcerebral 0:bd3a560e245e 505 x[0] = lpf_in;
faithcerebral 0:bd3a560e245e 506 //moving x and y values by one sample
faithcerebral 0:bd3a560e245e 507 x[3] = x[2]; x[2] = x[1]; x[1] = x[0];
faithcerebral 0:bd3a560e245e 508
faithcerebral 0:bd3a560e245e 509 y[3] = y[2]; y[2] = y[1]; y[1] = y[0];
faithcerebral 0:bd3a560e245e 510
faithcerebral 0:bd3a560e245e 511 //the output is given by:
faithcerebral 0:bd3a560e245e 512 y[0] = (coeff[3]*x[0])+(coeff[2]*x[1])+ (coeff[1]*x[2])+(coeff[0]*x[3]) + (3*y[1])+(3*y[2])+(y[3]);
faithcerebral 0:bd3a560e245e 513
faithcerebral 0:bd3a560e245e 514 lpf_out = y[0];
faithcerebral 0:bd3a560e245e 515
faithcerebral 0:bd3a560e245e 516 return lpf_out;
faithcerebral 0:bd3a560e245e 517
faithcerebral 0:bd3a560e245e 518 }
faithcerebral 0:bd3a560e245e 519
faithcerebral 0:bd3a560e245e 520 /***************************
faithcerebral 0:bd3a560e245e 521 read the ads channels*/
faithcerebral 0:bd3a560e245e 522 void read(void){
faithcerebral 0:bd3a560e245e 523 int a;
faithcerebral 0:bd3a560e245e 524 int b;
faithcerebral 0:bd3a560e245e 525 volatile int c;
faithcerebral 0:bd3a560e245e 526
faithcerebral 0:bd3a560e245e 527
faithcerebral 0:bd3a560e245e 528 spi.write(STOP_READ_DATA_CONTINUOUSLY); //disable conversions to read registers
faithcerebral 0:bd3a560e245e 529 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 530
faithcerebral 0:bd3a560e245e 531 spi.write(READ_DEVICE_ID ); //start reading at register 0
faithcerebral 0:bd3a560e245e 532 wait(wait_time2);
faithcerebral 0:bd3a560e245e 533 spi.write(EIGHT_BYTE_READ_WRITE); //read 8 registers
faithcerebral 0:bd3a560e245e 534 //spi.write(0x06);
faithcerebral 0:bd3a560e245e 535 wait(WAIT_DSHD );
faithcerebral 0:bd3a560e245e 536 for (c=1; c<=8; c++){
faithcerebral 0:bd3a560e245e 537 a = spi.write(FETCH_DATA); //send dummy variable to get data
faithcerebral 0:bd3a560e245e 538 wait(WAIT_DSHD);
faithcerebral 0:bd3a560e245e 539 pc.printf("%x\n",a);
faithcerebral 0:bd3a560e245e 540
faithcerebral 0:bd3a560e245e 541 }
faithcerebral 0:bd3a560e245e 542 wait(wait_time);
faithcerebral 0:bd3a560e245e 543
faithcerebral 0:bd3a560e245e 544 }
faithcerebral 0:bd3a560e245e 545 /*********************************************************
faithcerebral 0:bd3a560e245e 546 Interrupt from the DRDY to rad the data from the adc
faithcerebral 0:bd3a560e245e 547 ************************************************************/
faithcerebral 0:bd3a560e245e 548 void ISR1(){
faithcerebral 0:bd3a560e245e 549 //call function read data
faithcerebral 0:bd3a560e245e 550 myled = !myled;
faithcerebral 0:bd3a560e245e 551
faithcerebral 0:bd3a560e245e 552
faithcerebral 0:bd3a560e245e 553 for(int i = 0; i <= (test.num_channels+1); i++)
faithcerebral 0:bd3a560e245e 554 {
faithcerebral 0:bd3a560e245e 555 //try changing to wait_time
faithcerebral 0:bd3a560e245e 556 wait(wait_time);
faithcerebral 0:bd3a560e245e 557 test.data_out1[i] = spi.write(FETCH_DATA); //send the dummy variable to recieve the data.
faithcerebral 0:bd3a560e245e 558 wait(wait_time);
faithcerebral 0:bd3a560e245e 559 test.data_out2[i] = spi.write(FETCH_DATA);
faithcerebral 0:bd3a560e245e 560 wait(wait_time);
faithcerebral 0:bd3a560e245e 561 test.data_out3[i] = spi.write(FETCH_DATA);
faithcerebral 0:bd3a560e245e 562 wait(wait_time);
faithcerebral 0:bd3a560e245e 563 }
faithcerebral 0:bd3a560e245e 564
faithcerebral 0:bd3a560e245e 565 test.count = test.count +1;
faithcerebral 0:bd3a560e245e 566 }
faithcerebral 0:bd3a560e245e 567