View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
External clock from Microcontroller |
Posted: Wed Feb 10, 2016 4:27 am |
|
|
Hi everyone
Is there any pic microcontroller which can generate a clock signal and this clock signal will be fed to other circuits?
Thanks and best regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Wed Feb 10, 2016 5:12 am |
|
|
Almost all of them....
However depends on what speed you need.
The PWM output from any PIC with a PWM, can generate a clock up to Fosc/4., but only in quite large 'steps'. There are a couple of PIC16's with programmable frequency generators.
I have a PIC30, currently running a programmable bit of logic that needs a 1.82MHz, and 2.5Mhz clock. Two PWM's from the PIC30, merrily develop these, and, and to quite reasonable accuracies, but with the master clock at 60MHz, these both have reasonable divisions involved. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Wed Feb 10, 2016 5:31 am |
|
|
Thanks Ttelmah
Are you talking about the OUTPUT COMPARE section?
I have PIC24HJ128GP306 microcontroller. Have this controller the option of programmable frequency generator?
What max clock speed i can achieve if i use 20Mhz external clock?
Thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9232 Location: Greensville,Ontario
|
|
Posted: Wed Feb 10, 2016 6:27 am |
|
|
hmm.. programmable frequency generator?
Sounds like a great use for a PIC with NCO ??
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Wed Feb 10, 2016 10:11 am |
|
|
I don't think any of the PIC24's have the frequency generator option. As I said, some of the PIC16's. Chips like the 16F18854. As Jay says, chips with the NCO.
On the PIC24, are you actually running at 20MHz?. Remember these chips have a programmable PLL, and the output frequency from this allowed is 12.5MHz to 80Mhz. You could be running at almost any frequency in this range, and it is the clock speed out of the PLL that determines the PWM resolution and your CPU clock rate. The PWM frequency/resolution is programmed in Tcy steps (so Fosc/2).
On your chip, go and check the PWM resolution. Remember this is at the maximum clock rate supported. I think it'll be 25nSec at 80MHz, so 100nSec at 20MHz. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Feb 11, 2016 1:02 am |
|
|
Thanks Ttelmah
Quote: |
On your chip, go and check the PWM resolution.
|
The resolution of PWM is 16-bit
I tried a code to generate the PWM of 50% duty cycle.But the calculated values and real world PWM does not match.I have done the following calculations
Quote: |
Fosc = 20MHz -> Fcy = Fosc/2 = 10 MHz
Tcy = 1/Fcy = 100nS
According to the formula given in datasheet of Microcontroller
PWM Frequency = 1/PWM Period
and
PWM Period = (PR2 + 1) * TCY * (Timer2 Prescale Value)
if i set the value of PR2 = 65472 and Timer2 Prescale Value = 1
then the value of PWM Period will be 6.54mS
and the value of PWM Frequency = 152.73Hz
|
But i am getting a Period of 57.34Hz, checked with oscilloscope.
My code is
Code: |
#include <24HJ128GP306.h>
#fuses HS,NOWDT
#use delay(clock=20M)
void main()
{
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 0x0FFC0);
setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
set_pwm_duty(1,0x8000);// 50% duty cycle
while(TRUE)
{
}
}
|
Where i am doing a mistake? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Thu Feb 11, 2016 3:05 am |
|
|
You are not running at 20MHz.....
You don't have the primary oscillator enabled. So it is not running, and the chip is 'failing safe', and dropping back to the internal oscillator at 7.37MHz. So 56.28Hz, +/- the error on the internal oscillator.
This is why we repeat again and again, if having a timing issue always start with the basic 'flash an LED' test.
On these chips as well as 'selecting' the crystal, you need to enable the primary oscillator. Add the 'PR' fuse. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Feb 11, 2016 5:46 am |
|
|
Thanks Ttelmah
I enabled the primary oscillator and it is working well with the provided value.I also toggling an LED with 1 sec time interval.
But the problem is, PWM is working on only one value i-e 65535 or 0xFFFF.
On other values it is not working.
I have calculated different values and put these values one by one but no response.Output is always HIGH except with the value 65535.
Calculated values are in the below link
[img]http://postimg.org/image/8uht4c0h9/[/img] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Thu Feb 11, 2016 6:19 am |
|
|
Er. But you say you have got it working with FFC0 above?.
You do understand that it is the timer you have to change to change the duty here. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Feb 11, 2016 6:33 am |
|
|
Yes with a value of 0xFFC0 and onwards i am getting frequency of approx 152 Hz with 50% duty cycle.
If i need 2.4KHz freq with a 50% duty cycle and prescalar value is 1, should i load 4095 value? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1351
|
|
Posted: Thu Feb 11, 2016 8:30 am |
|
|
Also keep in mind that some of the PIC24 pics need additional configurations options to run the PWM. For example on a PIC24FJ that I use, I have to do more than just:
setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
I need to add in a parameter to tell it to sync to timer2 as well (I want to say it was something like COMPARE_SYNC_TIMER2 or something similar). Before I did that, changes to the timer period had no effect on the PWM frequency. I am not familiar with your particular PIC, so you may want to double check the configuration settings for your PWM in the datasheet. |
|
|
|