/
main.c
212 lines (178 loc) · 3.88 KB
/
main.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* joystik_driver.c
*
* Created: 03.10.2020 15:44:08
* Author : bayre
*/
#define F_CPU 9600000
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <avr/interrupt.h>
#define _BV(bit) (1 << (bit))
#define SetBit(x,y) (x|=(1<<y))
#define ClrBit(x,y) (x&=~(1<<y))
#define ToggleBit(x,y) (x^=(1<<y))
#define FlipBit(x,y) (x^=(1<<y)) // Same as ToggleBit.
#define TestBit(x,y) (x&(1<<y))
uint16_t Rx_coord = 510;
uint16_t Ry_coord = 496;
unsigned char Rkey = 0;
volatile unsigned char TransmitKey = 0;
uint16_t cnt_low = 0;
uint16_t cnt_high = 0;
#define DRV_X PORTB0
#define DRV_Y PORTB1
#define BUTON PINB2
#define FSTFL 0
#define SETBX 1
#define SETBY 2
#define STOPF 3
#define MAGICK_NUMBER 38 //Number of ticks in 1ms
#define DOWNV 25
#define MAXIV 87
#define ADCTR 4
uint16_t readADC(uint8_t adc_num){
ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
uint8_t trys = ADCTR;
uint16_t value = 0;
switch(adc_num){
case 1:{
ADMUX = _BV(MUX0);
break;
}
case 2:{
ADMUX = _BV(MUX1);
break;
}
case 3:{
ADMUX = _BV(MUX1) | _BV(MUX0);
break;
}
}
_delay_us(50);
while(trys){
ADCSRA |= _BV(ADSC);
while ((ADCSRA & _BV(ADIF)) == 0);
ADCSRA |= _BV(ADIF);
value += ADCW;
trys--;
}
ADCSRA &= ~(1<<ADEN);
return value/ADCTR;
}
uint8_t frontChecker = 0;
uint8_t cycleChecker = 0;
uint8_t globalFlags = 0;
uint8_t tickChecker = 0;
uint8_t xTrn = MAGICK_NUMBER + MAGICK_NUMBER/2;
uint8_t yTrn = MAGICK_NUMBER + MAGICK_NUMBER/2;
ISR(TIM0_OVF_vect){
if (TestBit(globalFlags, FSTFL)){
PORTB |= (_BV(DRV_X) | _BV(DRV_Y));
ClrBit(globalFlags, FSTFL);
}
if ((frontChecker >= xTrn) && (!TestBit(globalFlags, SETBX))) {
ClrBit(PORTB, DRV_X);
SetBit(globalFlags, SETBX);
}
if ((frontChecker >= yTrn) && (!TestBit(globalFlags, SETBY))) {
ClrBit(PORTB, DRV_Y);
SetBit(globalFlags, SETBY);
}
frontChecker++;
if (tickChecker>=MAGICK_NUMBER){
tickChecker = 0;
cycleChecker++;
} else {
tickChecker++;
}
if (cycleChecker >= 20) {
cycleChecker = 0;
PORTB |= (_BV(DRV_X) | _BV(DRV_Y));
ClrBit(globalFlags, SETBX);
ClrBit(globalFlags, SETBY);
frontChecker = 0;
tickChecker = 0;
}
if(TransmitKey){
cnt_low=cnt_low+1;
if (cnt_low >= 37500){
cnt_low = 0;
cnt_high=cnt_high+1;
}
}
}
#define OLDTR 8
uint8_t old_tr = 0;
uint8_t tr_mem_x[OLDTR] = {MAGICK_NUMBER + MAGICK_NUMBER/2};
uint8_t tr_mem_y[OLDTR] = {MAGICK_NUMBER + MAGICK_NUMBER/2};
uint8_t tr_ind = 0;
uint16_t tr_sum_x = 0;
uint16_t tr_sum_y = 0;
void addelem(uint8_t *x, uint8_t *y){
tr_sum_x = 0;
tr_sum_y = 0;
if (old_tr>=(OLDTR-1)){
for(tr_ind=0; tr_ind <= (OLDTR - 2); tr_ind++){
tr_mem_x[tr_ind] = tr_mem_x[tr_ind+1];
tr_mem_y[tr_ind] = tr_mem_y[tr_ind+1];
}
}
tr_mem_x[old_tr] = *x;
tr_mem_y[old_tr] = *y;
for(tr_ind=0; tr_ind<=old_tr; tr_ind++){
tr_sum_x += tr_mem_x[tr_ind];
tr_sum_y += tr_mem_y[tr_ind];
}
if (old_tr<(OLDTR-1)){
old_tr++;
}
*x = (uint8_t)(tr_sum_x/OLDTR);
*y = (uint8_t)(tr_sum_y/OLDTR);
}
int main(void)
{
uint8_t xTrn_tmp = 0;
uint8_t yTrn_tmp = 0;
ACSR = _BV(ACD);
TCCR0A = 0;
TCCR0B = _BV(CS00);
TCNT0 = 0;
TIMSK0 = _BV(TOIE0);
DDRB = _BV(DRV_X) | _BV(DRV_Y);
PORTB = 0;
globalFlags |= _BV(FSTFL);
sei();
while (1)
{
//Read X
Rx_coord = readADC(2);
//Read Y
Ry_coord = readADC(3);
//Check button
if ((PINB & _BV(BUTON)) && !Rkey && !TransmitKey){
Rkey = 1;
}
if (!(PINB & _BV(BUTON)) && Rkey){
Rkey = 0;
cnt_low = 0;
cnt_high = 0;
TransmitKey = 1;
}
xTrn_tmp = Rx_coord * (MAXIV-DOWNV) / 1024;
yTrn_tmp = Ry_coord * (MAXIV-DOWNV) / 1024;
if (!TestBit(globalFlags, STOPF)) {
addelem(&xTrn_tmp, &yTrn_tmp);
xTrn = xTrn_tmp + DOWNV;
yTrn = yTrn_tmp + DOWNV;
}
if (TransmitKey){
if (TestBit(globalFlags, STOPF) || (!TestBit(globalFlags, STOPF) && (cnt_high>=5))){
ToggleBit(globalFlags, STOPF);
TransmitKey = 0;
}
}
_delay_ms(2);
}
}