![](/media/cache/group/logo.001.png.50x50_q85.png)
51 52 with same code
Dependencies: MtSense07
Fork of MtConnect04S_MtSense07 by
main.cpp@2:87f1b98c0d09, 2017-03-23 (annotated)
- Committer:
- bcc6
- Date:
- Thu Mar 23 08:43:52 2017 +0000
- Revision:
- 2:87f1b98c0d09
- Parent:
- 1:40c18f027e6c
- Child:
- 4:4ca64a863fbf
Add interrupt test function for AK9750.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcc6 | 0:418880413158 | 1 | /* Copyright (c) 2016 MtM Technology Corporation, MIT License |
bcc6 | 0:418880413158 | 2 | * |
bcc6 | 0:418880413158 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bcc6 | 0:418880413158 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bcc6 | 0:418880413158 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bcc6 | 0:418880413158 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bcc6 | 0:418880413158 | 7 | * furnished to do so, subject to the following conditions: |
bcc6 | 0:418880413158 | 8 | * |
bcc6 | 0:418880413158 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
bcc6 | 0:418880413158 | 10 | * substantial portions of the Software. |
bcc6 | 0:418880413158 | 11 | * |
bcc6 | 0:418880413158 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bcc6 | 0:418880413158 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bcc6 | 0:418880413158 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bcc6 | 0:418880413158 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bcc6 | 0:418880413158 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bcc6 | 0:418880413158 | 17 | */ |
bcc6 | 0:418880413158 | 18 | #include "mbed.h" |
bcc6 | 0:418880413158 | 19 | #include "AK9750.h" |
bcc6 | 0:418880413158 | 20 | #include "AK09912.h" |
bcc6 | 0:418880413158 | 21 | #include "AK09970.h" |
bcc6 | 1:40c18f027e6c | 22 | #include "config.h" |
bcc6 | 1:40c18f027e6c | 23 | |
bcc6 | 0:418880413158 | 24 | Serial pc(p15, p16); |
bcc6 | 0:418880413158 | 25 | |
bcc6 | 0:418880413158 | 26 | DigitalOut reset_sensor(p4, 1); // Reset pin for AK09970 (Low active) |
bcc6 | 0:418880413158 | 27 | I2C i2c(p3, p2); |
bcc6 | 2:87f1b98c0d09 | 28 | AK9750 ak9750(i2c, p0); |
bcc6 | 0:418880413158 | 29 | AK09912 ak09912(i2c); |
bcc6 | 0:418880413158 | 30 | AK09970 ak09970(i2c); |
bcc6 | 0:418880413158 | 31 | |
bcc6 | 2:87f1b98c0d09 | 32 | volatile bool isIntEvent = false; |
bcc6 | 2:87f1b98c0d09 | 33 | |
bcc6 | 2:87f1b98c0d09 | 34 | //============================================================================== |
bcc6 | 2:87f1b98c0d09 | 35 | static void IntEventCallback() { |
bcc6 | 2:87f1b98c0d09 | 36 | isIntEvent = true; |
bcc6 | 2:87f1b98c0d09 | 37 | } |
bcc6 | 2:87f1b98c0d09 | 38 | |
bcc6 | 2:87f1b98c0d09 | 39 | //============================================================================== |
bcc6 | 0:418880413158 | 40 | int main() { |
bcc6 | 0:418880413158 | 41 | /* Disable the hardware flow control of Serial, then show the mbed version */ |
bcc6 | 1:40c18f027e6c | 42 | pc.set_flow_control(SerialBase::Disabled); |
bcc6 | 1:40c18f027e6c | 43 | pc.baud(CONFIG_BAUDRATE); |
bcc6 | 0:418880413158 | 44 | pc.printf("\n"); |
bcc6 | 0:418880413158 | 45 | pc.printf("mbed version(%d.%d.%d)\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
bcc6 | 0:418880413158 | 46 | pc.printf("\n"); |
bcc6 | 0:418880413158 | 47 | |
bcc6 | 0:418880413158 | 48 | /* Reset sensor */ |
bcc6 | 0:418880413158 | 49 | reset_sensor = 1; |
bcc6 | 0:418880413158 | 50 | wait_ms(10); |
bcc6 | 0:418880413158 | 51 | reset_sensor = 0; |
bcc6 | 0:418880413158 | 52 | wait_ms(10); |
bcc6 | 0:418880413158 | 53 | reset_sensor = 1; |
bcc6 | 0:418880413158 | 54 | wait_ms(10); |
bcc6 | 0:418880413158 | 55 | |
bcc6 | 0:418880413158 | 56 | /* Config device and check device ID */ |
bcc6 | 0:418880413158 | 57 | uint8_t id; |
bcc6 | 0:418880413158 | 58 | |
bcc6 | 0:418880413158 | 59 | ak9750.ConfigDevice(); |
bcc6 | 0:418880413158 | 60 | ak9750.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 61 | pc.printf("AK9750_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 62 | if(id != ak9750.DEVICE_ID){ |
bcc6 | 0:418880413158 | 63 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 64 | while(1); |
bcc6 | 0:418880413158 | 65 | } |
bcc6 | 0:418880413158 | 66 | |
bcc6 | 0:418880413158 | 67 | ak09912.ConfigDevice(); |
bcc6 | 0:418880413158 | 68 | ak09912.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 69 | pc.printf("AK09912_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 70 | if(id != ak09912.DEVICE_ID){ |
bcc6 | 0:418880413158 | 71 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 72 | while(1); |
bcc6 | 0:418880413158 | 73 | } |
bcc6 | 0:418880413158 | 74 | |
bcc6 | 0:418880413158 | 75 | ak09970.ConfigDevice(); |
bcc6 | 0:418880413158 | 76 | ak09970.GetDeviceID(&id); |
bcc6 | 0:418880413158 | 77 | pc.printf("AK09970_DEVICE_ID(0x%02X)\n", id); |
bcc6 | 0:418880413158 | 78 | if(id != ak09970.DEVICE_ID){ |
bcc6 | 0:418880413158 | 79 | pc.printf("Read Device ID fail !\n"); |
bcc6 | 0:418880413158 | 80 | while(1); |
bcc6 | 0:418880413158 | 81 | } |
bcc6 | 0:418880413158 | 82 | |
bcc6 | 2:87f1b98c0d09 | 83 | |
bcc6 | 0:418880413158 | 84 | /* Main loop */ |
bcc6 | 1:40c18f027e6c | 85 | #if (CONFIG_RUN_MODE == RUN_ALL_SENSOR_COM) |
bcc6 | 0:418880413158 | 86 | AK9750::Data ak9750_data; |
bcc6 | 0:418880413158 | 87 | AK09912::Data ak09912_data; |
bcc6 | 0:418880413158 | 88 | AK09970::Data ak09970_data; |
bcc6 | 0:418880413158 | 89 | int32_t triggered_area; |
bcc6 | 0:418880413158 | 90 | while (1) { |
bcc6 | 0:418880413158 | 91 | ak9750.GetData(&ak9750_data); |
bcc6 | 0:418880413158 | 92 | triggered_area = ak9750.GetTriggeredAreaNum(&ak9750_data); |
bcc6 | 0:418880413158 | 93 | pc.printf("AK9750(%5.1fpA, %5.1fpA, %5.1fpA, %5.1fpA, %2.1f'C, Area%d)\n", |
bcc6 | 0:418880413158 | 94 | ak9750_data.ir1, ak9750_data.ir2, ak9750_data.ir3, ak9750_data.ir4, ak9750_data.tmp, triggered_area); |
bcc6 | 0:418880413158 | 95 | |
bcc6 | 0:418880413158 | 96 | ak09912.GetData(&ak09912_data); |
bcc6 | 0:418880413158 | 97 | pc.printf("AK09912(%5.1fuT/LSB, %5.1fuT/LSB, %5.1fuT/LSB, %2.1f'C)\n", |
bcc6 | 0:418880413158 | 98 | ak09912_data.x, ak09912_data.y, ak09912_data.z, ak09912_data.t); |
bcc6 | 0:418880413158 | 99 | |
bcc6 | 0:418880413158 | 100 | ak09970.GetData(&ak09970_data); |
bcc6 | 0:418880413158 | 101 | pc.printf("AK09970(%6.1fuT/LSB, %6.1fuT/LSB, %6.1fuT/LSB)\n", |
bcc6 | 0:418880413158 | 102 | ak09970_data.x, ak09970_data.y, ak09970_data.z); |
bcc6 | 0:418880413158 | 103 | |
bcc6 | 0:418880413158 | 104 | pc.printf("\n"); |
bcc6 | 0:418880413158 | 105 | wait(1); |
bcc6 | 0:418880413158 | 106 | } |
bcc6 | 1:40c18f027e6c | 107 | #elif (CONFIG_RUN_MODE == RUN_IR_AK9750_APP) |
bcc6 | 1:40c18f027e6c | 108 | AK9750::Data ak9750_data; |
bcc6 | 1:40c18f027e6c | 109 | int32_t count = 0; |
bcc6 | 1:40c18f027e6c | 110 | while (1) { |
bcc6 | 1:40c18f027e6c | 111 | ak9750.GetData(&ak9750_data); |
bcc6 | 1:40c18f027e6c | 112 | pc.printf(" 3:%d,%d,%d,%d,%d,\r", count, (int32_t)ak9750_data.ir1, (int32_t)ak9750_data.ir2, (int32_t)ak9750_data.ir3, (int32_t)ak9750_data.ir4); |
bcc6 | 1:40c18f027e6c | 113 | count++; |
bcc6 | 1:40c18f027e6c | 114 | wait(1); |
bcc6 | 1:40c18f027e6c | 115 | } |
bcc6 | 1:40c18f027e6c | 116 | #elif (CONFIG_RUN_MODE == RUN_COMPASS_AK09912_APP) |
bcc6 | 1:40c18f027e6c | 117 | AK09912::Data ak09912_data; |
bcc6 | 1:40c18f027e6c | 118 | int32_t count = 0; |
bcc6 | 1:40c18f027e6c | 119 | while (1) { |
bcc6 | 1:40c18f027e6c | 120 | ak09912.GetData(&ak09912_data); |
bcc6 | 1:40c18f027e6c | 121 | pc.printf("C:%d,%d,%d,%d\r", count, (int32_t)ak09912_data.x, (int32_t)ak09912_data.y, (int32_t)ak09912_data.z); |
bcc6 | 1:40c18f027e6c | 122 | count++; |
bcc6 | 1:40c18f027e6c | 123 | wait(1); |
bcc6 | 1:40c18f027e6c | 124 | } |
bcc6 | 1:40c18f027e6c | 125 | #elif (CONFIG_RUN_MODE == RUN_MAGNETIC_AK09970_APP) |
bcc6 | 1:40c18f027e6c | 126 | AK09970::Data ak09970_data; |
bcc6 | 1:40c18f027e6c | 127 | int32_t count = 0; |
bcc6 | 1:40c18f027e6c | 128 | while (1) { |
bcc6 | 1:40c18f027e6c | 129 | ak09970.GetData(&ak09970_data); |
bcc6 | 1:40c18f027e6c | 130 | pc.printf("C:%d,%d,%d,%d\r", count, (int32_t)ak09970_data.x, (int32_t)ak09970_data.y, (int32_t)ak09970_data.z); |
bcc6 | 1:40c18f027e6c | 131 | count++; |
bcc6 | 1:40c18f027e6c | 132 | wait(1); |
bcc6 | 1:40c18f027e6c | 133 | } |
bcc6 | 2:87f1b98c0d09 | 134 | #elif (CONFIG_RUN_MODE == RUN_IR_AK9750_INT_COM) |
bcc6 | 2:87f1b98c0d09 | 135 | // Enable interrupt sources and set callback function |
bcc6 | 2:87f1b98c0d09 | 136 | ak9750.SetIntEvent(AK9750::INT_DR, &IntEventCallback); |
bcc6 | 2:87f1b98c0d09 | 137 | |
bcc6 | 2:87f1b98c0d09 | 138 | while (1) { |
bcc6 | 2:87f1b98c0d09 | 139 | /* If any interrupt occurred */ |
bcc6 | 2:87f1b98c0d09 | 140 | if (isIntEvent) { |
bcc6 | 2:87f1b98c0d09 | 141 | /* Read interrupt status */ |
bcc6 | 2:87f1b98c0d09 | 142 | uint8_t status = ak9750.GetIntStatus(); |
bcc6 | 2:87f1b98c0d09 | 143 | if (status & AK9750::INT_DR) { |
bcc6 | 2:87f1b98c0d09 | 144 | pc.printf("INT_DR\n"); |
bcc6 | 2:87f1b98c0d09 | 145 | /* Read data */ |
bcc6 | 2:87f1b98c0d09 | 146 | AK9750::Data data; |
bcc6 | 2:87f1b98c0d09 | 147 | ak9750.GetData(&data); |
bcc6 | 2:87f1b98c0d09 | 148 | pc.printf("AK9750(%5.1fpA, %5.1fpA, %5.1fpA, %5.1fpA)\n", data.ir1, data.ir2, data.ir3, data.ir4); |
bcc6 | 2:87f1b98c0d09 | 149 | } |
bcc6 | 2:87f1b98c0d09 | 150 | if (status & AK9750::INT_IR24L) pc.printf("INT_IR24L\n"); |
bcc6 | 2:87f1b98c0d09 | 151 | if (status & AK9750::INT_IR24H) pc.printf("INT_IR24H\n"); |
bcc6 | 2:87f1b98c0d09 | 152 | if (status & AK9750::INT_IR13L) pc.printf("INT_IR13L\n"); |
bcc6 | 2:87f1b98c0d09 | 153 | if (status & AK9750::INT_IR13H) pc.printf("INT_IR13H\n"); |
bcc6 | 2:87f1b98c0d09 | 154 | isIntEvent = false; |
bcc6 | 2:87f1b98c0d09 | 155 | } |
bcc6 | 2:87f1b98c0d09 | 156 | } |
bcc6 | 1:40c18f027e6c | 157 | #else |
bcc6 | 1:40c18f027e6c | 158 | #errror "Config run mode ...FAIL" |
bcc6 | 1:40c18f027e6c | 159 | #endif |
bcc6 | 0:418880413158 | 160 | } |