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 CCS Technical Support

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
zonemikel



Joined: 13 Oct 2007
Posts: 53
Location: Texas

View user's profile Send private message Visit poster's website Yahoo Messenger

Simple question on how to calculate duty for pwm to motors
PostPosted: Sun Jul 25, 2010 11:55 am     Reply with quote

Hello everyone this is my motors.h file. It is taken mostly from a post i found on these forums.

Code:
// These are the PWM output pins for the 16F887.
#define P1A PIN_C2
#define P1B PIN_D5
#define P1C PIN_D6
#define P1D PIN_D7
#define PERIOD 124 //for setting pwm fq

#byte PSTRCON = 0x9d


// Call this routine to set the PWM steering.  You can OR
// together the PWM steering constants from the PIC's
// header file.
void set_pulse_steering(int32 steering)
{
int8 temp;

temp = make8(steering, 3);  // Get high byte only

PSTRCON = temp;   // Setup PWM steering register

// Set the TRIS to outputs for the enabled steering pins.
// Also set the selected pins to a low level.
if(temp & 1)
   output_low(P1A);
if(temp & 2)
   output_low(P1B);
if(temp & 4)
   output_low(P1C);
if(temp & 8)
   output_low(P1D);
}

void initMotors(){

setup_timer_2(T2_DIV_BY_16, PERIOD, 1);  // Set 1000 Hz PWM freq
set_pwm1_duty((int)(PERIOD*(speed*.25)));  // Set PWM for 25% duty cycle

// Set up PWM so that all four outputs are active high.
setup_ccp1(CCP_PWM_H_H);
}

// usage setSpeed(0-100,l-r-f); so for forward 100% setSpeed(100,f)
void setSpeed(int spd, char side){
   // set the pwm speed to desired speed (1-100%)
   if(spd > 99){ spd = 99; } // never more than 100%
   //set_pwm1_duty((int)(PERIOD*(speed*.1)));
   set_pwm1_duty((spd));
   speed = spd; // update global var to reflect new speed
   // set the side thats desired
   if(side == 'l') // pwm - (MTR-L) - high
   set_pulse_steering( CCP_PULSE_STEERING_B); // left motor (P1B)
   if(side == 'r') // pwm - (MTR-R) - high
   set_pulse_steering( CCP_PULSE_STEERING_D); // right motor (P1D)
   if(side == 'f') // both motors pwm on one side
   set_pulse_steering( CCP_PULSE_STEERING_B | CCP_PULSE_STEERING_D);
   if(side == 's'){ // stop !!
   set_pulse_steering(0);
   speed=0;
   }
}


The main problem i have is with the
Code:

   if(spd > 99){ spd = 99; } // never more than 100%
   //set_pwm1_duty((int)(PERIOD*(speed*.1)));
   set_pwm1_duty((spd));

section. I'm trying to make it so I can set simply 1-100% but I really dont know what i'm doing.
With the line of code that was commented out it would just be off or like full speed, so i made the line of code that is not commented where we just pass "spd" to it and "spd" cannot be more than 99. This helps a little.

Anyway can somone please explain to me what my max duty would be, and how I can calculate it since my clock is 4Mhz and timer2 is set as it is in the "initMotors()" function. I'm using enhanced pwm but i'm only controlling two motors.

thanks for your time !
_________________
Smart people know how stupid they are.
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