View previous topic :: View next topic |
Author |
Message |
nehallove
Joined: 16 Jan 2008 Posts: 61
|
PWM problem in PIC18F25K20 |
Posted: Tue Nov 03, 2009 5:34 pm |
|
|
Hi Everyone,
I am having trouble with setting pwm on the PIC25K20.
Code: |
setup_oscillator(OSC_1MHZ); //! oscillator freq is 1 MHz
enable_interrupts(GLOBAL); //! enable global interrupt
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 255, 14);
set_pwm1_duty(235);
while(1);
|
In setup_timer_2 even if I set anything from 1 to 16 as the postscale. Total time period is always stays at 16.2 mS. There is no effect of the postscale.
When I try to change the period from 0-255. None of them actually work and change the period value. Only 255 value works.
Please throw some light on this issue.
Thank you,
nehal _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 03, 2009 6:15 pm |
|
|
Quote: |
In setup_timer_2 even if I set anything from 1 to 16 as the postscale.
Total time period is always stays at 16.2 mS. There is no effect of the postscale. |
See this thread which explains the PWM postscaler:
http://www.ccsinfo.com/forum/viewtopic.php?t=29786
Quote: |
When I try to change the period from 0-255. None of them actually work
and change the period value. Only 255 value works.
|
How are you measuring this ? With an oscilloscope ?
See this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=39786&start=1
If that doesn't help, then:
Post your own code (a short but complete program) similar to the code
shown in the link above. The link shows two PWM channels. If you're
only using one, then just post the code for that. But show the #include,
#fuses, #use delay, main(), etc. It must compile with no errors (test it).
Also post your compiler version. |
|
|
Guest
|
|
Posted: Wed Nov 04, 2009 1:39 pm |
|
|
Hi PCM programmer,
Thank you for quick reply. Now I got the idea behind the postscaler but still that value of the period in timer settings still confuse me.
Code: |
setup_timer_2(T2_DIV_BY_16, 255, 1);
|
Here in this settings only value of 255 for the period works. Other than that value pwm waveform is not produced. I am checking the pwm wavefore with the oscilloscope.
Here is my code with the fuses.
Code: | #include <18F25K20.h>
//*#include <18F2520.h>
#device *=16 ICD=TRUE // Use 16 bit pointers, enable ICD
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC //Internal RC Osc
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
//#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES DEBUG //Debug mode for use with ICD
#FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
//#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=1000000) |
Code: |
setup_oscillator(OSC_1MHZ); //! oscillator freq is 1 MHz
//pwm settings for the led
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 255, 1);
set_pwm1_duty(235);
while(1);
|
I also try your code in the link but has the same problem.
Thank you,
nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 04, 2009 1:50 pm |
|
|
Quote: | Also post your compiler version. |
|
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Wed Nov 04, 2009 3:05 pm |
|
|
compiler version is "4.083" _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 04, 2009 4:38 pm |
|
|
I think it probably should work. Are you sure you're looking at pin C2
for the output ?
Also, I notice you didn't post a complete test program. You left out
the main(), and any code in main. Post a complete program that is
very short, like this one. (and test it). Don't use DIV_BY_16.
Use div by 1, as shown below.
Code: |
#include <18F25K20.h>
#fuses INTRC_IO, NOWDT, NOLVP
#use delay(clock=1000000)
//======================================
void main()
{
setup_oscillator(OSC_1MHZ);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(235);
while(1);
} |
|
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Wed Nov 04, 2009 5:14 pm |
|
|
Code: | #include <18F25K20.h>
#fuses INTRC_IO, NOWDT, NOLVP
#use delay(clock=1000000)
//======================================
void main()
{
setup_oscillator(OSC_1MHZ);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 100, 1);
set_pwm1_duty(235);
while(1);
}
|
I put the same code as give it to me and I tried to change the period value from 255 to some other values. e.g. 100 over here. But it doesn't work. I am checking port RC2 with my oscilloscope.
Thank you,
nehal _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 04, 2009 5:34 pm |
|
|
Are you testing this in a real hardware board, or is this Proteus ? |
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Wed Nov 04, 2009 5:56 pm |
|
|
testing on PICDEM 2 Plus prototype board. _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 04, 2009 6:42 pm |
|
|
I assume you're trying to drive the piezo buzzer on pin C2 (with jumper
J9 installed).
Try using a different type of PIC. See if it works. |
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 05, 2009 3:17 am |
|
|
You do realise that with all the last few posts, you won't get anything except a 'high' out of the PWM.....
The value given to the set_pwmX_duty function, can either be an 8bit integer, or a 'long' (16bit) integer. If an 8bit value is used, it is effectively multiplied by 4 before use. To make a constant into a 16bit value, add the letter 'L' after the number.
As it stands, you have a timer value of 100, allowing PWM width values from 0 to 403, and are giving it a width value of 940 (235*4).....
I posted details about this only recently, and several times in the past.
Best Wishes |
|
|
|