https://www.hackster.io/projects/bfb38d
Dependencies: mbed MMA8652 TextLCD MAG3110
main.cpp@3:2cc1ff874a75, 2019-04-17 (annotated)
- Committer:
- suntopbd
- Date:
- Wed Apr 17 04:28:33 2019 +0000
- Revision:
- 3:2cc1ff874a75
- Parent:
- 2:ad0b044d0a10
using microbit on board sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 1:7418a52375a0 | 1 | |
simon | 0:334327d1a416 | 2 | #include "mbed.h" |
simon | 0:334327d1a416 | 3 | #include "TextLCD.h" |
suntopbd | 3:2cc1ff874a75 | 4 | #include "MAG3110.h" |
suntopbd | 3:2cc1ff874a75 | 5 | #include "MMA8652.h" |
simon | 0:334327d1a416 | 6 | |
suntopbd | 3:2cc1ff874a75 | 7 | MAG3110 mag(P20, P19); // i2c pins SDA, SCL for magnetometer // |
suntopbd | 3:2cc1ff874a75 | 8 | MMA8652 acc(P20, P19); // i2c pins SDA, SCL for accelerometer // |
suntopbd | 3:2cc1ff874a75 | 9 | // LCD pins rs, en, d4, d5, d6, d7 and 16x2 lcd // |
suntopbd | 3:2cc1ff874a75 | 10 | TextLCD lcd(P6, P8, P7, P13, P14, P15,TextLCD::LCD16x2); |
suntopbd | 3:2cc1ff874a75 | 11 | |
suntopbd | 3:2cc1ff874a75 | 12 | PwmOut ChargePump(P0); // pwm output to drive charge pump |
suntopbd | 3:2cc1ff874a75 | 13 | InterruptIn ButtonA(P5); // active low push switch with interrupt |
suntopbd | 3:2cc1ff874a75 | 14 | InterruptIn ButtonB(P11); // active low push switch with interrupt |
suntopbd | 3:2cc1ff874a75 | 15 | |
suntopbd | 3:2cc1ff874a75 | 16 | AnalogIn Vref_2v5(P3); // 2.5v ref on adc for vcc independent measurement |
suntopbd | 3:2cc1ff874a75 | 17 | AnalogIn ADCP4 (P4); |
suntopbd | 3:2cc1ff874a75 | 18 | AnalogIn ADCP10 (P10); |
suntopbd | 3:2cc1ff874a75 | 19 | |
suntopbd | 3:2cc1ff874a75 | 20 | float frequency = 9000.0f; // min 1.5 kHz for stable operation |
suntopbd | 3:2cc1ff874a75 | 21 | float duty = 50.0f; // min 20% to max 80 percent for stable operation |
suntopbd | 3:2cc1ff874a75 | 22 | float heading = 0.0; // magnetometer heading |
suntopbd | 3:2cc1ff874a75 | 23 | float Xg =0.0; float Yg = 0.0; float Zg = 0.0; |
suntopbd | 3:2cc1ff874a75 | 24 | uint16_t vref =0; |
suntopbd | 3:2cc1ff874a75 | 25 | volatile int selector = 1; |
simon | 0:334327d1a416 | 26 | |
suntopbd | 3:2cc1ff874a75 | 27 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 28 | void EnableChargePump (void) |
suntopbd | 3:2cc1ff874a75 | 29 | { |
suntopbd | 3:2cc1ff874a75 | 30 | // 10kHz drive signal for Diode-Capacitor DC-DC (3.3V ->5.5V) converter |
suntopbd | 3:2cc1ff874a75 | 31 | ChargePump.period(1.0f/frequency); |
suntopbd | 3:2cc1ff874a75 | 32 | ChargePump.write(duty/100.0f); // 50% duty |
suntopbd | 3:2cc1ff874a75 | 33 | wait(.5); // Let boosted voltage stabilize to 5.5v (lcd supply as load) |
suntopbd | 3:2cc1ff874a75 | 34 | } |
suntopbd | 3:2cc1ff874a75 | 35 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 36 | void calMag(void) |
suntopbd | 3:2cc1ff874a75 | 37 | { |
suntopbd | 3:2cc1ff874a75 | 38 | //magnetometer calibration: finding max and min of X, Y axis |
suntopbd | 3:2cc1ff874a75 | 39 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
suntopbd | 3:2cc1ff874a75 | 40 | while(selector==1) |
suntopbd | 3:2cc1ff874a75 | 41 | { |
suntopbd | 3:2cc1ff874a75 | 42 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 43 | lcd.printf("Press A to Start"); |
suntopbd | 3:2cc1ff874a75 | 44 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 45 | lcd.printf("Mag Calibration"); |
suntopbd | 3:2cc1ff874a75 | 46 | wait(.3); |
suntopbd | 3:2cc1ff874a75 | 47 | } |
suntopbd | 3:2cc1ff874a75 | 48 | selector=1; |
suntopbd | 3:2cc1ff874a75 | 49 | lcd.cls(); |
suntopbd | 3:2cc1ff874a75 | 50 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 51 | lcd.printf("Calibration is"); |
suntopbd | 3:2cc1ff874a75 | 52 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 53 | lcd.printf("Starting Now.."); |
suntopbd | 3:2cc1ff874a75 | 54 | wait(2); |
suntopbd | 3:2cc1ff874a75 | 55 | lcd.cls(); |
suntopbd | 3:2cc1ff874a75 | 56 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 57 | lcd.printf("Rotate like an"); |
suntopbd | 3:2cc1ff874a75 | 58 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 59 | lcd.printf("Infinity Symbol"); |
suntopbd | 3:2cc1ff874a75 | 60 | wait(3); |
suntopbd | 3:2cc1ff874a75 | 61 | lcd.cls(); |
suntopbd | 3:2cc1ff874a75 | 62 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 63 | lcd.printf("Calibrating now"); |
suntopbd | 3:2cc1ff874a75 | 64 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 65 | lcd.printf("Keep swinging.."); |
suntopbd | 3:2cc1ff874a75 | 66 | tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); |
suntopbd | 3:2cc1ff874a75 | 67 | tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); |
suntopbd | 3:2cc1ff874a75 | 68 | int sample = 0; |
suntopbd | 3:2cc1ff874a75 | 69 | |
suntopbd | 3:2cc1ff874a75 | 70 | while(sample<201) { |
suntopbd | 3:2cc1ff874a75 | 71 | wait(0.05); |
suntopbd | 3:2cc1ff874a75 | 72 | newX = mag.readVal(MAG_OUT_X_MSB); |
suntopbd | 3:2cc1ff874a75 | 73 | newY = mag.readVal(MAG_OUT_Y_MSB); |
suntopbd | 3:2cc1ff874a75 | 74 | if (newX > tempXmax) tempXmax = newX; |
suntopbd | 3:2cc1ff874a75 | 75 | if (newX < tempXmin) tempXmin = newX; |
suntopbd | 3:2cc1ff874a75 | 76 | if (newY > tempYmax) tempYmax = newY; |
suntopbd | 3:2cc1ff874a75 | 77 | if (newY < tempYmin) tempYmin = newY; |
suntopbd | 3:2cc1ff874a75 | 78 | sample++; |
suntopbd | 3:2cc1ff874a75 | 79 | } |
suntopbd | 3:2cc1ff874a75 | 80 | |
suntopbd | 3:2cc1ff874a75 | 81 | mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
suntopbd | 3:2cc1ff874a75 | 82 | lcd.cls(); |
suntopbd | 3:2cc1ff874a75 | 83 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 84 | lcd.printf("Calibration has"); |
suntopbd | 3:2cc1ff874a75 | 85 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 86 | lcd.printf("been completed"); |
suntopbd | 3:2cc1ff874a75 | 87 | } |
suntopbd | 3:2cc1ff874a75 | 88 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 89 | void getMagHeading(void) |
suntopbd | 3:2cc1ff874a75 | 90 | { |
suntopbd | 3:2cc1ff874a75 | 91 | heading = mag.getHeading();; // magnetometer heading |
suntopbd | 3:2cc1ff874a75 | 92 | wait(.1); |
suntopbd | 3:2cc1ff874a75 | 93 | } |
suntopbd | 3:2cc1ff874a75 | 94 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 95 | void getAccXYZ(void) |
suntopbd | 3:2cc1ff874a75 | 96 | { |
suntopbd | 3:2cc1ff874a75 | 97 | float temp[3]; |
suntopbd | 3:2cc1ff874a75 | 98 | acc.ReadXYZ(temp); // accelerometer |
suntopbd | 3:2cc1ff874a75 | 99 | Xg = 10.0*temp[0]; Yg = 10.0*temp[1]; Zg = 10.0*temp[2]; |
suntopbd | 3:2cc1ff874a75 | 100 | wait(.1); |
suntopbd | 3:2cc1ff874a75 | 101 | } |
suntopbd | 3:2cc1ff874a75 | 102 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 103 | void WhenAPressed() |
suntopbd | 3:2cc1ff874a75 | 104 | { |
suntopbd | 3:2cc1ff874a75 | 105 | // button A ISR |
suntopbd | 3:2cc1ff874a75 | 106 | if (selector<8) |
suntopbd | 3:2cc1ff874a75 | 107 | selector++; |
suntopbd | 3:2cc1ff874a75 | 108 | if(selector>=8) |
suntopbd | 3:2cc1ff874a75 | 109 | selector=8; |
suntopbd | 3:2cc1ff874a75 | 110 | wait(.2); |
suntopbd | 3:2cc1ff874a75 | 111 | } |
suntopbd | 3:2cc1ff874a75 | 112 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 113 | void WhenBPressed() |
suntopbd | 3:2cc1ff874a75 | 114 | { |
suntopbd | 3:2cc1ff874a75 | 115 | // button B ISR |
suntopbd | 3:2cc1ff874a75 | 116 | if (selector>1) |
suntopbd | 3:2cc1ff874a75 | 117 | selector--; |
suntopbd | 3:2cc1ff874a75 | 118 | if(selector<=1) |
suntopbd | 3:2cc1ff874a75 | 119 | selector=1; |
suntopbd | 3:2cc1ff874a75 | 120 | wait(.2); |
simon | 0:334327d1a416 | 121 | } |
suntopbd | 3:2cc1ff874a75 | 122 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 123 | void updateLCD (void) |
suntopbd | 3:2cc1ff874a75 | 124 | { |
suntopbd | 3:2cc1ff874a75 | 125 | lcd.cls(); |
suntopbd | 3:2cc1ff874a75 | 126 | |
suntopbd | 3:2cc1ff874a75 | 127 | |
suntopbd | 3:2cc1ff874a75 | 128 | if(selector==1) |
suntopbd | 3:2cc1ff874a75 | 129 | { |
suntopbd | 3:2cc1ff874a75 | 130 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 131 | lcd.printf("System Voltage"); |
suntopbd | 3:2cc1ff874a75 | 132 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 133 | lcd.printf("Vcc:%fVolt", ((2.5*1023.0)/Vref_2v5.read_u16())); |
suntopbd | 3:2cc1ff874a75 | 134 | } |
suntopbd | 3:2cc1ff874a75 | 135 | |
suntopbd | 3:2cc1ff874a75 | 136 | if(selector==2) |
suntopbd | 3:2cc1ff874a75 | 137 | { |
suntopbd | 3:2cc1ff874a75 | 138 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 139 | lcd.printf("Charge Pump"); |
suntopbd | 3:2cc1ff874a75 | 140 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 141 | lcd.printf("Freq:%dkHz Cyc:%d%",int(frequency/1000.0f),int(duty)); |
suntopbd | 3:2cc1ff874a75 | 142 | } |
suntopbd | 3:2cc1ff874a75 | 143 | |
suntopbd | 3:2cc1ff874a75 | 144 | if(selector==3) |
suntopbd | 3:2cc1ff874a75 | 145 | { |
suntopbd | 3:2cc1ff874a75 | 146 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 147 | lcd.printf("Reading ADC val"); |
suntopbd | 3:2cc1ff874a75 | 148 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 149 | lcd.printf("ADC P4:%d", (ADCP4.read_u16())); |
suntopbd | 3:2cc1ff874a75 | 150 | } |
suntopbd | 3:2cc1ff874a75 | 151 | |
suntopbd | 3:2cc1ff874a75 | 152 | if(selector==4) |
suntopbd | 3:2cc1ff874a75 | 153 | { |
suntopbd | 3:2cc1ff874a75 | 154 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 155 | lcd.printf("Reading ADC data"); |
suntopbd | 3:2cc1ff874a75 | 156 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 157 | lcd.printf("ADC P10:%d", (ADCP10.read_u16())); |
suntopbd | 3:2cc1ff874a75 | 158 | } |
suntopbd | 3:2cc1ff874a75 | 159 | |
suntopbd | 3:2cc1ff874a75 | 160 | if(selector==5) |
suntopbd | 3:2cc1ff874a75 | 161 | { |
suntopbd | 3:2cc1ff874a75 | 162 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 163 | lcd.printf("Mag Sensor data"); |
suntopbd | 3:2cc1ff874a75 | 164 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 165 | lcd.printf("Mag Heading:%d", int(heading)); |
suntopbd | 3:2cc1ff874a75 | 166 | } |
suntopbd | 3:2cc1ff874a75 | 167 | |
suntopbd | 3:2cc1ff874a75 | 168 | if(selector==6) |
suntopbd | 3:2cc1ff874a75 | 169 | { |
suntopbd | 3:2cc1ff874a75 | 170 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 171 | lcd.printf("Acc Sensor Xaxis"); |
suntopbd | 3:2cc1ff874a75 | 172 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 173 | lcd.printf("Xg:%f", Xg); |
suntopbd | 3:2cc1ff874a75 | 174 | } |
suntopbd | 3:2cc1ff874a75 | 175 | |
suntopbd | 3:2cc1ff874a75 | 176 | if(selector==7) |
suntopbd | 3:2cc1ff874a75 | 177 | { |
suntopbd | 3:2cc1ff874a75 | 178 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 179 | lcd.printf("Acc Sensor Yaxis"); |
suntopbd | 3:2cc1ff874a75 | 180 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 181 | lcd.printf("Yg:%f", Yg); |
suntopbd | 3:2cc1ff874a75 | 182 | } |
suntopbd | 3:2cc1ff874a75 | 183 | |
suntopbd | 3:2cc1ff874a75 | 184 | if(selector==8) |
suntopbd | 3:2cc1ff874a75 | 185 | { |
suntopbd | 3:2cc1ff874a75 | 186 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 187 | lcd.printf("Acc Sensor Zaxis"); |
suntopbd | 3:2cc1ff874a75 | 188 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 189 | lcd.printf("Zg:%f", Zg); |
suntopbd | 3:2cc1ff874a75 | 190 | } |
suntopbd | 3:2cc1ff874a75 | 191 | wait(.1); |
suntopbd | 3:2cc1ff874a75 | 192 | } |
suntopbd | 3:2cc1ff874a75 | 193 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 194 | ////////////////////////////////// main //////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 195 | //////////////////////////////////////////////////////////////////////////////// |
suntopbd | 3:2cc1ff874a75 | 196 | |
suntopbd | 3:2cc1ff874a75 | 197 | int main() |
suntopbd | 3:2cc1ff874a75 | 198 | { |
suntopbd | 3:2cc1ff874a75 | 199 | EnableChargePump(); // enable lcd power |
suntopbd | 3:2cc1ff874a75 | 200 | ButtonA.fall(&WhenAPressed); // enable falling edge interrupt on button A |
suntopbd | 3:2cc1ff874a75 | 201 | ButtonB.fall(&WhenBPressed); // enable falling edge interrupt on button B |
suntopbd | 3:2cc1ff874a75 | 202 | calMag(); // init magnetometer and calibrate |
suntopbd | 3:2cc1ff874a75 | 203 | acc.begin(); // init accelerometer |
suntopbd | 3:2cc1ff874a75 | 204 | |
suntopbd | 3:2cc1ff874a75 | 205 | lcd.locate(0,0); |
suntopbd | 3:2cc1ff874a75 | 206 | lcd.printf("Press A or B to"); |
suntopbd | 3:2cc1ff874a75 | 207 | lcd.locate(0,1); |
suntopbd | 3:2cc1ff874a75 | 208 | lcd.printf("check sensor info"); |
suntopbd | 3:2cc1ff874a75 | 209 | wait(2); |
suntopbd | 3:2cc1ff874a75 | 210 | ////////////////////// while //////////////// |
suntopbd | 3:2cc1ff874a75 | 211 | |
suntopbd | 3:2cc1ff874a75 | 212 | while(1) |
suntopbd | 3:2cc1ff874a75 | 213 | { |
suntopbd | 3:2cc1ff874a75 | 214 | updateLCD(); |
suntopbd | 3:2cc1ff874a75 | 215 | getMagHeading(); |
suntopbd | 3:2cc1ff874a75 | 216 | getAccXYZ(); |
suntopbd | 3:2cc1ff874a75 | 217 | |
suntopbd | 3:2cc1ff874a75 | 218 | } ///// end of while ..... |
suntopbd | 3:2cc1ff874a75 | 219 | |
suntopbd | 3:2cc1ff874a75 | 220 | } ///// end of main ...... |
suntopbd | 3:2cc1ff874a75 | 221 | |
suntopbd | 3:2cc1ff874a75 | 222 | //////////////////////////////// end of code /////////////////////////////////// |