Author Topic: counter required to count to near 1 million  (Read 30556 times)

0 Members and 1 Guest are viewing this topic.

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #75 on: March 19, 2013, 05:48:23 pm »
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.

Just 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.
 

Offline olsenn

  • Frequent Contributor
  • **
  • Posts: 993
Re: counter required to count to near 1 million
« Reply #76 on: March 19, 2013, 08:19:49 pm »
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[15];

/* 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/Counter0
   TCCR0A = 0;               // Normal Mode
   TCCR0B = 1;               // No Prescaler
   TIMSK0 = 1;               // Enable Overflow Interrupt
   TCNT0 = 0;
   sei();
   
   /* Timer/Counter1 - Counter */
    PRR = (0<<PRTIM1);        // Power-on Timer/Counter1
   TCCR1A = 0;             
   TCCR1B = 7;               // Normal Mode, T1-Pin as Clk
   TIMSK1 = 1;               // Enable Overflow Interrupt
   TCNT1 = 0;                // Initialize Counter
   sei();                    // 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;
}
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: counter required to count to near 1 million
« Reply #77 on: March 19, 2013, 08:22:36 pm »
Nice, clean code. I like it. Not a single line that I have a question about.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #78 on: March 29, 2013, 06:16:38 pm »
@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 fun  ;)

Edit: 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.

Hm, 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 ?
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #79 on: March 29, 2013, 06:45:40 pm »
Hm, 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 ?

Yes, 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.
« Last Edit: March 29, 2013, 06:51:10 pm by marmad »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #80 on: March 29, 2013, 06:55:04 pm »
ok, found my problem, I'm oscilating at about 200KHz, may i assume due to the breadboard concoction ?
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #81 on: March 29, 2013, 06:59:32 pm »
ok, found my problem, I'm oscilating at about 200KHz, may i assume due to the breadboard concoction ?

Yes - 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.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #82 on: March 29, 2013, 07:07:48 pm »
hm with 10pF I get nearly 1MHz  ;)

I'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
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #83 on: March 29, 2013, 07:32:28 pm »
hm with 10pF I get nearly 1MHz  ;)

I'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
My 10M was actually 2x 4.7M  :)   Try 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.
« Last Edit: March 29, 2013, 07:34:18 pm by marmad »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #84 on: March 29, 2013, 07:33:54 pm »
Rechecking I miswired it all. will have another go and let you know
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #85 on: March 29, 2013, 08:37:15 pm »
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 ?
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #86 on: March 29, 2013, 08:52:52 pm »
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 ?

If 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.
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #87 on: March 29, 2013, 09:14:17 pm »
BTW, it is important to remember with CMOS to pull all unused inputs high or low.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #88 on: March 29, 2013, 09:16:11 pm »
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.
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: counter required to count to near 1 million
« Reply #89 on: March 29, 2013, 09:17:03 pm »
Simon, this must surely be a "longest development time for a simple project" contender!  LOL
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #90 on: March 29, 2013, 09:34:59 pm »
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.
Very strange. CMOS is normally super-stable; I didn't have any problems with my breadboard once I got the crystal caps adjusted.

Quote
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.
Since 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 of this diagram if 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).
« Last Edit: March 29, 2013, 09:53:57 pm by marmad »
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #91 on: March 29, 2013, 10:05:36 pm »
When I use to actually build these discrete logic circuits (doesn't happen much anymore  ;D ), 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.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #92 on: March 29, 2013, 10:48:56 pm »
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 ?
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #93 on: March 29, 2013, 10:50:27 pm »
Simon, this must surely be a "longest development time for a simple project" contender!  LOL

Well 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.
 

Offline marmad

  • Super Contributor
  • ***
  • Posts: 2979
  • Country: aq
    • DaysAlive
Re: counter required to count to near 1 million
« Reply #94 on: March 29, 2013, 11:21:32 pm »
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 ?

Well, 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).

Quote
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 ?

No, not close to 3ma - it will use much less current then the 555.
« Last Edit: March 29, 2013, 11:48:47 pm by marmad »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #95 on: March 30, 2013, 08:43:02 am »
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.

 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: counter required to count to near 1 million
« Reply #96 on: March 30, 2013, 08:48:43 am »
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.
CD4000 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
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: counter required to count to near 1 million
« Reply #97 on: March 30, 2013, 09:01:24 am »
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. ;)
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17729
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: counter required to count to near 1 million
« Reply #98 on: March 30, 2013, 09:11:07 am »
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
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: counter required to count to near 1 million
« Reply #99 on: March 30, 2013, 09:55:53 am »
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. :P
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf