counter required to count to near 1 million - Page 1
Forum section: electronics / beginners

author: Simon
author_rank: Unknown
timestamp: March 17, 2013, 09:01:00 pm
content: I need to count to nearly 1'000'000 on a counter, can anyone suggest a good counter ? this is for a clock signal generator

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 17, 2013, 09:14:09 pm
content: You need to provide a bit more information, what are you looking for? A pulse out after the counts or do you need a readout of the counts?What are you trying to brew this time?

author: Psi
author_rank: Super Contributor
timestamp: March 17, 2013, 09:23:32 pm
content: I dunno how/if they work or do what you want but DX sell industrial (looking) counters which go up to 999,999http://dx.com/p/cg8-digital-counter-ac-110-220v-118803http://dx.com/p/dhc3j-1-1-lcd-digital-counter-grey-black-138035Edit: looking at your comment about it being for a "clock signal generator" im thinking you want an IC counter and not a finished unit??

author: Simon
author_rank: Unknown
timestamp: March 17, 2013, 09:28:42 pm
content: no just a digital counter / frequency divider IC, I'm putting in a 32.768 KHz clock and want to fire an impulse every 30 seconds so make a circuit send an impulse every time the counter resets (or other convenient number occures)

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 17, 2013, 09:47:48 pm
content: Looking at cost and board space the best solution is probably a PIC10XXX.

author: Thor-Arne
author_rank: Unknown
timestamp: March 17, 2013, 09:48:51 pm
content: 74HC160 or 74HC4059 should be usable.

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 17, 2013, 09:50:49 pm
content: Quote from: Thor-Arne on March 17, 2013, 09:48:51 pmNot enough counts.

author: krish2487
author_rank: Frequent Contributor
timestamp: March 17, 2013, 09:51:17 pm
content: Cascade 6 decade counters...@thor-arneyou beat me by seconds...

