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

Committer:
j3
Date:
Mon Jun 20 22:52:47 2016 +0000
Revision:
1:c9d52433407f
Parent:
0:11c9d61c8876
Child:
2:6d410b7d0fd0
updated ow library

Who changed what in which revision?

UserRevisionLine numberNew 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 1:c9d52433407f 35 #include "OneWire/Masters/Masters.h"
j3 1:c9d52433407f 36 #include "OneWire/Slaves/RomCommands.h"
j3 1:c9d52433407f 37 #include "OneWire/Slaves/Sensors/DS1920/DS1920.h"
j3 0:11c9d61c8876 38 #include "Terminal.h"
j3 0:11c9d61c8876 39
j3 0:11c9d61c8876 40 using namespace OneWire;
j3 1:c9d52433407f 41 using namespace RomCommands;
j3 0:11c9d61c8876 42
j3 0:11c9d61c8876 43 //Uncomment the master for your setup
j3 0:11c9d61c8876 44
j3 1:c9d52433407f 45 //#define I2C_TO_OW_MASTER
j3 0:11c9d61c8876 46 //#define SERIAL_TO_OW_MASTER
j3 1:c9d52433407f 47 #define BIT_BANG_MASTER
j3 0:11c9d61c8876 48
j3 0:11c9d61c8876 49 void print_rom_id(Terminal & term, const RomId & romId);
j3 0:11c9d61c8876 50 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);
j3 0:11c9d61c8876 51
j3 0:11c9d61c8876 52 int main(void)
j3 0:11c9d61c8876 53 {
j3 0:11c9d61c8876 54 Terminal term(USBTX, USBRX);
j3 0:11c9d61c8876 55 char user_entry = 'y';
j3 0:11c9d61c8876 56
j3 0:11c9d61c8876 57 #if defined(I2C_TO_OW_MASTER)
j3 0:11c9d61c8876 58 //Instantiates a 1-wire master using the given I2C bus SDA/SCL pins
j3 0:11c9d61c8876 59 //Requires a DS248X series of masters on I2C bus
j3 0:11c9d61c8876 60 //https://para.maximintegrated.com/en/results.mvp?fam=1wire&tree=master
j3 0:11c9d61c8876 61
j3 1:c9d52433407f 62 I2C i2cBus(D14, D15);
j3 1:c9d52433407f 63 DS248x owm(i2cBus, 0x30);
j3 0:11c9d61c8876 64 term.printf("\nStarting I2C to 1-wire Demo\n");
j3 0:11c9d61c8876 65
j3 0:11c9d61c8876 66 #elif defined(SERIAL_TO_OW_MASTER)
j3 0:11c9d61c8876 67 //Instantiates a 1-wire master using the given UART tx/rx pins
j3 0:11c9d61c8876 68 //Requires a DS2480B serial to 1-wire master
j3 0:11c9d61c8876 69 //https://www.maximintegrated.com/en/products/interface/controllers-expanders/DS2480B.html
j3 0:11c9d61c8876 70
j3 1:c9d52433407f 71 Serial ow_uart(D1, D0);
j3 1:c9d52433407f 72 Ds2480b owm(ow_uart);
j3 0:11c9d61c8876 73 term.printf("\nStarting Serial to 1-wire Demo\n");
j3 0:11c9d61c8876 74
j3 0:11c9d61c8876 75 #elif defined(BIT_BANG_MASTER)
j3 0:11c9d61c8876 76 //Instantiates a 1-wire master on the given GPIO pin
j3 0:11c9d61c8876 77 //Requires MAX32600MBED platform
j3 0:11c9d61c8876 78 //https://www.maximintegrated.com/en/products/digital/microcontrollers/MAX32600MBED.html
j3 0:11c9d61c8876 79
j3 0:11c9d61c8876 80 OwGpio owm(D2);
j3 0:11c9d61c8876 81 term.printf("\nStarting Bit-Bang 1-wire Demo\n");
j3 0:11c9d61c8876 82
j3 0:11c9d61c8876 83 #else
j3 0:11c9d61c8876 84 #error One Wire Master Not Defined
j3 0:11c9d61c8876 85 #endif
j3 0:11c9d61c8876 86
j3 0:11c9d61c8876 87 OneWireMaster::CmdResult result = owm.OWInitMaster();
j3 0:11c9d61c8876 88
j3 0:11c9d61c8876 89 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 90 {
j3 0:11c9d61c8876 91 //Rom Iterator for slave devices.
j3 0:11c9d61c8876 92 //Encapsulates owm, ROM cntl fxs, and RomId of device into one object
j3 0:11c9d61c8876 93 //that slave member fxs use to implement ROM cntl fxs so that you don't have too.
j3 0:11c9d61c8876 94 //You just use the features of the slave device
j3 0:11c9d61c8876 95 MultidropRomIterator selector(owm);
j3 0:11c9d61c8876 96
j3 0:11c9d61c8876 97 //Search state var for main loop
j3 1:c9d52433407f 98 SearchState searchState;
j3 0:11c9d61c8876 99
j3 0:11c9d61c8876 100 while(user_entry != 'n')
j3 0:11c9d61c8876 101 {
j3 0:11c9d61c8876 102 result = owm.OWReset();
j3 0:11c9d61c8876 103 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 104 {
j3 0:11c9d61c8876 105 term.printf("\nOWReset success, starting search\n");
j3 0:11c9d61c8876 106
j3 1:c9d52433407f 107 result = OWFirst(owm, searchState);
j3 0:11c9d61c8876 108 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 109 {
j3 0:11c9d61c8876 110 do
j3 0:11c9d61c8876 111 {
j3 0:11c9d61c8876 112 //print current devices rom id
j3 0:11c9d61c8876 113 print_rom_id(term, searchState.romId);
j3 0:11c9d61c8876 114
j3 0:11c9d61c8876 115
j3 0:11c9d61c8876 116 //This could be a switch statement based on familyCodes
j3 0:11c9d61c8876 117 //if your app has more than one device on the bus
j3 0:11c9d61c8876 118 if(searchState.romId.familyCode() == 0x10)
j3 0:11c9d61c8876 119 {
j3 0:11c9d61c8876 120 DS1920Menu(term, searchState.romId, selector);
j3 0:11c9d61c8876 121 }
j3 0:11c9d61c8876 122
j3 0:11c9d61c8876 123 //find the next device if any
j3 1:c9d52433407f 124 result = OWNext(owm, searchState);
j3 0:11c9d61c8876 125 }
j3 0:11c9d61c8876 126 while(result == OneWireMaster::Success);
j3 0:11c9d61c8876 127 }
j3 0:11c9d61c8876 128 else
j3 0:11c9d61c8876 129 {
j3 0:11c9d61c8876 130 term.printf("\nSearch failed\n");
j3 0:11c9d61c8876 131 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 132 }
j3 0:11c9d61c8876 133 }
j3 0:11c9d61c8876 134 else
j3 0:11c9d61c8876 135 {
j3 0:11c9d61c8876 136 term.printf("\nFailed to find any 1-wire devices on bus\n");
j3 0:11c9d61c8876 137 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 138 }
j3 0:11c9d61c8876 139
j3 0:11c9d61c8876 140 user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
j3 0:11c9d61c8876 141 term.cls();
j3 0:11c9d61c8876 142 term.home();
j3 0:11c9d61c8876 143 }
j3 0:11c9d61c8876 144 }
j3 0:11c9d61c8876 145 else
j3 0:11c9d61c8876 146 {
j3 0:11c9d61c8876 147 term.printf("\nFailed to initialize the 1-wire Master\n");
j3 0:11c9d61c8876 148 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 149 }
j3 0:11c9d61c8876 150
j3 0:11c9d61c8876 151 term.printf("\nEnding Program\n");
j3 0:11c9d61c8876 152
j3 0:11c9d61c8876 153 return 0;
j3 0:11c9d61c8876 154 }
j3 0:11c9d61c8876 155 //***** end main ******************************************************
j3 0:11c9d61c8876 156
j3 0:11c9d61c8876 157
j3 0:11c9d61c8876 158 //*********************************************************************
j3 0:11c9d61c8876 159 void print_rom_id(Terminal & term, const RomId & romId)
j3 0:11c9d61c8876 160 {
j3 0:11c9d61c8876 161 //print the rom number
j3 0:11c9d61c8876 162 term.printf("\n");
j3 0:11c9d61c8876 163 int8_t idx = (RomId::byteLen - 1);
j3 0:11c9d61c8876 164 do
j3 0:11c9d61c8876 165 {
j3 0:11c9d61c8876 166 if(romId[idx] < 16)
j3 0:11c9d61c8876 167 {
j3 0:11c9d61c8876 168 term.printf("0x0%x ", romId[idx--]);
j3 0:11c9d61c8876 169 }
j3 0:11c9d61c8876 170 else
j3 0:11c9d61c8876 171 {
j3 0:11c9d61c8876 172 term.printf("0x%2x ", romId[idx--]);
j3 0:11c9d61c8876 173 }
j3 0:11c9d61c8876 174 }
j3 0:11c9d61c8876 175 while(idx >= 0);
j3 0:11c9d61c8876 176 term.printf("\n");
j3 0:11c9d61c8876 177 }
j3 0:11c9d61c8876 178
j3 0:11c9d61c8876 179
j3 0:11c9d61c8876 180 //*********************************************************************
j3 0:11c9d61c8876 181 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
j3 0:11c9d61c8876 182 {
j3 0:11c9d61c8876 183 char userEntry = '0';
j3 0:11c9d61c8876 184 uint8_t th, tl, idx;
j3 0:11c9d61c8876 185 uint8_t scratchPadBuff[8];
j3 0:11c9d61c8876 186 float temperature;
j3 0:11c9d61c8876 187
j3 0:11c9d61c8876 188 DS1920 tempIbutton(selector);
j3 0:11c9d61c8876 189 DS1920::CmdResult result = DS1920::OpFailure;
j3 1:c9d52433407f 190 tempIbutton.setRomId(romId);
j3 0:11c9d61c8876 191
j3 0:11c9d61c8876 192 while(userEntry != '7')
j3 0:11c9d61c8876 193 {
j3 0:11c9d61c8876 194 term.printf("\nDS1920 Menu Options\n");
j3 0:11c9d61c8876 195 term.printf("\n%t1. Write ScratchPad");
j3 0:11c9d61c8876 196 term.printf("\n%t2. Read Scratchpad");
j3 0:11c9d61c8876 197 term.printf("\n%t3. Copy Scratchpad");
j3 0:11c9d61c8876 198 term.printf("\n%t4. Convert Temperature");
j3 0:11c9d61c8876 199 term.printf("\n%t5. Recall EEPROM");
j3 0:11c9d61c8876 200 term.printf("\n%t6. Clear Screen");
j3 0:11c9d61c8876 201 term.printf("\n%t7. Quit");
j3 0:11c9d61c8876 202
j3 0:11c9d61c8876 203 userEntry = term.get_char("\nPlease select an option above: ", '1', '7');
j3 0:11c9d61c8876 204
j3 0:11c9d61c8876 205 switch(userEntry)
j3 0:11c9d61c8876 206 {
j3 0:11c9d61c8876 207 case '1':
j3 0:11c9d61c8876 208 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
j3 0:11c9d61c8876 209 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
j3 0:11c9d61c8876 210
j3 0:11c9d61c8876 211 result = tempIbutton.writeScratchPad(th, tl);
j3 0:11c9d61c8876 212 if(result == DS1920::Success)
j3 0:11c9d61c8876 213 {
j3 0:11c9d61c8876 214 term.printf("\nWrite Scratchpad Success\n");
j3 0:11c9d61c8876 215 }
j3 0:11c9d61c8876 216 else
j3 0:11c9d61c8876 217 {
j3 0:11c9d61c8876 218 term.printf("\nWrite Scratchpad Fail\n");
j3 0:11c9d61c8876 219 }
j3 0:11c9d61c8876 220
j3 0:11c9d61c8876 221 break;
j3 0:11c9d61c8876 222
j3 0:11c9d61c8876 223 case '2':
j3 0:11c9d61c8876 224
j3 0:11c9d61c8876 225 result = tempIbutton.readScratchPad(scratchPadBuff);
j3 0:11c9d61c8876 226 if(result == DS1920::Success)
j3 0:11c9d61c8876 227 {
j3 0:11c9d61c8876 228 term.printf("\nRead Scratchpad Success\n");
j3 0:11c9d61c8876 229 term.printf("\nScratchpad = ");
j3 0:11c9d61c8876 230 for(idx = 0; idx < 8; idx++)
j3 0:11c9d61c8876 231 {
j3 0:11c9d61c8876 232 if(scratchPadBuff[idx] < 16)
j3 0:11c9d61c8876 233 {
j3 0:11c9d61c8876 234 term.printf("0x0%x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 235 }
j3 0:11c9d61c8876 236 else
j3 0:11c9d61c8876 237 {
j3 0:11c9d61c8876 238 term.printf("0x%2x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 239 }
j3 0:11c9d61c8876 240 }
j3 0:11c9d61c8876 241 term.printf("\n");
j3 0:11c9d61c8876 242
j3 0:11c9d61c8876 243 }
j3 0:11c9d61c8876 244 else
j3 0:11c9d61c8876 245 {
j3 0:11c9d61c8876 246 term.printf("\nRead Scratchpad Fail\n");
j3 0:11c9d61c8876 247 }
j3 0:11c9d61c8876 248
j3 0:11c9d61c8876 249 break;
j3 0:11c9d61c8876 250
j3 0:11c9d61c8876 251 case '3':
j3 0:11c9d61c8876 252
j3 0:11c9d61c8876 253 result = tempIbutton.copyScratchPad();
j3 0:11c9d61c8876 254 if(result == DS1920::Success)
j3 0:11c9d61c8876 255 {
j3 0:11c9d61c8876 256 term.printf("\nCopy Scratchpad Success\n");
j3 0:11c9d61c8876 257 }
j3 0:11c9d61c8876 258 else
j3 0:11c9d61c8876 259 {
j3 0:11c9d61c8876 260 term.printf("\nCopy Scratchpad Fail\n");
j3 0:11c9d61c8876 261 }
j3 0:11c9d61c8876 262
j3 0:11c9d61c8876 263 break;
j3 0:11c9d61c8876 264
j3 0:11c9d61c8876 265 case '4':
j3 0:11c9d61c8876 266
j3 0:11c9d61c8876 267 result = tempIbutton.convertTemperature(temperature);
j3 0:11c9d61c8876 268 if(result == DS1920::Success)
j3 0:11c9d61c8876 269 {
j3 0:11c9d61c8876 270 term.printf("\nConvert Temperature Success\n");
j3 0:11c9d61c8876 271 term.printf("\nTemperature = %.1f", temperature);
j3 0:11c9d61c8876 272 }
j3 0:11c9d61c8876 273 else
j3 0:11c9d61c8876 274 {
j3 0:11c9d61c8876 275 term.printf("\nConvert Temperature Fail\n");
j3 0:11c9d61c8876 276 }
j3 0:11c9d61c8876 277
j3 0:11c9d61c8876 278 break;
j3 0:11c9d61c8876 279
j3 0:11c9d61c8876 280 case '5':
j3 0:11c9d61c8876 281
j3 0:11c9d61c8876 282 result = tempIbutton.recallEEPROM();
j3 0:11c9d61c8876 283 if(result == DS1920::Success)
j3 0:11c9d61c8876 284 {
j3 0:11c9d61c8876 285 term.printf("\nRecall EEPROM Success\n");
j3 0:11c9d61c8876 286 }
j3 0:11c9d61c8876 287 else
j3 0:11c9d61c8876 288 {
j3 0:11c9d61c8876 289 term.printf("\nRecall EEPROM Fail\n");
j3 0:11c9d61c8876 290 }
j3 0:11c9d61c8876 291
j3 0:11c9d61c8876 292 break;
j3 0:11c9d61c8876 293
j3 0:11c9d61c8876 294 case '6':
j3 0:11c9d61c8876 295 term.cls();
j3 0:11c9d61c8876 296 term.home();
j3 0:11c9d61c8876 297 break;
j3 0:11c9d61c8876 298
j3 0:11c9d61c8876 299 case '7':
j3 0:11c9d61c8876 300 term.printf("\nLeaving DS1920 Menu Options\n");
j3 0:11c9d61c8876 301 break;
j3 0:11c9d61c8876 302
j3 0:11c9d61c8876 303 default:
j3 0:11c9d61c8876 304 mbed_die();
j3 0:11c9d61c8876 305 break;
j3 0:11c9d61c8876 306 }
j3 0:11c9d61c8876 307 }
j3 0:11c9d61c8876 308 }