From: Michael McMaster Date: Wed, 14 Nov 2012 11:11:32 +0000 (+1000) Subject: Tested to work. X-Git-Tag: 1.0^0 X-Git-Url: http://git.codesrc.com/gitweb.cgi?a=commitdiff_plain;h=HEAD;p=chickenFan.git Tested to work. Finished. --- diff --git a/BOM.ods b/BOM.ods new file mode 100644 index 0000000..e3d5fbd Binary files /dev/null and b/BOM.ods differ diff --git a/Makefile b/Makefile index 36c577b..999e6c6 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,8 @@ chickenFan.elf: chickenFan.c avr-gcc -Wall -Os -std=c99 -mmcu=attiny25 $^ -o $@ # Use with arduinoisp example sketch. See http://hlt.media.mit.edu/?p=1706 -.PHONY: program -program: chickenFan.hex +.PHONY: burn +burn: chickenFan.hex avrdude -P /dev/ttyUSB0 -b 19200 -c avrisp -p t25 -v -e -U flash:w:chickenFan.hex .PHONY: clean diff --git a/chickenFan.c b/chickenFan.c index 2cb98ac..9a20904 100644 --- a/chickenFan.c +++ b/chickenFan.c @@ -32,6 +32,7 @@ // required for avr-libc delay functions. // // avr-libc: tested with 1.8.0 +// // 1 MHz internal oscillator. Assumes default fuse settings. #define F_CPU 1000000UL @@ -46,13 +47,13 @@ #define SETBIT(port, bitnum) port |= _BV(bitnum) #define CLRBIT(port, bitnum) port &= (uint8_t) ~_BV(bitnum) -#define READBIT(port, bitnum) (!!(port & _BV(bitnum))) +#define READBIT(port, bitnum) (port & (_BV(bitnum))) // One-Wire active-low protocol #define ONEWIRE_PIN PB0 -#define ONEWIRE_LOW() {SETBIT(DDRB, ONEWIRE_PIN); CLRBIT(PORTB, ONEWIRE_PIN); } -#define ONEWIRE_HI() CLRBIT(DDRB, ONEWIRE_PIN) +#define ONEWIRE_LOW() SETBIT(DDRB, ONEWIRE_PIN); +#define ONEWIRE_HI() CLRBIT(DDRB, ONEWIRE_PIN); #define ONEWIRE_READ() READBIT(PINB, ONEWIRE_PIN) static void oneWireInit() @@ -69,6 +70,9 @@ static void oneWireInit() // Ok, we're -supposed- to check that the slave device // also pulls the line low, but we just assume it will // and wait until it has finished. + + // Tests with a CRO show the DS18B20 holding the line low + // for 100us. (datasheet says 60-240us) } _delay_us(480); } @@ -78,21 +82,21 @@ static void oneWireWrite(uint8_t value) // LSB first, active low for (int i = 0; i < 8; ++i) { - ONEWIRE_LOW(); - if (value & _BV(i)) { - // Min 1uS delay taken care of by ifcondition. - // Don't tempt fate by waiting too long. - + ONEWIRE_LOW(); ONEWIRE_HI(); _delay_us(60); } else { + ONEWIRE_LOW(); _delay_us(60); ONEWIRE_HI(); + // Ensure the weak pullup has time to work + _delay_us(4); } + _NOP(); // 1uS recovery time. Probably not needed. } } @@ -106,14 +110,21 @@ static uint8_t oneWireReadBit() // we need to read it towards the END of that 15uS // period. - ONEWIRE_LOW(); // Up to 4 cycles ? - _NOP(); // Ensure we wait 1us + ONEWIRE_LOW(); //2 cycles ONEWIRE_HI(); // 2 cycles + + // We -start- going high 5 cycles after going low. + // 2.5V ~ 2ms later. 5V ~ 4ms later. + // Need to leave sufficient time for the weak 10k pullup + // to raise the bus or else we'll read 0 instead of 1, + // but still sample early enough before the time slot + // runs out. + _NOP(); + _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); - _NOP(); // Up to 12 cycles since the pin went low. uint8_t bit = ONEWIRE_READ(); // 2 cycles for I/O access? _delay_us(60); // Ensure we wait out the rest of the read slot. @@ -121,13 +132,18 @@ static uint8_t oneWireReadBit() return bit; } -static uint8_t oneWireRead() +uint8_t oneWireRead() { uint8_t result = 0; + uint8_t mask = 1; // LSB first, active low - for (int i = 0; i < 8; ++ i) + for (uint8_t i = 0; i < 8; ++i) { - result |= oneWireReadBit() << i; + if (oneWireReadBit()) + { + result |= mask; + } + mask <<= 1; } return result; } diff --git a/chickenFan.hex b/chickenFan.hex index 02c9f9d..b36c262 100644 --- a/chickenFan.hex +++ b/chickenFan.hex @@ -1,20 +1,19 @@ :100000000EC013C012C011C010C00FC00EC00DC072 :100010000CC00BC00AC009C008C007C006C011242C -:100020001FBECFEDCDBF3CD07DC0EACFB89AC098FF -:1000300080EA8A95F1F7B8980000B099FECF80EA7F -:100040008A95F1F7089520E030E090E0B89AC098E2 -:10005000AC01022E02C0559547950A94E2F740FF85 -:1000600005C0B89844E14A95F1F704C044E14A95C7 -:10007000F1F7B89800002F5F3F4F2830310531F776 -:100080000895B89AC0980000B898000000000000D9 -:100090000000000086B394E19A95F1F7817008950D -:1000A000BC9AC498C3DF8CECCEDF84E4CCDFE9DFFC -:1000B0008823E9F3BBDF8CECC6DF8EEBC4DFC0E046 -:1000C000D0E010E0DEDF0C2E01C0880F0A94EAF7C2 -:1000D000182B2196C830D105A9F7C0E0D0E000E088 -:1000E000D0DF0C2E01C0880F0A94EAF7082B219666 -:1000F000C830D105A9F7802F8295807F12951F7097 -:10010000812B893114F0C49A03C0883109F0C49856 -:100110008FE79AE126E0815090402040E1F700C04F -:080120000000C0CFF894FFCFEE +:100020001FBECFEDCDBF4CD073C0EACFB89A80EAE7 +:100030008A95F1F7B8980000B099FECF80EA8A95CA +:10004000F1F7089520E030E090E0AC01022E02C00C +:10005000559547950A94E2F7B89A40FF05C0B898BD +:1000600044E14A95F1F706C044E14A95F1F7B898A2 +:1000700000C000C000002F5F3F4F2830310529F736 +:100080000895B89AB8980000000000000000000031 +:10009000000086B394E19A95F1F7817008951F935B +:1000A000CF93DF93D8E011E0C0E0EBDF8111C12BEB +:1000B000110FD150D1F78C2FDF91CF911F9108955F +:1000C000BC9AC498B3DF8CECBDDF84E4BBDFD9DF1E +:1000D0008823E9F3ABDF8CECB5DF8EEBB3DFDFDF3A +:1000E000C82FDDDF8295807FC295CF708C2B893140 +:1000F00014F0C49A03C0883109F0C4982FE78AE14C +:1001000096E0215080409040E1F700C00000DACF37 +:04011000F894FFCF91 :00000001FF