-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
179 lines (164 loc) · 3.59 KB
/
main.c
File metadata and controls
179 lines (164 loc) · 3.59 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef F_CPU
#define F_CPU 16000000
#endif
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include "uart.h"
#define MIN_RESET_LENGTH 300
#define MAX_WAITING_FOR_RISE 1000
#define RISE_STEP 5
#define S_IDLE 0
#define S_RSET 1
#define S_PRSN 2
#define S_RCMD 3
#define S_DIAL 4
#define S_RSND 5
#define SERIAL_LENGTH 8
uint8_t s = S_IDLE;
uint16_t reset_time_cnt;
uint8_t tick_idx;
uint8_t recieved_bit_index;
uint8_t current_byte;
uint8_t response_bit_idx;
uint8_t response_byte_idx;
uint8_t serial[SERIAL_LENGTH];
void setup()
{
DDRC |= (1 << PC0);
DDRD = (1 << PD4);
PORTD &= ~(1 << PD4);
UART_setup();
// int setup
EICRA |= (1 << ISC01); // trigger on falling edge
EIMSK |= (1 << INT0); // enable int on INT0 pin
}
ISR(INT0_vect)
{
if (s == S_DIAL)
{
PORTD |= (1 << PD4);
s = S_RSND;
return;
}
if (s == S_IDLE)
{
s = S_RSET;
}
}
void main(void)
{
setup();
// read the serial and transmit to UART
eeprom_read_block((void *)&serial, (const void *)0, sizeof(serial));
for (uint8_t i = 0; i < SERIAL_LENGTH; i++)
{
UART_send_int(serial[i]);
}
UART_send_byte('\r');
UART_send_byte('\n');
// init completed signal
PORTC |= (1 << PC0);
_delay_ms(100);
PORTC &= ~(1 << PC0);
sei();
while (1)
{
if (s == S_RSET)
{
if (reset_time_cnt > MAX_WAITING_FOR_RISE)
{
s = S_IDLE;
reset_time_cnt = 0;
continue;
}
if (PIND & (1 << PD2))
{
if (reset_time_cnt < MIN_RESET_LENGTH)
{
s = S_IDLE;
reset_time_cnt = 0;
continue;
}
_delay_us(15);
PORTD |= (1 << PD4);
_delay_us(60);
PORTD &= ~(1 << PD4);
_delay_us(10);
s = S_RCMD;
reset_time_cnt = 0;
continue;
}
_delay_us(RISE_STEP);
reset_time_cnt += RISE_STEP;
}
if (s == S_RCMD)
{
while (PIND & (1 << PD2))
;
_delay_us(40);
if (PIND & (1 << PD2))
{
recieved_bit_index += 1 << tick_idx;
}
else
{
while (!(PIND & (1 << PD2)))
;
}
tick_idx++;
if (tick_idx > 7)
{
tick_idx = 0;
if (recieved_bit_index == 0x33)
{
current_byte = serial[response_byte_idx];
s = S_DIAL;
}
else
{
s = S_IDLE;
}
recieved_bit_index = 0;
}
continue;
}
if (s == S_RSND)
{
if (current_byte % 2)
{
_delay_us(2);
PORTD &= ~(1 << PD4);
}
else
{
_delay_us(40);
PORTD &= ~(1 << PD4);
}
s = S_DIAL;
current_byte = current_byte >> 1;
response_bit_idx++;
if (response_bit_idx > 7)
{
response_bit_idx = 0;
response_byte_idx++;
if (response_byte_idx >= SERIAL_LENGTH)
{
s = S_IDLE;
response_byte_idx = 0;
// respond completed signal
PORTC |= (1 << PC0);
_delay_ms(1000);
PORTC &= ~(1 << PC0);
continue;
}
else
{
current_byte = serial[response_byte_idx];
continue;
}
}
}
}
}