DC Motor Control

Questions and Comments on All Tutorials

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

DC Motor Control

Postby ThePyroElectro » Mon Nov 12, 2007 10:36 pm

The DC Motor Control Tutorial Write-up

If you want your next project to be mobile, being able to reliably control
motors is a must. In this tutorial, we use the LMD18245 to control a simple
12v DC motor.

Image

Questions & Comments?
Last edited by ThePyroElectro on Thu Aug 11, 2011 3:06 pm, edited 1 time in total.

Kevin
Newbie Pyro
Posts: 17
Joined: Fri Nov 23, 2007 9:06 pm

Postby Kevin » Fri Nov 23, 2007 9:48 pm

Hi Chris,

Firstly, I want to say that you have an awesome website for electronics tutorials. Keep up good work. :)

I have a question about the DC motor's speed. I have read the datasheet of the LMD18245, and I understand that to change the speed, you simply change V DAC of {M4,M3,M2,M1}. Therefore, you have the following code for full speed:

PORTC = 0b00001111; // 0x0F Full Speed

For 3/4 full speed, shouldn't it be 15*3/4 ~ 11 = 0b00001011 or am I missing something here?

This is what you have for 3/4 full speed:

PORTC = 0b00001110; //0X0B 3/4 Full Speed

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Fri Nov 23, 2007 11:02 pm

Hi, thanks for checking out the site.

You're right.

11 = 0x0B = 0b00001011

Looks like i made a typo. I'll go ahead and correct it in the tutorial.
Thanks for the comment

~Chris

Kevin
Newbie Pyro
Posts: 17
Joined: Fri Nov 23, 2007 9:06 pm

Postby Kevin » Sat Nov 24, 2007 2:07 am

Dang it, they charge a shipping fee for the H-Bridge driver sample now. Do you know a "free" alternative IC? :D

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Sat Nov 24, 2007 3:50 am

Kevin wrote:Dang it, they charge a shipping fee for the H-Bridge driver sample now. Do you know a "free" alternative IC? :D


The L298 is another very popular H-Bridge. I use the LMD18245 because
they are particularly easy to get setup, working & troubleshoot.

I'd stick it out if you can for shipping but st.com should have L298's for sample.

BushidoShonin
Newbie Pyro
Posts: 4
Joined: Wed Mar 05, 2008 2:38 pm

Postby BushidoShonin » Mon Mar 10, 2008 2:52 pm

When programming the PIC can you use variables to represent a specific port/pin? somethin like this:

Break = "PORTBbits.RD0"; // Brake
Dir= "PORTBbits.RD1"; // Direction

then later when you want to do something with these all you have to do is:

Break = 1;
Dir = 1;

Would something like this work?

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Mon Mar 10, 2008 4:05 pm

Sure you can 8) , use the #define statement at the very top of your program.

Example (top two lines are what you're looking for):

Code: Select all

#define Break PORTBbits.RD0 // Brake
#define Dir PORTBbits.RD1 // Direction

...rest of your program...
#include <p18f4550.h>
...
..
void main(void)..{
..
}


This makes it so whenever you type Break or Dir in your program it actually means "PORTBbits.RD0" or "PORTBbits.RD1" :D.

BushidoShonin
Newbie Pyro
Posts: 4
Joined: Wed Mar 05, 2008 2:38 pm

Postby BushidoShonin » Mon Mar 10, 2008 5:22 pm

Thanks a lot, but I have a ton more questions.

First: How would you define a number variable? Like this?
int var1 = 123

Second: How would you state a random number? Like this?
int randNum = (rand() / RAND_MAX) * 100 //Random number between 0 and 100

Third: When defining a var like in my last post, does this all have to be outsite the main function?

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Mon Mar 10, 2008 10:34 pm

Hi BushidoShonin,

From your last few questions, I'm led to believe you don't have much experience programming in C/C++ :o . If that's the case I'd really suggest finding a free online "Learn C in 24 Hours" ebook. You can find one just through a simple google search and then skim through it to get the basics.

:!: If that's not the case, here's the answer to your questions:

:arrow: (1)
In C numbers are stored in numerical data types (int, long int, double, float to name a few). The proper syntax for declaring a numerical data type is:

Code: Select all

int my_number = 0;


:arrow: (2)
In order to generate a random number you will need a library that provides some type of random number generator. The stdlib.h library included with the PIC C18 library provides such functionality with the rand() function.

Bear in mind that rand() may produce similar values in each trial unless you seed some dynamic value into the random number generator using srand().

:arrow: (3)
The previous post defined no actual value. It equated that whenever the compiler sees 'Dir' it should replace it with 'PORTBbits.RD1' before compiling the code.

The C18 compiler is based off of ANSI C which means variables should be declared after the main statement and not mid-way through the program.

1976922
Newbie Pyro
Posts: 1
Joined: Thu Nov 26, 2009 10:16 am

Postby 1976922 » Thu Nov 26, 2009 10:35 am

Hi Chris,

According to your project, it just utilize h-circuit and processor to control the motor. Do you have an experiment that using h-circuit and motor encoder/decoder via processor to be a close loop circuit to control the motor?

Since I have some confuse for the motor encoder, would you please to give me some comment or suggest?

Since I know that has chips to be the encoder/decoder, the motor which I have included a internal encoder. Is this encoder's function can be the Optical Encoder. Moreover, I don't have decoder chip. How about the flow path for program of decoder in 8051.

Thank you so much.
Matt

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Tue Dec 29, 2009 7:59 am

Hi Matt,

I'm working on a write-up like that. I don't currently have any tutorials or projects with closed loop systems. Sorry!

Best of luck.

direncserkan
Newbie Pyro
Posts: 3
Joined: Tue Feb 21, 2012 1:02 pm

Re: DC Motor Control

Postby direncserkan » Tue Feb 21, 2012 1:42 pm

Hi Chris could you please write the hex code i couldnt compile it

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: DC Motor Control

Postby ThePyroElectro » Tue Feb 21, 2012 5:53 pm

direncserkan wrote:Hi Chris could you please write the hex code i couldnt compile it


I can post the .hex here later today, however I urge you to download and install the C18 compiler from microchip.com and learn how to compile the code.

direncserkan
Newbie Pyro
Posts: 3
Joined: Tue Feb 21, 2012 1:02 pm

Re: DC Motor Control

Postby direncserkan » Tue Feb 21, 2012 8:48 pm

ThePyroElectro wrote:
I can post the .hex here later today, however I urge you to download and install the C18 compiler from microchip.com and learn how to compile the code.

Thank you i am wating for the .hex code and downloading C18 compiler

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: DC Motor Control

Postby ThePyroElectro » Wed Feb 22, 2012 6:13 am

direncserkan wrote:
ThePyroElectro wrote:
I can post the .hex here later today, however I urge you to download and install the C18 compiler from microchip.com and learn how to compile the code.

Thank you i am wating for the .hex code and downloading C18 compiler


Hi,

Here is the entire MPLAB project:

http://www.pyroelectro.com/files/motor_basic.zip

Good luck!


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 7 guests