Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Speed Control Of Dc Motor

Status
Not open for further replies.

nelson_kh

New Member
do anyone got any simple sample code in C language that can control the speed of dc motor?? it can help more easy to understand wat the datasheet talking about!! i set the CCP1L,CCP1CON,T2CON, PR2 and a output signal for motor alr but motor still cant move.i using pic06f877a. thanks for urs help!!!
 

jpanhalt

Well-Known Member
Most Helpful Member
Can you post your code? I can't do C, but many others here can. They will want to see the code.

You seem to be using the motor drive as a test of the code. There can be errors between the PIC and the motor, so I would suggest something more directly related to the PIC, like putting a scope probe or an LED on the output pin(s). That way, you can troubleshoot the PIC aspect without worrying whether the problem is in the PIC program or the motor drive circuit. John
 

Wingmax

New Member
nelson_kh said:
do anyone got any simple sample code in C language that can control the speed of dc motor?? it can help more easy to understand wat the datasheet talking about!! i set the CCP1L,CCP1CON,T2CON, PR2 and a output signal for motor alr but motor still cant move.i using pic06f877a. thanks for urs help!!!

If it's any help at all, you can refer to my post in Microcontroller section- "Setting up CCP PWM". It's a method of setting up the Pulse width Modulation. You use this method to drive the DC motor. Of course, there's always different techniques.
 

nelson_kh

New Member
thz wingmax....
i using the 20mega hz crystal... i just change PR2 oni izzit? the formula devide by two value of 4, the both 4 also is scale value?
if i use the pulse of 4khz wat different? i use power window as motor!
and the scale i use 16 can??
 

Krumlink

New Member
Moterz r controlld by voltege, so you need 2 increze your voltagr to make the go faester...

Spell correctly, it can be difficult to read it!
 

Wingmax

New Member
Refer to the formula 1 to find PR2 first: All you need is to substitute the frequency of the osc you use, the frequency of the pulses you want to generate, and choose a prescale so that the overall result would not > 255. this is because PR2 is a 8 bit register, max number is FFh or 255d. don't change the other number 4, it's a constant. And that's it.
 

nelson_kh

New Member
the prescale i can chose anynumber? from data sheet there write (1,4,16), we oni can chose this three number as prescale value?i use 20megahz, 2k hz pulse.

PR2 = 20000000
------------ - 1
2000 x 16 x 4

is this correct?
 

Wingmax

New Member
nelson_kh said:
the prescale i can chose anynumber? from data sheet there write (1,4,16), we oni can chose this three number as prescale value?i use 20megahz, 2k hz pulse.

PR2 = 20000000
------------ - 1
2000 x 16 x 4

is this correct?

That's correct. Now you have to calculate the value of CCPR1L and CCR1CON bit 4 and bit 5. And don't forget to set up the CCP1CON and T2CON etc. just follow my example program.
 

nelson_kh

New Member
Wingmax said:
That's correct. Now you have to calculate the value of CCPR1L and CCR1CON bit 4 and bit 5. And don't forget to set up the CCP1CON and T2CON etc. just follow my example program.




Form the example CCPR1L:CCP1CON<5:4> = 250/ 11111010. how u get the CCP1CON bit 4=0 bit 5=1??
i count the CCPR1L:CCP1CON by duty cycle50%, pulse 2k, scale16. get result is 312.5 which bigger then 256 how? since 16 is the biggest scale can use.
 

Wingmax

New Member
nelson_kh said:
Form the example CCPR1L:CCP1CON<5:4> = 250/ 11111010. how u get the CCP1CON bit 4=0 bit 5=1??
i count the CCPR1L:CCP1CON by duty cycle50%, pulse 2k, scale16. get result is 312.5 which bigger then 256 how? since 16 is the biggest scale can use.

Now you got me confused. How did you get that formula?
To calculate the value in CCPR1L: CCP1CON>5:4, you need formula 2) in my post, ie, (duty cycle x Fosc)/ TMR2 prescale

You need to work out the duty cycle first: 50% of 2K pulses= 0.5/2000= 0.00025.

now , (0.00025 x Fosc)/ 16
When you got this answer, then you convert it to binary.
The converted binary number becomes the value of CCPR1L and bit 4 and bit 5 of CCP1CON.
 

Wingmax

New Member
nelson_kh said:
Form the example CCPR1L:CCP1CON<5:4> = 250/ 11111010. how u get the CCP1CON bit 4=0 bit 5=1??
i count the CCPR1L:CCP1CON by duty cycle50%, pulse 2k, scale16. get result is 312.5 which bigger then 256 how? since 16 is the biggest scale can use.


Sorry, I calculated the value and it's 312.5 as you calculated. So now you just round off the number to 313 and convert it to binary. The LSB (bit 0)of the converted number is bit 4 of CCP1CON, bit 1 of the converted number is bit 5 of CCP1CON and so on...
 

