CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PWM issue

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PWM issue
PostPosted: Tue Jun 14, 2011 5:14 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 14, 2011 5:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 14, 2011 6:11 pm     Reply with quote

NICE! Yes, exactly! Cool, thanks! Smile 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

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 20, 2011 6:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 8:32 pm     Reply with quote

I think
Code:
set_pwm1_duty(80L);
should do what you anticipate.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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