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.
Revision 0:d961f715d82b, committed 2013-06-23
- Comitter:
- simonbarker
- Date:
- Sun Jun 23 15:17:39 2013 +0000
- Commit message:
- Finalised OneWire bus methods
Changed in this revision
| Onewire.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Onewire.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Onewire.cpp Sun Jun 23 15:17:39 2013 +0000
@@ -0,0 +1,89 @@
+#include "Onewire.h"
+
+Onewire::Onewire(PinName oneBus):oneBus_(oneBus){
+
+}
+void Onewire::writeBit(int bit) {
+ bit = bit & 0x01;
+ if (bit) {
+ // Write '1' bit
+ oneBus_.output();
+ oneBus_ = 0;
+ wait_us(5);
+ oneBus_.input();
+ wait_us(60);
+ } else {
+ // Write '0' bit
+ oneBus_.output();
+ oneBus_ = 0;
+ wait_us(70);
+ oneBus_.input();
+ wait_us(2);
+ }
+}
+
+int Onewire::readBit() {
+ char result;
+
+ oneBus_.output();
+ oneBus_ = 0;
+ wait_us(1);
+ oneBus_.input();
+ wait_us(5);
+ result = oneBus_.read();
+ wait_us(55);
+ return result;
+
+}
+
+int Onewire::init() {
+ oneBus_.output();
+ oneBus_ = 0;
+ wait_us(480);
+ oneBus_.input();
+ wait_us(60);
+ if (oneBus_.read() == 0) {
+ wait(0.0001);
+ return 1;
+ }
+ return 0;
+}
+int Onewire::readByte() {
+ int result = 0;
+
+ for (int loop = 0; loop < 8; loop++) {
+ // shift the result to get it ready for the next bit
+ result >>= 1;
+
+ // if result is one, then set MS bit
+ if (readBit())
+ result |= 0x80;
+ }
+ return result;
+}
+void Onewire::writeByte(char data) {
+ // Loop to write each bit in the byte, LS-bit first
+ for (int loop = 0; loop < 8; loop++) {
+ writeBit(data & 0x01);
+
+ // shift the data byte for the next bit
+ data >>= 1;
+ }
+}
+unsigned char Onewire::CRC(unsigned char* addr, unsigned char len) {
+ unsigned char i, j;
+ unsigned char crc = 0;
+
+ for (i = 0; i < len; i++) {
+ unsigned char inbyte = addr[i];
+ for (j = 0; j < 8; j++) {
+ unsigned char mix = (crc ^ inbyte) & 0x01;
+ crc >>= 1;
+ if (mix) crc ^= 0x8C;
+ inbyte >>= 1;
+ }
+ }
+
+ return crc;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Onewire.h Sun Jun 23 15:17:39 2013 +0000
@@ -0,0 +1,20 @@
+#ifndef Onewire_h
+#define Onewire_h
+
+#include "mbed.h"
+
+class Onewire{
+
+public:
+ Onewire(PinName oneBus);
+ void writeBit(int bit);
+ int readBit();
+ int init();
+ int readByte();
+ void writeByte(char data);
+ unsigned char CRC(unsigned char* addr, unsigned char len);
+
+private:
+ DigitalInOut oneBus_;
+};
+#endif
\ No newline at end of file