# tracking the state of a 4040 binary counter

I’m using OCR2B to provide a 2mhz clock signal for a binary counter. What I want to do is keep track of the outputs of Q0, Q1, Q2 to count up to 7. I don’t really know anything about timers or interrupts, but is there a way to do this in software?

is there a way to do this in software

Yes, just use the ISR to increment a variable. The bits of the variable will be the same as the bits of any binary counter clocked at the same rate.

7, in binary, is 111 so you want something to happen when Q0, Q1 and Q2 are all 1's?

You can use four two input NAND gates to AND the pins together to get a pulse when the count reaches 7: NOT((NOT (Q0 NAND Q1)) NAND Q2) The 'NOT' gates are just NAND gates with the two inputs tied together.

thanks grumpy_mike, I'll read up on IRS and see what I can do :D I'm sending the outputs of the 4040 to A,B,C of a 4051 multiplexer to step through the 8 output gates, and I need to know which gate is open. Basically I'm just trying to save those 2 output pins :) I'd like my program to "do stuff" at each stage, so I'm going to try to time it to happen in between each clock cycle. The atmega328 runs at 16 MHz, so if I clock the 4051 down to 2KHz I should have 8000 cycles to do stuff, right?

did you perhaps mean ISR?

did you perhaps mean ISR?

yup :blush:

ok, great! Now I think I can handle writing the ISR, but what interrupt am I looking for? Here is the 2MHz clock output code:

``````    TCCR2A = ((1 << WGM21) | (1 << COM2B0));
TCCR2B = (1 << CS20);
TIMSK2 = 0;
OCR2A = 3;
``````

from my understanding, the timer register TCCR2A is doing the counting and the output compare register OCR2A outputs the waveform. So the interrupt I’m looking for will trigger when OCR2A changes from low to high? and if I do run the ISR, won’t it interfere with the output? I’m probably waaay off base, this is my first time looking at atmega registers other than the PORTs.