//////////////////////////////////////////////////////
//ATtiny85 Activity_Counter &Frequency_meter programs;
// Time counting more than 255
// Mokhtar Zerdali 2022.
#include<avr/io.h>
#include<util/delay.h>
#include <TinyDebug.h>
#define F_CPU 8000000UL
#define INT_PIN PB2 // INT0 -> PB2 external interrupt pin
#define INT_PIN0 PB1
volatile unsigned long Overflowprb ; // value >256, and volatile between void programs
volatile unsigned long Overflowex ; // value >256, and volatile between void programs
volatile unsigned long Countprb;
volatile unsigned long Countex;
volatile unsigned long Time;
volatile unsigned long Start_Time;
volatile unsigned long Finish_Time;
volatile unsigned long Elapsed_Time;
volatile float Frequency;
volatile int PRB;// pulse rate beat
volatile boolean i;
volatile byte j;
void Global_setup()
{
Debug.begin();
//Debug.println(F("Hello, clock is running"));
DDRB = (1 << PB3) | (1 << PB4);
TCCR0A = 0X00; //TCCR0A to low for normal port operation and mode 0.
TCCR0B = 0X00; //WGM02=0
TCNT0 = 0; //initializing the counter to 0
TIMSK = bit (TOIE0); //|= (1 << TOIE0); //enabling overflow >255 interrupts of timer0
//Externall interrupts
GIMSK |= (1 << INT0); // enable external interrupt
GIMSK |= (1 << PCIE); // enable pin change
PCMSK = bit(PCINT1); // pin PB1 as pin change
sei(); //global interruption
// i = digitalRead(PB0);
}
////////////////////////////////////////////
void Pulse_RB()
{
Overflowprb = 0;
TCCR0B |= (1 << CS00); //No prescaling clk
// Mode sense of MCUCR Register to choose the adequate sense
MCUCR = (1 << ISC01); //3/ FALLING mode sense
//MCUCR = bit(ISC01) | bit(ISC00); //4// RISING mode sense
}
////////////////////////////////////////////
void External_counter()
{
Overflowex = 0;
TCCR0B |= (1 << CS01) | (1 << CS02); //external counting, Clk on Falling edge sense
/////////////////////////////////////Mode sense
MCUCR &= ~(bit(ISC01) | bit(ISC00)); //1/ INT0 on low level, while ISR senses by mode low level
// The low level of INT0 generates an interrupt request
}
///////////////////////////////
ISR (TIMER0_OVF_vect) //Interrupt vector for Timer0 >>>> Counter0
{
i = digitalRead(PB1);
if (i == HIGH)
{
Overflowprb++; // count number of overflow
}
else if (i == LOW)
{
Overflowex++; // count number of overflow
}
} // End of TIMER0_OVF_vect
///////////////////Interruption Routine///////////////////////
ISR(INT0_vect) // Interrupter vector for external inerruption
{
j= digitalRead(PB2);
i = digitalRead(PB1);
//digitalWrite(PB1, i);
if (i == HIGH)
{
Countprb = TCNT0 + (Overflowprb << 8); //n° overflow*256
Time = 0.000125 * Countprb; // 8MHz ; clk : 125 ns.
Start_Time = Time;
Debug.println("Frequency:");
Debug.print(Frequency);
Debug.println("Hz");
Debug.println("###:");
Debug.println("Pulse rate beat:");
Debug.print(PRB);// heart
Debug.println("/min");
Debug.println("###:");
}
else if (i == LOW)
{
Countex = TCNT0 + (Overflowex << 8); //n° overflow*256
Debug.println("Count:");
Debug.print(Countex);
Debug.println("");
Debug.println("###:");
}
}
///////////////end of interruption////////////////////////////////////////////////////
int main()
{
Global_setup();
i = digitalRead(PB1);
if (i == HIGH)
{
Pulse_RB(); // pulse rate beat program
while (1)
{
Countprb = TCNT0 + (Overflowprb << 8); //n° overflow*256
Time = 0.000125 * Countprb; // 8MHz ; clk : 125 ns.
Finish_Time = Time;
Elapsed_Time = Finish_Time - Start_Time;
Frequency = 1000 / float(Elapsed_Time); // on Hz
PRB = 60 * Frequency; //pulse rate beat
//if (Start_Time > 1500)
}
}
else if (i == LOW)
{
External_counter(); // counting program
while (1)
{
}
}
}