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

Simple question on how to calculate duty for pwm to motors

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 25, 2010 4:31 pm     Reply with quote

Here's an example program. When I turn the trimpot from 0 to +5v,
the PWM duty cycle goes from 0 to 100%.

This line calculates the duty cycle value for the set_pwm1_duty() function:
Quote:

duty = (adc_result / 255.0) * (PR2_VALUE +1);

The expression shown in bold changes from 0 to 1.0, when I turn the
knob on the trimpot. In terms of a percentage, that's from 0 to 100%.

If you want to use a percentage number (1 to 100), you will need
to divide it by 100 (to get 0 to 1.0) so it would be the same range as
the expression shown in bold above.
Code:

#include <16F877.H>
#device adc=8
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define PR2_VALUE 254

//==========================================
void main()
{   
int8 adc_result;
int8 duty;
 
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);

setup_ccp1(CCP_PWM);       
setup_timer_2(T2_DIV_BY_1, PR2_VALUE, 1);
set_pwm1_duty(0);

while(1)
  {
   adc_result = read_adc();
   duty = (adc_result / 255.0) * (PR2_VALUE +1);
   set_pwm1_duty(duty);
  }
       
}
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