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:
andrewae
Date:
Mon Jul 10 17:39:49 2017 +0000
Revision:
10:a43e5b99d61f
Parent:
8:7af592c694b2
Child:
11:11a01dc7a2c9
Fork of the MAXREFDES132_OneWire_Demo. I am creating a lab for the incoming NCHs. The purpose of this lab is to teach them about 1-Wire. This shield is particularly useful since it has the DS2484 on it.

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 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';
andrewae 10:a43e5b99d61f 48 char cmnd_entry = ' ';
andrewae 10:a43e5b99d61f 49 RomId myROM;
andrewae 10:a43e5b99d61f 50 array<uint8_t, 8> myArray;
andrewae 10:a43e5b99d61f 51 bool DS1920active = false;
andrewae 10:a43e5b99d61f 52
j3 8:7af592c694b2 53 I2C i2cBus(D14, D15);
j3 8:7af592c694b2 54 i2cBus.frequency(400000);
j3 8:7af592c694b2 55 DS2484 owm(i2cBus);
j3 8:7af592c694b2 56 term.printf("\nStarting I2C to 1-wire Demo\n");
j3 0:11c9d61c8876 57
j3 0:11c9d61c8876 58 OneWireMaster::CmdResult result = owm.OWInitMaster();
j3 0:11c9d61c8876 59
j3 0:11c9d61c8876 60 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 61 {
j3 0:11c9d61c8876 62 //Rom Iterator for slave devices.
j3 0:11c9d61c8876 63 //Encapsulates owm, ROM cntl fxs, and RomId of device into one object
j3 0:11c9d61c8876 64 //that slave member fxs use to implement ROM cntl fxs so that you don't have too.
j3 0:11c9d61c8876 65 //You just use the features of the slave device
j3 0:11c9d61c8876 66 MultidropRomIterator selector(owm);
j3 0:11c9d61c8876 67
j3 0:11c9d61c8876 68 //Search state var for main loop
j3 1:c9d52433407f 69 SearchState searchState;
j3 0:11c9d61c8876 70
j3 0:11c9d61c8876 71 while(user_entry != 'n')
j3 0:11c9d61c8876 72 {
j3 0:11c9d61c8876 73 result = owm.OWReset();
j3 0:11c9d61c8876 74 if(result == OneWireMaster::Success)
andrewae 10:a43e5b99d61f 75 {
andrewae 10:a43e5b99d61f 76 term.printf("\nOWReset successn");
andrewae 10:a43e5b99d61f 77 cmnd_entry = term.get_char("\nEnter 'a' to perform a Search ROM command\nEnter 'b' to find a specific device: ", 'a', 'b');
andrewae 10:a43e5b99d61f 78 term.cls();
andrewae 10:a43e5b99d61f 79 term.home();
andrewae 10:a43e5b99d61f 80
andrewae 10:a43e5b99d61f 81 switch(cmnd_entry)
andrewae 10:a43e5b99d61f 82 {
andrewae 10:a43e5b99d61f 83 case 'a':
j3 0:11c9d61c8876 84
andrewae 10:a43e5b99d61f 85 result = OWFirst(owm, searchState);
andrewae 10:a43e5b99d61f 86 if(result == OneWireMaster::Success)
andrewae 10:a43e5b99d61f 87 {
andrewae 10:a43e5b99d61f 88 do
andrewae 10:a43e5b99d61f 89 {
andrewae 10:a43e5b99d61f 90 //print current devices rom id
andrewae 10:a43e5b99d61f 91 print_rom_id(term, searchState.romId);
j3 0:11c9d61c8876 92
andrewae 10:a43e5b99d61f 93 //This could be a switch statement based on familyCodes
andrewae 10:a43e5b99d61f 94 //if your app has more than one device on the bus
andrewae 10:a43e5b99d61f 95 if(searchState.romId.familyCode() == 0x10)
andrewae 10:a43e5b99d61f 96 {
andrewae 10:a43e5b99d61f 97 DS1920active = true;
andrewae 10:a43e5b99d61f 98 //DS1920Menu(term, searchState.romId, selector);
andrewae 10:a43e5b99d61f 99 }
andrewae 10:a43e5b99d61f 100
andrewae 10:a43e5b99d61f 101 //find the next device if any
andrewae 10:a43e5b99d61f 102 result = OWNext(owm, searchState);
andrewae 10:a43e5b99d61f 103 }
andrewae 10:a43e5b99d61f 104 while(result == OneWireMaster::Success);
andrewae 10:a43e5b99d61f 105 if (DS1920active == true){
andrewae 10:a43e5b99d61f 106 DS1920Menu(term, searchState.romId, selector);
andrewae 10:a43e5b99d61f 107 }
andrewae 10:a43e5b99d61f 108 DS1920active = false;
andrewae 10:a43e5b99d61f 109 }
andrewae 10:a43e5b99d61f 110 else
j3 0:11c9d61c8876 111 {
andrewae 10:a43e5b99d61f 112 term.printf("\nSearch failed\n");
andrewae 10:a43e5b99d61f 113 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 114 }
andrewae 10:a43e5b99d61f 115 break;
andrewae 10:a43e5b99d61f 116
andrewae 10:a43e5b99d61f 117 case 'b':
andrewae 10:a43e5b99d61f 118 term.printf("\nPlease enter the ROMID you would like to search for starting with the MSB\n");
andrewae 10:a43e5b99d61f 119
andrewae 10:a43e5b99d61f 120 for (int i = 7; i > -1; i--){
andrewae 10:a43e5b99d61f 121 term.printf("\nEnter the %d byte: ",i);
andrewae 10:a43e5b99d61f 122 term.scanf("%x", &myArray[i]);
andrewae 10:a43e5b99d61f 123 myROM[i] = myArray[i];
andrewae 10:a43e5b99d61f 124 term.printf("%#x", myROM[i]);
andrewae 10:a43e5b99d61f 125 }
andrewae 10:a43e5b99d61f 126
andrewae 10:a43e5b99d61f 127 //print_rom_id(term, myROM);
andrewae 10:a43e5b99d61f 128
andrewae 10:a43e5b99d61f 129 result = OWVerify(owm, myROM);
andrewae 10:a43e5b99d61f 130 if(result == OneWireMaster::Success)
andrewae 10:a43e5b99d61f 131 {
andrewae 10:a43e5b99d61f 132 term.printf("\n\nYAY!");
andrewae 10:a43e5b99d61f 133 }
andrewae 10:a43e5b99d61f 134 else
andrewae 10:a43e5b99d61f 135 {
andrewae 10:a43e5b99d61f 136 term.printf("\n\nBOO!\n");
andrewae 10:a43e5b99d61f 137 }
andrewae 10:a43e5b99d61f 138 break;
j3 0:11c9d61c8876 139 }
j3 0:11c9d61c8876 140 }
j3 0:11c9d61c8876 141 else
j3 0:11c9d61c8876 142 {
j3 0:11c9d61c8876 143 term.printf("\nFailed to find any 1-wire devices on bus\n");
j3 0:11c9d61c8876 144 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 145 }
j3 0:11c9d61c8876 146
j3 0:11c9d61c8876 147 user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
j3 0:11c9d61c8876 148 term.cls();
j3 0:11c9d61c8876 149 term.home();
j3 0:11c9d61c8876 150 }
j3 0:11c9d61c8876 151 }
j3 0:11c9d61c8876 152 else
j3 0:11c9d61c8876 153 {
j3 0:11c9d61c8876 154 term.printf("\nFailed to initialize the 1-wire Master\n");
j3 0:11c9d61c8876 155 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 156 }
j3 0:11c9d61c8876 157
j3 0:11c9d61c8876 158 term.printf("\nEnding Program\n");
j3 0:11c9d61c8876 159
j3 0:11c9d61c8876 160 return 0;
j3 0:11c9d61c8876 161 }
j3 0:11c9d61c8876 162 //***** end main ******************************************************
j3 0:11c9d61c8876 163
j3 0:11c9d61c8876 164
j3 0:11c9d61c8876 165 //*********************************************************************
j3 0:11c9d61c8876 166 void print_rom_id(Terminal & term, const RomId & romId)
j3 0:11c9d61c8876 167 {
j3 0:11c9d61c8876 168 //print the rom number
andrewae 10:a43e5b99d61f 169 //term.printf("\n");
andrewae 10:a43e5b99d61f 170 //term.printf("%x ", romId.familyCode());
j3 0:11c9d61c8876 171 term.printf("\n");
j3 0:11c9d61c8876 172 int8_t idx = (RomId::byteLen - 1);
j3 0:11c9d61c8876 173 do
j3 0:11c9d61c8876 174 {
j3 0:11c9d61c8876 175 if(romId[idx] < 16)
j3 0:11c9d61c8876 176 {
j3 0:11c9d61c8876 177 term.printf("0x0%x ", romId[idx--]);
j3 0:11c9d61c8876 178 }
j3 0:11c9d61c8876 179 else
j3 0:11c9d61c8876 180 {
j3 0:11c9d61c8876 181 term.printf("0x%2x ", romId[idx--]);
j3 0:11c9d61c8876 182 }
j3 0:11c9d61c8876 183 }
j3 0:11c9d61c8876 184 while(idx >= 0);
j3 0:11c9d61c8876 185 term.printf("\n");
j3 0:11c9d61c8876 186 }
j3 0:11c9d61c8876 187
j3 0:11c9d61c8876 188
j3 0:11c9d61c8876 189 //*********************************************************************
j3 0:11c9d61c8876 190 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
j3 0:11c9d61c8876 191 {
j3 0:11c9d61c8876 192 char userEntry = '0';
j3 0:11c9d61c8876 193 uint8_t th, tl, idx;
j3 0:11c9d61c8876 194 uint8_t scratchPadBuff[8];
j3 0:11c9d61c8876 195 float temperature;
j3 0:11c9d61c8876 196
j3 0:11c9d61c8876 197 DS1920 tempIbutton(selector);
j3 0:11c9d61c8876 198 DS1920::CmdResult result = DS1920::OpFailure;
j3 1:c9d52433407f 199 tempIbutton.setRomId(romId);
j3 0:11c9d61c8876 200
j3 0:11c9d61c8876 201 while(userEntry != '7')
j3 0:11c9d61c8876 202 {
j3 0:11c9d61c8876 203 term.printf("\nDS1920 Menu Options\n");
j3 0:11c9d61c8876 204 term.printf("\n%t1. Write ScratchPad");
j3 0:11c9d61c8876 205 term.printf("\n%t2. Read Scratchpad");
j3 0:11c9d61c8876 206 term.printf("\n%t3. Copy Scratchpad");
j3 0:11c9d61c8876 207 term.printf("\n%t4. Convert Temperature");
j3 0:11c9d61c8876 208 term.printf("\n%t5. Recall EEPROM");
j3 0:11c9d61c8876 209 term.printf("\n%t6. Clear Screen");
j3 0:11c9d61c8876 210 term.printf("\n%t7. Quit");
j3 0:11c9d61c8876 211
j3 0:11c9d61c8876 212 userEntry = term.get_char("\nPlease select an option above: ", '1', '7');
j3 0:11c9d61c8876 213
j3 0:11c9d61c8876 214 switch(userEntry)
j3 0:11c9d61c8876 215 {
j3 0:11c9d61c8876 216 case '1':
j3 0:11c9d61c8876 217 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
j3 0:11c9d61c8876 218 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
j3 0:11c9d61c8876 219
j3 0:11c9d61c8876 220 result = tempIbutton.writeScratchPad(th, tl);
j3 0:11c9d61c8876 221 if(result == DS1920::Success)
j3 0:11c9d61c8876 222 {
j3 0:11c9d61c8876 223 term.printf("\nWrite Scratchpad Success\n");
j3 0:11c9d61c8876 224 }
j3 0:11c9d61c8876 225 else
j3 0:11c9d61c8876 226 {
j3 0:11c9d61c8876 227 term.printf("\nWrite Scratchpad Fail\n");
j3 0:11c9d61c8876 228 }
j3 0:11c9d61c8876 229
j3 0:11c9d61c8876 230 break;
j3 0:11c9d61c8876 231
j3 0:11c9d61c8876 232 case '2':
j3 0:11c9d61c8876 233
j3 0:11c9d61c8876 234 result = tempIbutton.readScratchPad(scratchPadBuff);
j3 0:11c9d61c8876 235 if(result == DS1920::Success)
j3 0:11c9d61c8876 236 {
j3 0:11c9d61c8876 237 term.printf("\nRead Scratchpad Success\n");
j3 0:11c9d61c8876 238 term.printf("\nScratchpad = ");
j3 0:11c9d61c8876 239 for(idx = 0; idx < 8; idx++)
j3 0:11c9d61c8876 240 {
j3 0:11c9d61c8876 241 if(scratchPadBuff[idx] < 16)
j3 0:11c9d61c8876 242 {
j3 0:11c9d61c8876 243 term.printf("0x0%x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 244 }
j3 0:11c9d61c8876 245 else
j3 0:11c9d61c8876 246 {
j3 0:11c9d61c8876 247 term.printf("0x%2x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 248 }
j3 0:11c9d61c8876 249 }
j3 0:11c9d61c8876 250 term.printf("\n");
j3 0:11c9d61c8876 251
j3 0:11c9d61c8876 252 }
j3 0:11c9d61c8876 253 else
j3 0:11c9d61c8876 254 {
j3 0:11c9d61c8876 255 term.printf("\nRead Scratchpad Fail\n");
j3 0:11c9d61c8876 256 }
j3 0:11c9d61c8876 257
j3 0:11c9d61c8876 258 break;
j3 0:11c9d61c8876 259
j3 0:11c9d61c8876 260 case '3':
j3 0:11c9d61c8876 261
j3 0:11c9d61c8876 262 result = tempIbutton.copyScratchPad();
j3 0:11c9d61c8876 263 if(result == DS1920::Success)
j3 0:11c9d61c8876 264 {
j3 0:11c9d61c8876 265 term.printf("\nCopy Scratchpad Success\n");
j3 0:11c9d61c8876 266 }
j3 0:11c9d61c8876 267 else
j3 0:11c9d61c8876 268 {
j3 0:11c9d61c8876 269 term.printf("\nCopy Scratchpad Fail\n");
j3 0:11c9d61c8876 270 }
j3 0:11c9d61c8876 271
j3 0:11c9d61c8876 272 break;
j3 0:11c9d61c8876 273
j3 0:11c9d61c8876 274 case '4':
j3 0:11c9d61c8876 275
j3 0:11c9d61c8876 276 result = tempIbutton.convertTemperature(temperature);
j3 0:11c9d61c8876 277 if(result == DS1920::Success)
j3 0:11c9d61c8876 278 {
j3 0:11c9d61c8876 279 term.printf("\nConvert Temperature Success\n");
j3 0:11c9d61c8876 280 term.printf("\nTemperature = %.1f", temperature);
j3 0:11c9d61c8876 281 }
j3 0:11c9d61c8876 282 else
j3 0:11c9d61c8876 283 {
j3 0:11c9d61c8876 284 term.printf("\nConvert Temperature Fail\n");
j3 0:11c9d61c8876 285 }
j3 0:11c9d61c8876 286
j3 0:11c9d61c8876 287 break;
j3 0:11c9d61c8876 288
j3 0:11c9d61c8876 289 case '5':
j3 0:11c9d61c8876 290
j3 0:11c9d61c8876 291 result = tempIbutton.recallEEPROM();
j3 0:11c9d61c8876 292 if(result == DS1920::Success)
j3 0:11c9d61c8876 293 {
j3 0:11c9d61c8876 294 term.printf("\nRecall EEPROM Success\n");
j3 0:11c9d61c8876 295 }
j3 0:11c9d61c8876 296 else
j3 0:11c9d61c8876 297 {
j3 0:11c9d61c8876 298 term.printf("\nRecall EEPROM Fail\n");
j3 0:11c9d61c8876 299 }
j3 0:11c9d61c8876 300
j3 0:11c9d61c8876 301 break;
j3 0:11c9d61c8876 302
j3 0:11c9d61c8876 303 case '6':
j3 0:11c9d61c8876 304 term.cls();
j3 0:11c9d61c8876 305 term.home();
j3 0:11c9d61c8876 306 break;
j3 0:11c9d61c8876 307
j3 0:11c9d61c8876 308 case '7':
j3 0:11c9d61c8876 309 term.printf("\nLeaving DS1920 Menu Options\n");
j3 0:11c9d61c8876 310 break;
j3 0:11c9d61c8876 311
j3 0:11c9d61c8876 312 default:
j3 0:11c9d61c8876 313 mbed_die();
j3 0:11c9d61c8876 314 break;
j3 0:11c9d61c8876 315 }
j3 0:11c9d61c8876 316 }
j3 0:11c9d61c8876 317 }