-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf800.c
71 lines (61 loc) · 1.69 KB
/
sf800.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
************************************************************
* PiMash -
* Microcontroller component of the raspberry-pi
* based RIMS controller
************************************************************
*/
#include <avr/io.h>
#include <util/atomic.h>
#include <util/delay.h>
#include "sf800.h"
#include "uart.h"
// SF800 k factor is 5600 pulse/liter. To report in liter/min:
// pulses/sec * 1/5600 liter/pulses * 60 sec/min = liter/min
#define K_FACTOR 60.0f / 5600.0f
// timer variables
int flwtimerof_count = 0;
int flwcount =0;
// ********************************************************
void start_flwtimer () {
TCNT0 = 0;
// External clock on falling edge, noise cancelling
TCCR0A = 0;
// Clock on T1 (PD6) on falling edge
TCCR0B |= (1 << CS02) | (1 << CS00);
// No interrupts
TIMSK0 |= (1 << TOIE0);
}
// ********************************************************
void stop_flwtimer () {
TCCR0B &= ~(1 << CS00) & ~(1 << CS01) & ~(1 << CS02);
}
// ********************************************************
ISR (TIMER0_OVF_vect) {
// Capture pulses every 1 second and reset counter 1
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (flwtimerof_count++ == 46) {
flwtimerof_count = 0;
flwcount = TCNT1;
TCNT1 = 0;
}
}
}
// ********************************************************
void init_sf800 () {
PORTD = 0;
DDRD &= ~(1 << PD6);
// External clock on falling edge, noise cancelling
TCCR1A = 0;
// Clock on T1 (PD6) on falling edge
TCCR1B |= (1 << CS11) | (1 << CS12);
TCCR1C = 0;
// No interrupts
TIMSK1 = 0;
}
// ********************************************************
double get_flwrate () {
double flowrate = flwcount;
flowrate *= K_FACTOR;
return flowrate;
}