author: c4757p
author_rank: Super Contributor
timestamp: March 17, 2013, 09:53:39 pm
content: Quote from: PA0PBZ on March 17, 2013, 09:47:48 pmYep. I'm often the last to suggest a microcontroller, but they do make for nice, quick, custom "logic" chips when you need something this specific. I've got a pile of PIC10F200s (both DIP-8 and SOT-23-6 -holy crapthat is a small microcontroller, it's smaller than its 0805 decoupling cap!) that frequently get used for this sort of application. A cascade of 7400 series counters would work, but I'd probably prefer a single 10F200.

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 17, 2013, 09:58:17 pm
content: A DIP-8 $ 0.30 is hard to beat with 2-4 DIP 16-24 devices...

author: Psi
author_rank: Super Contributor
timestamp: March 17, 2013, 10:06:44 pm
content: I had a very quick look at clock divider chips on digikey but you really need a divide and then a multiply to get 30s, which is two chips.A mcu is going to be cheaper unless you can find a dirt cheap clock logic IC with a half-minute output.

author: Thor-Arne
author_rank: Unknown
timestamp: March 17, 2013, 10:07:45 pm
content: Both 74HC160 and 74HC4059 can be pre-programmed with dip switches, that's why I suggested those.A micro controller is also usable ofcourse, I just generally don't suggest those if it can be done easily with standard logic.

author: jimmc
author_rank: Frequent Contributor
timestamp: March 17, 2013, 10:09:30 pm
content: As has been said PIC route is the simplest, there's a ready made solution herehttp://www.tayloredge.com/reference/Circuits/ClockDivider2/.Using standard logic devices a '4060 will divide by 16384, followed by a '4024, '390 or '393 plus gate to divide by 60Jim

author: mrflibble
author_rank: Super Contributor
timestamp: March 17, 2013, 10:10:28 pm
content: The MCU gets my vote.... even if it is a PIC. XD

author: Thor-Arne
author_rank: Unknown
timestamp: March 17, 2013, 10:13:51 pm
content: Quote from: jimmc on March 17, 2013, 10:09:30 pmI agree, thisisthe simplest solution.

author: rdl
author_rank: Super Contributor
timestamp: March 17, 2013, 10:24:16 pm
content: Could you use a higher frequency crystal with a 4521?

author: Psi
author_rank: Super Contributor
timestamp: March 17, 2013, 10:27:53 pm
content: Quote from: jimmc on March 17, 2013, 10:09:30 pmhow would it work?32768Hz / 16384 = 2 second clock2Hz / 60 = 33ms clock

author: PA0PBZ
author_rank: Super Contributor
timestamp: March 17, 2013, 10:37:46 pm
content: Quote from: Psi on March 17, 2013, 10:27:53 pmHaving a bad day Psi?32768Hz / 16384 = 0.5 second0.5 second / 60 = 30 secondJust saying...

author: Simon
author_rank: Unknown
timestamp: March 17, 2013, 10:56:24 pm
content: the MCU is out, I'm getting fed up with MCU's at the moment as nothing about C is made simple and when you nearly have your problem solved but for a detail and ask a question on a forum you get told you missed some basic concept and have to go back to learning. Waste of time.From my school days i seems to remember that I can have a counter that counts to t least what i need and use logic gates to sent out a reset signal at the right count, simples I'll get it done faster with basic digital circuitry than any of the MCU jargons, anythinf complex i'll take to an arduino, sad i know but as it is only with arduino that everything is made easy for idiots like me that is the route i will have to take.My main concern is that I remeber something about counters not being sycronous and drifting due to propagation delays as one flip/flop clocked another, that is my main concern. What about the HCF4060M013TR ? it has built in oscillator circuitry

author: jpb
author_rank: Super Contributor
timestamp: March 17, 2013, 11:04:10 pm
content: A brief search at Farnell led to the SN74LV8154N which can be used as a 32 bit counter (you need 20 bits I guess). It is a dual 16bit but can be connected as one 32bit. They only seem to have the SMD version in stock. You might need a 4 input AND gate (or 3 x 2 input AND gates) to detect when it reaches 0xF0000 and use that as both a reset and the clock (I may be out by one on this but the error of 1 part in a million is probably worth living with to simplify the circuit).

author: c4757p
author_rank: Super Contributor
timestamp: March 17, 2013, 11:04:51 pm
content: Quote from: Simon on March 17, 2013, 10:56:24 pmThen I guess my suggestion of PIC10F200 iswaythe hell out! With 384 bytes ROM and 16 bytes RAM, I don't think you'd have much luck programming it in anything but assembly.I can definitely understand the draw of discrete logic chips. I use them a lot and love them. Sorry I can't help much with this specific problem though, if you want to use discrete logic - digital timing isn't exactly my forte.

author: Simon
author_rank: Unknown
timestamp: March 17, 2013, 11:09:30 pm
content: Quote from: jpb on March 17, 2013, 11:04:10 pmso long as it does not have ripple type drift problems. I don't mind a slightly more complicated logic gate array to detect the overflow.

author: David_AVD
author_rank: Super Contributor
timestamp: March 17, 2013, 11:59:51 pm
content: Quote from: Simon on March 17, 2013, 10:56:24 pmSounds like you need to put in some time to learn how to write (C or asm) code. The real waste of time happens when you refuse to learn.

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 12:01:39 am
content: Quote from: Simon on March 17, 2013, 10:56:24 pmI've never been a fan of the 4060. A better discrete chip for large counts is theMC14541B. It has 2^8, 2^10, 2^13, and 2^16 outputs, with the built-in oscillator circuitry, and pin controls for auto-reset and output logic level - plus it's not a ripple counter like the 4060 - and has a smaller pin count.Combine this with any Divide-by-N 4-bit counter (like the MC14526B) and you can easily have precision 30 second pulses from 32kHz - done completely with synchronous counters - and without the need for any extra gates.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 07:06:55 am
content: So do I need to specifically look for a synchronized counter to make sure it does not drift off timing due to propagation delays ?

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 07:10:12 am
content: Quote from: David_AVD on March 17, 2013, 11:59:51 pmI conclude my dyslexia must be worse than I realized as what seems simple to everyone else I am mystified by. I will go forth with the arduino for now (I never dreamt I'd ever have to say that) as at least stuff is plainly explained all in one place and I don't have to read books written for PC's to adapt to different hardware and try and understand how to use libraries.

author: David_AVD
author_rank: Super Contributor
timestamp: March 18, 2013, 08:07:45 am
content: Honestly, some people poo-poo the Arduino but it really can help people get over that initial hurdle in programming.I even used one recently to delve into the world of touch screens at short notice. Thanks to the "sketches" available I had it running in minutes. That showed me that it should be equally doable on a PIC without reinventing the wheel.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 08:14:08 am
content: you may not be doing it on a pic as it is avr's that have built in touch sense interfaces. Unless your looking at a secondary unit dedicated to the touch bit

author: David_AVD
author_rank: Super Contributor
timestamp: March 18, 2013, 08:20:21 am
content: Quote from: Simon on March 18, 2013, 08:14:08 amAlmost all of the TFT screens with touch on eBay, etc come with the LCD controller and touch A2D chips. The UTFT and UTouch libraries make it super simple on the Arduino and it shouldn't be too much harder on a PIC.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 08:30:59 am
content: Quote from: Simon on March 18, 2013, 07:06:55 amAnyone got an answer ?

author: Thor-Arne
author_rank: Unknown
timestamp: March 18, 2013, 09:21:35 am
content: Quote from: Simon on March 18, 2013, 08:30:59 amIf my memory serves me well, the propagation delay issue was quite high in frequency, between 10 and 20 MHz.Take a look at the 74HC4060 you can use a couple of those to divide the 32.768kHz down to 2 Hz (divide by 256 and divide 64). It has internal gates for hooking up a crystal- or rc-oscillator.

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 09:32:38 am
content: Quote from: Simon on March 18, 2013, 07:06:55 amIt's not about drift but unstable outputs (since overflow ripples). For most uses (especially divide-by-n), it doesn't make any difference.Quote from: Thor-Arne on March 18, 2013, 09:21:35 amAs mentioned already, the 4541 is a better chip than the 4060 - and also has the built-in oscillator..

author: Psi
author_rank: Super Contributor
timestamp: March 18, 2013, 09:52:18 am
content: Quote from: PA0PBZ on March 17, 2013, 10:37:46 pmlol, yeah, bad day

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 10:08:56 am
content: I think i may be getting confused between lack of synchronization between outputs and frequency error. I'll have a look at some of the suggested parts

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 11:04:30 am
content: The 4541 will not use a crystal and only has certain outputs available

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: March 18, 2013, 11:55:33 am
content: Quote from: c4757p on March 17, 2013, 11:04:51 pmIt's entirely feasible to do C on the 10F200 (using the non-free version of the microchip compiler).If written carefully, efficiency isn't that far off assembler.For something a simple as this, probably even the free version and sloppy coding would still do the job

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 11:58:18 am
content: except I want to do this the old fashioned way. It will drive an old clock so still be in keeping

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 12:36:18 pm
content: Quote from: Simon on March 18, 2013, 11:04:30 amTrue - I've always had an external clock available when I used it - although I do wonder if hooking a crystal up to the RC pins (as shown for a 4060) wouldn't work just fine - I'll have to breadboard that if I get a chance. As far has only having certain outputs available, for high counts (like one million) the salient point is the highest division output - with a 32kHz frequency, the 4060 divides down to a minimum of 2Hz; the 4541 down to .5Hz - which just means 4 bits more needed to get to .033Hz (30 seconds) - instead of 6.But in any case, if you use a divide-by-n counter as the final stage you can avoid extra decode logic. So if you combine a 4060 (2Hz frequency at it's 2^14 output) with, for example, a MC14569B (which is a divide-by-n dual 4-bit counter), you can have any timing period between 1 - 127.5 seconds (in .5 second increments) - settable with switches if desired.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 12:41:31 pm
content: yes that is true

author: olsenn
author_rank: Frequent Contributor
timestamp: March 18, 2013, 12:44:05 pm
content: I just finished making a frequency counter with the Timer/Counter1 module of a Atmel Atmega328P. Technically the counter is 16-bit, but by incrementing a variable on overflow, you can keep track of as many counts as you'd like.I'm not sure if this helps?

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 12:45:06 pm
content: well I'm going to avoid an MCU on this one

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: March 18, 2013, 01:45:50 pm
content: In terms of cost vs. division ratio, the (74hc)4020,40 and 60 are hard to beat if you can live with binary ratio. The HC4060 has an oscillator as well, so probably a very cheap 2-chip solution.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 03:07:17 pm
content: what do you mean by binary ratio ? like 1000 versus 1024 ?

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 03:40:00 pm
content: Quote from: Simon on March 18, 2013, 03:07:17 pmMost discrete chips which can count very high are binary counters, so they do binary division - divide by 2, 4, 8, 16, etc; i.e. 2^n. This matters when you consider what frequency you are starting with - and what frequency you want to end up with. In your case, starting with 32.768kHz and wanting to end with .033Hz means that you can't do it with a simple binary divide. OTOH, if you wanted to end up with a 32 second pulse, this would be easy to do with a single chip (like the MC14536B) since .03125Hz is 32768 divided by 2^20.Since you want a final frequency which is not evenly divisible (from 32768) by a power of 2 (and you don't want to use an MCU), you have to use a minimum of two discrete chips - either two counters or a counter and decode logic. In binary terms (or hex for easier writing), you want to divide 8000h by F0000h.

