ATtiny Frequency Measure/Counter as I2C Slave

I've been working on this project for quite some time off and on. I'm pretty close to something that's stable, but it seems there are a few hitches. My main issue that that the device seems to become not responsive and disappear from the bus after a short time and I need to physically reconnect it to get it to start reading again and then it will disappear again after a short time. Often the first read doesn't work, but the following reads will work.

I'm using TinyWireS library on the ATTiny85 and have written some code to measure frequency in two ways using Timer 1 because Timer 0 is used by the I2C. PB1 is the input pin. I'm measuring lower frequencies.. 10hz-1000hz.

Method 1: Measure the time between two pulses via interrupt. With a few simple calculations based on the chip frequency and prescaler, I can compute the period and this the frequency.

Method 2: Use a Gate Timer. So, count the incoming pulses via interrupt and then then Timer 1 Overflows, we get the total pulses and then again, via some simple calculations based on chip frequency and prescaler, we can get the frequency.

Method 1 has been more accurate and precise, but method 1 seems more "stable".

I think there is something going on between these PCINT and Overflow interrupts and the I2C interrupts that is causing this issue. I'm having real difficulty getting this to work without falling off the bus. I also think this may have to do with clock stretching via the ATTiny as well. I'm running the bus speed for this device at 10kHz too..

I have some 16Mhz crystals on the way and will be able to test them tomorrow to see if the performance improves. I'm looking for some guidance on this project and wondering if someone can help to find the issue.

Hi gismo

Did you manage to make it work? How high frequencies can it handle? If you have any tips and maybe some code, it would be fantastic.

RuneJ:
Hi gismo

Did you manage to make it work? How high frequencies can it handle? If you have any tips and maybe some code, it would be fantastic.

Hey @RuneJ I did get it to work! Last time I worked on it was 8/15/15, I got a little side tracked. I'm still using Method 1 as described above and I am using the 16mhz crystal which has made it more than accurate enough for my purpose and probably more precise than I can measure with my limited tools. I am currently only measuring low frequencies, but you can run the clock at 16mhz. Simply, you feed your pulses into the ATTINY interrupt and it's computing the frequency/time between each pulse with a software counter. I don't know how many cycles are used to do the computations, so I couldn't say how high you could measure? Probably >1mhz?

I can share the code. Maybe I'll make GIT soon and get the code up there. I'll keep you posted.

The real trouble I had was that the I2C communication part which can get a little screwed up from external noise. So, I need to work on the circuit so it's less affected.

Hi gismo

That's great! I only need to measure below 20kHz, so I guess even the built-in 8Mhz clock is sufficient (albeit not as precise).

Hi gismo
can you please share the code for method 1. i am using method one but cant solve it for almost amonth
thank you