View previous topic :: View next topic |
Author |
Message |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
Different duty cycles on PWM ports |
Posted: Thu Oct 04, 2007 4:00 pm |
|
|
Hi.
Is it possible to output 2 different duty cycles on say pwm pin RB1, RB2 at the same time using the following type of code?
Code: | output_high(pin_C6);
output_high(pin_C7);
set_pwm1_duty(40);
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_C);
set_pwm2_duty(70);
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_D); |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 04, 2007 4:44 pm |
|
|
I assume this is for a 16F886. The data sheet shows that there is only
one PWM generator driving pins P1A-P1D. This means there is only
one frequency and one duty cycle possible.
There are some options available with the output pins. You can invert
some of the outputs, so they would have the opposite duty cycle.
The enhanced PWM module is intended as a motor driver circuit. |
|
![](templates/subSilver/images/spacer.gif) |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Oct 04, 2007 5:29 pm |
|
|
I am using one 16f886 to drive 2 h-bridges. Each bridge needs 1 pwm input but the duty cycles need to be different as i am driving a small robot that has 2 motors. Running in the forward state the speed of the motors are different even with the same pwm output, so the robot doesn't run straight. Have you got any advice as to solve this problem?? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 04, 2007 5:48 pm |
|
|
It sounds like you need a tachometer on each motor, and you need a
control loop in your program to maintain the correct speed on each one.
I haven't done anything in robotics. The robotics people may have other
suggestions. |
|
![](templates/subSilver/images/spacer.gif) |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Oct 04, 2007 6:22 pm |
|
|
I want to manually do the speed control with the normal analog joysticks that you find in remote's. I would prefer to use the input from the analog to determine the speed of each motor so it would be a completely manual thing. Do you have any sugestion or idee if it would be possible?? |
|
![](templates/subSilver/images/spacer.gif) |
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Fri Oct 05, 2007 2:24 am |
|
|
as pcm said u could use a control loop and a tacho on each motor. the adc will set the referance speed from the joystick and the the control loop adjusts each motors speed via the pwm till the motor is running at the correct speed.
below is some code i wrote in c but not CCS for a dc motor controller. it would keep the motor at a steady speed even with the load on the motor being changed. the adc sets the referance speed and then ther is a PIC controlled to control the dutycycle of the pwm.
Code: | Reference_Speed = adc_result/2;
if (Reference_Speed > 1500)
Reference_Speed = 1500;
if (Reference_Speed < 300)
Reference_Speed = 300;
if (Speed_Flag == 1)
{
Previous_Error_Speed = Error_Speed;
Error_Speed = Reference_Speed - Actual_Speed;
KpN = 1;
KpD = 1;
Proportional_Factor = Error_Speed * KpN / KpD;
KiN = 1;
KiD = 10;
Integral_Factor = Integral_Factor + (KiN * Error_Speed / KiD);
if (Integral_Factor > 30000)
Integral_Factor = 30000;
if (Integral_Factor < -30000)
Integral_Factor = -30000;
KdN = 1;
KdD = 1;
Derivative_Factor = (Error_Speed - Previous_Error_Speed) * KdN / KdD;
dutyCycleCount = Proportional_Factor + Integral_Factor + Derivative_Factor;
Speed_Flag = 0;
}
if (dutyCycleCount > 225)
dutyCycleCount = 225;
if (dutyCycleCount < 40)
dutyCycleCount = 40;
lcd_goto(0x00);
printf("Ref SPD %4d RPM", Reference_Speed);
lcd_goto(0x40);
printf("Act SPD %4d RPM", Actual_Speed);
DAC0 = Actual_Speed; |
|
|
![](templates/subSilver/images/spacer.gif) |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Sat Oct 06, 2007 5:11 pm |
|
|
Thanks for the reply but for my purpose it would not work as i am not able to use any back reference from the motor. Basically all i want to do is control 2 DC motor's H-bridge PWM pins at different speeds from the pic via a remote control where the joystick is mounted on. I tried using pwm outputs from the standard pwm pins but as they cannot give out different duty cycles at the same time, i would have to find some other solution as maybe software pwm via 4 other pins that changes with the analog from the joystick. |
|
![](templates/subSilver/images/spacer.gif) |
|