Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "C12832.h" 00003 #include "microteslameter.h" 00004 #include "MLX90393.h" 00005 #define FIELD_LIMIT 100 00006 00007 // Global variable: 00008 00009 PwmOut r (p23); //PWM output for the RBG led 00010 PwmOut g (p24); //PWM output for the RBG led 00011 PwmOut b (p25); //PWM output for the RBG led 00012 AnalogIn p1(p19); // define analog input on p19 just for debug purpose 00013 C12832 lcd(p5, p7, p6, p8, p11); //define pin for LCD screen 00014 InterruptIn button(p14); // button used for zeroing - joystick 00015 I2C i2c(p28, p27); 00016 Serial pc(d+, d-); 00017 Timeout to1; // timeer... 00018 MLX90393 sensor(MLX90393::i2c_address,&i2c); 00019 00020 float CorrX,CorrY,CorrZ; 00021 bool Led_on=false; 00022 bool Blink_started=false; 00023 bool CleanScreen=false; 00024 00025 USBSerial serial; 00026 00027 int main() 00028 { 00029 float X,Y,Z,modulus,T; 00030 int content_buffer[63]; 00031 char read_buffer[11]; 00032 00033 init(); 00034 00035 lcd.cls(); // clean screen 00036 button.rise(&startZeroing); //link button on the joystick with the start of zeroing 00037 00038 pc.printf("Microteslameter"); 00039 if(1)//activate deactivate memory dump 00040 { 00041 pc.printf("Memory dump at startup: \n"); 00042 for (int i = 0; i<63; i++) { 00043 sensor.RR(read_buffer,i,0); 00044 content_buffer[i] = (read_buffer[0]*65536)+(read_buffer[1]*256) + read_buffer[2]; 00045 } 00046 for (int i = 0; i<63; i++) { 00047 pc.printf("%i \n",content_buffer[i]); 00048 } 00049 } 00050 //sensor.WR(read_buffer, 2, 2016, 0); //put DIF_FILT at max 00051 00052 while(1) 00053 { 00054 //X=p1.read()*200; //use potentiometer for debug 00055 //Y=0; 00056 //Z=0; 00057 00058 MeasureXYZT(&X,&Y,&Z,&T); 00059 CorrectXYZ(&X,&Y,&Z); // correct for zeroing and convert LSB in uT 00060 modulus=calculateModulus(X,Y,Z); 00061 //--------------- interface---------------- 00062 setColor(normalize(modulus)); 00063 updateLCD(X,Y,Z,modulus); 00064 pc.printf("T= %f,X= %f, Y= %f, Z= %f\n",T,X,Y,Z); 00065 } 00066 } 00067 00068 00069 void init(void) 00070 { 00071 b=1; // RGB led off 00072 r=1; // RGB led off 00073 g=1; // RGB led off 00074 i2c.frequency(7000); 00075 } 00076 00077 00078 void setColor(int intensity) // give intensity color from green 0% to red 100% 00079 { 00080 float duty_cycle; 00081 if(intensity>=100) //if intensity if greater than 100 blink the led in red 00082 { 00083 if(Blink_started==false) to1.attach(&blink, 0.2); //attach timer to the blink function to start blinking the led, only if this was not done previously 00084 } 00085 else 00086 { 00087 to1.detach(); //detach timer to stop blinking 00088 Blink_started=false; 00089 } 00090 00091 duty_cycle=100-intensity; 00092 duty_cycle=duty_cycle/100; 00093 r=duty_cycle; //balance between red and green depending on intensity 00094 g=1-duty_cycle; 00095 } 00096 00097 void updateLCD(float X,float Y,float Z,float modulus) 00098 { 00099 if(CleanScreen==true) // clean screen in case the screen displayed something else 00100 { 00101 lcd.cls(); 00102 CleanScreen=false; 00103 } 00104 //lcd.setmode(XOR); 00105 lcd.line(lcd.width()/2, 0, lcd.width()/2, lcd.height(), 1); // vertical line to separate field to norm... 00106 lcd.locate(0,0); 00107 lcd.printf("X= %7.1fuT",X); 00108 lcd.locate(0,10); 00109 lcd.printf("Y= %7.1fuT",Y); 00110 lcd.locate(0,20); 00111 lcd.printf("Z= %7.1fuT",Z); 00112 lcd.locate((lcd.width()/2)+5,0); 00113 lcd.printf("n= %7.1fuT",modulus); 00114 if(modulus>FIELD_LIMIT) 00115 { 00116 lcd.locate((lcd.width()/2)+20,15); 00117 lcd.printf("ALERT"); 00118 lcd.print_bm(bitmWarning,(lcd.width()/2)+2,15); // print warning sign 00119 lcd.copy_to_lcd(); // update lcd 00120 } 00121 else 00122 { 00123 lcd.locate((lcd.width()/2)+2,15); 00124 lcd.printf(" "); // clean the warning sign 00125 } 00126 wait(0.010); // needed? 00127 00128 } 00129 00130 00131 float calculateModulus(float X, float Y, float Z) 00132 { 00133 float norm; 00134 norm=X*X; 00135 norm+=Y*Y; 00136 norm+=Z*Z; 00137 norm=sqrt(norm); 00138 return norm; 00139 00140 } 00141 00142 float normalize(float field) //normalize the field versus the field limit to have something in percent of field limit 00143 { 00144 return (field/FIELD_LIMIT )*100; 00145 } 00146 00147 void blink() 00148 { 00149 if(Led_on==false) 00150 { 00151 r=0;//led on in red 00152 Led_on=true; 00153 } 00154 else 00155 { 00156 r=1; //led off 00157 Led_on=false; 00158 } 00159 to1.attach(&blink, 0.2); 00160 } 00161 00162 void MeasureXYZT(float *X,float *Y,float *Z, float *T) 00163 { 00164 char read_buffer[11]; 00165 sensor.SM(read_buffer, 15, 0); //start measurement 00166 wait(0.2);// needed? according the AN worse case (big filter and low conversion time) the measurment take 198.5 ms 00167 sensor.RM(read_buffer, 15, 0);//read measurement 00168 00169 // concatenate the two bytes 00170 *T=read_buffer[1]*256+read_buffer[2]; 00171 *X=read_buffer[3]*256+read_buffer[4]; 00172 *Y=read_buffer[5]*256+read_buffer[6]; 00173 *Z=read_buffer[7]*256+read_buffer[8]; 00174 00175 //deal with sign 00176 if(*X>32768) *X-=65536; 00177 if(*Y>32768) *Y-=65536; 00178 if(*Z>32768) *Z-=65536; 00179 } 00180 00181 void startZeroing() 00182 { 00183 float X,Y,Z,T; 00184 int iteration=20;// to be defined 00185 //char read_buffer[11]; 00186 CorrX=0;CorrY=0;CorrZ=0; 00187 lcd.cls(); 00188 for(int i=0;i<iteration;i++) 00189 { 00190 MeasureXYZT(&X,&Y,&Z,&T); 00191 CorrX+=X; 00192 CorrY+=Y; 00193 CorrZ+=Z; 00194 lcd.locate(0,0); 00195 lcd.printf("ZEROING progress: %i%%",i*100/iteration); 00196 } 00197 CorrX/=iteration; 00198 CorrY/=iteration; 00199 CorrZ/=iteration; 00200 CleanScreen=true; 00201 } 00202 00203 00204 00205 float getGainXY(int GAIN_SEL,int RES_XYZ) 00206 { 00207 return 2.576; //fix value for the moment... for RES_XYZ=3 and GAIN_SEL=4 00208 } 00209 00210 float getGainZ(int GAIN_SEL,int RES_XYZ) 00211 { 00212 return 4.698; //fix value for the moment... for RES_XYZ=3 and GAIN_SEL=4 00213 } 00214 00215 void CorrectXYZ(float *X,float *Y,float *Z) // correct for zeroing and convert LSB in uT 00216 { 00217 *X-=CorrX; 00218 *Y-=CorrY; 00219 *Z-=CorrZ; 00220 00221 *X= *X*getGainXY(0,0); 00222 *Y= *Y*getGainXY(0,0); 00223 *Z= *Z*getGainZ(0,0); 00224 }
Generated on Sun Jul 17 2022 16:26:31 by
