#define HZ 12
#include <Wire.h>
#include <OLED_I2C.h> // http://rcl-radio.ru/wp-content/uploads/2022/01/OLED_I2C.zip
OLED myOLED(SDA, SCL, 8);
extern uint8_t SmallFont[];
char i;
unsigned long times0,times1,t0,t1,tic,f;
int w=1;
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.setClock(50000L);
delay(300);
myOLED.begin();
myOLED.setBrightness(100);
DDRB |= PB5; // D13 ARUINO > OUTPUT
DDRD &= ~(1 << 2); // D2 ARDUINO > INPUT (INT0)
EICRA |= (1<<ISC00); // любое изменение на INT0
PORTD |= (1 << PD5); // подтягивающий резистор на PD5 (вход T1)
EIMSK |= (1<<INT0); // разрешить внешние прерывания INT0
noInterrupts();
// TIMER_1 INPUT T1
TCCR1A = 0;TCCR1B = 0;TCNT1=0;
TCCR1B = (1 << CS12) | (1 << CS11) | (1 << CS10);
TIMSK1 = (1 << TOIE1);
TCKCSR = (1 << F2XEN) | (1 << TC2XS1);
interrupts();
}
void loop(){
if(w==1){
EIMSK &=~ (1<<INT0);
PORTB |=(1 << PB5);
Serial.print("T1 ");Serial.print(t0/(1000.0*HZ),3);Serial.println(" mS");
Serial.print("T2 ");Serial.print(t1/(1000.0*HZ),3);Serial.println(" mS");
Serial.print("T ");Serial.print((t1+t0)/(1000.0*HZ),3);Serial.println(" mS");
Serial.print("F ");Serial.print((1000000.0*HZ)/(t1+t0),3);Serial.println(" Hz");
Serial.println();
//////////////////////
myOLED.clrScr();
myOLED.setFont(SmallFont);
myOLED.print(F("T1"),LEFT,0);myOLED.printNumF(t0/(1000.0*HZ),3, 30, 0);myOLED.print(F("mS"), RIGHT, 0);
myOLED.print(F("T2"),LEFT,10);myOLED.printNumF(t1/(1000.0*HZ),3, 30, 10);myOLED.print(F("mS"), RIGHT, 10);
myOLED.print(F("T "),LEFT,20);myOLED.printNumF((t1+t0)/(1000.0*HZ),3, 30, 20);myOLED.print(F("mS"), RIGHT, 20);
myOLED.print(F("F "),LEFT,30);myOLED.printNumF((1000000.0*HZ)/(t1+t0),3, 30, 30);myOLED.print(F("Hz"), RIGHT, 30);
///////////
myOLED.drawLine(0, 40, 128, 40);
float t_dl = t0;
float t_sk = t1;
float t_pr = t0+t1;
myOLED.drawLine(0, 60, 5, 60);
myOLED.drawLine(5, 60, 5, 45);
myOLED.drawLine(5, 45, 5+(t_dl/t_pr)*120, 45);
myOLED.drawLine(5+(t_dl/t_pr)*120, 60, 5+(t_dl/t_pr)*120, 45);
myOLED.drawLine(5+(t_dl/t_pr)*120, 60, 5+(t_dl/t_pr)*120+(t_sk/t_pr)*120, 60);
///////////
myOLED.update();
PORTB &=~(1 << PB5);
delay(400);
w=0;i=0;f=tic*0xFFFF+TCNT1;times0=f;times1=f;
EICRA|=(1<<ISC01);EICRA|=(1<<ISC00);EIMSK |= (1<<INT0);}
}
ISR(INT0_vect){
if(i>1){i=0;w=1;}
if(i==0){EICRA&=~(1<<ISC01);EICRA|=(1<<ISC00);
f=tic*0xFFFF+TCNT1;
times0=f;t1=f-times1;}
if(i==1){
f=(tic*0xFFFF+TCNT1);
times1=f;t0=f-times0;}
i++;}
ISR(TIMER1_OVF_vect) {tic++;}