summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2021-09-13 19:58:57 +0200
committerChristian Pointner <equinox@spreadspace.org>2021-09-13 19:58:57 +0200
commit7e4f02dd4e72e97b8f5335876142b5bd6179cee1 (patch)
tree1de7b3fb766a3e5b3a079d1c9e5b1435f0134325
parentmove fan specific code seperate file (diff)
add average for fan tacho
-rw-r--r--table-fan/fan.c45
-rw-r--r--table-fan/fan.h2
-rw-r--r--table-fan/table-fan.c2
3 files changed, 32 insertions, 17 deletions
diff --git a/table-fan/fan.c b/table-fan/fan.c
index daa33f4..a843924 100644
--- a/table-fan/fan.c
+++ b/table-fan/fan.c
@@ -54,6 +54,35 @@ void fan_init(void)
TCCR4B = (1<<CS41); // start timer
}
+#define TACHO_DATA_LEN 8
+static uint16_t tacho_data[TACHO_DATA_LEN];
+static uint8_t tacho_data_idx = 0;
+static uint16_t tacho_last_ts = 0;
+
+ISR(TIMER1_CAPT_vect)
+{
+ uint16_t current = ICR1;
+ if(current > tacho_last_ts) {
+ tacho_data[tacho_data_idx] = current - tacho_last_ts;
+ } else {
+ tacho_data[tacho_data_idx] = (UINT16_MAX - tacho_last_ts) + current;
+ }
+ tacho_last_ts = current;
+ tacho_data_idx = (tacho_data_idx >= (TACHO_DATA_LEN-1)) ? 0 : tacho_data_idx+1;
+}
+
+uint16_t fan_get_rpm(void)
+{
+ uint32_t sum = 0;
+ cli();
+ for(uint8_t i = 0; i < TACHO_DATA_LEN; i++) {
+ sum += tacho_data[i];
+ }
+ sei();
+
+ return (1875000 / (sum / TACHO_DATA_LEN));
+}
+
void fan_speed_set(uint8_t val)
{
OCR4B = (val > PWM_VAL_MAX) ? PWM_VAL_MAX : val;
@@ -75,19 +104,3 @@ void fan_speed_dec(void)
if(OCR4B > 0)
OCR4B--;
}
-
-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);
-}
diff --git a/table-fan/fan.h b/table-fan/fan.h
index 125d7aa..111c947 100644
--- a/table-fan/fan.h
+++ b/table-fan/fan.h
@@ -25,6 +25,8 @@
void fan_init(void);
+uint16_t fan_get_rpm(void);
+
void fan_speed_set(uint8_t val);
uint8_t fan_speed_get(void);
void fan_speed_inc(void);
diff --git a/table-fan/table-fan.c b/table-fan/table-fan.c
index e3c3842..1f0cf4f 100644
--- a/table-fan/table-fan.c
+++ b/table-fan/table-fan.c
@@ -38,8 +38,8 @@ void handle_cmd(uint8_t cmd)
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;
}
+ printf("\rspeed: %5d rpm", fan_get_rpm());
}
int main(void)