View previous topic :: View next topic |
Author |
Message |
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
PWM Problem |
Posted: Wed Feb 18, 2015 9:31 am |
|
|
I'm having problem with pwm... I'm using L298 motor driver and PIC18F2550 mcu...
suppose
Code: |
left_motor_speed=255;
right_motor_speed=255;
set_pwm1_duty(left_motor_speed); //Left Motor Speed
set_pwm2_duty(right_motor_speed); //Right Motor Speed
|
the motor did not run,
but if i use
Code: |
set_pwm1_duty(255); //Left Motor Speed
set_pwm2_duty(255); //Right Motor Speed |
it works fine, or if i use
Code: |
set_pwm1_duty(left_motor_speed); //Left Motor Speed
set_pwm2_duty(255); //Right Motor Speed |
it also works, so i didnt get the problem with this variable !!!
plz help.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Wed Feb 18, 2015 9:59 am |
|
|
You need to post a small, compilable program that doesn't work.
'code snippets' don't tell the complete story.
You could have 'something' else causing the problem that we annot see.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Feb 18, 2015 11:56 am |
|
|
and particularly the declaration of the motor_speed variables. I can see where a factor of 4 change in what the PWM will actually 'do' will appear depending on how these are declared.... |
|
|
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
|
Posted: Thu Feb 19, 2015 4:49 am |
|
|
Code: | #include <18f2520.h>
#fuses HS,NOWDT,NOPUT
#use delay (crystal = 20MHz)
#define R2 PIN_C0 //with ccp2
#define L2 PIN_C3 //with ccp1
int16 a=250;
int16 b=250;
void main(){
//************timer 2 for PWM *******************
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
delay_us(100);
while(TRUE){
output_high(L2);
set_pwm1_duty(a);
output_low(R2);
set_pwm2_duty(b);
}
}
|
this is sample code... |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Feb 19, 2015 8:00 am |
|
|
OK. So your code is short enough.
I assume you are really using 18F2520 NOT 18F2550.
(You've got different parts in separate posts.)
Before I start to experiment:-
Can you tell us EXACTLY what it is doing/not doing so we don't have to guess. What do your pulses look like etc.
Saying "the motor did not run" is not entirely helpful.
Mike |
|
|
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
|
Posted: Thu Feb 19, 2015 8:28 am |
|
|
i dont have any oscilloscope, and when i burn this code the motor rotate but the rpm is like 1 or less...but if i directly give
set_pwm1_duty(250);
instead of
set_pwm1_duty(a);
works fine. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Feb 19, 2015 9:28 am |
|
|
Straight away, it will be different.
set_pwm1_duty(250);
Will treat '250', as an int8. So will set the PWM, to effectively 1000.....
load 'a' with 1000.
If you want to use values 0 to 255, then declare 'a' as an int8, not an int16.
Read the manual on set_pwmx_duty. Particularly what is done, if 'value' is an 8 bit item. |
|
|
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
|
Posted: Thu Feb 19, 2015 10:09 am |
|
|
int8 or int16 it doesn't work...
and set_pwm, it also takes int16 values... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Feb 19, 2015 10:31 am |
|
|
Code: |
int8 a;
a=250;
set_pwm1_duty(a);
//is _identical_ to:
set_pwm1_duty(250);
int16 a
a=248; //only because 250, does not exactly divide by 4
set_pwm1_duty(a)
//gives the same result as:
set_pwm1_duty(62);
//or
int16 a
a=250;
set_pwm1_duty(a)
//is the same as:
set_pwm1_duty(250L); //note the 'L'
int16 a
a=1000;
set_pwm1_duty(a)
//gives the same result as:
set_pwm1_duty(250);
|
The point is that set_pwm1_duty, is an _overloaded_ function.
It has two different versions. One accepts an int8, the other accepts an int16. It switches which version is used according to the type of the number it receives. If you use an int8, it multiplies this by 4 and loads this as the value to use. If you use an int16, this multiplication does not occur. |
|
|
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
|
Posted: Thu Feb 19, 2015 11:06 am |
|
|
u dont get my problem bro...
int8 a;
a=250;
set_pwm1_duty(a);
its not the problem...
the problem is
int8 a,b;
a=250;
b=250;
set_pwm1_duty(a);
set_pwm2_duty(b);
when i give values from 2 variable in 2 pwm port it didnt works, but one variable works... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 19, 2015 12:42 pm |
|
|
Post your CCS compiler version. It's a 4-digit number, such as 5.035
and is at the top of the .LST file. The .LST file will be in your project
directory after you compile a project with no errors. I will install and
test your version in hardware. |
|
|
mazedm80
Joined: 18 Feb 2015 Posts: 12
|
|
Posted: Thu Feb 19, 2015 1:13 pm |
|
|
Version 5.013 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Thu Feb 19, 2015 1:46 pm |
|
|
If you're using an 18F2520 then look at the datasheet for the 28 pin usage.
I read that either pin 12 or pin 24 can be used for CCP2......and that ccp1 is on pin 11, so I 'm confused as to which PIC you're using and which I/O pin you've chosen for the CCP2 output.
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 19, 2015 2:17 pm |
|
|
I installed vs. 5.013 and compiled your program, as shown below, and
ran it on an 18F2620 and it worked. I see PWM on both channels on my
scope. I don't have an 18F2520, but the 18F2620 is in the same PIC family. Just to be sure, I compiled your program for both PICs with
vs. 5.013 and the code looks the same.
I think, maybe your power supply is getting loaded down when you run
both PWM channels, and it's shutting down the PIC.
Code: |
#include <18f2520.h>
#fuses HS,NOWDT,NOPUT
#use delay (crystal = 20MHz)
#define R2 PIN_C0 //with ccp2
#define L2 PIN_C3 //with ccp1
int16 a=250;
int16 b=250;
void main(){
//************timer 2 for PWM *******************
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
delay_us(100);
while(TRUE){
output_high(L2);
set_pwm1_duty(a);
output_low(R2);
set_pwm2_duty(b);
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Feb 19, 2015 2:53 pm |
|
|
mazedm80 wrote: | i dont have any oscilloscope, and when i burn this code the motor rotate but the rpm is like 1 or less...but if i directly give
set_pwm1_duty(250);
instead of
set_pwm1_duty(a);
works fine. |
OK. So you don't have a 'scope (makes life difficult).
You need to check out PCM programmer's theory.
Disconnect the motor drives, and monitor the PWM outputs with a voltmeter.
Failing that you could try with a pair of LEDs and associated resistors.
(Getting desperate now.)
Mike |
|
|
|