high frequency divider to visual indicator

Hello!

I want to make a clock signal from another chip, which varies in the order of a few hundred kHz up to a few MHz, divided down to a flashing LED. I can't remember the exact numbers at the moment but I want to divide this clock pulse by exactly 68000(?) or something (can check exact numbers later - wrote it on a bit of paper somwhere!).

What's the best way to do this? Use an interrupt to constantly monitor an input pin and after a count of 32000 turn an LED on and reset the count, then after another 32000 turn it off and reset count again (for 50% duty cycle of the ~10-0.5 Hz output)? Or some other way?

I'm pretty new to this and slightly lost when it comes to doing something continuously with a relatively high frequency clock! I've only ever made code to look at a pin state whenever I fancy checking in it's own time.

Any smaller/simpler way of doing this? The arduino seems a bit overkill, but seems a lot more user friendly to work with. I will probably take the chip out and fit it in the most basic built I can get away with once programmed as part of another circuit.

Piplodocus: Any smaller/simpler way of doing this? The arduino seems a bit overkill, but seems a lot more user friendly to work with. I will probably take the chip out and fit it in the most basic built I can get away with once programmed as part of another circuit.

You could use an ATmega85 chip programmed using arduino IDE. It only has 8 pins (5/6 usable) so will be a lot smaller and less overkill. Or you could use a binary counter if the clock is divided down to the power of 2. What is the upper frequency your interested in? If it's to fast then maybe the ATmega will not be quick enough.

Riva: Or you could use a binary counter if the clock is divided down to the power of 2.

I don't think it is from memory. I worked out a way of doing it a couple years back (that I now can't remember), but it involved 3 logic ICs, one of which had to try correcting the output duty cycle too. Hence why a small neat u-processor seemed a better solution.

Riva: What is the upper frequency your interested in? If it's to fast then maybe the ATmega will not be quick enough.

I think it will (just about). If when it goes too fast it gets a little wonky on the output it probably doesn't matter as the output LED will be going fast enough to be almost solid on by that point, so it's visual indication becomes of minimal importance by the time I run out of headroom I think. What kinda speed clock signal do you reckon I could feasibly use? Is 20 MHz the ATmega base clock so to run a counter will be A LOT lower than 20 MHz? I may be able to divide first, then sort any slight non-integer error every few cycles.

Riva: You could use an ATmega85 chip programmed using arduino IDE. It only has 8 pins (5/6 usable) so will be a lot smaller and less overkill.

Which chip is this? The ones I google seem to come up with ATmega85xx and have more legs?!? I assume I'd buy it blank, then can use the Arduino ISP (http://arduino.cc/en/Tutorial/ArduinoISP) to program it with the standard Arduino bootloader? Would that work? Then can use the normal interface to program/upload my own code?

Piplodocus:

Riva: You could use an ATmega85 chip programmed using arduino IDE. It only has 8 pins (5/6 usable) so will be a lot smaller and less overkill.

Which chip is this? The ones I google seem to come up with ATmega85xx and have more legs?!? I assume I'd buy it blank, then can use the Arduino ISP (http://arduino.cc/en/Tutorial/ArduinoISP) to program it with the standard Arduino bootloader? Would that work? Then can use the normal interface to program/upload my own code?

My error here, I should have said ATtiny85. Once you have downloaded and placed the core files for the tiny85 (I use these but there are others) then you can program using the arduino IDE and upload using a ISP programmer or an UNO/MEGA loaded with ArduinoISP. I have seen but never used a bootloader on the tiny as it does not have a hardware UART but I think a software UART bootloader is available. As the chip is so small it will also be missing other hardware (like SPI, TWI, Timer2) compared to the likes of an UNO. The default clock speeds used on ARV arduino's are 8 & 16MHz but running at 20MHz should not matter (you may not be able to find a bootloader for this speed though but ISP will work fine). One way of doing this would be to used a timer in counter mode (maybe with prescaler) as I think you can get faster counting speeds than using the interrupt pin.

Google "divide by N counter" http://www.onsemi.com/pub_link/Collateral/MC14018B-D.PDF

If it were me, I'd use an ATmel chip with that supports connecting an asynchronous timer to an external clock source. For example the ATmega328 and Timer2. Set the EXCLK bit in the ASSR register and connect your clock to PB6.

As Timer2 is only 8-bit, hopefully you can set a prescaler to cut down on the number of overflow interrupts that will be required to count higher than 255.

Any smaller/simpler way of doing this? The arduino seems a bit overkill, but seems a lot more user friendly to work with. I will probably take the chip out and fit it in the most basic built I can get away with once programmed as part of another circuit.

Try using a 24-Stage Frequency Divider IC like the MC14521B or similar. Only about $0.60 each.

