attiny13 frequency pulse input

Hi i am using attiny13. I have square pulse input of 5volts and frequency ranging from 3Khz to 5Khz. I want my output high only when i have this input and my output low when i have input with other frequency.
I am unsing pinchange interrupt and timer to find out the pulse width but not able to solve it . can anybody help me out on this and share the code. Its more than a month i am stuck on it.
thnx

main.c (3.46 KB)

It appears that you are not setting TCCR0B (You have it commented out) but if you don't set it, the default value is all zeros which means the timer is OFF. I believe you want ot set to 1 (no pre-scale)

/*
 * struggle.c
 *
 * Created: 29.07.2019 12:49:33
 * Author : GXG0223A
 */ 
#define F_CPU 9600000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
volatile uint8_t timer_ovf = 0;
volatile int count = 0;
volatile int pulses = 0;

#define PRE_SCALE (1)   // make sure TCCR0B is set to match

#define COUNT_MIN (F_CPU/2/PRE_SCALE/5000)  // COUNT_MIN == max frequency = 5kHz
#define COUNT_MAX (F_CPU/2/PRE_SCALE/3000)  // COUNT_MAX == min frequency = 3kHz

ISR (TIM0_OVF_vect) {
  timer_ovf++;   // count timer overflows since reset in rising edge pin change interrupt
}

ISR(PCINT0_vect) {

// here we check the level of our pin of interest
// if it is HIGH, a rising edge interrupt has happened
// if it is LOW, a falling edge interrupt has happened

// PINB – Port B Input Pins Address (page 57)
// check if PB0 (receiver) bit is 1 (HIGH)
  if (PINB & (1<<PB0)) {
    timer_ovf = 0;                   // reset timer overflow counter at rising edge pin change interrupt
    count =0;                      // TCNT0 – Timer/Counter Register (page 73)
    TIFR0 = (1<<TOV0);
    TCNT0 = 0;                      // initialize counter register at rising edge pin change interrupt
  }
// otherwise PB0 (receiver) bit is 0 (LOW)
// can also be checked like: if ( !(PINB & (1<<PB0)) ) {...}
  else {
    count = TCNT0;                    // use counter register at falling edge pin change interrupt
    // time between rising and falling edge (pulse width)
    // count number of timer overflows
    // multiply them by 256 (if we have an 8-bit counter)
    // add the timer counts from beginning of last overflow
    pulses = timer_ovf * 256 + count;
  } 
  if (pulses >= COUNT_MIN && pulses <= COUNT_MAX )  {
    
    PORTB |= (1 << PB1);    //  <=5kHz and >= 3kHz
  }
  else {
    PORTB &= ~(1 << PB1);
  }
}                          // end ISR pin change interrupt

void setup() 
{
  cli();                // disable global interrupts

  GIMSK |= (1 << PCIE);          // generally enable pin change interrupt
  PCMSK |= (1 << PCINT0);           // enable pin change interrupt on PB0 (pin 5)
  TIMSK0 |= (1 << TOIE0);           // Timer/Counter0 Overflow Interrupt Enable
  GTCCR |= (1 << TSM) | (1 << PSR10);       // halt timer

  //TCCR0B |= ((1 << CS00)|(1<<CS02));        // select internal system clock (clkI/O) and set prescaler to 1024
  TCCR0B |= ((1 << CS00));        // select internal system clock (clkI/O) and set prescaler to 1

  GTCCR &= ~(1 << TSM);            // start timer
  DDRB |= (1 << PB1);    // set position_lights as output (PB1, pin 6)
  PORTB |= (1 << PB1);                   // switch on position_lights (set PB1 to HIGH=5V)
  DDRB &= ~(1 << PB0);   // set receiver pin as input (PB0, pin 5)
  sei();
} // end setup

int main(void)
{
  while (1) {
  }
}

if you calculate then count_min is 120 and count_max is 200 and see the pulse formal which is alwz greater than atleast 256. The comparision never works.

I think
#define F_CPU 9600000
won't work, you need
#define F_CPU 9600000UL

I have been using #define F_CPU 9600000 everytime in other programming with attiny13 and it works fine