By Michael Wasko and Haoran Xiao
Dependencies: MAG3110 MMA8451Q SLCD TSI mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 #include "MAG3110.h" 00004 #include "TSISensor.h" 00005 #include "SLCD.h" 00006 00007 #define ACC_ID 0 00008 #define MAG_ID 1 00009 #define ADC_ID 2 00010 #define TSI_ID 3 00011 #define NUM_OF_SENSOR 4 00012 00013 union Serial_Float{ 00014 float f; 00015 char bt[4]; 00016 }; 00017 00018 union Serial_Int{ 00019 int i; 00020 char bt[4]; 00021 }; 00022 00023 typedef union _data { 00024 float f; 00025 char s[4]; 00026 } myData; 00027 00028 Serial pc(USBTX, USBRX); 00029 MMA8451Q acc(PTE25,PTE24,0x1d<<1); 00030 MAG3110 mag(PTE25,PTE24); 00031 AnalogIn adc(PTE30); 00032 TSISensor tsi; 00033 SLCD slcd; 00034 DigitalOut led2(LED2); 00035 bool sirq_flags[NUM_OF_SENSOR] = {0}; // interrupt flags 00036 int SR = 0; // total sampling rate 00037 00038 void send(float data, int id); 00039 void send(int data, int id); 00040 void read_cmd(); 00041 void display(int val); 00042 00043 Ticker acc_timer; 00044 Ticker mag_timer; 00045 Ticker adc_timer; 00046 Ticker tsi_timer; 00047 void acc_timer_irq(); 00048 void mag_timer_irq(); 00049 void adc_timer_irq(); 00050 void tsi_timer_irq(); 00051 00052 // initial sampling frequency: 00053 float sp_freq[NUM_OF_SENSOR] = {1,1,1,1}; 00054 // current sensor states: 00055 int sp_flag[NUM_OF_SENSOR] = {0}; 00056 00057 int main(){ 00058 // Initialize sampling rate for display 00059 SR = 3*sp_freq[0]*sp_flag[0]+sp_freq[1]*sp_flag[1]+sp_freq[2]*sp_flag[2]+sp_freq[3]*sp_flag[3]; 00060 led2 = 1; 00061 // set baud rate to 460800 00062 pc.baud(460800); 00063 // display current sampling rate on LCD 00064 display(SR); 00065 while(1){ 00066 // check requests, execute and clear 00067 if(sirq_flags[ACC_ID]){ 00068 send(acc.getAccX(),0); 00069 send(acc.getAccY(),1); 00070 send(acc.getAccZ(),2); 00071 sirq_flags[ACC_ID] = 0; 00072 } 00073 if(sirq_flags[MAG_ID]){ 00074 send(mag.readVal(0x1d),3); 00075 sirq_flags[MAG_ID] = 0; 00076 } 00077 if(sirq_flags[ADC_ID]){ 00078 send(float(adc.read()*3.3),4); 00079 sirq_flags[ADC_ID] = 0; 00080 } 00081 if(sirq_flags[TSI_ID]){ 00082 send(tsi.readPercentage(),5); 00083 sirq_flags[TSI_ID] = 0; 00084 } 00085 if(pc.readable()){ 00086 read_cmd(); 00087 // refresh current sampling rate after the command is read: 00088 SR = 3*sp_freq[0]*sp_flag[0]+sp_freq[1]*sp_flag[1]+sp_freq[2]*sp_flag[2]+sp_freq[3]*sp_flag[3]; 00089 display(SR); 00090 } 00091 } 00092 } 00093 00094 // interrupt functions for each ticker 00095 void acc_timer_irq(){ 00096 sirq_flags[ACC_ID] = 1; 00097 } 00098 void mag_timer_irq(){ 00099 sirq_flags[MAG_ID] = 1; 00100 } 00101 void adc_timer_irq(){ 00102 sirq_flags[ADC_ID] = 1; 00103 } 00104 void tsi_timer_irq(){ 00105 sirq_flags[TSI_ID] = 1; 00106 } 00107 00108 // serial write (overloaded with different data type) 00109 void send(float data,int id){ 00110 Serial_Float srdata; 00111 srdata.f = data; 00112 for(int i = 0; i < 4; i++) 00113 pc.putc(srdata.bt[i]); 00114 pc.putc('0'+id); 00115 } 00116 void send(int data, int id){ 00117 Serial_Int srdata; 00118 srdata.i = data; 00119 for(int i = 0; i < 4; i++) 00120 pc.putc(srdata.bt[i]); 00121 pc.putc('0'+id); 00122 } 00123 00124 // read command 00125 void read_cmd(){ 00126 __disable_irq(); 00127 led2 = !led2; 00128 char op; char id; myData freq; 00129 while(pc.readable()){//Flush input 00130 pc.getc(); 00131 } 00132 pc.putc('r'); //confirm ready 00133 op = pc.getc(); // get operation 00134 id = pc.getc(); // get sensor id 00135 if(op == 'c') { 00136 if (id == '0'){ 00137 acc_timer.detach(); 00138 sp_flag[ACC_ID] = 0; 00139 } 00140 else if (id == '1'){ 00141 mag_timer.detach(); 00142 sp_flag[MAG_ID] = 0; 00143 } 00144 else if (id == '2'){ 00145 adc_timer.detach(); 00146 sp_flag[ADC_ID] = 0; 00147 } 00148 else if (id == '3'){ 00149 tsi_timer.detach(); 00150 sp_flag[TSI_ID] = 0; 00151 } 00152 } 00153 if(op == 'o') { 00154 for (int i=0; i<4; i++) { 00155 freq.s[i] = pc.getc(); 00156 } 00157 switch(id){ 00158 case '0': 00159 sp_freq[0] = freq.f; 00160 acc_timer.attach(&acc_timer_irq, 1.0/freq.f); 00161 sp_flag[ACC_ID] = 1; 00162 break; 00163 case '1': 00164 sp_freq[1] = freq.f; 00165 mag_timer.attach(&mag_timer_irq, 1.0/freq.f); 00166 sp_flag[MAG_ID] = 1; 00167 break; 00168 case '2': 00169 sp_freq[2] = freq.f; 00170 adc_timer.attach(&adc_timer_irq, 1.0/freq.f); 00171 sp_flag[ADC_ID] = 1; 00172 break; 00173 case '3': 00174 sp_freq[3] = freq.f; 00175 tsi_timer.attach(&tsi_timer_irq, 1.0/freq.f); 00176 sp_flag[TSI_ID] = 1; 00177 break; 00178 } 00179 } 00180 led2 = !led2; 00181 __enable_irq(); 00182 } 00183 00184 // LCD function 00185 void display(int val){ 00186 char buf[16]; 00187 sprintf(buf,"%d%d%d",val/100,(val%100)/10,val%10); 00188 slcd.printf(buf); 00189 slcd.Home(); 00190 }
Generated on Sun Jul 17 2022 17:28:47 by 1.7.2