If you use Q18, it’s output is divided by 262,144 (218).
For 262.144 kHz in, Q18 will go high 1 sec and low 1 sec (0.5Hz).
For 2.62144 MHz in, Q18 will go high 0.1 sec and low 0.1 sec (5Hz).

dlloyd:
Try using a 24-Stage Frequency Divider IC like the MC14521B or similar. Only about $0.60 each.

If you use Q18, it’s output is divided by 262,144 (218).
For 262.144 kHz in, Q18 will go high 1 sec and low 1 sec (0.5Hz).
For 2.62144 MHz in, Q18 will go high 0.1 sec and low 0.1 sec (5Hz).

Don’t think I follow this. I can drop the clock massively, but then it ends up an odd relationship to the accurate division I want to do, and I’d then need more further division I can’t do very easily. Can’t see how I’d get it to divide by 68,000 or something that’s not 2x

There’s definitely some very useful info here. Thanks for the tips.

The ATtiny13/25/85/xx etc look great size/simplicity wise. So you’re saying I can program straight to a blank one? I thought all the Atmel chips needed to have an Arduino bootloader burned to them with a programmer first else they weren’t “arduinos”, or would work when plugged in the Uno, and you couldn’t use the normal Arduino ISP? Maybe I’m confused. I think I’ll prototype with the standard UNO chip then try to change to one of these once I know it works.

I think I need to google some more stuff brought up here.

So I can run the whole Arduino u-proc off an external clock coming from my other chip and just make it do something every x number of clock cycles? Sounds like the code will be v simple so a good plan in theory. It should avoid the clock being too fast for it from what I can tell though if the chip runs off up to 16 MHz. I don’t think it’d ever get that fast. I read that it doesn’t like changing clock freq by more than 2% between cycles. This might be a problem but hopefully it won’t as 2% of very fast happens very fast! I guess I’ll find out with a prototype! If so does this mean I can also do away with the xtal and it’s a single 8-pin solution?

So if my external clock is less than the normal Atmel clock (8?/16?MHz) do I need to prescale? How do you mean 256 bit is a problem. If the external clock is on/off can I not easily make it do something every 68000 pulses? Can I only make it do something every <256 pulses? Is the idea every “something-less-than-256-but-a-divisor-of-68,000” I increment some other variable, then when that variable gets to the equivalent of 68,000 it clocks the LED and resets itself?

If I need to prescale what’s this built in prescaler thing? Is that any use?

What are the pros and cons of using the external timer you mention?

Any useful links of similar projects using external clocks like this?

Don’t think I follow this. I can drop the clock massively, but then it ends up an odd relationship to the accurate division I want to do, and I’d then need more further division I can’t do very easily. Can’t see how I’d get it to divide by 68,000 or something that’s not 2x

Your initial requirement seemed vague with many “?”, so I took the visual LED to be of main importance.

You could get whatever you want in a small CPLD for about $1.20 with free software for programming. You could divide by exactly 68000, control the duty cycle, add out of range indicators, add gating control, add phase shifted and/or inverted outputs, etc, etc. All done in real time (parallel clocked) without jitter or latency issues.

http://www.digikey.com/product-search/en/integrated-circuits-ics/embedded-cplds-complex-programmable-logic-devices/2556261?k=cpld

Piplodocus:
The ATtiny13/25/85/xx etc look great size/simplicity wise. So you’re saying I can program straight to a blank one?
Yes, if you use an ISP programmer like this or use your UNO with ArduinoISP sketch loaded so it acts as the programmer.

I thought all the Atmel chips needed to have an Arduino bootloader burned to them with a programmer first else they weren’t “arduinos”, or would work when plugged in the Uno, and you couldn’t use the normal Arduino ISP? Maybe I’m confused. I think I’ll prototype with the standard UNO chip then try to change to one of these once I know it works.
The chip does not need a bootloader to be an Arduino it just needs to be possible to write/upload code using the Arduino IDE.

So if my external clock is less than the normal Atmel clock (8?/16?MHz) do I need to prescale?
The Arduino AVR core files are written to work properly with either 8/16MHz processor clock speeds. You can use other speeds but things like delay() will not delay the expected time and serial may/will not work (not relevant to tiny85 unless you use a bootloader). A prescaler is just a device (built into the MCU) that can divide a clock signal down by certain amounts (but always power of 2). As an example a 16x prescaler will only increment it’s output once every 16 clock cycles fed to it.