author: SeanB
author_rank: Super Contributor
timestamp: March 18, 2013, 03:49:59 pm
content: Divide by 2 instead of divide by ten. Much simpler with the logic, as there is only one pin to decode. With divide by 10 you need to decode a few pins to get the terminal count and use that to reset the counter.As to ripple counters having issues, it is only if you are using them and decoding the outputs, which can very briefly have invalid counts as the counters change state, the count ripples down the chain from the fastest to the slowest. Only an issue if you are latching the outputs, if you are going to just decode to get the TC to reset you just add a RC delay to the comparator output to both ignore these very short duration pulses and to get a long enough reset pulse so the counters are going to be reset correctly ( nothing worse than having half the counters being reset by a too short pulse, it just needs to be less in time than a half of your clock, at 32kHz that will be pretty long).If you are using a clock crystal drive it with a 4007 uncommitted gate, there is a 32kHz oscillator in the appnotes that has both low drift and low drive so it has best accuracy.Otherwise just take a clock module and power it off 1V5 from a red LED, and use the drive pulses to clock a pair of 4017 chips arranged as a divide by 3 and the other as divide by 10 ( no jumpers at all) and use either the carry out to give a 15s pulse or one of the count outputs to give a 3s clock.

author: jimmc
author_rank: Frequent Contributor
timestamp: March 18, 2013, 04:24:58 pm
content: I think Sean has covered most of it.I would just point out that the internal oscillator of the '4060 can drive a 32768Hz crystal e.g.http://www.doctronics.co.uk/4060.htm.Turn away now Simon.As an idea of what can be done with a PIC seehttp://www.leapsecond.com/tools/PPSDIV.ASM9 simultaneous square wave outputs -- one for each frequency decade from 100 kHz to 0.001 Hz (1000 s) from a 10MHz clock!Jim

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 04:36:22 pm
content: Quote from: jimmc on March 18, 2013, 04:24:58 pmThe sort of thing I'd have been doing as a full version but not right now. I just need to get this thing ticking every 30s

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 04:39:29 pm
content: Quote from: SeanB on March 18, 2013, 03:49:59 pmAs many people already pointed out here, you can hook a 32kHz crystal directly to a 4060 - so why would you need a 4007?QuoteNot sure what you mean by 'clock module', but this solution seems odd to say the least. The frequency he's after is not evenly divisible by 10 either, so why use decade counters? It's less counting in the same package size - and even with two of them cascaded the lowest frequency you could get from 32.768kHz is 327Hz - far from what he's after. This makes no sense - in less, by 'clock module' you mean a NON-discrete logic chip - which seems to defeat the point of the discussion - I thought we were talking about how to accomplish it with a couple of discrete chips.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 04:42:29 pm
content: At the moment I'm looking at a 4541 followed by any other counter that will finish it off right. As I'll have logic gates knocking around anyway I can use one or two to make the clock gen

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 04:52:11 pm
content: Quote from: Simon on March 18, 2013, 04:42:29 pmWhat are your parameters for the output pulse (I mean, aside from the 30s frequency)? Is there a minimum or maximum pulse length?

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 05:32:45 pm
content: not really, i'll need to have a bit of pulse circuitry connected to the reset line so that when the reset is trigered it make the pulse fire a pulse, even if it is something like charging a capacitor with resistor in parallel with a comparator or opamp

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 05:33:48 pm
content: Quote from: Simon on March 18, 2013, 04:42:29 pmWell, if you don't need the oscillator section of the 4541 - and can provide the 32768Hz clock signal from elsewhere - I would suggest using the 4541 as the last stage of the counter - as shown in the attached drawing. If you do it this way, you can set whether the output pulse every 30 seconds is positive or negative-going by just tying pin 9 on the 4541 high or low.

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 05:43:53 pm
content: Hmm... just realized that the HEF4526B may not be the cheapest divide-by-n counter- if so, you can substitute any divide-by-n 4-bit counter (dual or otherwise) - I haven't used any for awhile so I don't know which is currently the cheapest. You just want to divide by 15 before the 4541.

