diff --git a/main.c b/main.c index 280337c..9e6c0a9 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ int main (void) { unsigned char devid[8]={40,12,203,62,0,0,0,108}; + short i; // activate temperature sensor if (ow_reset()) @@ -18,6 +19,8 @@ int main (void) { do { + for (i=0; i<500; i++) + ow_mswait(); printf("%i deg F\n", ow_temp_read(devid, OW_TEMP_FAHRENHEIT)); } while (!kbhit()); diff --git a/ow-apple2.s b/ow-apple2.s index d493da6..244b727 100644 --- a/ow-apple2.s +++ b/ow-apple2.s @@ -5,6 +5,7 @@ .export _ow_read_impl .export _ow_reset_impl .export _ow_wait_impl + .export _ow_mswait_impl ; timing loops assume standard ~1 MHz speed for unaccelerated ; 8-bit Apple IIs @@ -100,3 +101,13 @@ _ow_wait_impl: : bit PB2 bpl :- rts + +; wait 1 ms +_ow_mswait_impl: + ldy #100 ;100*10=1000 us=1 ms +: nop + nop + nop + dey + bne :- + rts diff --git a/ow-common.s b/ow-common.s index f4a5e3a..5d46af2 100644 --- a/ow-common.s +++ b/ow-common.s @@ -6,6 +6,7 @@ .export _ow_readbyte .export _ow_reset .export _ow_wait + .export _ow_mswait ; provided by system-specific routines at link time @@ -14,6 +15,7 @@ .import _ow_read_impl .import _ow_reset_impl .import _ow_wait_impl + .import _ow_mswait_impl .bss @@ -26,6 +28,7 @@ owtmp: .res 1 _ow_send1 := _ow_send1_impl _ow_send0 := _ow_send0_impl _ow_wait := _ow_wait_impl +_ow_mswait := _ow_mswait_impl ; reset bus: return 0 if 1-Wire devices present, 1 otherwise diff --git a/ow-temp.c b/ow-temp.c index c7be66d..162f3db 100644 --- a/ow-temp.c +++ b/ow-temp.c @@ -2,21 +2,26 @@ short ow_temp_read(unsigned char* devid, unsigned char format) { - unsigned char t1, t2; + unsigned char data[8]; short rawtemp; + unsigned char i; if (devid[0]!=40) return OW_ERR_INVALIDDEVICE; - ow_selectdevice(devid); - ow_writebyte(68); // ask for a temperature reading - ow_wait(); // wait for operation to complete - ow_selectdevice(devid); - ow_writebyte(190); // retrieve the reading - t1=ow_readbyte(); - t2=ow_readbyte(); - ow_reset(); + do + { + ow_selectdevice(devid); + ow_writebyte(68); // ask for a temperature reading + ow_wait(); // wait for operation to complete + ow_selectdevice(devid); + ow_writebyte(190); // retrieve the reading + for (i=0; i<8; i++) + data[i]=ow_readbyte(); + ow_mswait(); + } + while (ow_crc(data,8)!=0); // repeat until we get a valid reading - rawtemp=(short)(t1+((int)t2<<8)); + rawtemp=(short)(data[0]+((int)data[1]<<8)); switch (format) { case OW_TEMP_FAHRENHEIT: @@ -28,3 +33,4 @@ short ow_temp_read(unsigned char* devid, unsigned char format) return rawtemp; } } + diff --git a/ow-temp.h b/ow-temp.h index b8a4cf0..9866f61 100644 --- a/ow-temp.h +++ b/ow-temp.h @@ -4,6 +4,7 @@ #include "ow.h" // 1-Wire temperature sensor functions +// (works with DS18B20; unknown if it'll work with others) short ow_temp_read(unsigned char* devid, unsigned char format); diff --git a/ow.c b/ow.c index c331b5d..823e45a 100644 --- a/ow.c +++ b/ow.c @@ -1,5 +1,25 @@ #include "ow.h" +unsigned char ow_crc_lookup[256]= +{ + 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, + 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, + 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, + 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, + 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, + 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, + 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, + 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, + 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, + 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, + 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, + 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, + 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, + 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, + 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, + 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 +}; + void ow_selectdevice(unsigned char* devid) { ow_reset(); @@ -14,3 +34,14 @@ void ow_selectdevice(unsigned char* devid) ow_writebyte(devid[7]); return; } + +unsigned char ow_crc(unsigned char* data, unsigned char len) +{ + unsigned char crc=0; + unsigned char i; + + for (i=0; i