summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2021-09-12 23:38:19 +0200
committerChristian Pointner <equinox@spreadspace.org>2021-09-12 23:38:19 +0200
commitfa0c389e56f76a631a4616bf36b85ff7ee875c04 (patch)
treed521e746b3d84667225b60161482d8ec99198096
parentbasic PWM works now (diff)
add fan tacho
-rw-r--r--table-fan/Makefile1
-rw-r--r--table-fan/table-fan.c36
2 files changed, 33 insertions, 4 deletions
diff --git a/table-fan/Makefile b/table-fan/Makefile
index c41073f..85f972c 100644
--- a/table-fan/Makefile
+++ b/table-fan/Makefile
@@ -26,6 +26,7 @@ OBJ := $(NAME).o
LIBS := util led lufa-descriptor-usbserial usbio
EXTERNAL_LIBS := lufa
SPREADAVR_PATH := ../contrib/avr-utils
+PRINTF_HAS_FLOAT := 1
RESET_FUNC := $(SPREADAVR_PATH)/tools/reset_lufa_cdc
RESET_PARAM := 'r'
diff --git a/table-fan/table-fan.c b/table-fan/table-fan.c
index 2cf9a32..0a49c91 100644
--- a/table-fan/table-fan.c
+++ b/table-fan/table-fan.c
@@ -65,6 +65,37 @@ inline void pwm_dec(void)
PWM_VAL--;
}
+
+void tacho_init(void)
+{
+ DDRD &= ~(1<<PD4);
+ PORTD |= (1<<PD4);
+ TCCR1B = 0; // make sure timer is stopped
+
+ TCCR1A = 0;
+ TCNT1 = 0;
+ TIMSK1 = (1<<ICIE1);
+
+ TCCR1B = (1<<CS12); // start timer
+}
+
+ISR(TIMER1_CAPT_vect)
+{
+ static uint16_t tacho_last_ts = 0;
+ uint16_t current = ICR1;
+ uint16_t diff = 0;
+ if(current > tacho_last_ts) {
+ diff = current - tacho_last_ts;
+ } else {
+ diff = (UINT16_MAX - tacho_last_ts) + current;
+ }
+ tacho_last_ts = current;
+
+ float rpm = 1875000.0 / (float)diff ;
+ printf("\rtacho: %8.2f rpm", rpm);
+}
+
+
void handle_cmd(uint8_t cmd)
{
switch(cmd) {
@@ -89,12 +120,9 @@ int main(void)
led_init();
usbio_init();
pwm_init();
+ tacho_init();
sei();
- // FAN tacho input (enable Pull-Up)
- DDRD &= ~(1<<PD4);
- PORTD |= (1<<PD4);
-
// rotary encoder
DDRB &= ~((1<<PB4) | (1<<PB5));
PORTB |= (1<<PB4) | (1<<PB5);