Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Onewire.cpp
00001 #include "Onewire.h" 00002 00003 Onewire::Onewire(PinName oneBus):oneBus_(oneBus){ 00004 00005 } 00006 void Onewire::writeBit(int bit) { 00007 bit = bit & 0x01; 00008 if (bit) { 00009 // Write '1' bit 00010 oneBus_.output(); 00011 oneBus_ = 0; 00012 wait_us(5); 00013 oneBus_.input(); 00014 wait_us(60); 00015 } else { 00016 // Write '0' bit 00017 oneBus_.output(); 00018 oneBus_ = 0; 00019 wait_us(70); 00020 oneBus_.input(); 00021 wait_us(2); 00022 } 00023 } 00024 00025 int Onewire::readBit() { 00026 char result; 00027 00028 oneBus_.output(); 00029 oneBus_ = 0; 00030 wait_us(1); 00031 oneBus_.input(); 00032 wait_us(5); 00033 result = oneBus_.read(); 00034 wait_us(55); 00035 return result; 00036 00037 } 00038 00039 int Onewire::init() { 00040 oneBus_.output(); 00041 oneBus_ = 0; 00042 wait_us(480); 00043 oneBus_.input(); 00044 wait_us(60); 00045 if (oneBus_.read() == 0) { 00046 wait(0.0001); 00047 return 1; 00048 } 00049 return 0; 00050 } 00051 int Onewire::readByte() { 00052 int result = 0; 00053 00054 for (int loop = 0; loop < 8; loop++) { 00055 // shift the result to get it ready for the next bit 00056 result >>= 1; 00057 00058 // if result is one, then set MS bit 00059 if (readBit()) 00060 result |= 0x80; 00061 } 00062 return result; 00063 } 00064 void Onewire::writeByte(char data) { 00065 // Loop to write each bit in the byte, LS-bit first 00066 for (int loop = 0; loop < 8; loop++) { 00067 writeBit(data & 0x01); 00068 00069 // shift the data byte for the next bit 00070 data >>= 1; 00071 } 00072 } 00073 unsigned char Onewire::CRC(unsigned char* addr, unsigned char len) { 00074 unsigned char i, j; 00075 unsigned char crc = 0; 00076 00077 for (i = 0; i < len; i++) { 00078 unsigned char inbyte = addr[i]; 00079 for (j = 0; j < 8; j++) { 00080 unsigned char mix = (crc ^ inbyte) & 0x01; 00081 crc >>= 1; 00082 if (mix) crc ^= 0x8C; 00083 inbyte >>= 1; 00084 } 00085 } 00086 00087 return crc; 00088 } 00089
Generated on Mon Jul 18 2022 03:31:55 by
1.7.2