CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Dual clocks from one PIC12F675
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 07, 2009 3:18 pm     Reply with quote

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);
}
Guest








PostPosted: Wed Jul 08, 2009 7:01 am     Reply with quote

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



Joined: 03 Jun 2009
Posts: 28

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 7:40 am     Reply with quote

Oops, that was me above ^
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 2:10 pm     Reply with quote

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
marcusayoung



Joined: 03 Jun 2009
Posts: 28

View user's profile Send private message

PostPosted: Wed Jul 08, 2009 3:42 pm     Reply with quote

here is the link to the datasheet:

http://ww1.microchip.com/downloads/en/DeviceDoc/41211D_.pdf

page 1 (under Peripheral features) states: "- 10-bit PWM, max frequency 20 kHz."

But now i see on p. 79 where it lists a table for Timer Prescale, PR2, and Resolution values, and 20k max only applies to 10 bit resolution.

Its irrelevant now but its frustrating when you search through a sea of parts, considering all the things you need, and overlook stuff so easily. oh well...

but you guys are very helpful so its ok!
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jul 14, 2009 6:44 pm     Reply with quote

Something to keep in mind about the PIC18F is that they have the fast-interrupt feature that allows essentially a 3Tcy latency into the routine because of the dedicated save stack...

Read the rules in the datasheet carefully, but if the routine is short and doesn't use referenced arrays and some other caveats, It's pretty darned fast for an ISR.

Otherwise, a PIC with more than one PWM unit would work too. (or a 16bit timer with a counter.. again, fast interrupts with MINIMAL CODE..)

The 100KHz would be PWM while the 1Hz could be bit toggled (Make sure to use FAST IO too!)

Cheers,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
marcusayoung



Joined: 03 Jun 2009
Posts: 28

View user's profile Send private message

PostPosted: Thu Jul 16, 2009 2:30 pm     Reply with quote

PCM programmer wrote:
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);
}


I just tried this code with a 12F683, works great.

Now for all the trade-offs in my application...

For the variable 10KHz-100KHz via potentiometer, 1kHz increments (90 steps; is 6.5 bits even possible?) is not sufficient. The minimum resolution i would like to see is 7bits. 8bits would be great since i could simply utilize the lower ADC register. This might be possible with a 20MHz Fosc, but it doesn't look like the internal oscillator goes that high. Is it possible to do a piece wise function for the different frequency ranges/ resolutions if i'm stuck with 8MHz?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 16, 2009 3:02 pm     Reply with quote

You can do 10KHz to 100KHz with an 8 MHz crystal. It's just that, at the
upper frequency range, you will have a large step size. In other words,
with an 8 MHz crystal, this will give you 100 KHz:
Code:
setup_timer_2(T2_DIV_BY_1, 19, 1);   

And this will give you 10 KHz:
Quote:
setup_timer_2(T2_DIV_BY_1, 199, 1);

If you lower the frequency to the next step in the first line, you change
the middle parameter to 20. This is about a 5% change. You're changing
from 100 KHz down to 95 KHz.

In the 2nd line, the frequency step size will be about 0.5 %, because
your middle parameter is 199. Going to 198 is a much smaller % change
than going from 19 to 20.


If that's not satisfactory, then answer this question:
Is generating a frequency the only thing the PIC has to do ?
and nothing else at all ? In other words, no rs232, no watching switches,
no nothing.


Last edited by PCM programmer on Thu Jul 16, 2009 3:13 pm; edited 1 time in total
marcusayoung



Joined: 03 Jun 2009
Posts: 28

View user's profile Send private message

PostPosted: Thu Jul 16, 2009 3:13 pm     Reply with quote

Ok, i see what you mean.

Is "setup_timer_x()" and "set_pwm1_duty()" simply loading each register associated with the PWM module with the values i'm looking for according to the period and pulse width equations given in the datasheet? Does CCS have a database of their macros so i can look into what each one does?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 16, 2009 3:14 pm     Reply with quote

Put the compiler's List file format in "Symbolic" mode, and look at
the .LST file, while consulting the data sheet. This will show you
what it's doing.
marcusayoung



Joined: 03 Jun 2009
Posts: 28

View user's profile Send private message

PostPosted: Mon Jul 20, 2009 11:06 am     Reply with quote

ok, I figured it out. I achieved a 10khz-100khz oscillator with an acceptable resolution.

Quote:

If that's not satisfactory, then answer this question:
Is generating a frequency the only thing the PIC has to do ?
and nothing else at all ? In other words, no rs232, no watching switches,
no nothing.


Even though it was satisfactory, I still have to do one more critical task in determining the constants needed to generate the PWM signal. I'm still new to interrupts (I'm currently going through tutorials and app. notes related to these), but is it possible to "toggle" between two loops based on two modules of the PIC that last changed??

EG.
Code:

While (ADC){
calculate PWM
}

While (Timerx){
calculate PWM
}

... or conversely using if conditions?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group