diff options
Diffstat (limited to 'table-fan/table-fan.c')
-rw-r--r-- | table-fan/table-fan.c | 76 |
1 files changed, 5 insertions, 71 deletions
diff --git a/table-fan/table-fan.c b/table-fan/table-fan.c index 6580b26..e3c3842 100644 --- a/table-fan/table-fan.c +++ b/table-fan/table-fan.c @@ -2,7 +2,7 @@ * spreadspace avr utils * * - * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org> + * Copyright (C) 2021 Christian Pointner <equinox@spreadspace.org> * * This file is part of spreadspace avr utils. * @@ -21,7 +21,6 @@ */ -#include <avr/io.h> #include <avr/wdt.h> #include <avr/interrupt.h> #include <avr/power.h> @@ -31,80 +30,16 @@ #include "led.h" #include "usbio.h" -#define PWM_VAL OCR4B -#define PWM_MAX 160 - -void pwm_init(void) -{ - DDRB |= (1<<PB6); - TCCR4B = 0; // make sure timer is stopped - - TCCR4A = (1<<COM4B1) | (1<<PWM4B); - TCCR4D = (1<<WGM40); - TCNT4 = 0; - OCR4C = 160; - - OCR4B = 80; // set duty cycle to 50:50 - TCCR4B = (1<<CS41); // start timer -} - -inline void pwm_set(uint8_t val) -{ - PWM_VAL = val; -} - -inline void pwm_inc(void) -{ - if(PWM_VAL < PWM_MAX) - PWM_VAL++; -} - -inline void pwm_dec(void) -{ - if(PWM_VAL > 0) - 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; - - uint16_t rpm = 1875000 / diff; - printf("\rspeed: %6d rpm", rpm); -} - +#include "fan.h" void handle_cmd(uint8_t cmd) { switch(cmd) { - case '+': pwm_inc(); break; // printf("pwm = %d\r\n", PWM_VAL); break; - case '-': pwm_dec(); break; // printf("pwm = %d\r\n", PWM_VAL); break; + case '+': fan_speed_inc();/* printf("pwm = %d\r\n", fan_speed_get()); */ break; + case '-': fan_speed_dec();/* printf("pwm = %d\r\n", fan_speed_get()); */ break; case 'r': reset2bootloader(); break; default: return; // printf("error\r\n"); return; } - } int main(void) @@ -117,8 +52,7 @@ int main(void) led_init(); usbio_init(); - pwm_init(); - tacho_init(); + fan_init(); sei(); // rotary encoder |