You are on page 1of 2

//Fclk=1000000

//PB3 =Pin 2 INT for period count


//PB2= pin 7 TimerCounterClockSource T0/SLC
#include <avr/io.h>
#include <avr/interrupt.h>
#include <TinyWireM.h>
#include <USI_TWI_Master.h>
#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
LiquidCrystal_I2C lcd(0x20, 16, 2);
float F=F;
int N=0;
float T= T;
int vlag =1;
int EN=1; // PB1 pin 6 opent ANDpoort OSC.
int IN=2; //PB2 pin als input voor Teller T0
int RE= TIFR & 1<<TOV0; //RE =rising edge on T0 INT0 pin 2=PB7
int OVFL1 = TIFR & 1<<TOV1;//Overflow1 TMR1
void setup() {
pinMode(EN, OUTPUT);
TCNT0=0;//normal port operation
TCCR0A =0x00 ;
//TIMSK= 0x04; // enable TMR1 overflow interrupt
sei(); // enable global interrupts
//TCCR1A = 0; // set entire TCCR1A register to 0
//TCCR1 = 0;
TCNT1=5;
TCCR1 =0x04 ;//PS1=8
TinyWireM.begin();
DDRB |= (1 << PB2);//PB2 out=SCL
lcd.init();
lcd.clear();// initialize the lcd
lcd.setCursor(0,0 );//lijn 1
lcd.print("frequency");}
//fr tc=1us tclT1=PS1x1usec=8usec T1 telt tot 250 T1max=250
;dT1=tcl*T1max=8*250=2Msec
void loop (){
lcd.setCursor(0,1);
TIMSK=0x02;//Enable Overflow Interrupt Enable timer0
sei();
DDRB |= (0 << PB2);//PB2=input=T0 start teller
TCCR0B =0x07 ; // EXT clock source rising edge on T0 of INT0 pin 2=PB7
vlag=1;N=0;
digitalWrite(EN, HIGH);// Enable EN 0p PB1 Pin 1 (op AND) opening osc. op Pin 7=
PB2
TCNT0=0;
//was 5 correctie=5
pinMode(IN, INPUT_PULLUP);// PB2 als input voor teller T0
while (RE & vlag) {vlag=0;}//Rising edge on T0
// wait for the second overflow event rising edge TOV0=1
TIFR &= ~(1 << TOV0);// reset INT. TIFR TOV0=0
// tot hier OK
//Start Tijdtelling tellen CNT1//TMR1 is periodeteller tussen rising edges na2de
puls
TCNT1=5;
while (vlag==0 & OVFL1 & RE !=1)// wait for OVFL1 CNT1 na 2Msec(8x250Us)
{ N++;TCNT1=5;OVFL1=0; }
// while loop stopt als RE=1 bij 2de impuls
DDRB |= (1 << PB2);//PB2 out=SCL
digitalWrite(EN, LOW);// Geen Osc. op Pin 7=PB2
// display F
T=N*0.002+TCNT1*0.000008;// + inhoud CNT1
F=1/T;
DDRB |= (1 << PB2);//PB2 out=SCL
//lcd.print(N );
N=0; }

You might also like