|
|
View previous topic :: View next topic |
Author |
Message |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
no change in period of generated square wave with changing.. |
Posted: Thu Feb 22, 2018 8:37 am |
|
|
Code: |
#include <18f4431.h>
#fuses XT,NOWDT,NOLVP,HS
#use delay(clock=20000000)
#INT_TIMER2
void pwm_interrupt()
{
output_toggle(PIN_C0);
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts (INT_TIMER2);
setup_timer_2(T2_DIV_BY_16,255,1);
while(1)
{
}
}
|
Hello! The above program is produce a square wave shown in the attached image. The value of X=4mS. Now if I change delay clock from 20 MHz to some other value, then the value should also change. But this is not happening. I mean there is no change in X if I change delay clock from 20 MHz. I would like to know why?
Thanks in advance!
Last edited by PROMOD on Thu Feb 22, 2018 11:13 am; edited 2 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Thu Feb 22, 2018 9:03 am |
|
|
Of course it won't.
The clock setting does not change the frequency of anything. You set it to match what your _hardware_ is running at (there is an exception here if using an internal clock).
You also have both 'XT' (oscillator for a 4MHz max crystal), and HS selected.
To change the frequency, you need to change the PR2 value (the middle value fed to the setup_timer_2 function). Change this to 127 to double the frequency.
However I also have to say 'why use the timer interrupt'. If your chip has a PWM use the PWM hardware. You don't tell us what chip, so we chan't be sure on this. |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Thu Feb 22, 2018 11:27 am |
|
|
Ttelmah wrote: | You set it to match what your _hardware_ is running at (there is an exception here if using an internal clock). | Could you please give me a example?
Ttelmah wrote: | You also have both 'XT' (oscillator for a 4MHz max crystal), and HS selected. | Would you please explain it more why have you written this information? Do you mean that if no crystal is added externally then by default it works 4Mhz?
Ttelmah wrote: | To change the frequency, you need to change the PR2 value (the middle value fed to the setup_timer_2 function). Change this to 127 to double the frequency. | Is there any other option to change the frequency other than using prescaler, PR2, postscaler, and PWM?
Ttelmah wrote: | However I also have to say 'why use the timer interrupt'. If your chip has a PWM use the PWM hardware. You don't tell us what chip, so we chan't be sure on this. | I have edited my original post for the device. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Thu Feb 22, 2018 12:23 pm |
|
|
The clock statement says to the compiler 'This device has a clock at xxxx'.
To run at 20MHz, you chip needs a 20MHz crystal.
The clock statement needs to match what your chip actually has.
You need to read the chip's data sheet. XT, is the fuse used when the chip has a crystal up to a maximum of 4MHz. HS is the fuse for a crystal over this.
If you don't have a crystal, you don't need either of these oscillators. Instead:
Code: |
#include <18f4431.h>
#fuses NOWDT,NOLVP
#use delay(INTERNAL=8MHz)
|
The internal oscillator supports operation at 1MHz, 2MHz, 4MHz & 8MHz (and ones below down to 125KHz). _Data sheet_.
Currently your chip has FCMEN (fail safe clock monitor) enabled by default, which if the specified oscillator does not work, automatically switches to the internal oscillator at 4MHz. Because you have not got a crystal, this is the speed you are currently running.
4MHz/(4*16*256) = 244.14* per second = 4.096mSec
Just change PR2. That is what it is there for...
The advantage of using the PWM, rather than the interrupt is that at the moment it is using typically perhaps 70 instructions to go into the interrupt and out again. The PWM generates the clock without this overhead. However you can just change the PR2 value, and carry on using the interrupt.
You can change the oscillator frequency on the fly, but only to the frequencies supported. Look at the 'setup_oscillator' function, and the values for this in the processor include file.
If you want just a programmable clock output, then chips like the 16F1717 have a programmable NCO. |
|
|
|
|
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
|