From bb74a1a4449a4255c3edb96e78b9feed59fc3ac1 Mon Sep 17 00:00:00 2001
From: Pat Thoyts <patthoyts@users.sourceforge.net>
Date: Fri, 1 Jan 2016 22:09:58 +0000
Subject: [PATCH] Fan controller firmware rev 1

---
 .gitignore    |  5 ++++
 Makefile      | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 README.md     | 10 ++++++++
 fan_control.c | 48 +++++++++++++++++++++++++++++++++++
 4 files changed, 133 insertions(+)
 create mode 100644 .gitignore
 create mode 100755 Makefile
 create mode 100644 README.md
 create mode 100644 fan_control.c

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2859f3f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+*~
+*.o
+*.elf
+*.hex
+
diff --git a/Makefile b/Makefile
new file mode 100755
index 0000000..a37ca5d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,70 @@
+# -*- Makefile -*-
+
+PROJECT := fan_control
+DEVICE  := attiny85
+F_CPU   := 8000000UL
+INC     := -I.
+AVRDUDE := avrdude -c usbasp -p $(DEVICE)
+
+CSRCS    = $(PROJECT).c
+OBJS     = $(CSRCS:.c=.o)
+
+CC      := avr-gcc
+LD      := avr-gcc
+OBJCOPY := avr-objcopy
+RM      := del >NUL
+CFLAGS  :=-Wall -Wmissing-prototypes -Wcast-align -Wshadow \
+          -std=gnu99 -fshort-enums -pedantic-errors -Os -mcall-prologues \
+          -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
+LDFLAGS := -Wall -mmcu=$(DEVICE)# -Wl,-v
+LIBS    :=
+
+V := @
+Q := $(V:1=)
+QUIET_CC      = $(Q:@=@echo   CC      $@ &)$(CC)
+QUIET_LD      = $(Q:@=@echo   LD      $@ &)$(LD)
+QUIET_OBJCOPY = $(Q:@=@echo   OBJCOPY $@ &)$(OBJCOPY)
+QUIET_AVRDUDE = $(Q:@=@echo   AVRDUDE $@ &)$(AVRDUDE)
+
+all: $(PROJECT).hex
+
+%.hex: %.elf
+	$(QUIET_OBJCOPY) -j .text -j .data -O ihex $< $@
+
+%.elf: $(OBJS)
+	$(QUIET_LD) $(LDFLAGS) $^ $(LIBS) -o $@
+
+%.o: %.c
+	$(QUIET_CC) $(CFLAGS) $(INC) -c $<
+
+flash: $(PROJECT).hex
+	$(QUIET_AVRDUDE) -U flash:w:$<:i
+
+clean:
+	-@$(RM) $(addprefix $(PROJECT), .elf .hex)
+
+.PHONY: clean
+.SECONDARY: $(addsuffix .elf, $(PROJECT)) $(OBJS)
+
+# Fuse high byte:
+# 0xdd = 1 1 0 1   1 1 0 1
+#        ^ ^ ^ ^   ^ \-+-/
+#        | | | |   |   +------ BODLEVEL 2..0 (brownout trigger level -> 2.7V)
+#        | | | |   +---------- EESAVE (preserve EEPROM on Chip Erase -> not preserved)
+#        | | | +-------------- WDTON (watchdog timer always on -> disable)
+#        | | +---------------- SPIEN (enable serial programming -> enabled)
+#        | +------------------ DWEN (debug wire enable)
+#        +-------------------- RSTDISBL (disable external reset -> enabled)
+#
+# Fuse low byte:
+# 0xe1 = 1 1 1 0   0 0 0 1
+#        ^ ^ \+/   \--+--/
+#        | |  |       +------- CKSEL 3..0 (clock selection -> HF PLL)
+#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)
+#        | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
+#        +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
+# For 16.5MHz internal: -U hfuse:w:0xdd:m -U lfuse:w:0xe1:m
+# For 12MHz crystal: -U hfuse:w:0xdd:m -U lfuse:w:0b11111111:m
+# For 8MHz internal: -U hfuse:w:0xd7:m -U lfuse:w:0xe2:m
+fuse:
+	$(AVRDUDE) -U hfuse:w:0xd7:m -U lfuse:w:0xe2:m
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bd317e4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,10 @@
+Fan Control Board
+
+This is a board to control a small 24V, 33mA computer cooling fan.
+The requirement is to toggle the fan every 15 minutes.
+
+The curcuit is an ATtiny85 with 78L05 to drop the 12V input to regulated
+5V and the microcontroller counts seconds then asserts the gate of a
+2N7000 MOSFET to turn the fan on or off. MOSFET is on PB3 and an LED is on
+PB2. The other pins are unused.
+
diff --git a/fan_control.c b/fan_control.c
new file mode 100644
index 0000000..0a9d301
--- /dev/null
+++ b/fan_control.c
@@ -0,0 +1,48 @@
+/* fan_control - Copyright (c) 2016 Pat Thoyts <patthoyts@users.sourceforge.net>
+ *
+ * ATtiny85 with LED on pin PB2 and a 2N7000 MOSFET on PB3 which controls
+ * a computer fan run off 12V supply. Toggle the fan every 15 minutes.
+ * Start with the fan ON
+ *
+ */
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/power.h>
+#include <avr/sleep.h>
+#include <avr/wdt.h>
+#include <util/delay.h>
+
+#define LED_PIN PB2
+#define FAN_PIN PB3
+
+int
+main(void)
+{
+    unsigned long seconds = 0;
+    uint8_t counter = 0;
+
+    wdt_enable(WDTO_1S);
+
+    power_adc_disable();
+
+    /* set all unused pins high-z, led and fan pins output and low */
+    DDRB = _BV(DDB2) | _BV(DDB3);
+    PORTB = ~(_BV(PB2));
+
+    while (1)
+    {
+        _delay_ms(250);
+        wdt_reset();
+        PORTB ^= _BV(PB2); /* toggle led */
+        if (++counter == 4)
+        {
+            if (++seconds == (60 * 15)) /* every 15 mins */
+            {
+                PORTB ^= _BV(PB3); /* toggle fan */
+                seconds = 0;
+            }
+            counter = 0;
+        }
+    }
+}
-- 
2.23.0