author: marmad
author_rank: Super Contributor
timestamp: March 18, 2013, 07:21:55 pm
content: @Simon - BTW, I just breadboarded a 4541 clocked by a 32.768kHz crystal hooked to the built-in oscillator pins 1 and 3 (similar to the circuit shown in 4060 datasheets). As I thought, it's no problem at all getting it to work - I got a solid .5Hz output after tinkering with the load capacitor.

author: Simon
author_rank: Unknown
timestamp: March 18, 2013, 10:45:14 pm
content: You mean it works ?

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 01:23:46 am
content: Quote from: Simon on March 18, 2013, 10:45:14 pmYes, it worked fine - I got a perfect 500mHz square wave out at pin 8 of the 4541. Because you can basically hook up a crystal to any CMOS inverter or inverting gate (within the frequency limits of the device, of course). You're just biasing the inverter into it's linear region and using it like an amplifier with a feedback network that includes the crystal.Edit: If you'd like, I'll draw the modified schematic including the crystal oscillator components - for the 33mHz output frequency.

author: Rufus
author_rank: Super Contributor
timestamp: March 19, 2013, 01:33:59 am
content: Quote from: mikeselectricstuff on March 18, 2013, 11:55:33 amComplete program for a PIC12F1822 which uses 4.1% of available program memory with the crappy free XC8 compiler (2.9% with the pro version). It pulses pin 5 high for 2 seconds in 30.Code:// PIC 12F1822 Timer project#include <pic.h>// Some Typestypedef unsigned long Dword;typedef unsigned int Word;typedef unsigned char Byte;typedef signed char Char;typedef unsigned char Bool;typedef bit Bit;// PIC12F/LF1822 Configuration pragmas// Oscillator Selection// #pragma config FOSC=ECH // External Clock, High Power Mode (4-32 MHz): device clock supplied to CLKIN pin// #pragma config FOSC=ECM // External Clock, Medium Power Mode (0.5-4 MHz): device clock supplied to CLKIN pin// #pragma config FOSC=ECL // External Clock, Low Power Mode (0-0.5 MHz): device clock supplied to CLKIN pin#pragma config FOSC=INTOSC // Internal Oscillator: I/O function on CLKIN pin// #pragma config FOSC=EXTRC // External Oscillator: External RC circuit connected to CLKIN pin// #pragma config FOSC=HS // High-Speed crystal/resonator connected between OSC1 and OSC2 pins// #pragma config FOSC=XT // Crystal/resonator connected between OSC1 and OSC2 pins// #pragma config FOSC=LP // Low-power crystal connected between OSC1 and OSC2 pins// Watchdog Timer Enable#pragma config WDTE=ON // Enabled// #pragma config WDTE=NSLEEP // Enabled while running and disabled in Sleep// #pragma config WDTE=SWDTEN // Controlled by the SWDTEN bit in the WDTCON register// #pragma config WDTE=OFF // Disabled// Power-up Timer Enable// #pragma config PWRTE=OFF // Disabled#pragma config PWRTE=ON // Enabled// MCLR Pin Function Select#pragma config MCLRE=ON // MCLR/VPP pin function is MCLR// #pragma config MCLRE=OFF // MCLR/VPP pin function is digital input// Flash Program Memory Code Protection#pragma config CP=OFF // Disabled// #pragma config CP=ON // Enabled// Data Memory Code Protection#pragma config CPD=OFF // Disabled// #pragma config CPD=ON // Enabled// Brown-out Reset Enable#pragma config BOREN=ON // Enabled// #pragma config BOREN=NSLEEP // Enabled while running and disabled in Sleep// #pragma config BOREN=SBODEN // Controlled by the SBOREN bit in the BORCON register// #pragma config BOREN=OFF // Disabled// Clock Out Enable#pragma config CLKOUTEN=OFF // CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin// #pragma config CLKOUTEN=ON // CLKOUT function is enabled on the CLKOUT pin// Internal/External Clock Switchover// #pragma config IESO=ON // Enabled#pragma config IESO=OFF // Disabled// Fail-Safe Clock Monitor// #pragma config FCMEN=ON // Enabled#pragma config FCMEN=OFF // Disabled// Flash Memory Self-Write Protection#pragma config WRT=OFF // Write protection off// #pragma config WRT=BOOT // 000h to 1FFh write protected, 200h to 7FFh may be modified by EECON control// #pragma config WRT=HALF // 000h to 3FFh write protected, 400h to 7FFh may be modified by EECON control// #pragma config WRT=ALL // 000h to 7FFh write protected, no addresses may be modified by EECON control// X4 Clock PLL// #pragma config PLLEN=ON // Enabled#pragma config PLLEN=OFF // Disabled// Stack Overflow/Underflow Reset#pragma config STVREN=ON // Enabled// #pragma config STVREN=OFF // Disabled// Brown-out Reset Voltage Selection// #pragma config BORV=LO // Low trip point selected#pragma config BORV=HI // High trip point selected// Low-Voltage Programming#pragma config LVP=ON // Enabled// #pragma config LVP=OFF // Disabled, high voltage on MCLR/VPP must be used for programming// ID Location value assignment#pragma config IDLOC0=0#pragma config IDLOC1=0#pragma config IDLOC2=0#pragma config IDLOC3=0// Counts down and kicks watchdog if not zero in timer interruptvolatile Byte wd_interlock;//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// initialise - set up all hardwarevoidinitialise(void) {// OSCON Defaults to 500kHz for INTOSCLATA = 0x00;ANSELA = 0x00; // All I/O digitalTRISA = 0x00; // All outputsWDTCONbits.WDTPS = 12; // approx 4 second watchdog time out// Setup Timer 1 to use 32kHz external crystal and generate// a timer interrupt at 0.5Hz// Disable Gate functionT1GCON = 0x00;// Select T1OSC crystal, prescale of 1, enable T1OSC, no sync with FOSC// Timer 1 disabledT1CON = 0x8C;// Wait for T1OSC stablewhile(!OSCSTATbits.T1OSCR) {};// Clear Timer 1 interrupt flag and enable Timer 1 interruptsTMR1IF = 0;TMR1IE = 1;// Enable general and peripheral intsINTCON = 0xC0;// Run timerT1CONbits.TMR1ON = 1;}//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *// Interrupt Servicevoid interruptisr(void) {static Byte sec2 = 0;// Timer 1 interrupt at 0.5 Hz.if(TMR1IF) {TMR1IF = 0;// Handle watchdog and interlockif(wd_interlock) {wd_interlock--;CLRWDT();}// Count 15 interrupts in 30 seconds, set RA2 high for// 2 secondssec2++;if(sec2 > 14) {sec2 = 0;LATAbits.LATA2 = 1;} else {LATAbits.LATA2 = 0;}}}//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//voidmain(void) {initialise();for(;;) { // Loop foreverwd_interlock = 10;SLEEP();}}The 12F1822 might be a bit overkill but I had a spare tube of them and they are still only 85p 1 off of in DIP8 from Farnell. It has a specific 32kHz low power oscillator functional in sleep mode, haven't looked if anything cheaper has that. Timing was about 150 ppm fast with just a junk box watch crystal plugged into a bread board. Pulled about 28uA running on 3.3v.

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 01:38:32 am
content: Do people even read previous messages posted in the threads here?Without going back and re-reading, I'd estimate that Simon has stated between 5 - 10 times that he doesn't want to use an MCU.

