View previous topic :: View next topic |
Author |
Message |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
Dual clocks from one PIC12F675 |
Posted: Thu Jul 02, 2009 3:13 pm |
|
|
Hi All,
I'm pretty sure this is such a novice code question, but I can't seem to wrap my head around it.
I would like to generate two clocks from one PIC. The usual method I'm using is the delay function, where I turn on a GPIO on for some delay time and conversely for turning it off, all in a forever loop. The problem I'm having is I can't think of how to do this for two different frequencies (because I obviously can't use the aforementioned method twice in the same forever loop).
So, would I generate one clock and pass it back into the pic, then use some One Shot type algorithm (for a smaller freq. clock) and drive it out of another GPIO maybe (This seems to use too many pins)? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 02, 2009 3:21 pm |
|
|
1. What are the two clock frequencies ?
2. What is the desired duty cycle for each one ?
3. What is the oscillator frequency of the PIC ? |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Thu Jul 02, 2009 3:22 pm |
|
|
duty cycle = 50% for each.
One frequency should be 10KHz, the other should be 1Hz. |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Thu Jul 02, 2009 3:23 pm |
|
|
sorry...PIC is running at 4.3MHz |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 02, 2009 3:45 pm |
|
|
You could do it with a timer interrupt and set the interrupt rate for
20 KHz. Then toggle the 10 KHz pin each time there is an interrupt.
You can increment a 16-bit static counter inside the timer isr,
and when it counts up to 10K, you can toggle the 1 Hz pin.
The problem is that the interrupt handler takes too many instruction
cycles for it to be able to service the interrupt at a 20 KHz rate
with your 4.3 MHz oscillator frequency. A 20 KHz interrupt occurs
every 50 us. You could do it with an #int_global handler. That would
considerably shorten the overhead time for the interrupt. But it's more
complicated to write. |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Thu Jul 02, 2009 3:54 pm |
|
|
Hmmm, sounds tricky.
I know the 4.3MHz is kinda strange, but i've found that it outputs a more accurate 1Hz square wave, so ideally i'd like to keep using the method i explained above for this signal.
Aside from that, the datasheet says GP4 is a CLKOUT, might it be possible to to set 10K_CLK = INTERNAL_OSC/CONSTANT? or am i looking at the same problem as before? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 02, 2009 4:23 pm |
|
|
Quote: |
I know the 4.3MHz is kinda strange, but i've found that it outputs a more
accurate 1Hz square wave, |
That means you already have a existing method for outputting a 1 Hz
square wave. What is it ?
Quote: |
might it be possible to to set 10K_CLK = INTERNAL_OSC/CONSTANT? |
With an external crystal, the CLKOUT pin is part of the crystal oscillator
circuit. With the internal oscillator, the CLKOUT pin is Fosc/4 (1 MHz). |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
if space or a tiny bit more power is no problem |
Posted: Fri Jul 03, 2009 6:50 pm |
|
|
This problem has other solutions - in my practice - designs are blend of hardware and PIC - letting each do what it does best.
If you want to accomplish anything ELSE with that PIC of yours - here is a way to do it with zero code space required.
You see, this problem is PERFECTLY solvable with maybe $3 worth of HARDWARE MAX- and with perfect results.
Unless your (PCB_Space|| budget) =0
you might check the data sheets for the cmos (CD) 4518 or the 74hc390
and several other similar parts - google "DUAL DECADE COUNTER" for many others.
Two of these - wired properly is under $1 @ purchase quan =1
and you can have your choice of synchronous or ripple divide by 10,000.
*Heck* add one MORE of these chips and a 1 mhz oscillator -
and here is your freq source
+ plus a bonus of every other divide by 10,2 and 5 - sub multiple
between 1 mhz and 1 hz.
In production this whole shooting match would run about $1.60 for 10K quantities. |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Tue Jul 07, 2009 12:58 pm |
|
|
In a similar question, but now disregarding the 1Hz solution for now...
Is it possible to achieve a variable 10kHz-100kHz (50% duty cycle, amplitude irrelevant) through the use of a potentiometer/ADC or is there just WAY too much overhead involved? I am using a PIC12F675.
I know its possible to go that high, I achieved it using NOP statements (@4MHz clock). But to make it variable, combined with reading the ADC is seemingly impossible. Even if I used the available counters to reduce overhead I might not be saving much. Its not clear if the 20MHz available internal clock will be high enough.
thoughts? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 07, 2009 1:18 pm |
|
|
If you use a PIC that has a hardware PWM module (i.e. a CCP), then it's
easy to do. Here's an example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=26308&start=5
Do you have to use the 12F675 ?
Does the 12F675 program have to do anything else, other than
reading the ADC and bit-banging your output frequency ? |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Tue Jul 07, 2009 1:40 pm |
|
|
The whole goal of the program is to produce the aforementioned variable clock based on one or the other of two registers: the ADC or a TIMERX. I was planning on working out the "one or the other" part using the Interrupt On Change register to determine which input to base the clock time constant off of. One pin will have a potentiometer (ADC), another will have a momentary switch (TIMERX). The timer will begin counting after one push of the switch and stop counting/ acquire it's value when the switch is pressed again.
No, I do not have to use the 12F675, its just what i have. The 12F683 has a PWM, but the datasheet states "10-bit PWM, max frequency 20 kHz" :/
It doesn't look like the other 8-bit series devices can do any better either.
But i still have to wonder if there's a work around since i got a 100kHz clock out of my 12F675 at 4MHz... |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 07, 2009 3:18 pm |
|
|
You need to do some physical tests. I just now programmed a 12F683
to do a 100 KHz square wave, 50% duty cycle, with the PWM module.
I can see the signal on my oscilloscope. However, one problem with
the program below is that it can't smoothly do every frequency in your
desired range. What is the minimum frequency step size that you
require ? 1 KHz ?
Code: | #include <12F683.h>
#fuses INTRC_IO, NOWDT, MCLR, PUT, BROWNOUT
#use delay(clock=4000000)
//=============================
void main()
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 9, 1);
set_pwm1_duty(5);
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Jul 08, 2009 7:01 am |
|
|
Ok, looks like i'll have to give the 12F683 a try. Thanks for trying that out for me.
I wonder why you were able to get 100kHz (so easily?) when Microchip is claiming a 20kHz max on that feature? I've read many a datasheet before, so i understand how misleading they can be sometimes... |
|
![](templates/subSilver/images/spacer.gif) |
marcusayoung
Joined: 03 Jun 2009 Posts: 28
|
|
Posted: Wed Jul 08, 2009 7:40 am |
|
|
Oops, that was me above ^ |
|
![](templates/subSilver/images/spacer.gif) |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Wed Jul 08, 2009 2:10 pm |
|
|
Quote: | ...I wonder why you were able to get 100kHz (so easily?) when Microchip is claiming a 20kHz max on that feature? I've read many a datasheet before, so i understand how misleading they can be sometimes... |
Where did you see this 20kHz limit on PWM frequency? I think you misread something. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
![](templates/subSilver/images/spacer.gif) |
|