Simple demo for exercising the board
Dependencies: MAX11300 MAX4822 OneWire Terminal ds3231 mbed
main.cpp@8:8e36fe48b351, 2016-08-22 (annotated)
- Committer:
- j3
- Date:
- Mon Aug 22 23:10:57 2016 +0000
- Revision:
- 8:8e36fe48b351
- Parent:
- 6:18a831a757b5
- Child:
- 9:a5fd406b24a6
Added saving 4-20mA cal coef too 1-Wire EEPROM.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:24b8a6f0a563 | 1 | /********************************************************************** |
j3 | 0:24b8a6f0a563 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:24b8a6f0a563 | 3 | * |
j3 | 0:24b8a6f0a563 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:24b8a6f0a563 | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:24b8a6f0a563 | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:24b8a6f0a563 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:24b8a6f0a563 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:24b8a6f0a563 | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:24b8a6f0a563 | 10 | * |
j3 | 0:24b8a6f0a563 | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:24b8a6f0a563 | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:24b8a6f0a563 | 13 | * |
j3 | 0:24b8a6f0a563 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:24b8a6f0a563 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:24b8a6f0a563 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:24b8a6f0a563 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:24b8a6f0a563 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:24b8a6f0a563 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:24b8a6f0a563 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:24b8a6f0a563 | 21 | * |
j3 | 0:24b8a6f0a563 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:24b8a6f0a563 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:24b8a6f0a563 | 24 | * Products, Inc. Branding Policy. |
j3 | 0:24b8a6f0a563 | 25 | * |
j3 | 0:24b8a6f0a563 | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:24b8a6f0a563 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:24b8a6f0a563 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:24b8a6f0a563 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:24b8a6f0a563 | 30 | * ownership rights. |
j3 | 0:24b8a6f0a563 | 31 | **********************************************************************/ |
j3 | 0:24b8a6f0a563 | 32 | |
j3 | 0:24b8a6f0a563 | 33 | #include "mbed.h" |
j3 | 0:24b8a6f0a563 | 34 | #include "ds3231.h" |
j3 | 0:24b8a6f0a563 | 35 | #include "MAX4822.h" |
j3 | 0:24b8a6f0a563 | 36 | #include "MAX11300.h" |
j3 | 0:24b8a6f0a563 | 37 | #include "Terminal.h" |
j3 | 0:24b8a6f0a563 | 38 | #include "OneWire.h" |
j3 | 0:24b8a6f0a563 | 39 | |
j3 | 0:24b8a6f0a563 | 40 | using namespace OneWire; |
j3 | 0:24b8a6f0a563 | 41 | using namespace RomCommands; |
j3 | 0:24b8a6f0a563 | 42 | |
j3 | 0:24b8a6f0a563 | 43 | void set_rtc(Terminal & term, Ds3231 & rtc); |
j3 | 0:24b8a6f0a563 | 44 | void get_time_date(Terminal & term, Ds3231 & rtc); |
j3 | 0:24b8a6f0a563 | 45 | void search_ow_bus(Terminal & term, DS2484 & owm); |
j3 | 0:24b8a6f0a563 | 46 | void print_rom_id(Terminal & term, RomId & romId); |
j3 | 8:8e36fe48b351 | 47 | bool calibrate_420_io(Terminal & term, MAX11300 & pixi, DS2484 & owm, uint16_t *calData, bool loopAlreadyCal); |
j3 | 5:2fe6dd435618 | 48 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector); |
j3 | 8:8e36fe48b351 | 49 | bool check_420_cal(Terminal & term, DS2484 & owm, uint16_t *calData); |
j3 | 0:24b8a6f0a563 | 50 | |
j3 | 0:24b8a6f0a563 | 51 | int main(void) |
j3 | 0:24b8a6f0a563 | 52 | { |
j3 | 0:24b8a6f0a563 | 53 | Terminal term(USBTX, USBRX); |
j3 | 0:24b8a6f0a563 | 54 | term.baud(57600); |
j3 | 0:24b8a6f0a563 | 55 | term.cls(); |
j3 | 0:24b8a6f0a563 | 56 | term.home(); |
j3 | 0:24b8a6f0a563 | 57 | term.printf("Starting Demo\n"); |
j3 | 0:24b8a6f0a563 | 58 | |
j3 | 0:24b8a6f0a563 | 59 | SPI spi_bus(D11, D12, D13); |
j3 | 0:24b8a6f0a563 | 60 | MAX4822 rly_drvr(spi_bus, D10); |
j3 | 0:24b8a6f0a563 | 61 | DigitalOut rly_drvr_set(D7, 1); |
j3 | 0:24b8a6f0a563 | 62 | DigitalOut rly_drvr_reset(D6, 1); |
j3 | 0:24b8a6f0a563 | 63 | |
j3 | 0:24b8a6f0a563 | 64 | rly_drvr.reset_all_relays(rly_drvr_reset); |
j3 | 0:24b8a6f0a563 | 65 | |
j3 | 1:03c04017a728 | 66 | MAX11300 pixi(spi_bus, D8, NC, D9); |
j3 | 0:24b8a6f0a563 | 67 | |
j3 | 0:24b8a6f0a563 | 68 | //set all analog outs to 0 |
j3 | 0:24b8a6f0a563 | 69 | for(uint8_t idx = 0; idx < 9; idx++) |
j3 | 0:24b8a6f0a563 | 70 | { |
j3 | 0:24b8a6f0a563 | 71 | pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(idx), 0); |
j3 | 0:24b8a6f0a563 | 72 | } |
j3 | 0:24b8a6f0a563 | 73 | |
j3 | 0:24b8a6f0a563 | 74 | //ensure latching relays are reset |
j3 | 0:24b8a6f0a563 | 75 | for(uint8_t idx = 0; idx < 3; idx++) |
j3 | 0:24b8a6f0a563 | 76 | { |
j3 | 0:24b8a6f0a563 | 77 | pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 1); |
j3 | 0:24b8a6f0a563 | 78 | wait_ms(100); |
j3 | 0:24b8a6f0a563 | 79 | pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(idx + 9), 0); |
j3 | 0:24b8a6f0a563 | 80 | } |
j3 | 0:24b8a6f0a563 | 81 | |
j3 | 0:24b8a6f0a563 | 82 | I2C i2c_bus(D14, D15); |
j3 | 0:24b8a6f0a563 | 83 | i2c_bus.frequency(400000); |
j3 | 0:24b8a6f0a563 | 84 | |
j3 | 0:24b8a6f0a563 | 85 | Ds3231 rtc(i2c_bus); |
j3 | 0:24b8a6f0a563 | 86 | |
j3 | 0:24b8a6f0a563 | 87 | DS2484 owm(i2c_bus); |
j3 | 0:24b8a6f0a563 | 88 | OneWireMaster::CmdResult owm_result = owm.OWInitMaster(); |
j3 | 0:24b8a6f0a563 | 89 | while(owm_result != OneWireMaster::Success) |
j3 | 0:24b8a6f0a563 | 90 | { |
j3 | 0:24b8a6f0a563 | 91 | wait_ms(100); |
j3 | 0:24b8a6f0a563 | 92 | term.printf("Failed to initialize OneWire master\n"); |
j3 | 0:24b8a6f0a563 | 93 | owm_result = owm.OWInitMaster(); |
j3 | 0:24b8a6f0a563 | 94 | } |
j3 | 0:24b8a6f0a563 | 95 | term.printf("OneWire master initialized\n\n"); |
j3 | 0:24b8a6f0a563 | 96 | |
j3 | 8:8e36fe48b351 | 97 | float aio_slope = 0; |
j3 | 8:8e36fe48b351 | 98 | float aio_offset = 0; |
j3 | 8:8e36fe48b351 | 99 | float aii_slope = 0; |
j3 | 8:8e36fe48b351 | 100 | float aii_offset = 0; |
j3 | 8:8e36fe48b351 | 101 | uint16_t calData[4]; |
j3 | 8:8e36fe48b351 | 102 | |
j3 | 8:8e36fe48b351 | 103 | bool currentLoopCal = false; |
j3 | 8:8e36fe48b351 | 104 | if(check_420_cal(term, owm, calData)) |
j3 | 8:8e36fe48b351 | 105 | { |
j3 | 8:8e36fe48b351 | 106 | aio_slope = (((calData[1]*1.0) - (calData[0]*1.0))/16.0); |
j3 | 8:8e36fe48b351 | 107 | aio_offset = ((-1.0*aio_slope*4.0) + calData[0]); |
j3 | 8:8e36fe48b351 | 108 | |
j3 | 8:8e36fe48b351 | 109 | aii_slope = (16.0/((calData[3]*1.0) - (calData[2]*1.0))); |
j3 | 8:8e36fe48b351 | 110 | aii_offset = ((-1.0*aii_slope*calData[2]) + 4.0); |
j3 | 8:8e36fe48b351 | 111 | |
j3 | 8:8e36fe48b351 | 112 | currentLoopCal = true; |
j3 | 8:8e36fe48b351 | 113 | } |
j3 | 8:8e36fe48b351 | 114 | |
j3 | 0:24b8a6f0a563 | 115 | int32_t user_entry = 0; |
j3 | 0:24b8a6f0a563 | 116 | int32_t relay = 0; |
j3 | 0:24b8a6f0a563 | 117 | |
j3 | 2:0c0994f70fd2 | 118 | uint16_t analog_out_ch, analog_val; |
j3 | 2:0c0994f70fd2 | 119 | float avo_volts, aio_mA, aii_mA; |
j3 | 0:24b8a6f0a563 | 120 | |
j3 | 2:0c0994f70fd2 | 121 | const int32_t QUIT = 15; |
j3 | 0:24b8a6f0a563 | 122 | |
j3 | 1:03c04017a728 | 123 | MAX11300::CmdResult pixi_result; |
j3 | 1:03c04017a728 | 124 | MAX4822::CmdResult rly_drvr_result; |
j3 | 1:03c04017a728 | 125 | |
j3 | 0:24b8a6f0a563 | 126 | while(user_entry != QUIT) |
j3 | 0:24b8a6f0a563 | 127 | { |
j3 | 2:0c0994f70fd2 | 128 | term.printf("1. Set RTC\n"); |
j3 | 2:0c0994f70fd2 | 129 | term.printf("2. Get Time/Date\n"); |
j3 | 2:0c0994f70fd2 | 130 | term.printf("3. Search OneWire bus\n"); |
j3 | 2:0c0994f70fd2 | 131 | term.printf("4. Set non-latching relay\n"); |
j3 | 2:0c0994f70fd2 | 132 | term.printf("5. Reset non-latching relay\n"); |
j3 | 2:0c0994f70fd2 | 133 | term.printf("6. Set all non-latching relays\n"); |
j3 | 2:0c0994f70fd2 | 134 | term.printf("7. Reset all non-latching relays\n"); |
j3 | 2:0c0994f70fd2 | 135 | term.printf("8. Set latching relay\n"); |
j3 | 2:0c0994f70fd2 | 136 | term.printf("9. Reset latching relay\n"); |
j3 | 0:24b8a6f0a563 | 137 | term.printf("10. Set 0-10 analog out\n"); |
j3 | 0:24b8a6f0a563 | 138 | term.printf("11. Set 4-20 out\n"); |
j3 | 0:24b8a6f0a563 | 139 | term.printf("12. Get 4-20 in\n"); |
j3 | 2:0c0994f70fd2 | 140 | term.printf("13. Calibrate 4-20 io\n"); |
j3 | 0:24b8a6f0a563 | 141 | term.printf("%d. Clear Screen\n", (QUIT - 1)); |
j3 | 0:24b8a6f0a563 | 142 | term.printf("%d. Quit\n\n", QUIT); |
j3 | 0:24b8a6f0a563 | 143 | |
j3 | 0:24b8a6f0a563 | 144 | user_entry = term.get_int32("Please select an option above: ", 1, QUIT); |
j3 | 0:24b8a6f0a563 | 145 | |
j3 | 0:24b8a6f0a563 | 146 | //get non-latching relay |
j3 | 0:24b8a6f0a563 | 147 | if((user_entry == 4) || (user_entry == 5)) |
j3 | 0:24b8a6f0a563 | 148 | { |
j3 | 0:24b8a6f0a563 | 149 | relay = term.get_int32("Select a relay from 1 to 8: ", 1, 8); |
j3 | 0:24b8a6f0a563 | 150 | } |
j3 | 0:24b8a6f0a563 | 151 | |
j3 | 0:24b8a6f0a563 | 152 | //get latching relay |
j3 | 0:24b8a6f0a563 | 153 | if((user_entry == 8) || (user_entry == 9)) |
j3 | 0:24b8a6f0a563 | 154 | { |
j3 | 0:24b8a6f0a563 | 155 | relay = (8 + term.get_int32("Select a relay from 1 to 3: ", 1, 3)); |
j3 | 0:24b8a6f0a563 | 156 | } |
j3 | 0:24b8a6f0a563 | 157 | |
j3 | 0:24b8a6f0a563 | 158 | switch(user_entry) |
j3 | 0:24b8a6f0a563 | 159 | { |
j3 | 0:24b8a6f0a563 | 160 | case 1: |
j3 | 0:24b8a6f0a563 | 161 | set_rtc(term, rtc); |
j3 | 0:24b8a6f0a563 | 162 | break; |
j3 | 0:24b8a6f0a563 | 163 | |
j3 | 0:24b8a6f0a563 | 164 | case 2: |
j3 | 0:24b8a6f0a563 | 165 | get_time_date(term, rtc); |
j3 | 0:24b8a6f0a563 | 166 | break; |
j3 | 0:24b8a6f0a563 | 167 | |
j3 | 0:24b8a6f0a563 | 168 | case 3: |
j3 | 0:24b8a6f0a563 | 169 | search_ow_bus(term, owm); |
j3 | 0:24b8a6f0a563 | 170 | break; |
j3 | 0:24b8a6f0a563 | 171 | |
j3 | 0:24b8a6f0a563 | 172 | case 4: |
j3 | 1:03c04017a728 | 173 | rly_drvr_result = rly_drvr.set_relay(static_cast<MAX4822::RelayChannel>(relay)); |
j3 | 1:03c04017a728 | 174 | if(rly_drvr_result != MAX4822::Success) |
j3 | 1:03c04017a728 | 175 | { |
j3 | 1:03c04017a728 | 176 | term.printf("Failed to set relay\n"); |
j3 | 1:03c04017a728 | 177 | } |
j3 | 0:24b8a6f0a563 | 178 | break; |
j3 | 0:24b8a6f0a563 | 179 | |
j3 | 0:24b8a6f0a563 | 180 | case 5: |
j3 | 1:03c04017a728 | 181 | rly_drvr_result = rly_drvr.reset_relay(static_cast<MAX4822::RelayChannel>(relay)); |
j3 | 1:03c04017a728 | 182 | if(rly_drvr_result != MAX4822::Success) |
j3 | 1:03c04017a728 | 183 | { |
j3 | 1:03c04017a728 | 184 | term.printf("Failed to reset relay\n"); |
j3 | 1:03c04017a728 | 185 | } |
j3 | 0:24b8a6f0a563 | 186 | break; |
j3 | 0:24b8a6f0a563 | 187 | |
j3 | 0:24b8a6f0a563 | 188 | case 6: |
j3 | 0:24b8a6f0a563 | 189 | rly_drvr.set_all_relays(rly_drvr_set); |
j3 | 0:24b8a6f0a563 | 190 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 191 | break; |
j3 | 0:24b8a6f0a563 | 192 | |
j3 | 0:24b8a6f0a563 | 193 | case 7: |
j3 | 0:24b8a6f0a563 | 194 | rly_drvr.reset_all_relays(rly_drvr_reset); |
j3 | 0:24b8a6f0a563 | 195 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 196 | break; |
j3 | 0:24b8a6f0a563 | 197 | |
j3 | 0:24b8a6f0a563 | 198 | case 8: |
j3 | 1:03c04017a728 | 199 | pixi_result = pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 1); |
j3 | 1:03c04017a728 | 200 | if(pixi_result != MAX11300::Success) |
j3 | 1:03c04017a728 | 201 | { |
j3 | 1:03c04017a728 | 202 | term.printf("Failed to set relay\n"); |
j3 | 1:03c04017a728 | 203 | } |
j3 | 0:24b8a6f0a563 | 204 | break; |
j3 | 0:24b8a6f0a563 | 205 | |
j3 | 0:24b8a6f0a563 | 206 | case 9: |
j3 | 1:03c04017a728 | 207 | pixi_result = pixi.gpio_write(static_cast<MAX11300::MAX11300_Ports>(relay), 0); |
j3 | 1:03c04017a728 | 208 | if(pixi_result != MAX11300::Success) |
j3 | 1:03c04017a728 | 209 | { |
j3 | 1:03c04017a728 | 210 | term.printf("Failed to reset relay\n"); |
j3 | 1:03c04017a728 | 211 | } |
j3 | 0:24b8a6f0a563 | 212 | break; |
j3 | 0:24b8a6f0a563 | 213 | |
j3 | 0:24b8a6f0a563 | 214 | case 10: |
j3 | 2:0c0994f70fd2 | 215 | analog_out_ch = (term.get_int32("Please select a 0-10V output; 1-8: ", 1, 8) - 1); |
j3 | 2:0c0994f70fd2 | 216 | avo_volts = term.get_float("Please enter a voltage from 0.0 to 10.0 volts: ", -0.1, 10.1); |
j3 | 2:0c0994f70fd2 | 217 | analog_val = static_cast<uint16_t>((avo_volts/10.0) * 4095); |
j3 | 1:03c04017a728 | 218 | |
j3 | 5:2fe6dd435618 | 219 | term.printf("DAC Code = %d\n", analog_val); |
j3 | 5:2fe6dd435618 | 220 | |
j3 | 2:0c0994f70fd2 | 221 | pixi_result = pixi.single_ended_dac_write(static_cast<MAX11300::MAX11300_Ports>(analog_out_ch), analog_val); |
j3 | 1:03c04017a728 | 222 | if(pixi_result != MAX11300::Success) |
j3 | 1:03c04017a728 | 223 | { |
j3 | 1:03c04017a728 | 224 | term.printf("Failed to set 0-10 out\n"); |
j3 | 1:03c04017a728 | 225 | } |
j3 | 0:24b8a6f0a563 | 226 | break; |
j3 | 0:24b8a6f0a563 | 227 | |
j3 | 0:24b8a6f0a563 | 228 | case 11: |
j3 | 8:8e36fe48b351 | 229 | if(currentLoopCal) |
j3 | 1:03c04017a728 | 230 | { |
j3 | 8:8e36fe48b351 | 231 | aio_mA = term.get_float("Please enter a current from 4.0 to 20.0 mA: ", 3.9, 20.1); |
j3 | 8:8e36fe48b351 | 232 | analog_val = static_cast<uint16_t>((aio_slope * aio_mA) + aio_offset); |
j3 | 8:8e36fe48b351 | 233 | |
j3 | 8:8e36fe48b351 | 234 | pixi_result = pixi.single_ended_dac_write(MAX11300::PORT8, analog_val); |
j3 | 8:8e36fe48b351 | 235 | if(pixi_result != MAX11300::Success) |
j3 | 8:8e36fe48b351 | 236 | { |
j3 | 8:8e36fe48b351 | 237 | term.printf("Failed to set 4-20 out\n"); |
j3 | 8:8e36fe48b351 | 238 | } |
j3 | 8:8e36fe48b351 | 239 | } |
j3 | 8:8e36fe48b351 | 240 | else |
j3 | 8:8e36fe48b351 | 241 | { |
j3 | 8:8e36fe48b351 | 242 | term.printf("Please calibrate 4-20mA loop first, option 13.\n"); |
j3 | 1:03c04017a728 | 243 | } |
j3 | 0:24b8a6f0a563 | 244 | break; |
j3 | 0:24b8a6f0a563 | 245 | |
j3 | 0:24b8a6f0a563 | 246 | case 12: |
j3 | 8:8e36fe48b351 | 247 | if(currentLoopCal) |
j3 | 1:03c04017a728 | 248 | { |
j3 | 8:8e36fe48b351 | 249 | pixi_result = pixi.single_ended_adc_read(MAX11300::PORT12, analog_val); |
j3 | 8:8e36fe48b351 | 250 | aii_mA = ((aii_slope*analog_val) + aii_offset); |
j3 | 8:8e36fe48b351 | 251 | if(pixi_result == MAX11300::Success) |
j3 | 8:8e36fe48b351 | 252 | { |
j3 | 8:8e36fe48b351 | 253 | term.printf("4-20 in = %.2fmA\n", aii_mA); |
j3 | 8:8e36fe48b351 | 254 | } |
j3 | 8:8e36fe48b351 | 255 | else |
j3 | 8:8e36fe48b351 | 256 | { |
j3 | 8:8e36fe48b351 | 257 | term.printf("Failed to read 4-20 in\n"); |
j3 | 8:8e36fe48b351 | 258 | } |
j3 | 1:03c04017a728 | 259 | } |
j3 | 1:03c04017a728 | 260 | else |
j3 | 1:03c04017a728 | 261 | { |
j3 | 8:8e36fe48b351 | 262 | term.printf("Please calibrate 4-20mA loop first, option 13.\n"); |
j3 | 1:03c04017a728 | 263 | } |
j3 | 0:24b8a6f0a563 | 264 | break; |
j3 | 0:24b8a6f0a563 | 265 | |
j3 | 2:0c0994f70fd2 | 266 | case 13: |
j3 | 8:8e36fe48b351 | 267 | if(calibrate_420_io(term, pixi, owm, calData, currentLoopCal)) |
j3 | 8:8e36fe48b351 | 268 | { |
j3 | 8:8e36fe48b351 | 269 | aio_slope = (((calData[1]*1.0) - (calData[0]*1.0))/16.0); |
j3 | 8:8e36fe48b351 | 270 | aio_offset = ((-1.0*aio_slope*4.0) + calData[0]); |
j3 | 8:8e36fe48b351 | 271 | |
j3 | 8:8e36fe48b351 | 272 | aii_slope = (16.0/((calData[3]*1.0) - (calData[2]*1.0))); |
j3 | 8:8e36fe48b351 | 273 | aii_offset = ((-1.0*aii_slope*calData[2]) + 4.0); |
j3 | 8:8e36fe48b351 | 274 | |
j3 | 8:8e36fe48b351 | 275 | currentLoopCal = true; |
j3 | 8:8e36fe48b351 | 276 | |
j3 | 8:8e36fe48b351 | 277 | term.printf("Calibration data saved to 1-Wire EEPROM\n"); |
j3 | 8:8e36fe48b351 | 278 | } |
j3 | 2:0c0994f70fd2 | 279 | break; |
j3 | 2:0c0994f70fd2 | 280 | |
j3 | 0:24b8a6f0a563 | 281 | case (QUIT - 1): |
j3 | 0:24b8a6f0a563 | 282 | term.cls(); |
j3 | 0:24b8a6f0a563 | 283 | term.home(); |
j3 | 0:24b8a6f0a563 | 284 | break; |
j3 | 0:24b8a6f0a563 | 285 | |
j3 | 0:24b8a6f0a563 | 286 | case QUIT: |
j3 | 0:24b8a6f0a563 | 287 | term.printf("\nEnding Program\n"); |
j3 | 0:24b8a6f0a563 | 288 | break; |
j3 | 0:24b8a6f0a563 | 289 | |
j3 | 0:24b8a6f0a563 | 290 | default: |
j3 | 0:24b8a6f0a563 | 291 | mbed_die(); |
j3 | 0:24b8a6f0a563 | 292 | break; |
j3 | 0:24b8a6f0a563 | 293 | } |
j3 | 0:24b8a6f0a563 | 294 | } |
j3 | 0:24b8a6f0a563 | 295 | } |
j3 | 0:24b8a6f0a563 | 296 | |
j3 | 0:24b8a6f0a563 | 297 | //********************************************************************* |
j3 | 0:24b8a6f0a563 | 298 | void set_rtc(Terminal & term, Ds3231 & rtc) |
j3 | 0:24b8a6f0a563 | 299 | { |
j3 | 0:24b8a6f0a563 | 300 | //default, use bit masks in ds3231.h for desired operation |
j3 | 0:24b8a6f0a563 | 301 | ds3231_cntl_stat_t rtc_control_status = {0,0}; |
j3 | 0:24b8a6f0a563 | 302 | ds3231_time_t rtc_time; |
j3 | 0:24b8a6f0a563 | 303 | ds3231_calendar_t rtc_calendar; |
j3 | 0:24b8a6f0a563 | 304 | |
j3 | 0:24b8a6f0a563 | 305 | rtc.set_cntl_stat_reg(rtc_control_status); |
j3 | 0:24b8a6f0a563 | 306 | |
j3 | 0:24b8a6f0a563 | 307 | //get day from user |
j3 | 0:24b8a6f0a563 | 308 | rtc_calendar.day = term.get_int32("\nPlease enter day of week, 1 for Sunday (1-7): ", 1, 7); |
j3 | 0:24b8a6f0a563 | 309 | |
j3 | 0:24b8a6f0a563 | 310 | //get day of month from user |
j3 | 0:24b8a6f0a563 | 311 | rtc_calendar.date = term.get_int32("\nPlease enter day of month (1-31): ", 1, 31); |
j3 | 0:24b8a6f0a563 | 312 | |
j3 | 0:24b8a6f0a563 | 313 | //get month from user |
j3 | 0:24b8a6f0a563 | 314 | rtc_calendar.month = term.get_int32("\nPlease enter the month, 1 for January (1-12): ", 1, 12); |
j3 | 0:24b8a6f0a563 | 315 | |
j3 | 0:24b8a6f0a563 | 316 | //get year from user |
j3 | 0:24b8a6f0a563 | 317 | rtc_calendar.year = term.get_int32("\nPlease enter the year (0-99): ", 0, 99); |
j3 | 0:24b8a6f0a563 | 318 | |
j3 | 0:24b8a6f0a563 | 319 | //Get time mode |
j3 | 0:24b8a6f0a563 | 320 | rtc_time.mode = term.get_int32("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1); |
j3 | 0:24b8a6f0a563 | 321 | |
j3 | 0:24b8a6f0a563 | 322 | if(rtc_time.mode) |
j3 | 0:24b8a6f0a563 | 323 | { |
j3 | 0:24b8a6f0a563 | 324 | //Get AM/PM status |
j3 | 0:24b8a6f0a563 | 325 | rtc_time.am_pm = term.get_int32("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1); |
j3 | 0:24b8a6f0a563 | 326 | //Get hour from user |
j3 | 0:24b8a6f0a563 | 327 | rtc_time.hours = term.get_int32("\nPlease enter the hour (1-12): ", 1, 12); |
j3 | 0:24b8a6f0a563 | 328 | } |
j3 | 0:24b8a6f0a563 | 329 | else |
j3 | 0:24b8a6f0a563 | 330 | { |
j3 | 0:24b8a6f0a563 | 331 | //Get hour from user |
j3 | 0:24b8a6f0a563 | 332 | rtc_time.hours = term.get_int32("\nPlease enter the hour (0-23): ", 0, 23); |
j3 | 0:24b8a6f0a563 | 333 | } |
j3 | 0:24b8a6f0a563 | 334 | |
j3 | 0:24b8a6f0a563 | 335 | //Get minutes from user |
j3 | 0:24b8a6f0a563 | 336 | rtc_time.minutes = term.get_int32("\nPlease enter the minute (0-59): ", 0, 59); |
j3 | 0:24b8a6f0a563 | 337 | |
j3 | 0:24b8a6f0a563 | 338 | |
j3 | 0:24b8a6f0a563 | 339 | //Get seconds from user |
j3 | 0:24b8a6f0a563 | 340 | rtc_time.seconds = term.get_int32("\nPlease enter the second (0-59): ", 0, 59); |
j3 | 0:24b8a6f0a563 | 341 | |
j3 | 0:24b8a6f0a563 | 342 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 343 | |
j3 | 0:24b8a6f0a563 | 344 | rtc.set_time(rtc_time); |
j3 | 0:24b8a6f0a563 | 345 | rtc.set_calendar(rtc_calendar); |
j3 | 0:24b8a6f0a563 | 346 | } |
j3 | 0:24b8a6f0a563 | 347 | |
j3 | 0:24b8a6f0a563 | 348 | //********************************************************************* |
j3 | 0:24b8a6f0a563 | 349 | void get_time_date(Terminal & term, Ds3231 & rtc) |
j3 | 0:24b8a6f0a563 | 350 | { |
j3 | 0:24b8a6f0a563 | 351 | time_t epoch_time; |
j3 | 0:24b8a6f0a563 | 352 | |
j3 | 0:24b8a6f0a563 | 353 | epoch_time = rtc.get_epoch(); |
j3 | 0:24b8a6f0a563 | 354 | term.printf("\n%s\n", ctime(&epoch_time)); |
j3 | 0:24b8a6f0a563 | 355 | } |
j3 | 0:24b8a6f0a563 | 356 | |
j3 | 0:24b8a6f0a563 | 357 | //********************************************************************* |
j3 | 0:24b8a6f0a563 | 358 | void search_ow_bus(Terminal & term, DS2484 & owm) |
j3 | 0:24b8a6f0a563 | 359 | { |
j3 | 0:24b8a6f0a563 | 360 | OneWireMaster::CmdResult result = owm.OWReset(); |
j3 | 5:2fe6dd435618 | 361 | MultidropRomIterator selector(owm); |
j3 | 0:24b8a6f0a563 | 362 | SearchState search_state; |
j3 | 0:24b8a6f0a563 | 363 | |
j3 | 0:24b8a6f0a563 | 364 | if(result == OneWireMaster::Success) |
j3 | 0:24b8a6f0a563 | 365 | { |
j3 | 0:24b8a6f0a563 | 366 | term.printf("\nOWReset success, starting search\n"); |
j3 | 0:24b8a6f0a563 | 367 | |
j3 | 0:24b8a6f0a563 | 368 | result = OWFirst(owm, search_state); |
j3 | 0:24b8a6f0a563 | 369 | if(result == OneWireMaster::Success) |
j3 | 0:24b8a6f0a563 | 370 | { |
j3 | 0:24b8a6f0a563 | 371 | while(result == OneWireMaster::Success) |
j3 | 0:24b8a6f0a563 | 372 | { |
j3 | 0:24b8a6f0a563 | 373 | //print current devices rom id |
j3 | 0:24b8a6f0a563 | 374 | print_rom_id(term, search_state.romId); |
j3 | 0:24b8a6f0a563 | 375 | |
j3 | 5:2fe6dd435618 | 376 | if(search_state.romId.familyCode() == 0x10) |
j3 | 6:18a831a757b5 | 377 | { |
j3 | 6:18a831a757b5 | 378 | DS1920Menu(term, search_state.romId, selector); |
j3 | 6:18a831a757b5 | 379 | } |
j3 | 5:2fe6dd435618 | 380 | |
j3 | 0:24b8a6f0a563 | 381 | //find the next device if any |
j3 | 0:24b8a6f0a563 | 382 | result = OWNext(owm, search_state); |
j3 | 0:24b8a6f0a563 | 383 | } |
j3 | 0:24b8a6f0a563 | 384 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 385 | } |
j3 | 0:24b8a6f0a563 | 386 | else |
j3 | 0:24b8a6f0a563 | 387 | { |
j3 | 0:24b8a6f0a563 | 388 | term.printf("\nSearch failed\n"); |
j3 | 0:24b8a6f0a563 | 389 | term.printf("\nError code = %d\n", result); |
j3 | 0:24b8a6f0a563 | 390 | } |
j3 | 0:24b8a6f0a563 | 391 | } |
j3 | 0:24b8a6f0a563 | 392 | else |
j3 | 0:24b8a6f0a563 | 393 | { |
j3 | 0:24b8a6f0a563 | 394 | term.printf("\nFailed to find any 1-wire devices on bus\n"); |
j3 | 0:24b8a6f0a563 | 395 | term.printf("\nError code = %d\n", result); |
j3 | 0:24b8a6f0a563 | 396 | } |
j3 | 0:24b8a6f0a563 | 397 | } |
j3 | 0:24b8a6f0a563 | 398 | |
j3 | 0:24b8a6f0a563 | 399 | //********************************************************************* |
j3 | 0:24b8a6f0a563 | 400 | void print_rom_id(Terminal & term, RomId & romId) |
j3 | 0:24b8a6f0a563 | 401 | { |
j3 | 0:24b8a6f0a563 | 402 | //print the rom number |
j3 | 0:24b8a6f0a563 | 403 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 404 | int8_t idx = (RomId::byteLen - 1); |
j3 | 0:24b8a6f0a563 | 405 | do |
j3 | 0:24b8a6f0a563 | 406 | { |
j3 | 0:24b8a6f0a563 | 407 | if(romId[idx] < 16) |
j3 | 0:24b8a6f0a563 | 408 | { |
j3 | 0:24b8a6f0a563 | 409 | term.printf("0x0%x ", romId[idx--]); |
j3 | 0:24b8a6f0a563 | 410 | } |
j3 | 0:24b8a6f0a563 | 411 | else |
j3 | 0:24b8a6f0a563 | 412 | { |
j3 | 0:24b8a6f0a563 | 413 | term.printf("0x%2x ", romId[idx--]); |
j3 | 0:24b8a6f0a563 | 414 | } |
j3 | 0:24b8a6f0a563 | 415 | } |
j3 | 0:24b8a6f0a563 | 416 | while(idx >= 0); |
j3 | 0:24b8a6f0a563 | 417 | term.printf("\n"); |
j3 | 0:24b8a6f0a563 | 418 | } |
j3 | 2:0c0994f70fd2 | 419 | |
j3 | 2:0c0994f70fd2 | 420 | //********************************************************************* |
j3 | 8:8e36fe48b351 | 421 | bool calibrate_420_io(Terminal & term, MAX11300 & pixi, DS2484 & owm, uint16_t *calData, bool loopAlreadyCal) |
j3 | 2:0c0994f70fd2 | 422 | { |
j3 | 2:0c0994f70fd2 | 423 | char user_entry; |
j3 | 2:0c0994f70fd2 | 424 | //initial vals for aio determined imperically on one pcb |
j3 | 2:0c0994f70fd2 | 425 | static uint16_t aio_4mA = 518; |
j3 | 2:0c0994f70fd2 | 426 | static uint16_t aio_20mA = 2592; |
j3 | 8:8e36fe48b351 | 427 | |
j3 | 8:8e36fe48b351 | 428 | if(loopAlreadyCal) |
j3 | 8:8e36fe48b351 | 429 | { |
j3 | 8:8e36fe48b351 | 430 | aio_4mA = calData[0]; |
j3 | 8:8e36fe48b351 | 431 | aio_20mA = calData[1]; |
j3 | 8:8e36fe48b351 | 432 | } |
j3 | 2:0c0994f70fd2 | 433 | uint16_t aii_4mA, aii_20mA; |
j3 | 2:0c0994f70fd2 | 434 | |
j3 | 4:2a25a4e00541 | 435 | term.cls(); |
j3 | 4:2a25a4e00541 | 436 | term.home(); |
j3 | 4:2a25a4e00541 | 437 | |
j3 | 2:0c0994f70fd2 | 438 | //cal aio |
j3 | 2:0c0994f70fd2 | 439 | term.printf("\nConnect DMM in series with AIO and AII.\n"); |
j3 | 4:2a25a4e00541 | 440 | term.printf("AIO----(DMMM)----AII\n\n"); |
j3 | 4:2a25a4e00541 | 441 | term.printf("Use 'i' and 'd' keys to increase/decrease measurement to calibration value.\n\n"); |
j3 | 4:2a25a4e00541 | 442 | term.printf("Setting AIO calibration val to 4mA, enter 'q' when DMM measurement = 4mA.\n\n"); |
j3 | 2:0c0994f70fd2 | 443 | |
j3 | 2:0c0994f70fd2 | 444 | do |
j3 | 2:0c0994f70fd2 | 445 | { |
j3 | 2:0c0994f70fd2 | 446 | pixi.single_ended_dac_write(MAX11300::PORT8, aio_4mA); |
j3 | 4:2a25a4e00541 | 447 | user_entry = term.get_char("increase (i), decrease (d), quit (q) when DMM = 4mA: ", 'd', 'q'); |
j3 | 2:0c0994f70fd2 | 448 | |
j3 | 2:0c0994f70fd2 | 449 | if(user_entry == 'i') |
j3 | 2:0c0994f70fd2 | 450 | { |
j3 | 2:0c0994f70fd2 | 451 | aio_4mA++; |
j3 | 2:0c0994f70fd2 | 452 | } |
j3 | 2:0c0994f70fd2 | 453 | else if(user_entry == 'd') |
j3 | 2:0c0994f70fd2 | 454 | { |
j3 | 2:0c0994f70fd2 | 455 | aio_4mA--; |
j3 | 2:0c0994f70fd2 | 456 | } |
j3 | 2:0c0994f70fd2 | 457 | else if(user_entry == 'q') |
j3 | 2:0c0994f70fd2 | 458 | { |
j3 | 4:2a25a4e00541 | 459 | term.printf("\nSetting AIO calibration val to 20mA, enter 'q' when DMM measurement = 20mA.\n\n"); |
j3 | 2:0c0994f70fd2 | 460 | } |
j3 | 2:0c0994f70fd2 | 461 | else |
j3 | 2:0c0994f70fd2 | 462 | { |
j3 | 2:0c0994f70fd2 | 463 | term.printf("Not an option, please read the instructions.\n"); |
j3 | 2:0c0994f70fd2 | 464 | } |
j3 | 2:0c0994f70fd2 | 465 | } |
j3 | 2:0c0994f70fd2 | 466 | while(user_entry != 'q'); |
j3 | 2:0c0994f70fd2 | 467 | |
j3 | 2:0c0994f70fd2 | 468 | do |
j3 | 2:0c0994f70fd2 | 469 | { |
j3 | 2:0c0994f70fd2 | 470 | pixi.single_ended_dac_write(MAX11300::PORT8, aio_20mA); |
j3 | 4:2a25a4e00541 | 471 | user_entry = term.get_char("increase (i), decrease (d), quit (q) when DMM = 20mA: ", 'd', 'q'); |
j3 | 2:0c0994f70fd2 | 472 | |
j3 | 2:0c0994f70fd2 | 473 | if(user_entry == 'i') |
j3 | 2:0c0994f70fd2 | 474 | { |
j3 | 2:0c0994f70fd2 | 475 | aio_20mA++; |
j3 | 2:0c0994f70fd2 | 476 | } |
j3 | 2:0c0994f70fd2 | 477 | else if(user_entry == 'd') |
j3 | 2:0c0994f70fd2 | 478 | { |
j3 | 2:0c0994f70fd2 | 479 | aio_20mA--; |
j3 | 2:0c0994f70fd2 | 480 | } |
j3 | 2:0c0994f70fd2 | 481 | else if(user_entry == 'q') |
j3 | 2:0c0994f70fd2 | 482 | { |
j3 | 4:2a25a4e00541 | 483 | term.printf("\nExecuting AII Cal\n\n"); |
j3 | 2:0c0994f70fd2 | 484 | } |
j3 | 2:0c0994f70fd2 | 485 | else |
j3 | 2:0c0994f70fd2 | 486 | { |
j3 | 2:0c0994f70fd2 | 487 | term.printf("Not an option, please read the instructions.\n"); |
j3 | 2:0c0994f70fd2 | 488 | } |
j3 | 2:0c0994f70fd2 | 489 | } |
j3 | 2:0c0994f70fd2 | 490 | while(user_entry != 'q'); |
j3 | 2:0c0994f70fd2 | 491 | |
j3 | 8:8e36fe48b351 | 492 | calData[0] = aio_4mA; |
j3 | 8:8e36fe48b351 | 493 | calData[1] = aio_20mA; |
j3 | 2:0c0994f70fd2 | 494 | |
j3 | 4:2a25a4e00541 | 495 | uint8_t idx; |
j3 | 2:0c0994f70fd2 | 496 | //cal aii |
j3 | 2:0c0994f70fd2 | 497 | pixi.single_ended_dac_write(MAX11300::PORT8, aio_4mA); |
j3 | 4:2a25a4e00541 | 498 | for(idx = 0; idx < 10; idx++) |
j3 | 4:2a25a4e00541 | 499 | { |
j3 | 4:2a25a4e00541 | 500 | term.printf("AII 4mA input cal...\n"); |
j3 | 4:2a25a4e00541 | 501 | wait(1.0); |
j3 | 4:2a25a4e00541 | 502 | } |
j3 | 2:0c0994f70fd2 | 503 | pixi.single_ended_adc_read(MAX11300::PORT12, aii_4mA); |
j3 | 4:2a25a4e00541 | 504 | |
j3 | 8:8e36fe48b351 | 505 | calData[2] = aii_4mA; |
j3 | 8:8e36fe48b351 | 506 | |
j3 | 2:0c0994f70fd2 | 507 | pixi.single_ended_dac_write(MAX11300::PORT8, aio_20mA); |
j3 | 4:2a25a4e00541 | 508 | for(idx = 0; idx < 10; idx++) |
j3 | 4:2a25a4e00541 | 509 | { |
j3 | 4:2a25a4e00541 | 510 | term.printf("AII 20mA input cal...\n"); |
j3 | 4:2a25a4e00541 | 511 | wait(1.0); |
j3 | 4:2a25a4e00541 | 512 | } |
j3 | 2:0c0994f70fd2 | 513 | pixi.single_ended_adc_read(MAX11300::PORT12, aii_20mA); |
j3 | 2:0c0994f70fd2 | 514 | |
j3 | 8:8e36fe48b351 | 515 | calData[3] = aii_20mA; |
j3 | 2:0c0994f70fd2 | 516 | |
j3 | 8:8e36fe48b351 | 517 | |
j3 | 8:8e36fe48b351 | 518 | bool rtn_val = false; |
j3 | 8:8e36fe48b351 | 519 | |
j3 | 8:8e36fe48b351 | 520 | //Write cal data to eeprom |
j3 | 8:8e36fe48b351 | 521 | OneWireMaster::CmdResult result = owm.OWReset(); |
j3 | 8:8e36fe48b351 | 522 | MultidropRomIterator selector(owm); |
j3 | 8:8e36fe48b351 | 523 | SearchState search_state; |
j3 | 8:8e36fe48b351 | 524 | |
j3 | 8:8e36fe48b351 | 525 | DS2431 eeprom(selector); |
j3 | 4:2a25a4e00541 | 526 | |
j3 | 8:8e36fe48b351 | 527 | search_state.findFamily(0x2D); |
j3 | 8:8e36fe48b351 | 528 | result = OWNext(owm, search_state); |
j3 | 8:8e36fe48b351 | 529 | if(result == OneWireMaster::Success) |
j3 | 8:8e36fe48b351 | 530 | { |
j3 | 8:8e36fe48b351 | 531 | eeprom.setRomId(search_state.romId); |
j3 | 8:8e36fe48b351 | 532 | |
j3 | 8:8e36fe48b351 | 533 | OneWireSlave::CmdResult slaveResult = eeprom.writeMemory(0, "CalTrue!", 8); |
j3 | 8:8e36fe48b351 | 534 | if(slaveResult == OneWireSlave::Success) |
j3 | 8:8e36fe48b351 | 535 | { |
j3 | 8:8e36fe48b351 | 536 | uint8_t writeCalData[8]; |
j3 | 8:8e36fe48b351 | 537 | //cal data stored MSB first; |
j3 | 8:8e36fe48b351 | 538 | writeCalData[0] = ((aio_4mA >> 8) & 0xFF); |
j3 | 8:8e36fe48b351 | 539 | writeCalData[1] = (aio_4mA & 0xFF); |
j3 | 8:8e36fe48b351 | 540 | writeCalData[2] = ((aio_20mA >> 8) & 0xFF); |
j3 | 8:8e36fe48b351 | 541 | writeCalData[3] = (aio_20mA & 0xFF); |
j3 | 8:8e36fe48b351 | 542 | writeCalData[4] = ((aii_4mA >> 8) & 0xFF); |
j3 | 8:8e36fe48b351 | 543 | writeCalData[5] = (aii_4mA & 0xFF); |
j3 | 8:8e36fe48b351 | 544 | writeCalData[6] = ((aii_20mA >> 8) & 0xFF); |
j3 | 8:8e36fe48b351 | 545 | writeCalData[7] = (aii_20mA & 0xFF); |
j3 | 8:8e36fe48b351 | 546 | |
j3 | 8:8e36fe48b351 | 547 | slaveResult = eeprom.writeMemory(8, writeCalData, 8); |
j3 | 8:8e36fe48b351 | 548 | if(slaveResult == OneWireSlave::Success) |
j3 | 8:8e36fe48b351 | 549 | { |
j3 | 8:8e36fe48b351 | 550 | rtn_val = true; |
j3 | 8:8e36fe48b351 | 551 | term.printf("\nCal done.\n\n"); |
j3 | 8:8e36fe48b351 | 552 | wait(1.0); |
j3 | 8:8e36fe48b351 | 553 | } |
j3 | 8:8e36fe48b351 | 554 | else |
j3 | 8:8e36fe48b351 | 555 | { |
j3 | 8:8e36fe48b351 | 556 | term.printf("Failed to write cal data.\n\n"); |
j3 | 8:8e36fe48b351 | 557 | } |
j3 | 8:8e36fe48b351 | 558 | } |
j3 | 8:8e36fe48b351 | 559 | else |
j3 | 8:8e36fe48b351 | 560 | { |
j3 | 8:8e36fe48b351 | 561 | term.printf("Failed to write cal message.\n\n"); |
j3 | 8:8e36fe48b351 | 562 | } |
j3 | 8:8e36fe48b351 | 563 | } |
j3 | 8:8e36fe48b351 | 564 | |
j3 | 8:8e36fe48b351 | 565 | return rtn_val; |
j3 | 2:0c0994f70fd2 | 566 | } |
j3 | 2:0c0994f70fd2 | 567 | |
j3 | 5:2fe6dd435618 | 568 | //********************************************************************* |
j3 | 5:2fe6dd435618 | 569 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector) |
j3 | 5:2fe6dd435618 | 570 | { |
j3 | 5:2fe6dd435618 | 571 | char userEntry = '0'; |
j3 | 5:2fe6dd435618 | 572 | uint8_t th, tl, idx; |
j3 | 5:2fe6dd435618 | 573 | uint8_t scratchPadBuff[8]; |
j3 | 5:2fe6dd435618 | 574 | float temperature; |
j3 | 5:2fe6dd435618 | 575 | |
j3 | 5:2fe6dd435618 | 576 | DS1920 tempIbutton(selector); |
j3 | 5:2fe6dd435618 | 577 | DS1920::CmdResult result = DS1920::OpFailure; |
j3 | 5:2fe6dd435618 | 578 | tempIbutton.setRomId(romId); |
j3 | 5:2fe6dd435618 | 579 | |
j3 | 5:2fe6dd435618 | 580 | while(userEntry != '7') |
j3 | 5:2fe6dd435618 | 581 | { |
j3 | 5:2fe6dd435618 | 582 | term.printf("\nDS1920 Menu Options\n"); |
j3 | 5:2fe6dd435618 | 583 | term.printf("\n%t1. Write ScratchPad"); |
j3 | 5:2fe6dd435618 | 584 | term.printf("\n%t2. Read Scratchpad"); |
j3 | 5:2fe6dd435618 | 585 | term.printf("\n%t3. Copy Scratchpad"); |
j3 | 5:2fe6dd435618 | 586 | term.printf("\n%t4. Convert Temperature"); |
j3 | 5:2fe6dd435618 | 587 | term.printf("\n%t5. Recall EEPROM"); |
j3 | 5:2fe6dd435618 | 588 | term.printf("\n%t6. Clear Screen"); |
j3 | 5:2fe6dd435618 | 589 | term.printf("\n%t7. Quit"); |
j3 | 5:2fe6dd435618 | 590 | |
j3 | 5:2fe6dd435618 | 591 | userEntry = term.get_char("\nPlease select an option above: ", '1', '7'); |
j3 | 5:2fe6dd435618 | 592 | |
j3 | 5:2fe6dd435618 | 593 | switch(userEntry) |
j3 | 5:2fe6dd435618 | 594 | { |
j3 | 5:2fe6dd435618 | 595 | case '1': |
j3 | 5:2fe6dd435618 | 596 | th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100); |
j3 | 5:2fe6dd435618 | 597 | tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0); |
j3 | 5:2fe6dd435618 | 598 | |
j3 | 5:2fe6dd435618 | 599 | result = tempIbutton.writeScratchPad(th, tl); |
j3 | 5:2fe6dd435618 | 600 | if(result == DS1920::Success) |
j3 | 5:2fe6dd435618 | 601 | { |
j3 | 5:2fe6dd435618 | 602 | term.printf("\nWrite Scratchpad Success\n"); |
j3 | 5:2fe6dd435618 | 603 | } |
j3 | 5:2fe6dd435618 | 604 | else |
j3 | 5:2fe6dd435618 | 605 | { |
j3 | 5:2fe6dd435618 | 606 | term.printf("\nWrite Scratchpad Fail\n"); |
j3 | 5:2fe6dd435618 | 607 | } |
j3 | 5:2fe6dd435618 | 608 | |
j3 | 5:2fe6dd435618 | 609 | break; |
j3 | 5:2fe6dd435618 | 610 | |
j3 | 5:2fe6dd435618 | 611 | case '2': |
j3 | 5:2fe6dd435618 | 612 | |
j3 | 5:2fe6dd435618 | 613 | result = tempIbutton.readScratchPad(scratchPadBuff); |
j3 | 5:2fe6dd435618 | 614 | if(result == DS1920::Success) |
j3 | 5:2fe6dd435618 | 615 | { |
j3 | 5:2fe6dd435618 | 616 | term.printf("\nRead Scratchpad Success\n"); |
j3 | 5:2fe6dd435618 | 617 | term.printf("\nScratchpad = "); |
j3 | 5:2fe6dd435618 | 618 | for(idx = 0; idx < 8; idx++) |
j3 | 5:2fe6dd435618 | 619 | { |
j3 | 5:2fe6dd435618 | 620 | if(scratchPadBuff[idx] < 16) |
j3 | 5:2fe6dd435618 | 621 | { |
j3 | 5:2fe6dd435618 | 622 | term.printf("0x0%x ", scratchPadBuff[idx]); |
j3 | 5:2fe6dd435618 | 623 | } |
j3 | 5:2fe6dd435618 | 624 | else |
j3 | 5:2fe6dd435618 | 625 | { |
j3 | 5:2fe6dd435618 | 626 | term.printf("0x%2x ", scratchPadBuff[idx]); |
j3 | 5:2fe6dd435618 | 627 | } |
j3 | 5:2fe6dd435618 | 628 | } |
j3 | 5:2fe6dd435618 | 629 | term.printf("\n"); |
j3 | 5:2fe6dd435618 | 630 | |
j3 | 5:2fe6dd435618 | 631 | } |
j3 | 5:2fe6dd435618 | 632 | else |
j3 | 5:2fe6dd435618 | 633 | { |
j3 | 5:2fe6dd435618 | 634 | term.printf("\nRead Scratchpad Fail\n"); |
j3 | 5:2fe6dd435618 | 635 | } |
j3 | 5:2fe6dd435618 | 636 | |
j3 | 5:2fe6dd435618 | 637 | break; |
j3 | 5:2fe6dd435618 | 638 | |
j3 | 5:2fe6dd435618 | 639 | case '3': |
j3 | 5:2fe6dd435618 | 640 | |
j3 | 5:2fe6dd435618 | 641 | result = tempIbutton.copyScratchPad(); |
j3 | 5:2fe6dd435618 | 642 | if(result == DS1920::Success) |
j3 | 5:2fe6dd435618 | 643 | { |
j3 | 5:2fe6dd435618 | 644 | term.printf("\nCopy Scratchpad Success\n"); |
j3 | 5:2fe6dd435618 | 645 | } |
j3 | 5:2fe6dd435618 | 646 | else |
j3 | 5:2fe6dd435618 | 647 | { |
j3 | 5:2fe6dd435618 | 648 | term.printf("\nCopy Scratchpad Fail\n"); |
j3 | 5:2fe6dd435618 | 649 | } |
j3 | 5:2fe6dd435618 | 650 | |
j3 | 5:2fe6dd435618 | 651 | break; |
j3 | 5:2fe6dd435618 | 652 | |
j3 | 5:2fe6dd435618 | 653 | case '4': |
j3 | 5:2fe6dd435618 | 654 | |
j3 | 5:2fe6dd435618 | 655 | result = tempIbutton.convertTemperature(temperature); |
j3 | 5:2fe6dd435618 | 656 | if(result == DS1920::Success) |
j3 | 5:2fe6dd435618 | 657 | { |
j3 | 5:2fe6dd435618 | 658 | term.printf("\nConvert Temperature Success\n"); |
j3 | 5:2fe6dd435618 | 659 | term.printf("\nTemperature = %.1f", temperature); |
j3 | 5:2fe6dd435618 | 660 | } |
j3 | 5:2fe6dd435618 | 661 | else |
j3 | 5:2fe6dd435618 | 662 | { |
j3 | 5:2fe6dd435618 | 663 | term.printf("\nConvert Temperature Fail\n"); |
j3 | 5:2fe6dd435618 | 664 | } |
j3 | 5:2fe6dd435618 | 665 | |
j3 | 5:2fe6dd435618 | 666 | break; |
j3 | 5:2fe6dd435618 | 667 | |
j3 | 5:2fe6dd435618 | 668 | case '5': |
j3 | 5:2fe6dd435618 | 669 | |
j3 | 5:2fe6dd435618 | 670 | result = tempIbutton.recallEEPROM(); |
j3 | 5:2fe6dd435618 | 671 | if(result == DS1920::Success) |
j3 | 5:2fe6dd435618 | 672 | { |
j3 | 5:2fe6dd435618 | 673 | term.printf("\nRecall EEPROM Success\n"); |
j3 | 5:2fe6dd435618 | 674 | } |
j3 | 5:2fe6dd435618 | 675 | else |
j3 | 5:2fe6dd435618 | 676 | { |
j3 | 5:2fe6dd435618 | 677 | term.printf("\nRecall EEPROM Fail\n"); |
j3 | 5:2fe6dd435618 | 678 | } |
j3 | 5:2fe6dd435618 | 679 | |
j3 | 5:2fe6dd435618 | 680 | break; |
j3 | 5:2fe6dd435618 | 681 | |
j3 | 5:2fe6dd435618 | 682 | case '6': |
j3 | 5:2fe6dd435618 | 683 | term.cls(); |
j3 | 5:2fe6dd435618 | 684 | term.home(); |
j3 | 5:2fe6dd435618 | 685 | break; |
j3 | 5:2fe6dd435618 | 686 | |
j3 | 5:2fe6dd435618 | 687 | case '7': |
j3 | 5:2fe6dd435618 | 688 | term.printf("\nLeaving DS1920 Menu Options\n"); |
j3 | 5:2fe6dd435618 | 689 | break; |
j3 | 5:2fe6dd435618 | 690 | |
j3 | 5:2fe6dd435618 | 691 | default: |
j3 | 5:2fe6dd435618 | 692 | mbed_die(); |
j3 | 5:2fe6dd435618 | 693 | break; |
j3 | 5:2fe6dd435618 | 694 | } |
j3 | 5:2fe6dd435618 | 695 | } |
j3 | 5:2fe6dd435618 | 696 | } |
j3 | 8:8e36fe48b351 | 697 | |
j3 | 8:8e36fe48b351 | 698 | //********************************************************************* |
j3 | 8:8e36fe48b351 | 699 | bool check_420_cal(Terminal & term, DS2484 & owm, uint16_t *calData) |
j3 | 8:8e36fe48b351 | 700 | { |
j3 | 8:8e36fe48b351 | 701 | OneWireMaster::CmdResult result = owm.OWReset(); |
j3 | 8:8e36fe48b351 | 702 | MultidropRomIterator selector(owm); |
j3 | 8:8e36fe48b351 | 703 | SearchState search_state; |
j3 | 8:8e36fe48b351 | 704 | |
j3 | 8:8e36fe48b351 | 705 | DS2431 eeprom(selector); |
j3 | 8:8e36fe48b351 | 706 | |
j3 | 8:8e36fe48b351 | 707 | bool rtn_val = false; |
j3 | 8:8e36fe48b351 | 708 | |
j3 | 8:8e36fe48b351 | 709 | term.printf("\n\nPlease remove any 1-Wire devices connected to bus on start up and calibrartion of 4-20mA loop.\n\n"); |
j3 | 8:8e36fe48b351 | 710 | wait(2.5); |
j3 | 8:8e36fe48b351 | 711 | |
j3 | 8:8e36fe48b351 | 712 | search_state.findFamily(0x2D); |
j3 | 8:8e36fe48b351 | 713 | result = OWNext(owm, search_state); |
j3 | 8:8e36fe48b351 | 714 | if(result == OneWireMaster::Success) |
j3 | 8:8e36fe48b351 | 715 | { |
j3 | 8:8e36fe48b351 | 716 | eeprom.setRomId(search_state.romId); |
j3 | 8:8e36fe48b351 | 717 | uint8_t temp_data[8]; |
j3 | 8:8e36fe48b351 | 718 | |
j3 | 8:8e36fe48b351 | 719 | OneWireSlave::CmdResult slaveResult = eeprom.readMemory(0, temp_data, 8); |
j3 | 8:8e36fe48b351 | 720 | |
j3 | 8:8e36fe48b351 | 721 | if((temp_data[0] == 'C') && (slaveResult == OneWireSlave::Success)) |
j3 | 8:8e36fe48b351 | 722 | { |
j3 | 8:8e36fe48b351 | 723 | slaveResult = eeprom.readMemory(8, temp_data, 8); |
j3 | 8:8e36fe48b351 | 724 | if(slaveResult == OneWireSlave::Success) |
j3 | 8:8e36fe48b351 | 725 | { |
j3 | 8:8e36fe48b351 | 726 | calData[0] = ((temp_data[0] << 8) | temp_data[1]); |
j3 | 8:8e36fe48b351 | 727 | calData[1] = ((temp_data[2] << 8) | temp_data[3]); |
j3 | 8:8e36fe48b351 | 728 | calData[2] = ((temp_data[4] << 8) | temp_data[5]); |
j3 | 8:8e36fe48b351 | 729 | calData[3] = ((temp_data[6] << 8) | temp_data[7]); |
j3 | 8:8e36fe48b351 | 730 | |
j3 | 8:8e36fe48b351 | 731 | rtn_val = true; |
j3 | 8:8e36fe48b351 | 732 | } |
j3 | 8:8e36fe48b351 | 733 | else |
j3 | 8:8e36fe48b351 | 734 | { |
j3 | 8:8e36fe48b351 | 735 | term.printf("Failed to read row 1 of EEPROM\n\n"); |
j3 | 8:8e36fe48b351 | 736 | } |
j3 | 8:8e36fe48b351 | 737 | } |
j3 | 8:8e36fe48b351 | 738 | else |
j3 | 8:8e36fe48b351 | 739 | { |
j3 | 8:8e36fe48b351 | 740 | if(slaveResult == OneWireSlave::Success) |
j3 | 8:8e36fe48b351 | 741 | { |
j3 | 8:8e36fe48b351 | 742 | term.printf("No cal data stored, please calibrate 4-20mA loop\n\n"); |
j3 | 8:8e36fe48b351 | 743 | } |
j3 | 8:8e36fe48b351 | 744 | else |
j3 | 8:8e36fe48b351 | 745 | { |
j3 | 8:8e36fe48b351 | 746 | term.printf("Failed to read row 0 of EEPROM\n\n"); |
j3 | 8:8e36fe48b351 | 747 | } |
j3 | 8:8e36fe48b351 | 748 | } |
j3 | 8:8e36fe48b351 | 749 | } |
j3 | 8:8e36fe48b351 | 750 | |
j3 | 8:8e36fe48b351 | 751 | return rtn_val; |
j3 | 8:8e36fe48b351 | 752 | } |