-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_sensor.c
123 lines (99 loc) · 2.28 KB
/
m_sensor.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "common.h"
#include "m_sensor.h"
#include "device.h"
#include "task.h"
ISR (INT0_vect)
{
task_sm.piezo0_count = 10;
m_sensor_int_disable(0);
m_sensor.m0_count += M_0_ADD;
if (m_sensor.m0_count > M_0_THRESHOLD)
{
m_sensor.m0_count = M_0_THRESHOLD;
m_sensor.m0_alart = 1;
}
}
ISR (INT1_vect)
{
// task_sm.piezo0_count = 10;
m_sensor_int_disable(1);
m_sensor.m1_count += M_1_ADD;
if (m_sensor.m1_count > M_1_THRESHOLD)
{
m_sensor.m1_count = M_1_THRESHOLD;
m_sensor.m1_alart = 1;
}
}
void m_sensor_init(void)
{
MCUCR |= _BV(ISC10) | _BV(ISC00); /* INT0/ INT1 for logical change */
m_sensor_int_disable(0);
m_sensor_int_disable(1);
GIFR |= _BV(INT0) | _BV(INT1); /* clear flag */
m_sensor.m0_count = 0;
m_sensor.m1_count = 0;
m_sensor.m0_alart = 0;
m_sensor.m1_alart = 0;
}
void m_sensor_int_enable(unsigned char ch)
{
/* enable INT0/ INT1 */
if (ch == 0)
{
m_sensor._m0_delay = M_0_COUNT_TOP;
GIFR |= _BV(INT0);
GICR |= _BV(INT0);
}
else if (ch == 1)
{
m_sensor._m1_delay = M_1_COUNT_TOP;
GIFR |= _BV(INT1);
GICR |= _BV(INT1);
}
}
void m_sensor_int_disable(unsigned char ch)
{
if (ch == 0)
{
GICR &= ~( _BV(INT0) );
GIFR |= _BV(INT0);
m_sensor._m0_delay = M_0_COUNT_TOP;
}
else if (ch == 1)
{
GICR &= ~( _BV(INT1) );
GIFR |= _BV(INT1);
m_sensor._m1_delay = M_1_COUNT_TOP;
}
}
inline void m_sensor_timer_tick(void)
{
if (m_sensor._m0_delay > 0)
{
m_sensor._m0_delay--;
if (m_sensor._m0_delay == 0)
{
m_sensor_int_enable(0);
m_sensor.m0_count -= M_0_DEC;
if (m_sensor.m0_count <= 0)
{
m_sensor.m0_count = 0;
m_sensor.m0_alart = 0;
}
}
}
if (m_sensor._m1_delay > 0)
{
m_sensor._m1_delay--;
if (m_sensor._m1_delay == 0)
{
m_sensor_int_enable(1);
m_sensor.m1_count -= M_1_DEC;
if (m_sensor.m1_count <= 0)
{
m_sensor.m1_count = 0;
m_sensor.m1_alart = 0;
}
}
}
}