summaryrefslogtreecommitdiff
path: root/table-fan/table-fan.c
diff options
context:
space:
mode:
Diffstat (limited to 'table-fan/table-fan.c')
-rw-r--r--table-fan/table-fan.c36
1 files changed, 32 insertions, 4 deletions
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);