|
|
View previous topic :: View next topic |
Author |
Message |
Murali
Joined: 15 Feb 2007 Posts: 4
|
PWM operation |
Posted: Wed Apr 11, 2007 3:37 am |
|
|
I am trying to generate a clock using Timer or PWM.
In both the cases, i am finding the output frequency to be saturated after a level.
In PWM, the frequency is saturating at 57KHz.
The code used is
setup_timer_2(T2_DIV_BY_1, 22, 1);
set_pwm1_duty(46);
setup_ccp1(CCP_PWM);
XTAL = 24MHz, Configuration is OSC1/OSC2 -HS
As it is not working, I tried setting up Timer2 and in ISR I just toggled a port pin (Single statement).
The code used is
set_timer2(0);
setup_timer_2(T2_DIV_BY_1, 22, 1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
The frequency i get is only 75KHz.
Can anybody help me to identify the problem??? |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 11, 2007 4:31 am |
|
|
You are setting the 'on' time, to a figure higher than the available pulse width.
This line, says, that the maximum _10bit_ PWM resolutin, will be 88 counts:
setup_timer_2(T2_DIV_BY_1, 22, 1);
But you then set the pwm, using the _8bit_ resolution mode.
Change this:
set_pwm1_duty(46);
To:
set_pwm1_duty(46L);
Without the 'L', you are passing an 8bit value to the set_pwm1_duty function, which is automatically multiplied by 4, to give the required 10bit value. Adding the 'L', changes the value to a 16bit number, which then gives access to the full 10bit control.
Best Wishes |
|
|
Murali
Joined: 15 Feb 2007 Posts: 4
|
|
Posted: Wed Apr 11, 2007 4:40 am |
|
|
Thank You. I just did not think of that. PWM is working now.
But I am still thinking what happened to Timer2 clock generation techinque, which is simple. |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 11, 2007 6:43 am |
|
|
The problem is that your 'single statement', isn't.
First, 'toggling' a pin, implies reading it, modifying the read value, and then writing the result. If you are using 'standard_io', the read, and the write, will both involve a change to TRIS as well.
Then, calling an interrupt, has a very significant overhead. The compiler 'hides' this from you, unless you write your own 'int_global', but the default code, will involve saving about ten registers (more on 18 chips), then checking which source has actually interrupted, and calling the handler for this. Then when this returns, the interrupt flag is cleared, and all te registers restored,before returning to the main code. You can guess typically something like an extra 60 machine instructions...
Best Wishes |
|
|
Murali
Joined: 15 Feb 2007 Posts: 4
|
|
Posted: Wed Apr 11, 2007 7:45 pm |
|
|
Thank You Ttelmah,
Yes, I have seen that the compiler inserts many assembly codes.
Can u suggest any efficient way to generate a 2048KHz clock with timer operation. I also require 8KHz clock whose edge synchronized to 2048 KHz clock. This is for an audio codec (Winbond W681310)application. |
|
|
|
|
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
|