mirror of
https://github.com/aster94/logic-analyzer.git
synced 2026-05-01 15:10:43 +03:00
Update arduino_LA.ino
This commit is contained in:
@@ -5,41 +5,74 @@
|
||||
* Author : Vincenzo
|
||||
*/
|
||||
|
||||
|
||||
#define F_CPU 16000000UL
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#define samples 200
|
||||
#define prescaler 0x02
|
||||
volatile uint16_t timer1_overflow_count;
|
||||
|
||||
uint8_t initial, state, old_state;
|
||||
uint8_t pinChanged[samples];
|
||||
uint32_t timer[samples];
|
||||
uint16_t event = 0;
|
||||
|
||||
volatile uint16_t overflow_count = 0;
|
||||
volatile uint32_t total_count = 0;
|
||||
|
||||
void init_board() {
|
||||
|
||||
TCCR2B = TCCR2B & 0b11111000 | 0x02;
|
||||
TIMSK2 |= (1 << TIMSK2); // enable Timer2 overflow interrupt
|
||||
TCCR2A &= 0b11111100; // timer2 working parameters
|
||||
TCCR2B &= 0b11110111;
|
||||
|
||||
PORTC = (0 << 0); DDRC |= (1 << 0); // led A0
|
||||
DDRB |= 0x00; // pin 8-13 input
|
||||
PORTB |= 0x3F; // pull-up
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
overflow_count = 0;
|
||||
reset_timer1();
|
||||
event = 0;
|
||||
TCNT2 = 0;
|
||||
|
||||
PORTC = (1 << 0);
|
||||
initial = PINB;
|
||||
@@ -64,32 +97,11 @@ void sendData() {
|
||||
}
|
||||
|
||||
|
||||
//timer:
|
||||
uint32_t get_time() {
|
||||
|
||||
cli();
|
||||
uint8_t tcnt2_save = TCNT2;
|
||||
uint8_t flag_save = TOV2; //timer2 overflow flag
|
||||
|
||||
if (flag_save) {
|
||||
tcnt2_save = TCNT2;
|
||||
overflow_count++; //manual increment of the overflow counter
|
||||
TIFR2 = (0 << TOV2); //reset overflow flag to prevent execution of the ISR
|
||||
}
|
||||
|
||||
total_count = (overflow_count * 256 + tcnt2_save) / 2;
|
||||
sei();
|
||||
return total_count;
|
||||
}
|
||||
|
||||
ISR(TIMER2_OVF_vect) {
|
||||
overflow_count++;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
Serial.begin(9600);
|
||||
|
||||
init_board();
|
||||
init_timer();
|
||||
|
||||
start();
|
||||
|
||||
@@ -99,7 +111,7 @@ int main(void) {
|
||||
state = PINB;
|
||||
|
||||
if (old_state != state) {
|
||||
timer[event] = get_time();
|
||||
timer[event] = myMicros();
|
||||
pinChanged[event] = state ^ old_state;
|
||||
event++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user