nelson_kh

New Member
Wingmax said:
Sorry, I calculated the value and it's 312.5 as you calculated. So now you just round off the number to 313 and convert it to binary. The LSB (bit 0)of the converted number is bit 4 of CCP1CON, bit 1 of the converted number is bit 5 of CCP1CON and so on...

THZ!!
313 is 100111001. then the bit 4 of CCP1CON is 1 bit 5 is 0.... then the CCPR1L is 01001110..... is this correct??
The value calculated can not more then 256 right, i count is 313 can use??
 

Wingmax

New Member
nelson_kh said:
THZ!!
313 is 100111001. then the bit 4 of CCP1CON is 1 bit 5 is 0.... then the CCPR1L is 01001110..... is this correct??
The value calculated can not more then 256 right, i count is 313 can use??

The binary conversion is correct. And CCP1CON and CCPR1L are correct.You're almost there.
I think you got confused with PR2. PR2 is a 8 bit register and so the value can't be > 255. But CCPR1L and CCP1CON bit 4 and 5 combined becomes a 10 bit register. So 313 is OK to use. Value in CCPR1L and CCP1CON is for adjusting duty cycle or pulse width. Adjusting that will adjust the speed of the motor. PR2 is for adjusting the frequency or period of the pulses.
 

nelson_kh

New Member
Wingmax said:
The binary conversion is correct. And CCP1CON and CCPR1L are correct.You're almost there.
I think you got confused with PR2. PR2 is a 8 bit register and so the value can't be > 255. But CCPR1L and CCP1CON bit 4 and 5 combined becomes a 10 bit register. So 313 is OK to use. Value in CCPR1L and CCP1CON is for adjusting duty cycle or pulse width. Adjusting that will adjust the speed of the motor. PR2 is for adjusting the frequency or period of the pulses.

thank you so much, really help me alot!! i write this code why the speed cant change when i change theCCP1CON and CCPR1L?
#include <pic.h>
__CONFIG(WDTDIS & XT & UNPROTECT & LVPDIS);
#define XTAL 20000000 //crystal frequency = 20MHz

void main()
{
TRISC = 0x00;
TRISB = 0xFF;
PR2 = 156;
CCP1CON = 0b00011100;
CCPR1L = 0b01001110;
PORTC = 0b00000010;
T2CON = 0b00000110;
if (RB2==0) RC4=1;
else RC4=0;
}
RC5 is output to the turn the motor! and rb2 is input! is the code wrong?
 

Wingmax

New Member
nelson_kh said:
thank you so much, really help me alot!! i write this code why the speed cant change when i change theCCP1CON and CCPR1L?
#include <pic.h>
__CONFIG(WDTDIS & XT & UNPROTECT & LVPDIS);
#define XTAL 20000000 //crystal frequency = 20MHz

void main()
{
TRISC = 0x00;
TRISB = 0xFF;
PR2 = 156;
CCP1CON = 0b00011100;
CCPR1L = 0b01001110;
PORTC = 0b00000010;
T2CON = 0b00000110;
if (RB2==0) RC4=1;
else RC4=0;
}
RC5 is output to the turn the motor! and rb2 is input! is the code wrong?

I'm not 100% sure the PWM setting for 16F877 CCP1 is the same as the 16F628 as I haven't worked on one for while. But I know the ADC module in the 16F684 is different than the 16F877.

But anyway, does the motor turn at all? May be not , because you should use RC2 to control the motor instead of RC5. Check the data sheet, pin 17 is RC2/ CCP1.
 

nelson_kh

New Member
i m using a motor driver to control the direction and speed of motor turning. form the motor driver connected with motor and got 3 pin is connect to ic. 1 pin is RC1 and another 2 is the direction of turning which can set at RC4 or RC5. exp RC4 turn clockwise, RC5 turn anticlock wise!!then the code is ok?
 

Wingmax

New Member
nelson_kh said:
i m using a motor driver to control the direction and speed of motor turning. form the motor driver connected with motor and got 3 pin is connect to ic. 1 pin is RC1 and another 2 is the direction of turning which can set at RC4 or RC5. exp RC4 turn clockwise, RC5 turn anticlock wise!!then the code is ok?

So you're using RC1 to control the speed, RC4 and RC5 to switch direction?
You should use RC2 for PWM to control the speed, because RC2 is the dedicated pin for CCP1. Check data sheet.
 

nelson_kh

New Member
Wingmax said:
So you're using RC1 to control the speed, RC4 and RC5 to switch direction?
You should use RC2 for PWM to control the speed, because RC2 is the dedicated pin for CCP1. Check data sheet.


yaya i use RC2 wrong type last time!!! but the motor still not turning! Y? i use PIC simulate to test and see the RC2 some time on sometime off!! how come.... it should keep 'on' to turn the motor!!
 
Status
Not open for further replies.

Latest threads

Top