View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Changing the freq. |
Posted: Mon Mar 27, 2006 5:58 am |
|
|
Im making freq. changes from 1kHZ to 9 KHZ each second.
When the cycle is over, i start from begining.
Working freq. of the chip is 4 MHZ.
This is now conected like this:
PIC -- > freq. changer --> buzzer
Now, this may be a realy dump question, but what a hell :-)
Can i genarate this freq. changes diretcly on the chip so i can skip the HW freq. changer part ?
Like this :
PIC -- > buzzer
Thanx in advance ! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Mar 27, 2006 6:35 am |
|
|
Basically you can do this but all depends on the required accuracy, resolution and type of waveform.
Please provide more details like the type of waveform and what you are using the signal for. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Mar 27, 2006 8:55 am |
|
|
Are you trying to make a sound like a siren? That is much easier in software than in hardware. A PIC should be able to do it easily. Look up a "numerically controlled oscillator", except that you don't need to worry about converting to a sine wave. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Guest Guest
|
|
Posted: Mon Mar 27, 2006 11:30 am |
|
|
Thnx for replay guys !
Well, i need 25 to 35 kHZ freq.
I think i will add each half a second some 5 kHZ ( till 35 ) and then again.
This is the basic idea. Maybe i will use it on some other freq, in the future. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Mar 27, 2006 11:47 am |
|
|
There are several possible approaches to building a frequency generator, but in order to help you best we need to know more details. For example when you are building a siren for use in a toy or something like that you don't care a lot about the frequency being of by a few Herz now and then. When building a meassurement device you would have much tighter requirements.
In case of the afforementioned toy siren you wouldn't care a lot about the actual waveform and we would suggest the easy to generate square wave. For testing audio equipment however you need sinus waves and other applications might require sawtooth waveforms.
Everything is possible, but before we start generating a lot of code that is no use to you and a waste of our time, it would be better for you to give us some more specific details. |
|
|
Guest
|
|
Posted: Tue Mar 28, 2006 3:42 am |
|
|
OK. :-).
I want to generate freq. from 25 , 30, 35 Khz.
The value will rise each time, for 5 kHz.
I dont need any 100% correct values, but it would be nice :-)
There should be low power consumption ( nanowats ) and wenn you start the device, he will genarate this freq. ( endless , till shut down ).
Do you need anything else ? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Mar 28, 2006 5:10 am |
|
|
Quote: | Do you need anything else ? | I only asked this twice before, what shape of the waveform do you want?
The nanowats requirement is new. Considering the minimal requirements, are you sure you want to use a PIC? You can build this circuit with a few discrete components in less time and it will be more power efficient. |
|
|
Guest
|
|
Posted: Tue Mar 28, 2006 5:17 am |
|
|
Quote: | I only asked this twice before, what shape of the waveform do you want? | Im realy sorry, i dont know how i miss it.
I didnt get any special requests so we can choose the simpler version ( we dont have to convert to sin wave).
Quote: | Considering the minimal requirements, are you sure you want to use a PIC | Yes. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Mar 28, 2006 5:57 am |
|
|
The easiest waveform to use will be a square wave.
Generating the square wave can be implemented in several ways:
1) Toggling an output pin in a software loop (simple)
2) Toggle output pin in a timer based interrupt (medium difficulty)
3) CCP module (medium difficulty)
I would go for the CCP module as it has a very flexible counter and the capability of running in the background not interferring with your main program. Some newer chips allow you to save power by having your main program go to sleep with the CCP module still running.
Next thing you have to decide on is hardware:
- The chip(family)
- clock frequency. The lower the clock frequency the better for current consumption.
- Supply voltage. A Low voltage like 3.0V is a great way of reducing power consumption.
I don't have a clue as to what other tasks you want the processor perform so I let it up to you to decide on these issues. |
|
|
Guest
|
|
Posted: Tue Mar 28, 2006 10:01 am |
|
|
OK.
I will take CCP and the 16F913 chip becuse he can work with nanowats.
Should there be any additional problems regarding this config. ? |
|
|
Guest Guest
|
|
Posted: Thu Apr 06, 2006 5:30 am |
|
|
on 8 MHZ this should trigger 7,81 KHz ?
setup_timer_2(T2_DIV_BY_1, 255, 1); // 8000000/1024
If i would not use prescaller then i can reach 8000000 / 256 = 31,25 Khz which is not the max value that i need...
Do i miss something or am i going in wrong way .... |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 06, 2006 6:30 am |
|
|
You can't 'not use the prescaler'. It is always there. You can use a division by 1, but it is still fed by Fosc/4. However you can just reduce the comparator value. So:
setup_timer_2(T2_DIV_BY_1, 49, 1);
will trigger at 40KHz.
However remember that when this triggers, you need time to do things, and in this case, you would only have 50 instruction times between triggers. Not enough time to service an interrupt (but probably enough, if you 'poll' the interrupt flag, and then do something very simple).
Realistically, if you need to do much in the way of calculation/adjustment between cycles, then you need to use a higher clock frequency. Remember also though, that you can use the postscaler to reduce how often the interrupt occurs, while still keeping a faster PWM output, to give you time to do things.
Best Wishes |
|
|
guest Guest
|
|
Posted: Wed Apr 26, 2006 5:00 am |
|
|
Code: | #include <16F913.h>
#fuses NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
//------------------------------------------------------------------------------------
void main (void)
{
int period, duty;
do{
period = 100;
duty = 20;
pulse(period, duty);
}while(true);
}
//---------------------------------------------------------------------------------------
void pulse(int period, int duty){
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 5, 1); // 20 KhZ
delay_ms(250);
output_high(pin_B0);
setup_timer_2(T2_DIV_BY_1, 4, 1); // 25 KhZ
delay_ms(250);
output_high(pin_B1);
setup_timer_2(T2_DIV_BY_1, 3, 1); // 33 Khz // i need 30 KhZ
delay_ms(250);
output_high(pin_B2);
setup_timer_2(T2_DIV_BY_1, 2, 1); // 50 KHZ // i need 35 KhZ
delay_ms(250);
output_high(pin_B3);
output_low(pin_b0); output_low(pin_b1);
output_low(pin_b2); output_low(pin_b3);
//set_pwm1_duty(duty);
}
/*
//-- problems
1.) Accept getting the right freq. i get all the time the "klick" signal on my speaker. How to remove this. The operating freq is 4MHZ !
2.) I put led indicators but is there some way to measure this freq.
*/
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Apr 26, 2006 6:01 am |
|
|
Code: | setup_timer_2(T2_DIV_BY_1, 2, 1); // 50 KHZ // i need 35 KhZ | Are you sure you have a 4MHz clock? This looks more like a 400kHz clock.
Please add the correct flags for your clock to the #fuses line. |
|
|
Guest Guest
|
|
Posted: Wed Apr 26, 2006 6:22 am |
|
|
Are you sure you have a 4MHz clock?
* Yes, external 4mhz osc. Regarding to pcm programmer replay in other post i add EC_IO ( cuz im using PICDEM too ) to fuses so now this looks like:
#fuses EC_IO,XT,NOWDT, NOPROTECT, BROWNOUT, PUT
I still have problem with freq calculations but now
i get two start clicks, and after that it's OK.
Its getting better and better :-)) |
|
|
|