Demo of OneWire Library for mbed using the DS18B20 as an example slave device. Connect Tera Term t 9600bps 8N1 with terminal 'Receive' to auto and 'Transmit' to CR+LF

Dependencies:   OneWire Terminal mbed

Committer:
j3
Date:
Fri Mar 24 19:57:27 2017 +0000
Revision:
2:343505f6da8d
Parent:
1:819782bce064
updated libs, example.; Need to investigate 'mbed_wait_api' update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:e670c2af93db 1 /**********************************************************************
j3 0:e670c2af93db 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:e670c2af93db 3 *
j3 0:e670c2af93db 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:e670c2af93db 5 * copy of this software and associated documentation files (the "Software"),
j3 0:e670c2af93db 6 * to deal in the Software without restriction, including without limitation
j3 0:e670c2af93db 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:e670c2af93db 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:e670c2af93db 9 * Software is furnished to do so, subject to the following conditions:
j3 0:e670c2af93db 10 *
j3 0:e670c2af93db 11 * The above copyright notice and this permission notice shall be included
j3 0:e670c2af93db 12 * in all copies or substantial portions of the Software.
j3 0:e670c2af93db 13 *
j3 0:e670c2af93db 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:e670c2af93db 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:e670c2af93db 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:e670c2af93db 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:e670c2af93db 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:e670c2af93db 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:e670c2af93db 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:e670c2af93db 21 *
j3 0:e670c2af93db 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:e670c2af93db 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:e670c2af93db 24 * Products, Inc. Branding Policy.
j3 0:e670c2af93db 25 *
j3 0:e670c2af93db 26 * The mere transfer of this software does not imply any licenses
j3 0:e670c2af93db 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:e670c2af93db 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:e670c2af93db 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:e670c2af93db 30 * ownership rights.
j3 0:e670c2af93db 31 **********************************************************************/
j3 0:e670c2af93db 32
j3 0:e670c2af93db 33
j3 0:e670c2af93db 34 #include "mbed.h"
j3 0:e670c2af93db 35 #include "OneWire.h"
j3 0:e670c2af93db 36 #include "Terminal.h"
j3 0:e670c2af93db 37
j3 0:e670c2af93db 38 using namespace OneWire;
j3 0:e670c2af93db 39 using namespace RomCommands;
j3 0:e670c2af93db 40
j3 0:e670c2af93db 41 //Uncomment the master for your setup
j3 0:e670c2af93db 42
j3 0:e670c2af93db 43 //#define I2C_TO_OW_MASTER
j3 0:e670c2af93db 44 //#define SERIAL_TO_OW_MASTER
j3 0:e670c2af93db 45 #define BIT_BANG_MASTER
j3 0:e670c2af93db 46
j3 0:e670c2af93db 47 void print_rom_id(Terminal & term, const RomId & romId);
j3 0:e670c2af93db 48 void DS18B20Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);
j3 0:e670c2af93db 49
j3 0:e670c2af93db 50 int main(void)
j3 0:e670c2af93db 51 {
j3 0:e670c2af93db 52 Terminal term(USBTX, USBRX);
j3 0:e670c2af93db 53 char user_entry = 'y';
j3 0:e670c2af93db 54
j3 0:e670c2af93db 55 #if defined(I2C_TO_OW_MASTER)
j3 0:e670c2af93db 56 //Instantiates a 1-wire master using the given I2C bus SDA/SCL pins
j3 0:e670c2af93db 57 //Requires a DS248X series of masters on I2C bus
j3 0:e670c2af93db 58 //https://para.maximintegrated.com/en/results.mvp?fam=1wire&tree=master
j3 0:e670c2af93db 59
j3 0:e670c2af93db 60 I2C i2cBus(D14, D15);
j3 0:e670c2af93db 61 DS2484 owm(i2cBus);
j3 0:e670c2af93db 62 term.printf("\nStarting I2C to 1-wire Demo\n");
j3 0:e670c2af93db 63
j3 0:e670c2af93db 64 #elif defined(SERIAL_TO_OW_MASTER)
j3 0:e670c2af93db 65 //Instantiates a 1-wire master using the given UART tx/rx pins
j3 0:e670c2af93db 66 //Requires a DS2480B serial to 1-wire master
j3 0:e670c2af93db 67 //https://www.maximintegrated.com/en/products/interface/controllers-expanders/DS2480B.html
j3 0:e670c2af93db 68
j3 0:e670c2af93db 69 Serial ow_uart(D1, D0);
j3 0:e670c2af93db 70 Ds2480b owm(ow_uart);
j3 0:e670c2af93db 71 term.printf("\nStarting Serial to 1-wire Demo\n");
j3 0:e670c2af93db 72
j3 0:e670c2af93db 73 #elif defined(BIT_BANG_MASTER)
j3 0:e670c2af93db 74 //Instantiates a 1-wire master on the given GPIO pin
j3 0:e670c2af93db 75 //Requires MAX32600MBED platform
j3 0:e670c2af93db 76 //https://www.maximintegrated.com/en/products/digital/microcontrollers/MAX32600MBED.html
j3 0:e670c2af93db 77
j3 0:e670c2af93db 78 OwGpio owm(D2);
j3 0:e670c2af93db 79 term.printf("\nStarting Bit-Bang 1-wire Demo\n");
j3 0:e670c2af93db 80
j3 0:e670c2af93db 81 #else
j3 0:e670c2af93db 82 #error One Wire Master Not Defined
j3 0:e670c2af93db 83 #endif
j3 0:e670c2af93db 84
j3 0:e670c2af93db 85 OneWireMaster::CmdResult result = owm.OWInitMaster();
j3 0:e670c2af93db 86
j3 0:e670c2af93db 87 if(result == OneWireMaster::Success)
j3 0:e670c2af93db 88 {
j3 0:e670c2af93db 89 MultidropRomIterator selector(owm);
j3 0:e670c2af93db 90
j3 0:e670c2af93db 91 //Search state var for main loop
j3 0:e670c2af93db 92 SearchState searchState;
j3 0:e670c2af93db 93
j3 0:e670c2af93db 94 while(user_entry != 'n')
j3 0:e670c2af93db 95 {
j3 0:e670c2af93db 96 result = owm.OWReset();
j3 0:e670c2af93db 97 if(result == OneWireMaster::Success)
j3 0:e670c2af93db 98 {
j3 0:e670c2af93db 99 term.printf("\nOWReset success, starting search\n");
j3 0:e670c2af93db 100
j3 0:e670c2af93db 101 result = OWFirst(owm, searchState);
j3 0:e670c2af93db 102 if(result == OneWireMaster::Success)
j3 0:e670c2af93db 103 {
j3 0:e670c2af93db 104 do
j3 0:e670c2af93db 105 {
j3 0:e670c2af93db 106 //print current devices rom id
j3 0:e670c2af93db 107 print_rom_id(term, searchState.romId);
j3 0:e670c2af93db 108
j3 0:e670c2af93db 109
j3 0:e670c2af93db 110 //This could be a switch statement based on familyCodes
j3 0:e670c2af93db 111 //if your app has more than one device on the bus
j3 1:819782bce064 112 if(searchState.romId.familyCode() == 0x28)
j3 0:e670c2af93db 113 {
j3 0:e670c2af93db 114 DS18B20Menu(term, searchState.romId, selector);
j3 0:e670c2af93db 115 }
j3 0:e670c2af93db 116
j3 0:e670c2af93db 117 //find the next device if any
j3 0:e670c2af93db 118 result = OWNext(owm, searchState);
j3 0:e670c2af93db 119 }
j3 0:e670c2af93db 120 while(result == OneWireMaster::Success);
j3 0:e670c2af93db 121 }
j3 0:e670c2af93db 122 else
j3 0:e670c2af93db 123 {
j3 0:e670c2af93db 124 term.printf("\nSearch failed\n");
j3 0:e670c2af93db 125 term.printf("\nError code = %d\n", result);
j3 0:e670c2af93db 126 }
j3 0:e670c2af93db 127 }
j3 0:e670c2af93db 128 else
j3 0:e670c2af93db 129 {
j3 0:e670c2af93db 130 term.printf("\nFailed to find any 1-wire devices on bus\n");
j3 0:e670c2af93db 131 term.printf("\nError code = %d\n", result);
j3 0:e670c2af93db 132 }
j3 0:e670c2af93db 133
j3 0:e670c2af93db 134 user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
j3 0:e670c2af93db 135 term.cls();
j3 0:e670c2af93db 136 term.home();
j3 0:e670c2af93db 137 }
j3 0:e670c2af93db 138 }
j3 0:e670c2af93db 139 else
j3 0:e670c2af93db 140 {
j3 0:e670c2af93db 141 term.printf("\nFailed to initialize the 1-wire Master\n");
j3 0:e670c2af93db 142 term.printf("\nError code = %d\n", result);
j3 0:e670c2af93db 143 }
j3 0:e670c2af93db 144
j3 0:e670c2af93db 145 term.printf("\nEnding Program\n");
j3 0:e670c2af93db 146
j3 0:e670c2af93db 147 return 0;
j3 0:e670c2af93db 148 }
j3 0:e670c2af93db 149 //***** end main ******************************************************
j3 0:e670c2af93db 150
j3 0:e670c2af93db 151
j3 0:e670c2af93db 152 //*********************************************************************
j3 0:e670c2af93db 153 void print_rom_id(Terminal & term, const RomId & romId)
j3 0:e670c2af93db 154 {
j3 0:e670c2af93db 155 //print the rom number
j3 0:e670c2af93db 156 term.printf("\n");
j3 2:343505f6da8d 157 int8_t idx = 8;
j3 0:e670c2af93db 158 do
j3 0:e670c2af93db 159 {
j3 2:343505f6da8d 160 term.printf("0x%02x ", romId.buffer[idx--]);
j3 0:e670c2af93db 161 }
j3 0:e670c2af93db 162 while(idx >= 0);
j3 0:e670c2af93db 163 term.printf("\n");
j3 0:e670c2af93db 164 }
j3 0:e670c2af93db 165
j3 0:e670c2af93db 166
j3 0:e670c2af93db 167 //*********************************************************************
j3 0:e670c2af93db 168 void DS18B20Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
j3 0:e670c2af93db 169 {
j3 0:e670c2af93db 170 char userEntry = '0';
j3 0:e670c2af93db 171 uint8_t th, tl, idx, intResolution;
j3 1:819782bce064 172 DS18B20::Resolution resolution = DS18B20::TwelveBit;
j3 0:e670c2af93db 173 bool localPower = false;
j3 0:e670c2af93db 174 uint8_t scratchPadBuff[9];
j3 0:e670c2af93db 175 float temperature;
j3 0:e670c2af93db 176
j3 0:e670c2af93db 177 DS18B20 tempSensor(selector);
j3 0:e670c2af93db 178 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 0:e670c2af93db 179 tempSensor.setRomId(romId);
j3 0:e670c2af93db 180
j3 0:e670c2af93db 181 while(userEntry != '8')
j3 0:e670c2af93db 182 {
j3 0:e670c2af93db 183 term.printf("\nDS18B20 Menu Options\n");
j3 0:e670c2af93db 184 term.printf("\n%t1. Write ScratchPad");
j3 0:e670c2af93db 185 term.printf("\n%t2. Read Scratchpad");
j3 0:e670c2af93db 186 term.printf("\n%t3. Copy Scratchpad");
j3 0:e670c2af93db 187 term.printf("\n%t4. Convert Temperature");
j3 0:e670c2af93db 188 term.printf("\n%t5. Recall EEPROM");
j3 0:e670c2af93db 189 term.printf("\n%t6. Read Power Supply");
j3 0:e670c2af93db 190 term.printf("\n%t7. Clear Screen");
j3 0:e670c2af93db 191 term.printf("\n%t8. Continuously Convert Temperature");
j3 0:e670c2af93db 192 term.printf("\n%t9. Quit");
j3 0:e670c2af93db 193
j3 0:e670c2af93db 194 userEntry = term.get_char("\nPlease select an option above: ", '1', '9');
j3 0:e670c2af93db 195
j3 0:e670c2af93db 196 switch(userEntry)
j3 0:e670c2af93db 197 {
j3 0:e670c2af93db 198 case '1':
j3 0:e670c2af93db 199 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
j3 0:e670c2af93db 200 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
j3 0:e670c2af93db 201 intResolution = term.get_int32("\nPlease enter desired reolution (9 to 12): ", 9, 12);
j3 0:e670c2af93db 202
j3 0:e670c2af93db 203 switch(intResolution)
j3 0:e670c2af93db 204 {
j3 0:e670c2af93db 205 case 9:
j3 1:819782bce064 206 resolution = DS18B20::NineBit;
j3 0:e670c2af93db 207 case 10:
j3 1:819782bce064 208 resolution = DS18B20::TenBit;
j3 0:e670c2af93db 209 case 11:
j3 1:819782bce064 210 resolution = DS18B20::ElevenBit;
j3 0:e670c2af93db 211 case 12:
j3 0:e670c2af93db 212 default:
j3 1:819782bce064 213 resolution = DS18B20::TwelveBit;
j3 0:e670c2af93db 214 break;
j3 0:e670c2af93db 215 }
j3 0:e670c2af93db 216
j3 0:e670c2af93db 217 result = tempSensor.writeScratchPad(th, tl, resolution);
j3 0:e670c2af93db 218 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 219 {
j3 0:e670c2af93db 220 term.printf("\nWrite Scratchpad Success\n");
j3 0:e670c2af93db 221 }
j3 0:e670c2af93db 222 else
j3 0:e670c2af93db 223 {
j3 0:e670c2af93db 224 term.printf("\nWrite Scratchpad Fail\n");
j3 0:e670c2af93db 225 }
j3 0:e670c2af93db 226
j3 0:e670c2af93db 227 break;
j3 0:e670c2af93db 228
j3 0:e670c2af93db 229 case '2':
j3 0:e670c2af93db 230
j3 0:e670c2af93db 231 result = tempSensor.readScratchPad(scratchPadBuff);
j3 0:e670c2af93db 232 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 233 {
j3 0:e670c2af93db 234 term.printf("\nRead Scratchpad Success\n");
j3 0:e670c2af93db 235 term.printf("\nScratchpad = ");
j3 0:e670c2af93db 236 for(idx = 0; idx < 8; idx++)
j3 0:e670c2af93db 237 {
j3 0:e670c2af93db 238 if(scratchPadBuff[idx] < 16)
j3 0:e670c2af93db 239 {
j3 0:e670c2af93db 240 term.printf("0x0%x ", scratchPadBuff[idx]);
j3 0:e670c2af93db 241 }
j3 0:e670c2af93db 242 else
j3 0:e670c2af93db 243 {
j3 0:e670c2af93db 244 term.printf("0x%2x ", scratchPadBuff[idx]);
j3 0:e670c2af93db 245 }
j3 0:e670c2af93db 246 }
j3 0:e670c2af93db 247 term.printf("\n");
j3 0:e670c2af93db 248
j3 0:e670c2af93db 249 }
j3 0:e670c2af93db 250 else
j3 0:e670c2af93db 251 {
j3 0:e670c2af93db 252 term.printf("\nRead Scratchpad Fail\n");
j3 0:e670c2af93db 253 }
j3 0:e670c2af93db 254
j3 0:e670c2af93db 255 break;
j3 0:e670c2af93db 256
j3 0:e670c2af93db 257 case '3':
j3 0:e670c2af93db 258
j3 0:e670c2af93db 259 result = tempSensor.copyScratchPad();
j3 0:e670c2af93db 260 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 261 {
j3 0:e670c2af93db 262 term.printf("\nCopy Scratchpad Success\n");
j3 0:e670c2af93db 263 }
j3 0:e670c2af93db 264 else
j3 0:e670c2af93db 265 {
j3 0:e670c2af93db 266 term.printf("\nCopy Scratchpad Fail\n");
j3 0:e670c2af93db 267 }
j3 0:e670c2af93db 268
j3 0:e670c2af93db 269 break;
j3 0:e670c2af93db 270
j3 0:e670c2af93db 271 case '4':
j3 0:e670c2af93db 272
j3 0:e670c2af93db 273 result = tempSensor.convertTemperature(temperature);
j3 0:e670c2af93db 274 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 275 {
j3 0:e670c2af93db 276 term.printf("\nConvert Temperature Success\n");
j3 0:e670c2af93db 277 term.printf("\nTemperature = %.4f", temperature);
j3 0:e670c2af93db 278 }
j3 0:e670c2af93db 279 else
j3 0:e670c2af93db 280 {
j3 0:e670c2af93db 281 term.printf("\nConvert Temperature Fail\n");
j3 0:e670c2af93db 282 }
j3 0:e670c2af93db 283
j3 0:e670c2af93db 284 break;
j3 0:e670c2af93db 285
j3 0:e670c2af93db 286 case '5':
j3 0:e670c2af93db 287
j3 0:e670c2af93db 288 result = tempSensor.recallEEPROM();
j3 0:e670c2af93db 289 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 290 {
j3 0:e670c2af93db 291 term.printf("\nRecall EEPROM Success\n");
j3 0:e670c2af93db 292 }
j3 0:e670c2af93db 293 else
j3 0:e670c2af93db 294 {
j3 0:e670c2af93db 295 term.printf("\nRecall EEPROM Fail\n");
j3 0:e670c2af93db 296 }
j3 0:e670c2af93db 297
j3 0:e670c2af93db 298 break;
j3 0:e670c2af93db 299
j3 0:e670c2af93db 300 case '6':
j3 0:e670c2af93db 301 result = tempSensor.readPowerSupply(localPower);
j3 0:e670c2af93db 302 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 303 {
j3 0:e670c2af93db 304 term.printf("Read Power Supply Success\n");
j3 0:e670c2af93db 305 term.printf("DS18B20 %s\n", localPower ? "Has Local Power" : "Is Parasite Powered");
j3 0:e670c2af93db 306 }
j3 0:e670c2af93db 307 else
j3 0:e670c2af93db 308 {
j3 0:e670c2af93db 309 term.printf("Read Power Supply Failed\n");
j3 0:e670c2af93db 310 }
j3 0:e670c2af93db 311 break;
j3 0:e670c2af93db 312
j3 0:e670c2af93db 313 case '7':
j3 0:e670c2af93db 314 term.cls();
j3 0:e670c2af93db 315 term.home();
j3 0:e670c2af93db 316 break;
j3 0:e670c2af93db 317
j3 0:e670c2af93db 318 case '8':
j3 0:e670c2af93db 319 do
j3 0:e670c2af93db 320 {
j3 0:e670c2af93db 321 result = tempSensor.convertTemperature(temperature);
j3 0:e670c2af93db 322 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 323 {
j3 0:e670c2af93db 324 term.printf("\nTemperature = %.4f", temperature);
j3 0:e670c2af93db 325 }
j3 0:e670c2af93db 326 else
j3 0:e670c2af93db 327 {
j3 0:e670c2af93db 328 term.printf("\nConvert Temperature Fail\n");
j3 0:e670c2af93db 329 }
j3 0:e670c2af93db 330 }
j3 0:e670c2af93db 331 while(result == OneWireSlave::Success);
j3 0:e670c2af93db 332 break;
j3 0:e670c2af93db 333
j3 0:e670c2af93db 334 case '9':
j3 0:e670c2af93db 335 term.printf("\nLeaving DS18B20 Menu Options\n");
j3 0:e670c2af93db 336 break;
j3 0:e670c2af93db 337
j3 0:e670c2af93db 338 default:
j3 0:e670c2af93db 339 mbed_die();
j3 0:e670c2af93db 340 break;
j3 0:e670c2af93db 341 }
j3 0:e670c2af93db 342 }
j3 0:e670c2af93db 343 }