Update arduino_LA.ino

This commit is contained in:
aster94
2016-12-12 20:00:46 +01:00
committed by GitHub
parent b514046a15
commit 467c0d883e

View File

@@ -1,16 +1,26 @@
/*
* LA.cpp
*
* Created: 11/12/2016 19.35.51
* Author : Vincenzo
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define samples 200 #define samples 200
uint8_t state, old_state, initial; uint8_t initial, state, old_state;
uint8_t changed[samples]; uint8_t pinChanged[samples];
uint32_t timer[samples]; uint32_t timer[samples];
uint16_t event = 0; uint16_t event = 0;
volatile uint64_t overflow_count = 0; volatile uint16_t overflow_count = 0;
volatile uint64_t total_count = 0; volatile uint32_t total_count = 0;
void init_board() {
int main(void) {
Serial.begin(9600);
TCCR2B = TCCR2B & 0b11111000 | 0x02; TCCR2B = TCCR2B & 0b11111000 | 0x02;
TIMSK2 |= (1 << TIMSK2); // enable Timer2 overflow interrupt TIMSK2 |= (1 << TIMSK2); // enable Timer2 overflow interrupt
@@ -21,38 +31,20 @@ int main(void) {
DDRB |= 0x00; // pin 8-13 input DDRB |= 0x00; // pin 8-13 input
PORTB |= 0x3F; // pull-up PORTB |= 0x3F; // pull-up
start(); }
while (1) {
old_state = state;
state = PINB;
if (old_state != state) {
timer[event] = get_time();
changed[event] = old_state ^ state;
event++;
if (event == samples) {
sendData();
while (Serial.read() != 'G') ; //wait for the "go"
start();
}
}
}
}
void start() { void start() {
_delay_ms(1000); _delay_ms(1000);
TCNT2 = 0;
overflow_count = 0; overflow_count = 0;
event = 0; event = 0;
TCNT2 = 0;
PORTC = (1 << 0); PORTC = (1 << 0);
initial = PINB; initial = PINB;
state = initial; state = initial;
//in futuro potrei fare in modo che time[0] è impostato qui
} }
@@ -60,13 +52,13 @@ void sendData() {
PORTC = (0 << 0); //turn off led PORTC = (0 << 0); //turn off led
//initial data //initial data
Serial.print("S"); Serial.println(""); Serial.println("S");
Serial.print(initial); Serial.print(":"); Serial.print(initial); Serial.print(":");
Serial.println(samples); Serial.println(samples);
//data //data
for (int i = 0; i < samples; i++) { for (int i = 0; i < samples; i++) {
Serial.print(changed[i]); Serial.print(":"); Serial.print(pinChanged[i]); Serial.print(":");
Serial.println(timer[i]); Serial.println(timer[i]);
} }
} }
@@ -74,14 +66,15 @@ void sendData() {
//timer: //timer:
uint32_t get_time() { uint32_t get_time() {
cli(); cli();
uint8_t tcnt2_save = TCNT2; uint8_t tcnt2_save = TCNT2;
boolean flag_save = TIMSK2 & (1 << TIFR2); //timer2 overflow flag uint8_t flag_save = TOV2; //timer2 overflow flag
if (flag_save) { if (flag_save) {
tcnt2_save = TCNT2; tcnt2_save = TCNT2;
overflow_count++; //manual increment of the overflow counter overflow_count++; //manual increment of the overflow counter
TIFR2 = (1 << TIFR2); //reset overflow flag to prevent execution of the ISR TIFR2 = (0 << TOV2); //reset overflow flag to prevent execution of the ISR
} }
total_count = (overflow_count * 256 + tcnt2_save) / 2; total_count = (overflow_count * 256 + tcnt2_save) / 2;
@@ -92,3 +85,29 @@ uint32_t get_time() {
ISR(TIMER2_OVF_vect) { ISR(TIMER2_OVF_vect) {
overflow_count++; overflow_count++;
} }
int main(void) {
Serial.begin(9600);
init_board();
start();
while (1) {
old_state = state;
state = PINB;
if (old_state != state) {
timer[event] = get_time();
pinChanged[event] = state ^ old_state;
event++;
if (event == samples) {
sendData();
while (Serial.read() != 'G') ; //wait for the "go"
start();
}
}
}
}