author: David_AVD
author_rank: Super Contributor
timestamp: March 19, 2013, 02:16:39 am
content: Quote from: marmad on March 19, 2013, 01:38:32 amA MCU solution could help plenty of other people though. Sometimes threads spawn some great ideas.If Simon doesn't feel up to using a MCU, then he can still phaff around with the other methods and get there eventually.

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 02:30:18 am
content: Quote from: David_AVD on March 19, 2013, 02:16:39 amThat's true - although it would feel more like a real discussion - and that people were actually perusing the thread - if they mentioned that fact. When people don't bother to do that, you get multiple posts all saying the same thing - such as the 5 or 6 people who each suggested the 4060 chip - as if they were the first to do so. Just saying...

author: robrenz
author_rank: Super Contributor
timestamp: March 19, 2013, 03:03:13 am
content: Quote from: marmad on March 19, 2013, 01:38:32 amIMO only because he does not have the time for the learning curve at the moment. If presented with a canned solution that he could plug and play he may change his mind. I dont think the post is out of line

author: TerminalJack505
author_rank: Super Contributor
timestamp: March 19, 2013, 03:38:45 am
content: Here are a couple of ideas if your design isn't tied to a 32.768kHz crystal and really just require a pulse every 30 seconds...Idea #1: Use a 74HC5555 and a 3.932160 MHz crystal. Have the 74HC5555 divide by 217. This will give a pulse exactly every 30 seconds.Idea #2: Use a 30.72kHz crystal with a simple Pierce oscillator and a divide by 1024 IC. (Or 512? or 2048? My brain's not working at the moment.)

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 04:04:02 am
content: Quote from: TerminalJack505 on March 19, 2013, 03:38:45 amThis will give a frequency of 30Hz (a period every 33mS) - not 33mHz (a period every 30 seconds).QuoteThis also gives 30Hz - not 33mHz. It seems you're confused between frequency and period. It's not possible to get to the correct frequency using either of the crystal frequencies you mentioned and a single undecoded binary divider-chain.

author: TerminalJack505
author_rank: Super Contributor
timestamp: March 19, 2013, 04:27:49 am
content: Quote from: marmad on March 19, 2013, 04:04:02 amYep. I stand corrected.

author: David_AVD
author_rank: Super Contributor
timestamp: March 19, 2013, 06:36:10 am
content: Use a PIC.

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 01:03:09 pm
content: @Simon - Here is a quickly revised schematic (sorry about the sloppiness) showing the entire circuit with crystal putting out a 33mHz frequency (30 sec period) in it's simplest form (plus a DSO image of the output of the 4541). I don't think there's an easier - or smaller package count - way to do this using discrete chips. If you wanted programmability, adding switches to the 4526 could allow you to change the period from 4 - 32 seconds in 2 second increments - and adding a switch to the 4541 could give you shorter periods, for example, 0.5 - 4 seconds in 0.25 second increments. Of course, as many have mentioned, with a few lines of code it's incredibly simple to do using an MCU - and more flexible in the long run - but sometimes the old-fashioned way is kind of funEdit: Just did a quick check and it seems the 74HCT161 (another divide-by-n 4 bit counter) is perhaps a better buy than the HEF4526 - it just depends on what package size you need and if you plan on using higher CMOS voltages.

