View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
PWM issue |
Posted: Tue Jun 14, 2011 5:14 pm |
|
|
Hi There,
I'm using 200kHz PWM on a 16F883@20MHz. I set the timer value to 24 in order to get 200kHz and I'm left with very few settings for the pwm (from 0% to 100%). Is there any way I can get a better resolution?
My sample code is below:
Code: | #include <16F883.h>
#device adc=10 WRITE_EEPROM=ASYNC
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
#use delay(clock=20000000)
void main(void)
{
setup_timer_2(T2_DIV_BY_1,24,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(20); //80%
set_pwm2_duty(20); //80%
while (1);
}
|
Thanks for hints and suggestions!!!!
Ron |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 14, 2011 5:27 pm |
|
|
Use 10-bit PWM mode. You will get 4x the resolution of 8-bit mode.
To enable 10-bit mode, use a 'long' (int16) value for the duty cycle.
For a constant, the easy way to declare it as a 'long' is to put an 'L'
on the end. Try the following and look at your scope to see what happens:
Code: |
set_pwm1_duty(20L); |
|
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Tue Jun 14, 2011 6:11 pm |
|
|
NICE! Yes, exactly! Cool, thanks! I read something in the docu about that, and tried e.g. 0x001 and thought it would be represented as a 12bit number but i needed a long to make it work. Thanks!!! |
|
|
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
|
Posted: Mon Jun 20, 2011 6:37 pm |
|
|
PCM programmer wrote: | Use 10-bit PWM mode. You will get 4x the resolution of 8-bit mode.
To enable 10-bit mode, use a 'long' (int16) value for the duty cycle.
For a constant, the easy way to declare it as a 'long' is to put an 'L'
on the end. Try the following and look at your scope to see what happens:
Code: |
set_pwm1_duty(20L); |
|
Sir, the 4x resolution mean if want to get 80% of 24 = 20, in 10bit mode need to times with 4, 20*4=80, not like this? last time i saw other code with times 4, if i wrong please tell me what is wrong, i newbie
set_pwm1_duty(80L); |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Mon Jun 20, 2011 8:32 pm |
|
|
I think Code: | set_pwm1_duty(80L); | should do what you anticipate. |
|
|
|