![](/media/cache/group/MI_Logo_Small_Pos_RGB.gif.50x50_q85.png)
Demo program for MAXREFDES132. Searches 1-Wire® bus and prints ROM IDs of devices found. If the DS1920 is found presents user with menu for interfacing with the DS1920 temperature iButton
Dependencies: OneWire Terminal mbed
main.cpp@12:a649f4e955a4, 2017-07-10 (annotated)
- Committer:
- andrewae
- Date:
- Mon Jul 10 23:02:11 2017 +0000
- Revision:
- 12:a649f4e955a4
- Parent:
- 11:11a01dc7a2c9
Fixed print_rom_id
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:11c9d61c8876 | 1 | /********************************************************************** |
j3 | 0:11c9d61c8876 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:11c9d61c8876 | 3 | * |
j3 | 0:11c9d61c8876 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:11c9d61c8876 | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:11c9d61c8876 | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:11c9d61c8876 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:11c9d61c8876 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:11c9d61c8876 | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:11c9d61c8876 | 10 | * |
j3 | 0:11c9d61c8876 | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:11c9d61c8876 | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:11c9d61c8876 | 13 | * |
j3 | 0:11c9d61c8876 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:11c9d61c8876 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:11c9d61c8876 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:11c9d61c8876 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:11c9d61c8876 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:11c9d61c8876 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:11c9d61c8876 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:11c9d61c8876 | 21 | * |
j3 | 0:11c9d61c8876 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:11c9d61c8876 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:11c9d61c8876 | 24 | * Products, Inc. Branding Policy. |
j3 | 0:11c9d61c8876 | 25 | * |
j3 | 0:11c9d61c8876 | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:11c9d61c8876 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:11c9d61c8876 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:11c9d61c8876 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:11c9d61c8876 | 30 | * ownership rights. |
j3 | 0:11c9d61c8876 | 31 | **********************************************************************/ |
j3 | 0:11c9d61c8876 | 32 | |
j3 | 0:11c9d61c8876 | 33 | |
j3 | 0:11c9d61c8876 | 34 | #include "mbed.h" |
j3 | 4:9f799f2e157a | 35 | #include "OneWire.h" |
j3 | 0:11c9d61c8876 | 36 | #include "Terminal.h" |
j3 | 0:11c9d61c8876 | 37 | |
j3 | 0:11c9d61c8876 | 38 | using namespace OneWire; |
j3 | 1:c9d52433407f | 39 | using namespace RomCommands; |
j3 | 0:11c9d61c8876 | 40 | |
j3 | 0:11c9d61c8876 | 41 | void print_rom_id(Terminal & term, const RomId & romId); |
j3 | 0:11c9d61c8876 | 42 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector); |
j3 | 0:11c9d61c8876 | 43 | |
j3 | 0:11c9d61c8876 | 44 | int main(void) |
j3 | 0:11c9d61c8876 | 45 | { |
j3 | 0:11c9d61c8876 | 46 | Terminal term(USBTX, USBRX); |
j3 | 0:11c9d61c8876 | 47 | char user_entry = 'y'; |
j3 | 11:11a01dc7a2c9 | 48 | |
j3 | 8:7af592c694b2 | 49 | I2C i2cBus(D14, D15); |
j3 | 8:7af592c694b2 | 50 | i2cBus.frequency(400000); |
j3 | 8:7af592c694b2 | 51 | DS2484 owm(i2cBus); |
j3 | 8:7af592c694b2 | 52 | term.printf("\nStarting I2C to 1-wire Demo\n"); |
j3 | 0:11c9d61c8876 | 53 | |
j3 | 0:11c9d61c8876 | 54 | OneWireMaster::CmdResult result = owm.OWInitMaster(); |
j3 | 0:11c9d61c8876 | 55 | |
j3 | 0:11c9d61c8876 | 56 | if(result == OneWireMaster::Success) |
j3 | 0:11c9d61c8876 | 57 | { |
j3 | 0:11c9d61c8876 | 58 | //Rom Iterator for slave devices. |
j3 | 0:11c9d61c8876 | 59 | //Encapsulates owm, ROM cntl fxs, and RomId of device into one object |
j3 | 0:11c9d61c8876 | 60 | //that slave member fxs use to implement ROM cntl fxs so that you don't have too. |
j3 | 0:11c9d61c8876 | 61 | //You just use the features of the slave device |
j3 | 0:11c9d61c8876 | 62 | MultidropRomIterator selector(owm); |
j3 | 0:11c9d61c8876 | 63 | |
j3 | 0:11c9d61c8876 | 64 | //Search state var for main loop |
j3 | 1:c9d52433407f | 65 | SearchState searchState; |
j3 | 0:11c9d61c8876 | 66 | |
j3 | 0:11c9d61c8876 | 67 | while(user_entry != 'n') |
j3 | 0:11c9d61c8876 | 68 | { |
j3 | 0:11c9d61c8876 | 69 | result = owm.OWReset(); |
j3 | 0:11c9d61c8876 | 70 | if(result == OneWireMaster::Success) |
j3 | 11:11a01dc7a2c9 | 71 | { |
j3 | 11:11a01dc7a2c9 | 72 | term.printf("\nOWReset success, starting search\n"); |
j3 | 11:11a01dc7a2c9 | 73 | |
j3 | 11:11a01dc7a2c9 | 74 | result = OWFirst(owm, searchState); |
j3 | 11:11a01dc7a2c9 | 75 | if(result == OneWireMaster::Success) |
andrewae | 10:a43e5b99d61f | 76 | { |
j3 | 11:11a01dc7a2c9 | 77 | do |
andrewae | 10:a43e5b99d61f | 78 | { |
j3 | 11:11a01dc7a2c9 | 79 | //print current devices rom id |
j3 | 11:11a01dc7a2c9 | 80 | print_rom_id(term, searchState.romId); |
j3 | 11:11a01dc7a2c9 | 81 | |
j3 | 0:11c9d61c8876 | 82 | |
j3 | 11:11a01dc7a2c9 | 83 | //This could be a switch statement based on familyCodes |
j3 | 11:11a01dc7a2c9 | 84 | //if your app has more than one device on the bus |
j3 | 11:11a01dc7a2c9 | 85 | if(searchState.romId.familyCode() == 0x10) |
j3 | 0:11c9d61c8876 | 86 | { |
j3 | 11:11a01dc7a2c9 | 87 | DS1920Menu(term, searchState.romId, selector); |
j3 | 0:11c9d61c8876 | 88 | } |
j3 | 11:11a01dc7a2c9 | 89 | |
j3 | 11:11a01dc7a2c9 | 90 | //find the next device if any |
j3 | 11:11a01dc7a2c9 | 91 | result = OWNext(owm, searchState); |
j3 | 0:11c9d61c8876 | 92 | } |
j3 | 11:11a01dc7a2c9 | 93 | while(result == OneWireMaster::Success); |
j3 | 11:11a01dc7a2c9 | 94 | } |
j3 | 11:11a01dc7a2c9 | 95 | else |
j3 | 11:11a01dc7a2c9 | 96 | { |
j3 | 11:11a01dc7a2c9 | 97 | term.printf("\nSearch failed\n"); |
j3 | 11:11a01dc7a2c9 | 98 | term.printf("\nError code = %d\n", result); |
j3 | 11:11a01dc7a2c9 | 99 | } |
j3 | 0:11c9d61c8876 | 100 | } |
j3 | 0:11c9d61c8876 | 101 | else |
j3 | 0:11c9d61c8876 | 102 | { |
j3 | 0:11c9d61c8876 | 103 | term.printf("\nFailed to find any 1-wire devices on bus\n"); |
j3 | 0:11c9d61c8876 | 104 | term.printf("\nError code = %d\n", result); |
j3 | 0:11c9d61c8876 | 105 | } |
j3 | 0:11c9d61c8876 | 106 | |
j3 | 0:11c9d61c8876 | 107 | user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y'); |
j3 | 0:11c9d61c8876 | 108 | term.cls(); |
j3 | 0:11c9d61c8876 | 109 | term.home(); |
j3 | 0:11c9d61c8876 | 110 | } |
j3 | 0:11c9d61c8876 | 111 | } |
j3 | 0:11c9d61c8876 | 112 | else |
j3 | 0:11c9d61c8876 | 113 | { |
j3 | 0:11c9d61c8876 | 114 | term.printf("\nFailed to initialize the 1-wire Master\n"); |
j3 | 0:11c9d61c8876 | 115 | term.printf("\nError code = %d\n", result); |
j3 | 0:11c9d61c8876 | 116 | } |
j3 | 0:11c9d61c8876 | 117 | |
j3 | 0:11c9d61c8876 | 118 | term.printf("\nEnding Program\n"); |
j3 | 0:11c9d61c8876 | 119 | |
j3 | 0:11c9d61c8876 | 120 | return 0; |
j3 | 0:11c9d61c8876 | 121 | } |
j3 | 0:11c9d61c8876 | 122 | //***** end main ****************************************************** |
j3 | 0:11c9d61c8876 | 123 | |
j3 | 0:11c9d61c8876 | 124 | |
j3 | 0:11c9d61c8876 | 125 | //********************************************************************* |
j3 | 0:11c9d61c8876 | 126 | void print_rom_id(Terminal & term, const RomId & romId) |
j3 | 0:11c9d61c8876 | 127 | { |
j3 | 0:11c9d61c8876 | 128 | //print the rom number |
andrewae | 12:a649f4e955a4 | 129 | term.printf("\r\n"); |
andrewae | 12:a649f4e955a4 | 130 | for(int idx = 7; idx >= 0; idx--) { |
andrewae | 12:a649f4e955a4 | 131 | term.printf("0x%02x ", romId.buffer[idx]); |
j3 | 0:11c9d61c8876 | 132 | } |
andrewae | 12:a649f4e955a4 | 133 | term.printf("\r\n"); |
j3 | 0:11c9d61c8876 | 134 | } |
j3 | 0:11c9d61c8876 | 135 | |
j3 | 0:11c9d61c8876 | 136 | |
j3 | 0:11c9d61c8876 | 137 | //********************************************************************* |
j3 | 0:11c9d61c8876 | 138 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector) |
j3 | 0:11c9d61c8876 | 139 | { |
j3 | 0:11c9d61c8876 | 140 | char userEntry = '0'; |
j3 | 0:11c9d61c8876 | 141 | uint8_t th, tl, idx; |
j3 | 0:11c9d61c8876 | 142 | uint8_t scratchPadBuff[8]; |
j3 | 0:11c9d61c8876 | 143 | float temperature; |
j3 | 0:11c9d61c8876 | 144 | |
j3 | 0:11c9d61c8876 | 145 | DS1920 tempIbutton(selector); |
j3 | 0:11c9d61c8876 | 146 | DS1920::CmdResult result = DS1920::OpFailure; |
j3 | 1:c9d52433407f | 147 | tempIbutton.setRomId(romId); |
j3 | 0:11c9d61c8876 | 148 | |
j3 | 0:11c9d61c8876 | 149 | while(userEntry != '7') |
j3 | 0:11c9d61c8876 | 150 | { |
j3 | 0:11c9d61c8876 | 151 | term.printf("\nDS1920 Menu Options\n"); |
j3 | 0:11c9d61c8876 | 152 | term.printf("\n%t1. Write ScratchPad"); |
j3 | 0:11c9d61c8876 | 153 | term.printf("\n%t2. Read Scratchpad"); |
j3 | 0:11c9d61c8876 | 154 | term.printf("\n%t3. Copy Scratchpad"); |
j3 | 0:11c9d61c8876 | 155 | term.printf("\n%t4. Convert Temperature"); |
j3 | 0:11c9d61c8876 | 156 | term.printf("\n%t5. Recall EEPROM"); |
j3 | 0:11c9d61c8876 | 157 | term.printf("\n%t6. Clear Screen"); |
j3 | 0:11c9d61c8876 | 158 | term.printf("\n%t7. Quit"); |
j3 | 0:11c9d61c8876 | 159 | |
j3 | 0:11c9d61c8876 | 160 | userEntry = term.get_char("\nPlease select an option above: ", '1', '7'); |
j3 | 0:11c9d61c8876 | 161 | |
j3 | 0:11c9d61c8876 | 162 | switch(userEntry) |
j3 | 0:11c9d61c8876 | 163 | { |
j3 | 0:11c9d61c8876 | 164 | case '1': |
j3 | 0:11c9d61c8876 | 165 | th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100); |
j3 | 0:11c9d61c8876 | 166 | tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0); |
j3 | 0:11c9d61c8876 | 167 | |
j3 | 0:11c9d61c8876 | 168 | result = tempIbutton.writeScratchPad(th, tl); |
j3 | 0:11c9d61c8876 | 169 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 170 | { |
j3 | 0:11c9d61c8876 | 171 | term.printf("\nWrite Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 172 | } |
j3 | 0:11c9d61c8876 | 173 | else |
j3 | 0:11c9d61c8876 | 174 | { |
j3 | 0:11c9d61c8876 | 175 | term.printf("\nWrite Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 176 | } |
j3 | 0:11c9d61c8876 | 177 | |
j3 | 0:11c9d61c8876 | 178 | break; |
j3 | 0:11c9d61c8876 | 179 | |
j3 | 0:11c9d61c8876 | 180 | case '2': |
j3 | 0:11c9d61c8876 | 181 | |
j3 | 0:11c9d61c8876 | 182 | result = tempIbutton.readScratchPad(scratchPadBuff); |
j3 | 0:11c9d61c8876 | 183 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 184 | { |
j3 | 0:11c9d61c8876 | 185 | term.printf("\nRead Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 186 | term.printf("\nScratchpad = "); |
j3 | 0:11c9d61c8876 | 187 | for(idx = 0; idx < 8; idx++) |
j3 | 0:11c9d61c8876 | 188 | { |
j3 | 0:11c9d61c8876 | 189 | if(scratchPadBuff[idx] < 16) |
j3 | 0:11c9d61c8876 | 190 | { |
j3 | 0:11c9d61c8876 | 191 | term.printf("0x0%x ", scratchPadBuff[idx]); |
j3 | 0:11c9d61c8876 | 192 | } |
j3 | 0:11c9d61c8876 | 193 | else |
j3 | 0:11c9d61c8876 | 194 | { |
j3 | 0:11c9d61c8876 | 195 | term.printf("0x%2x ", scratchPadBuff[idx]); |
j3 | 0:11c9d61c8876 | 196 | } |
j3 | 0:11c9d61c8876 | 197 | } |
j3 | 0:11c9d61c8876 | 198 | term.printf("\n"); |
j3 | 0:11c9d61c8876 | 199 | |
j3 | 0:11c9d61c8876 | 200 | } |
j3 | 0:11c9d61c8876 | 201 | else |
j3 | 0:11c9d61c8876 | 202 | { |
j3 | 0:11c9d61c8876 | 203 | term.printf("\nRead Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 204 | } |
j3 | 0:11c9d61c8876 | 205 | |
j3 | 0:11c9d61c8876 | 206 | break; |
j3 | 0:11c9d61c8876 | 207 | |
j3 | 0:11c9d61c8876 | 208 | case '3': |
j3 | 0:11c9d61c8876 | 209 | |
j3 | 0:11c9d61c8876 | 210 | result = tempIbutton.copyScratchPad(); |
j3 | 0:11c9d61c8876 | 211 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 212 | { |
j3 | 0:11c9d61c8876 | 213 | term.printf("\nCopy Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 214 | } |
j3 | 0:11c9d61c8876 | 215 | else |
j3 | 0:11c9d61c8876 | 216 | { |
j3 | 0:11c9d61c8876 | 217 | term.printf("\nCopy Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 218 | } |
j3 | 0:11c9d61c8876 | 219 | |
j3 | 0:11c9d61c8876 | 220 | break; |
j3 | 0:11c9d61c8876 | 221 | |
j3 | 0:11c9d61c8876 | 222 | case '4': |
j3 | 0:11c9d61c8876 | 223 | |
j3 | 0:11c9d61c8876 | 224 | result = tempIbutton.convertTemperature(temperature); |
j3 | 0:11c9d61c8876 | 225 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 226 | { |
j3 | 0:11c9d61c8876 | 227 | term.printf("\nConvert Temperature Success\n"); |
j3 | 0:11c9d61c8876 | 228 | term.printf("\nTemperature = %.1f", temperature); |
j3 | 0:11c9d61c8876 | 229 | } |
j3 | 0:11c9d61c8876 | 230 | else |
j3 | 0:11c9d61c8876 | 231 | { |
j3 | 0:11c9d61c8876 | 232 | term.printf("\nConvert Temperature Fail\n"); |
j3 | 0:11c9d61c8876 | 233 | } |
j3 | 0:11c9d61c8876 | 234 | |
j3 | 0:11c9d61c8876 | 235 | break; |
j3 | 0:11c9d61c8876 | 236 | |
j3 | 0:11c9d61c8876 | 237 | case '5': |
j3 | 0:11c9d61c8876 | 238 | |
j3 | 0:11c9d61c8876 | 239 | result = tempIbutton.recallEEPROM(); |
j3 | 0:11c9d61c8876 | 240 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 241 | { |
j3 | 0:11c9d61c8876 | 242 | term.printf("\nRecall EEPROM Success\n"); |
j3 | 0:11c9d61c8876 | 243 | } |
j3 | 0:11c9d61c8876 | 244 | else |
j3 | 0:11c9d61c8876 | 245 | { |
j3 | 0:11c9d61c8876 | 246 | term.printf("\nRecall EEPROM Fail\n"); |
j3 | 0:11c9d61c8876 | 247 | } |
j3 | 0:11c9d61c8876 | 248 | |
j3 | 0:11c9d61c8876 | 249 | break; |
j3 | 0:11c9d61c8876 | 250 | |
j3 | 0:11c9d61c8876 | 251 | case '6': |
j3 | 0:11c9d61c8876 | 252 | term.cls(); |
j3 | 0:11c9d61c8876 | 253 | term.home(); |
j3 | 0:11c9d61c8876 | 254 | break; |
j3 | 0:11c9d61c8876 | 255 | |
j3 | 0:11c9d61c8876 | 256 | case '7': |
j3 | 0:11c9d61c8876 | 257 | term.printf("\nLeaving DS1920 Menu Options\n"); |
j3 | 0:11c9d61c8876 | 258 | break; |
j3 | 0:11c9d61c8876 | 259 | |
j3 | 0:11c9d61c8876 | 260 | default: |
j3 | 0:11c9d61c8876 | 261 | mbed_die(); |
j3 | 0:11c9d61c8876 | 262 | break; |
j3 | 0:11c9d61c8876 | 263 | } |
j3 | 0:11c9d61c8876 | 264 | } |
j3 | 0:11c9d61c8876 | 265 | } |