How do you mean 256 bit is a problem. If the external clock is on/off can I not easily make it do something every 68000 pulses? Can I only make it do something every <256 pulses? Is the idea every “something-less-than-256-but-a-divisor-of-68,000” I increment some other variable, then when that variable gets to the equivalent of 68,000 it clocks the LED and resets itself?
An 8 bit timer/counter will overflow and return to 0 once it counts beyond 255. As you need to count 68000 times you would need to count 265 overflows and another 160 counts to get this value. Just a little more work than having a counter that could count all the way to 68000 in one go (no AVR chips have counters big enough to do this).
Do you really need this amount of accuracy for a LED? If you stick to the nearest power of 2 (65536) and toggle the LED at this value then your only about 3.6% in error.

There are two ways of using the hardware timers to count an external clock source in an ATmega/ATtiny.

One is synchronous. That means that the clock signal has to stay HIGH or LOW for at least 4 CPU cycles. For an 8MHz ATmega328 (you can't use a 16MHz crystal because pin PB6 is taken by the external clock), the fastest your external clock can run is 2MHz.

The other is asynchronous. I'm not sure what the upper limit is, but asynchronous frees you from the 4-CPU-cycles limit. An ATtiny85 timer, for example can count at up to 64MHz (if I'm interpreting the datasheet right). The ATtiny85 is a bit of an oddball in the Atmel world though, I'm not sure if the ATmega328 timer can asynchronously count that fast. But I'd expect it to go to 20MHz, at least.

I looked over the datasheet but it doesn't look like the ATtiny85's timers can be put into asynchronous mode with an external clock like the ATmega328 can. However, since the ATtiny85's timer can be run at 64MHz in PLL-synchronous mode, that gives you a maximum timer counter frequency of 16MHz, which is pretty fast.

Divide by N means exactly that. N can be anything, and you can adjust it if you find that you need to tweak things a bit. You might have to cascade several chips to do it, but it will always divide by whatever you set the inputs of the chips to.

So the 4059 can

The HEF4059B is a divide-by-n counter which can be
programmed to divide an input frequency by any number
n from 3 to 15 999.

So, if you want exactly 68000, use one 4059 to divide by 100 and another to divide by 680 (or any other combination.)

tylernt: I looked over the datasheet but it doesn't look like the ATtiny85's timers can be put into asynchronous mode with an external clock like the ATmega328 can. However, since the ATtiny85's timer can be run at 64MHz in PLL-synchronous mode, that gives you a maximum timer counter frequency of 16MHz, which is pretty fast.

This sounds good to my ears, where do I find out more about how to do this? How do I access this timer/clock pulses in code to divide it? I can't seem to see many/any Arduino references to general timers other than delay() etc.

I'm guessing there's no chance then of just being able to clock the entire chip with this signal and use a fixed delay() command (which would bear no relation to real milliseconds if not running at 16 MHz), but would effectively give me a fixed division from whatever is coming in? Very dirty method if it would work, but if it works!

Piplodocus: This sounds good to my ears, where do I find out more about how to do this?

I'm looking at the ATtiny85 datasheet (http://www.atmel.com/images/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf), and I see that Timer0 is the one that supports an external clock source but unfortunately Timer1 is the one that can be run asynchronously. So, we will be forced to use Timer0 in synchronous mode. That's okay though, because section 11.3.3 / page 67 recommends that the external clock speed be no more than 6.4MHz. (That's 6.4Mhz for complete cycles -- rising edge to rising edge -- not 6.4MHz toggles!)

Assuming 6.4MHz good enough, you'll use the "Burn Bootloader" function in the Arduino IDE in conjunction with an ISP (USB ISP or Arduino as ISP) to make your ATtiny85 run at 16MHz from the internal PLL. I don't know if there's example code for exactly what you want to do, but Nick Gammon has some good info on timer manipulation: http://www.gammon.com.au/forum/?id=11504 His code is ATmega-specific, but the same principles will apply to the ATtiny, albeit with different register and bit names. The ATtiny85 register and bit information is in the datasheet here:

Section 11.9.3 page 79, TCCR0B register, CS0*x* bits for external clock Section 11.9.7, TIMSK register, TOIE0 bit for overflow interrupt enable

I see from section 11.3.3 page 67 that the external clock cannot be prescaled, but I think that's okay. You'll just be generating a lot of interrupts when your external clock is in the MHz range. This will limit the other things you can do with your Attiny, but if you don't need to do anything else, no big deal. Your ISR will need to be tight, nothing more than incrementing an integer. Then in your main loop continually check that integer and the TCNT0 register to see if it's the right time to toggle the LED.

I'm guessing there's no chance then of just being able to clock the entire chip with this signal

Only if the slew rate (rate of clock speed change from clock to clock) is within the Atmel specifications. In that scenario, you can no longer really measure the passage of time, you're limited to counting pulses. But it sounds like that will be fine.