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

set_pwm_duty()

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







set_pwm_duty()
PostPosted: Tue May 09, 2006 7:30 am     Reply with quote

Could some one explain the math behind the set_power_pwm0_duty() command and where I've gone wrong?
My goal is to set the pwm duty between 40-100% for a simple motor controller. But when I hook the output pins to a scope, I can't seem to get consistent results. I've tried hard coding the value from set_power_pwm0_duty() and depending on when I've done it, I'v gotten values from 2750 up to 6000 to get 100% duty.
I'm sure I'm overlooking something but I have NO clue what.




#include<18f4331.h>

#device adc=10
#fuses INTRC,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT
#use delay(clock=8000000) //8Mhz
#use rs232(baud=9600,xmit=PIN_E0,rcv=PIN_E1,parity=n,bits=8,INVERT)
setup_oscillator(OSC_8MHZ);

#define FORWARD 1
#define BACKWARD 0
#define OFF 0

int16 liLeftDuty;
int16 liRightDuty;


//////////////// functions ////////////////////////
void move(int left_speed,int left_direction, int right_speed, int right_direction);


void main(void)
{
/***********************************
freq= Fosc / (4*(period + 1) * postscale)
or
period = (Fosc /( 4 * freq * postscale)) -1

if we want 10khz
period =( 8mhz /(4 * 10khz * 1) -1
peroid = 199;
*****************************************/

int period = 199;

setup_power_pwm_pins(PWM_ODD_ON,PWM_ODD_ON,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_CLOCK_DIV_128 |PWM_FREE_RUN,1,0,period,0,1,0);
//forward
output_B(0b01010000);

while(true)
{
// half speed
delay_ms(1000);
printf("\n\n\r Half");
move(5,FORWARD,5,FORWARD);
set_power_pwm0_duty(liLeftDuty);
set_power_pwm2_duty(liRightDuty);

//full speed
delay_ms(1000)
printf("\n\n\r Full");
move(10,FORWARD,10,FORWARD);
set_power_pwm0_duty(liLeftDuty);
set_power_pwm2_duty(liRightDuty);

}//while true


}//main()

////////////////////////////////////////////////////////////////////////////
//void move(int left_speed,int left_direction,
// int right_speed, int right_direction)
// this is used to set the speed and direction of the bot
// speed is a value for 0-10
// since this uses PWM for the motors and anything below ~40% duty is useless
// setting the speed to 1 will be ~40% duty and 0 will be off
//
// example: move(10,FORWARD,5,FORWARD);
void move(int left_speed,int left_direction, int right_speed, int right_direction)
{
int direction;
int16 baseline = 1460; // ~40%
int16 factor = 214; // ~ (16232 - 6600 )/ 10

if(left_speed > 10)
left_speed = 10;
if(right_speed > 10)
right_speed = 10;

//calc speed first and set pwm
if(left_speed == 0)
liLeftDuty = left_speed;
else
liLeftDuty = (left_speed * factor) + baseline;

if(right_speed == 0)
liRightDuty = right_speed;
else
liRIghtDuty = (right_speed * factor) + baseline;

}//move
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 09, 2006 11:55 am     Reply with quote

What's your compiler version ? CCS has this statement on their
versions page:
Quote:
3.219 A problem with SETUP_POWER_PWM_PINS() is fixed
Jim
Guest







thanks got it
PostPosted: Tue May 09, 2006 12:08 pm     Reply with quote

version 3.212.. thanks. I'll upgrade..
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