added spectrometer timeout

Dependencies:   SDFileSystem mbed

Fork of All_Combined_Real2 by Colin Maxfield

Committer:
jphbergeson
Date:
Thu Mar 10 17:40:58 2016 +0000
Revision:
1:5fa445bd14a6
Parent:
0:26713d1db198
Child:
2:146625d6992d
Spectrometer & GPS working simultaneously

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jphbergeson 0:26713d1db198 1 #include "mbed.h"
jphbergeson 0:26713d1db198 2 #include "nmea.h"
jphbergeson 0:26713d1db198 3 #include "SDFileSystem.h"
jphbergeson 1:5fa445bd14a6 4 #include "SPI.h"
jphbergeson 0:26713d1db198 5
jphbergeson 1:5fa445bd14a6 6 #define NUM_PIXELS 2048
jphbergeson 0:26713d1db198 7 #define BUF_SIZE 1024
jphbergeson 1:5fa445bd14a6 8 #define OUTPUT_FILE "/sd/data.csv"
jphbergeson 0:26713d1db198 9
jphbergeson 1:5fa445bd14a6 10 // pinout for spectrometer
jphbergeson 1:5fa445bd14a6 11
jphbergeson 1:5fa445bd14a6 12 // Mosi (#8) -> PTD2
jphbergeson 1:5fa445bd14a6 13 // Miso (#7) -> PTD3
jphbergeson 1:5fa445bd14a6 14 // Sclk (#9) -> PTD1
jphbergeson 1:5fa445bd14a6 15
jphbergeson 1:5fa445bd14a6 16 // Pixel_rdy (#6) -> PTA1
jphbergeson 1:5fa445bd14a6 17 // Fifo_cs (#3) -> PTB23
jphbergeson 1:5fa445bd14a6 18 // Trigger (#13) -> PTA2
jphbergeson 1:5fa445bd14a6 19
jphbergeson 1:5fa445bd14a6 20 SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk
jphbergeson 1:5fa445bd14a6 21
jphbergeson 1:5fa445bd14a6 22 DigitalIn pixel_rdy(PTA1);
jphbergeson 1:5fa445bd14a6 23 DigitalOut fifo_cs(PTB23);
jphbergeson 1:5fa445bd14a6 24 DigitalOut trigger(PTA2);
jphbergeson 1:5fa445bd14a6 25
jphbergeson 1:5fa445bd14a6 26 // Blinking red LED
jphbergeson 0:26713d1db198 27 DigitalOut led(LED_RED);
jphbergeson 0:26713d1db198 28
jphbergeson 1:5fa445bd14a6 29 // GPS connection (through arduino)
jphbergeson 0:26713d1db198 30 Serial duino(PTC4, PTC3);
jphbergeson 1:5fa445bd14a6 31
jphbergeson 1:5fa445bd14a6 32 // USB serial to PC
jphbergeson 0:26713d1db198 33 Serial pc(USBTX, USBRX);
jphbergeson 0:26713d1db198 34
jphbergeson 0:26713d1db198 35 /**************************************************
jphbergeson 0:26713d1db198 36 ** SD FILE SYSTEM **
jphbergeson 0:26713d1db198 37 **************************************************/
jphbergeson 0:26713d1db198 38 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
jphbergeson 0:26713d1db198 39 FILE *fpData;
jphbergeson 0:26713d1db198 40
jphbergeson 1:5fa445bd14a6 41 void read_pixels() {
jphbergeson 1:5fa445bd14a6 42 int data[NUM_PIXELS];
jphbergeson 1:5fa445bd14a6 43 for (int i = 0; i < NUM_PIXELS; i++) {
jphbergeson 1:5fa445bd14a6 44
jphbergeson 1:5fa445bd14a6 45 while (!pixel_rdy);
jphbergeson 1:5fa445bd14a6 46
jphbergeson 1:5fa445bd14a6 47 fifo_cs = 0;
jphbergeson 1:5fa445bd14a6 48 data[i] = spi.write(0x00); // write a dummy byte just to read the data
jphbergeson 1:5fa445bd14a6 49 fifo_cs = 1;
jphbergeson 1:5fa445bd14a6 50
jphbergeson 1:5fa445bd14a6 51 }
jphbergeson 1:5fa445bd14a6 52
jphbergeson 1:5fa445bd14a6 53 // write to file and pc
jphbergeson 1:5fa445bd14a6 54 fpData = fopen(OUTPUT_FILE, "a");
jphbergeson 1:5fa445bd14a6 55 for (int i = 0; i < NUM_PIXELS; i++)
jphbergeson 1:5fa445bd14a6 56 {
jphbergeson 1:5fa445bd14a6 57 pc.printf(",%d", data[i]);
jphbergeson 1:5fa445bd14a6 58 fprintf(fpData, ",%d", data[i]);
jphbergeson 1:5fa445bd14a6 59 }
jphbergeson 1:5fa445bd14a6 60 pc.printf("\r\n");
jphbergeson 1:5fa445bd14a6 61 fprintf(fpData, "\r\n");
jphbergeson 1:5fa445bd14a6 62 fclose(fpData);
jphbergeson 1:5fa445bd14a6 63 }
jphbergeson 1:5fa445bd14a6 64
jphbergeson 0:26713d1db198 65 int main()
jphbergeson 0:26713d1db198 66 {
jphbergeson 0:26713d1db198 67 pc.baud(115200); // make sure to set computer TERA Term or whatever to 115200 baud!!!
jphbergeson 0:26713d1db198 68 duino.baud(9600);
jphbergeson 0:26713d1db198 69 pc.printf("Initializing ...\r\n");
jphbergeson 1:5fa445bd14a6 70
jphbergeson 1:5fa445bd14a6 71 fifo_cs = 1;
jphbergeson 1:5fa445bd14a6 72 trigger = 0;
jphbergeson 1:5fa445bd14a6 73 wait(0.5f);
jphbergeson 1:5fa445bd14a6 74
jphbergeson 0:26713d1db198 75 char buffer[BUF_SIZE];
jphbergeson 0:26713d1db198 76
jphbergeson 0:26713d1db198 77 while (true) {
jphbergeson 0:26713d1db198 78
jphbergeson 0:26713d1db198 79 led = !led;
jphbergeson 0:26713d1db198 80
jphbergeson 1:5fa445bd14a6 81 // --- gps stuff --- //
jphbergeson 0:26713d1db198 82 get_nmea(&duino, buffer, BUF_SIZE);
jphbergeson 0:26713d1db198 83
jphbergeson 0:26713d1db198 84 struct NMEA_data nmea = parse_line(buffer);
jphbergeson 0:26713d1db198 85
jphbergeson 0:26713d1db198 86 // determine whether there's a lock
jphbergeson 0:26713d1db198 87 char lock_str[BUF_SIZE];
jphbergeson 0:26713d1db198 88 if ( nmea.lock_flag == 'A' )
jphbergeson 0:26713d1db198 89 sprintf(lock_str, "Has lock");
jphbergeson 0:26713d1db198 90 else
jphbergeson 0:26713d1db198 91 sprintf(lock_str, "No lock");
jphbergeson 0:26713d1db198 92
jphbergeson 0:26713d1db198 93 // assemble data into summary string
jphbergeson 0:26713d1db198 94 char status_str[BUF_SIZE];
jphbergeson 1:5fa445bd14a6 95 sprintf(status_str, "%02d:%02d:%02d,%d/%d/%d,%dd %lf' %c %dd %lf' %c,%s",
jphbergeson 0:26713d1db198 96 nmea.hours, nmea.minutes, nmea.seconds,
jphbergeson 0:26713d1db198 97 nmea.month, nmea.day, nmea.year,
jphbergeson 0:26713d1db198 98 nmea.latitude, nmea.latitude_minutes, nmea.latitude_direction,
jphbergeson 0:26713d1db198 99 nmea.longitude, nmea.longitude_minutes, nmea.longitude_direction, lock_str);
jphbergeson 0:26713d1db198 100
jphbergeson 0:26713d1db198 101 // print to pc, sd card
jphbergeson 0:26713d1db198 102 pc.printf("%s", status_str);
jphbergeson 0:26713d1db198 103
jphbergeson 1:5fa445bd14a6 104 fpData = fopen(OUTPUT_FILE, "a");
jphbergeson 0:26713d1db198 105 fprintf(fpData, "%s", status_str);
jphbergeson 0:26713d1db198 106 fclose(fpData);
jphbergeson 1:5fa445bd14a6 107
jphbergeson 1:5fa445bd14a6 108 // --- end of gps stuff --- //
jphbergeson 1:5fa445bd14a6 109
jphbergeson 1:5fa445bd14a6 110
jphbergeson 1:5fa445bd14a6 111
jphbergeson 1:5fa445bd14a6 112 // Trigger an acquisition from spectrometer
jphbergeson 1:5fa445bd14a6 113 trigger = 1;
jphbergeson 1:5fa445bd14a6 114 wait_ms(1);
jphbergeson 1:5fa445bd14a6 115 trigger = 0;
jphbergeson 1:5fa445bd14a6 116 read_pixels();
jphbergeson 1:5fa445bd14a6 117
jphbergeson 1:5fa445bd14a6 118
jphbergeson 1:5fa445bd14a6 119
jphbergeson 0:26713d1db198 120 wait(0.5f);
jphbergeson 0:26713d1db198 121 }
jphbergeson 0:26713d1db198 122 }