lab 7

Dependencies:   SDFileSystem mbed

Committer:
jedh
Date:
Sat Dec 10 21:08:30 2016 +0000
Revision:
0:f6d3b930f382
jgk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jedh 0:f6d3b930f382 1 #include "stdio.h"
jedh 0:f6d3b930f382 2 #include "stdint.h"
jedh 0:f6d3b930f382 3 #include "mbed.h"
jedh 0:f6d3b930f382 4 #include "Freedom_headers.h"
jedh 0:f6d3b930f382 5
jedh 0:f6d3b930f382 6 #define USE_PC TRUE
jedh 0:f6d3b930f382 7 /* UNCOMMENT THE FOLLOWING LINE TO ENABLE SD CARD FILESYSTEM VIA DEFAULT SPI */
jedh 0:f6d3b930f382 8 #define USE_SD_CARD TRUE
jedh 0:f6d3b930f382 9 /* UNCOMMENT THE FOLLOWING LINE TO ENABLE DS1307 REAL-TIME CLOCK VIA IIC */
jedh 0:f6d3b930f382 10 #define USE_DS1307 TRUE
jedh 0:f6d3b930f382 11 /* UNCOMMENT THE FOLLOWING LINE TO ENABLE SIMPLE FIFO FUNCTIONS */
jedh 0:f6d3b930f382 12 #define USE_SIMPLE_FIFO TRUE
jedh 0:f6d3b930f382 13
jedh 0:f6d3b930f382 14 #ifdef USE_PC
jedh 0:f6d3b930f382 15 Serial pc(USBTX,USBRX);
jedh 0:f6d3b930f382 16
jedh 0:f6d3b930f382 17 #ifdef USE_SD_CARD
jedh 0:f6d3b930f382 18 #include "SDFileSystem.h"
jedh 0:f6d3b930f382 19 #define SD_MOSI PTD6
jedh 0:f6d3b930f382 20 #define SD_MISO PTD7
jedh 0:f6d3b930f382 21 #define SD_SCK PTD5
jedh 0:f6d3b930f382 22 #define SD_CS PTD4
jedh 0:f6d3b930f382 23 char sd_root_name[]="sd";
jedh 0:f6d3b930f382 24 char filename[80];
jedh 0:f6d3b930f382 25 SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, sd_root_name); /* MOSI,MISO,SCK,CS, "Name of directory structure" */
jedh 0:f6d3b930f382 26
jedh 0:f6d3b930f382 27 #endif /* #ifdef USE_SD_CARD*/
jedh 0:f6d3b930f382 28
jedh 0:f6d3b930f382 29 #ifdef USE_DS1307
jedh 0:f6d3b930f382 30 #include "EE3420_time.h"
jedh 0:f6d3b930f382 31
jedh 0:f6d3b930f382 32 #endif /* #ifdef USE_DS1307*/
jedh 0:f6d3b930f382 33
jedh 0:f6d3b930f382 34 #ifdef USE_SIMPLE_FIFO
jedh 0:f6d3b930f382 35
jedh 0:f6d3b930f382 36 /* comment out the folloing definitions to use the library defaults */
jedh 0:f6d3b930f382 37 #define SIMPLE_FIFO_SIZE 32
jedh 0:f6d3b930f382 38
jedh 0:f6d3b930f382 39 #include "simple_fifo.h"
jedh 0:f6d3b930f382 40
jedh 0:f6d3b930f382 41 #endif /* #ifdef USE_SIMPLE_FIFO*/
jedh 0:f6d3b930f382 42
jedh 0:f6d3b930f382 43 FIFO_FLOAT scope_data;
jedh 0:f6d3b930f382 44 int samples_active=0;
jedh 0:f6d3b930f382 45 float voltage_level;
jedh 0:f6d3b930f382 46 float ftemp;
jedh 0:f6d3b930f382 47 char timestr[80];
jedh 0:f6d3b930f382 48
jedh 0:f6d3b930f382 49 int sample_count=0;
jedh 0:f6d3b930f382 50
jedh 0:f6d3b930f382 51 AnalogIn voltage_sensor(PTB0);
jedh 0:f6d3b930f382 52 InterruptIn left_button(PTC12); /* button labeled SW3, wired active-low needing pull-up */
jedh 0:f6d3b930f382 53 Ticker t;
jedh 0:f6d3b930f382 54 #define SAMPLE_FREQUENCY 5000.0
jedh 0:f6d3b930f382 55
jedh 0:f6d3b930f382 56 void take_sample()
jedh 0:f6d3b930f382 57 {
jedh 0:f6d3b930f382 58 if(samples_active==1)
jedh 0:f6d3b930f382 59 {
jedh 0:f6d3b930f382 60 voltage_level=voltage_sensor.read();
jedh 0:f6d3b930f382 61 /*if(fifo_float_writable(&scope_data))
jedh 0:f6d3b930f382 62 {
jedh 0:f6d3b930f382 63 fifo_float_write(&scope_data,voltage_level);
jedh 0:f6d3b930f382 64 }
jedh 0:f6d3b930f382 65 else
jedh 0:f6d3b930f382 66 {
jedh 0:f6d3b930f382 67 printf("fifo overrun\n");
jedh 0:f6d3b930f382 68 samples_active=0;
jedh 0:f6d3b930f382 69 }*/
jedh 0:f6d3b930f382 70
jedh 0:f6d3b930f382 71
jedh 0:f6d3b930f382 72 sample_count++;
jedh 0:f6d3b930f382 73 }
jedh 0:f6d3b930f382 74 if(sample_count>= 2000)
jedh 0:f6d3b930f382 75 {
jedh 0:f6d3b930f382 76 samples_active=0;
jedh 0:f6d3b930f382 77 }
jedh 0:f6d3b930f382 78 }
jedh 0:f6d3b930f382 79
jedh 0:f6d3b930f382 80 void start_sample()
jedh 0:f6d3b930f382 81 {
jedh 0:f6d3b930f382 82 if(samples_active==0)
jedh 0:f6d3b930f382 83 {
jedh 0:f6d3b930f382 84 fifo_float_init(&scope_data);
jedh 0:f6d3b930f382 85 sample_count=0;
jedh 0:f6d3b930f382 86 samples_active=1;
jedh 0:f6d3b930f382 87 }
jedh 0:f6d3b930f382 88 else
jedh 0:f6d3b930f382 89 {
jedh 0:f6d3b930f382 90 samples_active=0;
jedh 0:f6d3b930f382 91 }
jedh 0:f6d3b930f382 92
jedh 0:f6d3b930f382 93 }
jedh 0:f6d3b930f382 94
jedh 0:f6d3b930f382 95 int main()
jedh 0:f6d3b930f382 96 {
jedh 0:f6d3b930f382 97
jedh 0:f6d3b930f382 98 #ifdef USE_PC
jedh 0:f6d3b930f382 99 pc.baud(115200); /* set serial port speed to 115200 - must be matched in terminal emulator */
jedh 0:f6d3b930f382 100 #endif
jedh 0:f6d3b930f382 101 #ifdef USE_SLCD
jedh 0:f6d3b930f382 102 slcd.All_Segments(0);
jedh 0:f6d3b930f382 103 #endif
jedh 0:f6d3b930f382 104 #ifdef USE_CHARACTER_LCD
jedh 0:f6d3b930f382 105 character_lcd_initialize();
jedh 0:f6d3b930f382 106 #endif
jedh 0:f6d3b930f382 107
jedh 0:f6d3b930f382 108 fifo_float_init(&scope_data);
jedh 0:f6d3b930f382 109
jedh 0:f6d3b930f382 110 #ifdef USE_PC
jedh 0:f6d3b930f382 111 pc.printf("\n");
jedh 0:f6d3b930f382 112 #endif
jedh 0:f6d3b930f382 113
jedh 0:f6d3b930f382 114 get_DS1307_time(&EE3420_DS1307_data);
jedh 0:f6d3b930f382 115 set_KL46Z_RTC_from_DS1307();
jedh 0:f6d3b930f382 116
jedh 0:f6d3b930f382 117 sprintf(filename,"/sd/voltage.csv");
jedh 0:f6d3b930f382 118
jedh 0:f6d3b930f382 119 FILE *fp = fopen(filename, "w");
jedh 0:f6d3b930f382 120 if(fp == NULL)
jedh 0:f6d3b930f382 121 {
jedh 0:f6d3b930f382 122 error("Could not open file ""%s"" for write\n", filename);
jedh 0:f6d3b930f382 123 }
jedh 0:f6d3b930f382 124 fprintf(fp,"date, time, scopes\n");
jedh 0:f6d3b930f382 125 fclose(fp);
jedh 0:f6d3b930f382 126
jedh 0:f6d3b930f382 127 samples_active=0;
jedh 0:f6d3b930f382 128
jedh 0:f6d3b930f382 129 left_button.mode(PullUp);
jedh 0:f6d3b930f382 130 left_button.fall(&start_sample);
jedh 0:f6d3b930f382 131
jedh 0:f6d3b930f382 132 t.attach(&take_sample,1.0/SAMPLE_FREQUENCY);
jedh 0:f6d3b930f382 133
jedh 0:f6d3b930f382 134 while(true)
jedh 0:f6d3b930f382 135 {
jedh 0:f6d3b930f382 136
jedh 0:f6d3b930f382 137 if(fifo_float_readable(&scope_data))
jedh 0:f6d3b930f382 138 {
jedh 0:f6d3b930f382 139
jedh 0:f6d3b930f382 140 fp = fopen(filename, "a");
jedh 0:f6d3b930f382 141 while(fifo_float_readable(&scope_data))
jedh 0:f6d3b930f382 142 {
jedh 0:f6d3b930f382 143 //get_DS1307_time(&EE3420_DS1307_data);
jedh 0:f6d3b930f382 144 //format_time_string_24(&EE3420_DS1307_data, timestr);
jedh 0:f6d3b930f382 145 fifo_float_read(&scope_data,&ftemp);
jedh 0:f6d3b930f382 146 //fprintf(fp, "20%02i/%02i/%02i, %02i:%02i:%02i, %f\n",EE3420_DS1307_data.year,EE3420_DS1307_data.month,
jedh 0:f6d3b930f382 147 //EE3420_DS1307_data.day,EE3420_DS1307_data.hours,EE3420_DS1307_data.min,EE3420_DS1307_data.sec,ftemp);
jedh 0:f6d3b930f382 148 fprintf(fp, "%f\n",ftemp);
jedh 0:f6d3b930f382 149 pc.printf("%f\n",ftemp);
jedh 0:f6d3b930f382 150 }
jedh 0:f6d3b930f382 151 fclose(fp);
jedh 0:f6d3b930f382 152 }
jedh 0:f6d3b930f382 153 }
jedh 0:f6d3b930f382 154 } //end of main()
jedh 0:f6d3b930f382 155
jedh 0:f6d3b930f382 156