author: Simon
author_rank: Unknown
timestamp: March 19, 2013, 03:04:13 pm
content: well I've got nothing against micro's I just had this row on the avr forum about the documentation of the libs. It seems that unless your a pro in programming mcu's take for ever so I'm going to give them a break, do some descrete stuff and play with the arduino for a bit.

author: olsenn
author_rank: Frequent Contributor
timestamp: March 19, 2013, 03:17:11 pm
content: I can send you the code for my frequency counter if you'd like... you'll see just how easy using the built-in 16-bit counter of the atmega328 is. The best part is, if you have an Arduino, you already have the IC

author: Simon
author_rank: Unknown
timestamp: March 19, 2013, 03:21:42 pm
content: sure why now, I'll have a look

author: c4757p
author_rank: Super Contributor
timestamp: March 19, 2013, 03:26:34 pm
content: Quote from: olsenn on March 19, 2013, 03:17:11 pmShhh... I was about to do a freq counter all in 7400s just for the fun of designing it. Stop tempting me with MCUs!(Oh well, I guess I'll at least need an external divider for the higher frequencies anyway...)

author: Simon
author_rank: Unknown
timestamp: March 19, 2013, 03:27:35 pm
content: haha, I guess a discrete solution will end up needing a wall wart

author: c4757p
author_rank: Super Contributor
timestamp: March 19, 2013, 03:45:20 pm
content: Quote from: Simon on March 19, 2013, 03:04:13 pmProgramming and electronics intersect often, but they're definitely very different skills that each take a long time to master. I highly recommend anyone interested in either one to diversify and cross over into the other, but it's not easy. I sometimes forget that - I'll recommend a MCU for something as if it's "easy" to just bang out some code, but I've been programming since I was eight years old and it's as easy for me at this point to write "instructions" in C or assembly as to write them in English. I can forget that the process of going from "I want to do this" to finished code and an implementation is pretty daunting if you're not used to it.QuoteThey're programmers. You can't tell them that their favorite stuff sucks, that's heresy. (AVR documentation in general does suck, and the library documentation sucks even harder. I'm with you there.)

author: Thor-Arne
author_rank: Unknown
timestamp: March 19, 2013, 04:43:01 pm
content: Quote from: c4757p on March 19, 2013, 03:26:34 pmIt's fun playing with counters, get on with it.I have one more or less compete sitting on a breadboard, just the control-timing left to do.While waiting for parts for another project I got bored and slammed it together.

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 05:02:59 pm
content: Quote from: c4757p on March 19, 2013, 03:45:20 pmIn terms of super long-term reliability and ultra-low current, MCU's still have a way to go to prove themselves. I still have ~40-year old objects built from discrete logic continuing to run merrily along - and +20-year old objects built from discrete CMOS logic still running off the same battery cell. In the same few decades, I've had numerous expired MCU-based objects due to lost bits from ROMs, PROMs, EPROMs, EEPROMs, Flash ROMs, etc. Of course, I know that they get continually better - but they can't yet compete against discrete logic's track record.

author: Simon
author_rank: Unknown
timestamp: March 19, 2013, 05:46:18 pm
content: Quote from: c4757p on March 19, 2013, 03:45:20 pmTo be fair pics are equally confusing and lacking, people either try to explain what a digital signal is or how some function works without the bit in the middle, So I think it's a combination of me being very dense and people who can't be bothered to write proper instructions.

author: marmad
author_rank: Super Contributor
timestamp: March 19, 2013, 05:48:23 pm
content: Quote from: Rufus on March 19, 2013, 01:33:59 amJust for comparison purposes, the discrete circuit shown in the previous schematic (MC14541 + HEF4526) uses ~5.2uA at 3V, ~12uA at 3.3V, or ~24uA at 5V. Attached is an image of the 4526 output.

author: olsenn
author_rank: Frequent Contributor
timestamp: March 19, 2013, 08:19:49 pm
content: As promised, here's my frequency counter. Don't worry about the lcd stuff, that's just my way of displaying the value on a HD44780. It's very simple#include <avr/io.h>#include <avr/interrupt.h>#include <string.h>#include "lcd.h"/* Definitions */#define F_CPU 25000000/* Prototypes */void startTimer100mSec(void);void startTimer1Sec(void);void startTimer10Sec(void);/* Declarations */volatile unsigned long int overflow0, overflow1, frequency;char disp;/* Interrupt Handlers */ISR (TIMER0_OVF_vect){overflow0++;}ISR (TIMER1_OVF_vect){overflow1++;}/* Functions */void startTimer100mSec(void){unsigned long int tmp;tmp = F_CPU/10;overflow0 = 0;TCNT0 = 0;while((overflow0*256 + TCNT0) < tmp) {}return;}void startTimer1Sec(void){overflow0 = 0;TCNT0 = 0;while((overflow0*256 + TCNT0) < F_CPU) {}return;}void startTimer10Sec(void){unsigned long int tmp;tmp = 10*F_CPU;overflow0 = 0;TCNT0 = 0;while((overflow0*256 + TCNT0) < tmp) {}return;}int main(void){DDRB = 0x3E; // PB0=Input, PB1-PB5=Output/* Timer/Counter0 - Gate Time */PRR = (0<<PRTIM0); // Power-on Timer/Counter0TCCR0A = 0; // Normal ModeTCCR0B = 1; // No PrescalerTIMSK0 = 1; // Enable Overflow InterruptTCNT0 = 0;sei();/* Timer/Counter1 - Counter */PRR = (0<<PRTIM1); // Power-on Timer/Counter1TCCR1A = 0;TCCR1B = 7; // Normal Mode, T1-Pin as ClkTIMSK1 = 1; // Enable Overflow InterruptTCNT1 = 0; // Initialize Countersei(); // Enable Interrupts/* Specifics */lcd_init(LCD_DISP_ON);while(1){frequency = 0;overflow0 = 0;overflow1 = 0;TCNT0 = 0;TCNT1 = 0;startTimer1Sec();frequency = (65535*overflow1) + TCNT1;sprintf(disp,"%lu",frequency);lcd_clrscr();lcd_home();lcd_puts(disp);}return 0;}

author: c4757p
author_rank: Super Contributor
timestamp: March 19, 2013, 08:22:36 pm
content: Nice, clean code. I like it. Not a single line that I have a question about.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 06:16:38 pm
content: Quote from: marmad on March 19, 2013, 01:03:09 pmHm, my result is rather different a short pulse every 2.5ish seconds, i need to go recheck. I couldn't work out what MR was on the 4526, is it Reset ?

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 06:45:40 pm
content: Quote from: Simon on March 29, 2013, 06:16:38 pmYes, MR is master reset - 0 = count / 1 = reset. If you are using my previously posted schematic, it sounds like the crystal may be running at a fifth harmonic or higher; use your DSO or a frequency counter to check the oscillator frequency at pin 1 to make sure you're getting the correct 32.768kHz.If you're not, you might have to fiddle with the load capacitance of the p.i. network/low pass filter for the crystal - I didn't have any spare 20-30pF caps laying around when I breadboarded it, so I used 100 and 110pf - but the lower values might work better. You can experiment with a few sizes until you get a lock with the crystal.Edit:If the oscillator frequency is good, then it would have to be incorrect logic in the divider chain.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 06:55:04 pm
content: ok, found my problem, I'm oscilating at about 200KHz, may i assume due to the breadboard concoction ?

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 06:59:32 pm
content: Quote from: Simon on March 29, 2013, 06:55:04 pmYes - as noted above, I had only a few values of caps available to me when I breadboarded, so I experimented until I got a lock with the crystal at 32768Hz.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 07:07:48 pm
content: hm with 10pF I get nearly 1MHzI've got wires going all over the place though and my 10M resistor is made up of 10x 1M resistors so i guess I've made a real mess. I guess I should get some 10M resistors and then try veroboard

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 07:32:28 pm
content: Quote from: Simon on March 29, 2013, 07:07:48 pmMy 10M was actually 2x 4.7MTry going slightly higher with the caps if you can - you're trying to create a low-pass filter that blocks the harmonics - while still allowing phase inversion. You can play with the 22k resistor as well:22kOhms + 200pF is a 36kHz low-pass filter. I still have my breadboard assembled so if you get stuck and need some help, let me know.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 07:33:54 pm
content: Rechecking I miswired it all. will have another go and let you know

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 08:37:15 pm
content: Right got it going, 100pF and 150-160K. Only problem is it i jump around near the circuit I get a load of oscilations that are much higher so cause false fires, could it be because I have the osciloscope connected ?, It was coming from the first counter on the low part of the cycle so maybe lack of load on the output ?

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 08:52:52 pm
content: Quote from: Simon on March 29, 2013, 08:37:15 pmIf you have the DSO connected to the oscillator portion of the circuit (between pins 1 and 3 of 4541), you can affect the oscillation. Probably better to connect the DSO to the divider stage between chips (pin 8 on the 4541 should output 500mHz / 2 pulses per second) for final adjustment. CMOS doesn't normally require a load - it's output is a rail to rail swing.

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 09:14:17 pm
content: BTW, it is important to remember with CMOS to pull all unused inputs high or low.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 09:16:11 pm
content: I think it is a grounding problem cause by the scope. The problem is not when I'm probing the oscillator but when just looking at either output stage. The random oscillations are occurring on the output of the first stage while the output is low. I'm not overly concerend for now as i also have a mess of wires on the breadboard that might be causing a problem.My next job is to create the correct length pulse as a 1s pulse is too much, I'd rather have 50-100ms, I'm thinking a 555, but it seems the trigger pulse has to be shorter than the output pulse, they are suggesting AC coupling, I guess i'll need to use say a 2N7000 as a buffer between the counter and the 555 unless there are other tricks available to get the short pulse.

author: David_AVD
author_rank: Super Contributor
timestamp: March 29, 2013, 09:17:03 pm
content: Simon, this must surely be a "longest development time for a simple project" contender! LOL

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 09:34:59 pm
content: Quote from: Simon on March 29, 2013, 09:16:11 pmVery strange. CMOS is normally super-stable; I didn't have any problems with my breadboard once I got the crystal caps adjusted.QuoteSince you want a shorter output pulse than input, if you have a spare gate (or inverter), you can easily make an edge detector (half-monostable) circuit. See the bottom right ofthis diagramif you don't know what I mean. You just need a gate, cap, and resistor; for example, a 1M resistor and 100nF cap will give you an output pulse of ~80ms (T = .8RC).

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 10:05:36 pm
content: When I use to actually build these discrete logic circuits (doesn't happen much anymore), I use to throw a MC14572 into the design as backup - it has 4x inverters, 1x 2-input NOR gate, and 1x 2-input NAND gate - which can be used in all kinds of different ways as glue logic.

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 10:48:56 pm
content: I got it working with the 555, have the 100mS pulse, it is ticking nicely now but there is still instability with occasional bursts of clock that cuase the clock to tick more often than 30s, I'm now 3 minutes ahead in just under 15 minutes. Bearing in mind my breadboard is a mess of wires going up into the air and back again, it aint pretty so I guess that is a problem. Would I be best having a seperate clock gen ?I mean If i put in a chip full of not gates I can use some for the clock and one for the edge detector, my 555 uses 3ma according to the datasheet so at the least I'd need to use the 1S pulse from the second counter to act on the enable pin as well as trigger the 555, not sure if that can be done with such close timings. i guess the cmos not gates will not use 3mA regardless ?

author: Simon
author_rank: Unknown
timestamp: March 29, 2013, 10:50:27 pm
content: Quote from: David_AVD on March 29, 2013, 09:17:03 pmWell I took some time out due to other commitments, got around to ordering the parts and decided to do it this afternoon. I'm quite pleased with myself actually, working project in one afternoon but then I didn't design the counter circuitry so had a head start.

author: marmad
author_rank: Super Contributor
timestamp: March 29, 2013, 11:21:32 pm
content: Quote from: Simon on March 29, 2013, 10:48:56 pmWell, the crystal is oscillating through 5 gates inside the 4541 - as opposed to the usual single one. I didn't see any instability when I tested it - but I didn't run it for that long. As you mentioned, you can get around this problem - and add an edge detector - with a simple package of 4x NOR or NAND gates (or 6x inverters). Use one for the crystal oscillator, one as an output buffer between the oscillator and count input on the 4541 (to give it a nice full-amplitude square wave), one as the edge detector, and a spare to invert the edge detection output if needed (if you want a positive-going 50-100ms pulse).QuoteNo, not close to 3ma - it will usemuchless current then the 555.

author: Simon
author_rank: Unknown
timestamp: March 30, 2013, 08:43:02 am
content: I'll use a set of nots then or maybe nands, I'll see what is cheaper and stock up. What is a good series of chips these days, I guess a CD4000 does not actually exist any more.

author: Kremmen
author_rank: Super Contributor
timestamp: March 30, 2013, 08:48:43 am
content: Quote from: Simon on March 30, 2013, 08:43:02 amCD4000 maybe not, but HEF4000 is there and CD40106 at least is still kicking:http://www.digikey.com/product-search/en/integrated-circuits-ics/logic-gates-and-inverters/2556317?k=40106

author: mrflibble
author_rank: Super Contributor
timestamp: March 30, 2013, 09:01:24 am
content: As an aside ... if you DIY a oscillator from gates like this, then it can be advantageous to use a DFF at the end to serve as a divide by 2. Just connect nQ to D, and your oscillator output to CLK. That (mostly) takes care of any deviation from 50% duty cycyle you might have. Obviously you'll have to double the original clock frequency.

author: Simon
author_rank: Unknown
timestamp: March 30, 2013, 09:11:07 am
content: Do you mean putting in a flip flop to act as a /2 counter ? Not much point really. I can't double the frequency as it's a watch crystal and if i did I'd just alter the second stage to divide by less to compensate. If i were to make more of these I'd put a dip-switch on the second counter to make it flexible for different timings

author: mrflibble
author_rank: Super Contributor
timestamp: March 30, 2013, 09:55:53 am
content: Well, as stated this is only if you have control over the input frequency and can double it. Or compensate for the output frequency being half in some other way. If not, well then that's out.

author: marmad
author_rank: Super Contributor
timestamp: March 30, 2013, 10:18:50 am
content: @Simon -Here's a nice webpageabout the Pierce oscillator and choosing values for the various components. You'll want to use the second schematic from the top that includes the output buffer.

author: marmad
author_rank: Super Contributor
timestamp: March 30, 2013, 11:05:04 am
content: Quote from: Simon on March 30, 2013, 08:43:02 amIf you don't need supply voltages above 5V, I think the 74HC04 is about as cheap as they come - but it's been awhile since I bought them.Also,an interesting Fairchild application noteon CMOS oscillators.

author: Simon
author_rank: Unknown
timestamp: April 06, 2013, 09:34:46 am
content: Well I tried a single gate clock gen and it works on, I get a clean-ish signal but my output from the whole lots is still bursting all over the place. Oddly I notice that it seems associated with sounds perhaps.I've now setup with one channel on the clock, and one on the final output, the clock is fine, the output responds to sound levels from my speakers possibly of particular frequencies. At first I thought the crystal was acting as a mic but the clock it is producing is clean.

author: marmad
author_rank: Super Contributor
timestamp: April 06, 2013, 10:26:19 am
content: Quote from: Simon on April 06, 2013, 09:34:46 amWell, this is not how CMOS gates act normally; they're usually very stable. It seems to me it has to be a power supply problem; unused/untied/incorrectly wired input; or a breadboard specific problem. Use your DSO to check each input and output against the datasheets to make sure they're at the correct levels.Is your power supply clean? What voltage are you running the circuit at?

author: Simon
author_rank: Unknown
timestamp: April 06, 2013, 10:42:48 am
content: I've got it running off a 5V regulator, I'm wondering if it's pickup between wires as my breadboard looks like transmission central. I think I'll try veroboarding it and see if it settles down.I need to get a smidtt gate and hopefully the pulse generator works on it.