forked from aster94/logic-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMEGA.ino
202 lines (180 loc) · 4.71 KB
/
MEGA.ino
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
/*
* LA.cpp
*
* Created: 11/12/2016 19.35.51
* Author : Vincenzo
* Modificaciones agregadas para funcionar con ArduinoMega2560 por Enmanuel Sancho Quintanilla
* La unidad minima en tiempo para este sistema es de 8 micro segundos lo que idealmente permitiria
* observar clocks con periodos de 62 kHz sin embargo para poder apreciar las señales logicas con suficiente
* resolucion se recomienda no superar los 30 kHz en el clock del sistema.
*/
#define baudrate 115200 //check if it is the same in processing
#define samples 10
#define pin_used
#define timezerooffset 125 //microsegundos
#define PULLUP true //Si queremos entradas con PULLUP lo dejamos activado(true), si queremos dejarlas al "aire" (false), en caso de desactivarlo deberemos aterrizar todos los pines que no utilizemos.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define prescaler 0x02
volatile uint16_t timer1_overflow_count;
uint8_t initial1, initial2, initial3, state1, state2, state3, old_state1, old_state2, old_state3;
uint8_t pinChanged1[samples];
uint8_t pinChanged2[samples];
uint8_t pinChanged3[samples];
uint32_t timer[samples];
uint32_t timefix;
uint16_t event = 0;
uint8_t cambio = 0;
void init_board()
{
// PORTC = (0 << 0); DDRC |= (1 << 0); // led A0
DDRB = 0x00;
DDRC = 0x00;
DDRL = 0x00;
if (PULLUP)
{
PORTA = B11111111; // pull-up
PORTC = B11111111; // Activamos el pull-up para que de no conectarse nada a puerto lea un uno siempre
PORTL = B11111111;
}
else
{
PORTA = B00000000;
PORTC = B00000000;
PORTL = B00000000;
}
}
void init_timer()
{
//clear
TCCR1A = 0b00000000;
TCCR1B = 0b00000000;
TIMSK1 = 0b00000000;
//settings
TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //normal port operation
TCCR1A |= (0 << WGM11) | (0 << WGM10); //normal operation
TCCR1B |= (0 << WGM13) | (0 << WGM12); //normal operation
TCCR1B |= prescaler; //(0 << CS12) | (0 << CS11) | (1 << CS10); //clock prescaler
sei(); //enable interrupts
TIMSK1 |= (1 << TOIE1); // enable overflow interrupt
}
ISR(TIMER1_OVF_vect)
{
timer1_overflow_count++;
}
void reset_timer1()
{
TCNT1 = 0;
timer1_overflow_count = 0;
}
uint32_t myMicros()
{
cli();
if (TIFR1 & (1 << TOV1))
{
TIFR1 = (0 << TOV1);
timer1_overflow_count++;
}
uint32_t total_time = (65536 * timer1_overflow_count + TCNT1) / 2;
sei();
return total_time;
}
void start()
{
_delay_ms(1000);
// Serial.print("hi");
reset_timer1();
event = 0;
//PORTC = (1 << 0);
initial1 = PINA;
initial2 = PINL;
initial3 = PINC;
state1 = initial1;
state2 = initial2;
state3 = initial3;
for (int i = 0; i < samples; i++)
{
pinChanged1[i] = 0;
pinChanged2[i] = 0;
pinChanged3[i] = 0;
//Serial.print(pinChanged1[i]); Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.println(pinChanged3[i]); //debug
}
}
void sendData()
{
//PORTC = (0 << 0); //turn off led
//initial data
Serial.println("S");
Serial.print(initial1);
Serial.print(',');
Serial.print(initial2);
Serial.print(',');
Serial.print(initial3);
Serial.print(":");
Serial.println(samples);
timefix = -timer[0] + timezerooffset;
for (int i = 0; i < samples; i++)
{
timer[i] = timer[i] + timefix;
}
//data
for (int i = 0; i < samples; i++)
{
Serial.print(pinChanged1[i]);
Serial.print(',');
Serial.print(pinChanged2[i]);
Serial.print(',');
Serial.print(pinChanged3[i]);
Serial.print(":");
Serial.println(timer[i]);
}
}
int main(void)
{
Serial.begin(baudrate);
init_board();
init_timer();
start();
while (1)
{
cambio = 0;
old_state1 = state1;
old_state2 = state2;
old_state3 = state3;
state1 = PINA;
state2 = PINL;
state3 = PINC;
//Serial.print(state1);Serial.print(" , ");Serial.print(state2);Serial.print(" , ");Serial.print(state3);Serial.print(" : ");Serial.println(event);
if (old_state1 != state1)
{
pinChanged1[event] = state1 ^ old_state1;
cambio = 1;
}
if (old_state2 != state2)
{
pinChanged2[event] = state2 ^ old_state2;
cambio = 1;
}
if (old_state3 != state3)
{
pinChanged3[event] = state3 ^ old_state3;
cambio = 1;
}
if (cambio == 1)
{
timer[event] = myMicros();
event++;
}
if (event == samples)
{
sendData();
while (Serial.read() != 'G')
{
;
} //wait for the "go"
start();
}
}
}