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:
Sun Jul 03 20:54:30 2016 +0000
Revision:
0:e670c2af93db
Child:
1:819782bce064
Init Commit

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 0:e670c2af93db 112 if(searchState.romId.familyCode() == DS18B20::FAMILY_CODE)
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 0:e670c2af93db 157 int8_t idx = (RomId::byteLen - 1);
j3 0:e670c2af93db 158 do
j3 0:e670c2af93db 159 {
j3 0:e670c2af93db 160 if(romId[idx] < 16)
j3 0:e670c2af93db 161 {
j3 0:e670c2af93db 162 term.printf("0x0%x ", romId[idx--]);
j3 0:e670c2af93db 163 }
j3 0:e670c2af93db 164 else
j3 0:e670c2af93db 165 {
j3 0:e670c2af93db 166 term.printf("0x%2x ", romId[idx--]);
j3 0:e670c2af93db 167 }
j3 0:e670c2af93db 168 }
j3 0:e670c2af93db 169 while(idx >= 0);
j3 0:e670c2af93db 170 term.printf("\n");
j3 0:e670c2af93db 171 }
j3 0:e670c2af93db 172
j3 0:e670c2af93db 173
j3 0:e670c2af93db 174 //*********************************************************************
j3 0:e670c2af93db 175 void DS18B20Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
j3 0:e670c2af93db 176 {
j3 0:e670c2af93db 177 char userEntry = '0';
j3 0:e670c2af93db 178 uint8_t th, tl, idx, intResolution;
j3 0:e670c2af93db 179 DS18B20::Resolution resolution = DS18B20::TWELVE_BIT;
j3 0:e670c2af93db 180 bool localPower = false;
j3 0:e670c2af93db 181 uint8_t scratchPadBuff[9];
j3 0:e670c2af93db 182 float temperature;
j3 0:e670c2af93db 183
j3 0:e670c2af93db 184 DS18B20 tempSensor(selector);
j3 0:e670c2af93db 185 OneWireSlave::CmdResult result = OneWireSlave::OperationFailure;
j3 0:e670c2af93db 186 tempSensor.setRomId(romId);
j3 0:e670c2af93db 187
j3 0:e670c2af93db 188 while(userEntry != '8')
j3 0:e670c2af93db 189 {
j3 0:e670c2af93db 190 term.printf("\nDS18B20 Menu Options\n");
j3 0:e670c2af93db 191 term.printf("\n%t1. Write ScratchPad");
j3 0:e670c2af93db 192 term.printf("\n%t2. Read Scratchpad");
j3 0:e670c2af93db 193 term.printf("\n%t3. Copy Scratchpad");
j3 0:e670c2af93db 194 term.printf("\n%t4. Convert Temperature");
j3 0:e670c2af93db 195 term.printf("\n%t5. Recall EEPROM");
j3 0:e670c2af93db 196 term.printf("\n%t6. Read Power Supply");
j3 0:e670c2af93db 197 term.printf("\n%t7. Clear Screen");
j3 0:e670c2af93db 198 term.printf("\n%t8. Continuously Convert Temperature");
j3 0:e670c2af93db 199 term.printf("\n%t9. Quit");
j3 0:e670c2af93db 200
j3 0:e670c2af93db 201 userEntry = term.get_char("\nPlease select an option above: ", '1', '9');
j3 0:e670c2af93db 202
j3 0:e670c2af93db 203 switch(userEntry)
j3 0:e670c2af93db 204 {
j3 0:e670c2af93db 205 case '1':
j3 0:e670c2af93db 206 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
j3 0:e670c2af93db 207 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
j3 0:e670c2af93db 208 intResolution = term.get_int32("\nPlease enter desired reolution (9 to 12): ", 9, 12);
j3 0:e670c2af93db 209
j3 0:e670c2af93db 210 switch(intResolution)
j3 0:e670c2af93db 211 {
j3 0:e670c2af93db 212 case 9:
j3 0:e670c2af93db 213 resolution = DS18B20::NINE_BIT;
j3 0:e670c2af93db 214 case 10:
j3 0:e670c2af93db 215 resolution = DS18B20::TEN_BIT;
j3 0:e670c2af93db 216 case 11:
j3 0:e670c2af93db 217 resolution = DS18B20::ELEVEN_BIT;
j3 0:e670c2af93db 218 case 12:
j3 0:e670c2af93db 219 default:
j3 0:e670c2af93db 220 resolution = DS18B20::TWELVE_BIT;
j3 0:e670c2af93db 221 break;
j3 0:e670c2af93db 222 }
j3 0:e670c2af93db 223
j3 0:e670c2af93db 224 result = tempSensor.writeScratchPad(th, tl, resolution);
j3 0:e670c2af93db 225 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 226 {
j3 0:e670c2af93db 227 term.printf("\nWrite Scratchpad Success\n");
j3 0:e670c2af93db 228 }
j3 0:e670c2af93db 229 else
j3 0:e670c2af93db 230 {
j3 0:e670c2af93db 231 term.printf("\nWrite Scratchpad Fail\n");
j3 0:e670c2af93db 232 }
j3 0:e670c2af93db 233
j3 0:e670c2af93db 234 break;
j3 0:e670c2af93db 235
j3 0:e670c2af93db 236 case '2':
j3 0:e670c2af93db 237
j3 0:e670c2af93db 238 result = tempSensor.readScratchPad(scratchPadBuff);
j3 0:e670c2af93db 239 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 240 {
j3 0:e670c2af93db 241 term.printf("\nRead Scratchpad Success\n");
j3 0:e670c2af93db 242 term.printf("\nScratchpad = ");
j3 0:e670c2af93db 243 for(idx = 0; idx < 8; idx++)
j3 0:e670c2af93db 244 {
j3 0:e670c2af93db 245 if(scratchPadBuff[idx] < 16)
j3 0:e670c2af93db 246 {
j3 0:e670c2af93db 247 term.printf("0x0%x ", scratchPadBuff[idx]);
j3 0:e670c2af93db 248 }
j3 0:e670c2af93db 249 else
j3 0:e670c2af93db 250 {
j3 0:e670c2af93db 251 term.printf("0x%2x ", scratchPadBuff[idx]);
j3 0:e670c2af93db 252 }
j3 0:e670c2af93db 253 }
j3 0:e670c2af93db 254 term.printf("\n");
j3 0:e670c2af93db 255
j3 0:e670c2af93db 256 }
j3 0:e670c2af93db 257 else
j3 0:e670c2af93db 258 {
j3 0:e670c2af93db 259 term.printf("\nRead Scratchpad Fail\n");
j3 0:e670c2af93db 260 }
j3 0:e670c2af93db 261
j3 0:e670c2af93db 262 break;
j3 0:e670c2af93db 263
j3 0:e670c2af93db 264 case '3':
j3 0:e670c2af93db 265
j3 0:e670c2af93db 266 result = tempSensor.copyScratchPad();
j3 0:e670c2af93db 267 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 268 {
j3 0:e670c2af93db 269 term.printf("\nCopy Scratchpad Success\n");
j3 0:e670c2af93db 270 }
j3 0:e670c2af93db 271 else
j3 0:e670c2af93db 272 {
j3 0:e670c2af93db 273 term.printf("\nCopy Scratchpad Fail\n");
j3 0:e670c2af93db 274 }
j3 0:e670c2af93db 275
j3 0:e670c2af93db 276 break;
j3 0:e670c2af93db 277
j3 0:e670c2af93db 278 case '4':
j3 0:e670c2af93db 279
j3 0:e670c2af93db 280 result = tempSensor.convertTemperature(temperature);
j3 0:e670c2af93db 281 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 282 {
j3 0:e670c2af93db 283 term.printf("\nConvert Temperature Success\n");
j3 0:e670c2af93db 284 term.printf("\nTemperature = %.4f", temperature);
j3 0:e670c2af93db 285 }
j3 0:e670c2af93db 286 else
j3 0:e670c2af93db 287 {
j3 0:e670c2af93db 288 term.printf("\nConvert Temperature Fail\n");
j3 0:e670c2af93db 289 }
j3 0:e670c2af93db 290
j3 0:e670c2af93db 291 break;
j3 0:e670c2af93db 292
j3 0:e670c2af93db 293 case '5':
j3 0:e670c2af93db 294
j3 0:e670c2af93db 295 result = tempSensor.recallEEPROM();
j3 0:e670c2af93db 296 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 297 {
j3 0:e670c2af93db 298 term.printf("\nRecall EEPROM Success\n");
j3 0:e670c2af93db 299 }
j3 0:e670c2af93db 300 else
j3 0:e670c2af93db 301 {
j3 0:e670c2af93db 302 term.printf("\nRecall EEPROM Fail\n");
j3 0:e670c2af93db 303 }
j3 0:e670c2af93db 304
j3 0:e670c2af93db 305 break;
j3 0:e670c2af93db 306
j3 0:e670c2af93db 307 case '6':
j3 0:e670c2af93db 308 result = tempSensor.readPowerSupply(localPower);
j3 0:e670c2af93db 309 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 310 {
j3 0:e670c2af93db 311 term.printf("Read Power Supply Success\n");
j3 0:e670c2af93db 312 term.printf("DS18B20 %s\n", localPower ? "Has Local Power" : "Is Parasite Powered");
j3 0:e670c2af93db 313 }
j3 0:e670c2af93db 314 else
j3 0:e670c2af93db 315 {
j3 0:e670c2af93db 316 term.printf("Read Power Supply Failed\n");
j3 0:e670c2af93db 317 }
j3 0:e670c2af93db 318 break;
j3 0:e670c2af93db 319
j3 0:e670c2af93db 320 case '7':
j3 0:e670c2af93db 321 term.cls();
j3 0:e670c2af93db 322 term.home();
j3 0:e670c2af93db 323 break;
j3 0:e670c2af93db 324
j3 0:e670c2af93db 325 case '8':
j3 0:e670c2af93db 326 do
j3 0:e670c2af93db 327 {
j3 0:e670c2af93db 328 result = tempSensor.convertTemperature(temperature);
j3 0:e670c2af93db 329 if(result == OneWireSlave::Success)
j3 0:e670c2af93db 330 {
j3 0:e670c2af93db 331 term.printf("\nTemperature = %.4f", temperature);
j3 0:e670c2af93db 332 }
j3 0:e670c2af93db 333 else
j3 0:e670c2af93db 334 {
j3 0:e670c2af93db 335 term.printf("\nConvert Temperature Fail\n");
j3 0:e670c2af93db 336 }
j3 0:e670c2af93db 337 }
j3 0:e670c2af93db 338 while(result == OneWireSlave::Success);
j3 0:e670c2af93db 339 break;
j3 0:e670c2af93db 340
j3 0:e670c2af93db 341 case '9':
j3 0:e670c2af93db 342 term.printf("\nLeaving DS18B20 Menu Options\n");
j3 0:e670c2af93db 343 break;
j3 0:e670c2af93db 344
j3 0:e670c2af93db 345 default:
j3 0:e670c2af93db 346 mbed_die();
j3 0:e670c2af93db 347 break;
j3 0:e670c2af93db 348 }
j3 0:e670c2af93db 349 }
j3 0:e670c2af93db 350 }